1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* Implements the tcl::prefix command for Jim Tcl
*
* (c) 2011 Steve Bennett <steveb@workware.net.au>
*
* See LICENSE for license details.
*/
#include <jim.h>
#include "utf8.h"
/**
* Returns the common initial length of the two strings.
*/
static int JimStringCommonLength(const char *str1, int charlen1, const char *str2, int charlen2)
{
int maxlen = 0;
while (charlen1-- && charlen2--) {
int c1;
int c2;
str1 += utf8_tounicode(str1, &c1);
str2 += utf8_tounicode(str2, &c2);
if (c1 != c2) {
break;
}
maxlen++;
}
return maxlen;
}
/* [tcl::prefix]
*/
static int Jim_TclPrefixCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_Obj *objPtr;
Jim_Obj *stringObj;
int option;
static const char * const options[] = { "match", "all", "longest", NULL };
enum { OPT_MATCH, OPT_ALL, OPT_LONGEST };
if (argc < 2) {
Jim_WrongNumArgs(interp, 1, argv, "subcommand ?arg ...?");
return JIM_ERR;
}
if (Jim_GetEnum(interp, argv[1], options, &option, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK)
return Jim_CheckShowCommands(interp, argv[1], options);
switch (option) {
case OPT_MATCH:{
int i;
int ret;
int tablesize;
const char **table;
Jim_Obj *tableObj;
Jim_Obj *errorObj = NULL;
Jim_Obj *messageObj = NULL;
static const char * const matchoptions[] = { "-error", "-exact", "-message", NULL };
enum { OPT_MATCH_ERROR, OPT_MATCH_EXACT, OPT_MATCH_MESSAGE };
int flags = JIM_ERRMSG | JIM_ENUM_ABBREV;
if (argc < 4) {
Jim_WrongNumArgs(interp, 2, argv, "?options? table string");
return JIM_ERR;
}
tableObj = argv[argc - 2];
stringObj = argv[argc - 1];
argc -= 2;
for (i = 2; i < argc; i++) {
int matchoption;
if (Jim_GetEnum(interp, argv[i], matchoptions, &matchoption, "option", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK)
return JIM_ERR;
switch (matchoption) {
case OPT_MATCH_EXACT:
flags &= ~JIM_ENUM_ABBREV;
break;
case OPT_MATCH_ERROR:
if (++i == argc) {
Jim_SetResultString(interp, "missing error options", -1);
return JIM_ERR;
}
errorObj = argv[i];
if (Jim_Length(errorObj) % 2) {
Jim_SetResultString(interp, "error options must have an even number of elements", -1);
return JIM_ERR;
}
break;
case OPT_MATCH_MESSAGE:
if (++i == argc) {
Jim_SetResultString(interp, "missing message", -1);
return JIM_ERR;
}
messageObj = argv[i];
break;
}
}
/* Do the match */
tablesize = Jim_ListLength(interp, tableObj);
table = Jim_Alloc((tablesize + 1) * sizeof(*table));
for (i = 0; i < tablesize; i++) {
Jim_ListIndex(interp, tableObj, i, &objPtr, JIM_NONE);
table[i] = Jim_String(objPtr);
}
table[i] = NULL;
ret = Jim_GetEnum(interp, stringObj, table, &i, messageObj ? Jim_String(messageObj) : NULL, flags);
Jim_Free(table);
if (ret == JIM_OK) {
Jim_ListIndex(interp, tableObj, i, &objPtr, JIM_NONE);
Jim_SetResult(interp, objPtr);
return JIM_OK;
}
if (tablesize == 0) {
Jim_SetResultFormatted(interp, "bad option \"%#s\": no valid options", stringObj);
return JIM_ERR;
}
if (errorObj) {
if (Jim_Length(errorObj) == 0) {
Jim_SetEmptyResult(interp);
return JIM_OK;
}
/* Do this the easy way. Build a list to evaluate */
objPtr = Jim_NewStringObj(interp, "return -level 0 -code error", -1);
Jim_ListAppendList(interp, objPtr, errorObj);
Jim_ListAppendElement(interp, objPtr, Jim_GetResult(interp));
return Jim_EvalObjList(interp, objPtr);
}
return JIM_ERR;
}
break;
case OPT_ALL:
if (argc != 4) {
Jim_WrongNumArgs(interp, 2, argv, "table string");
return JIM_ERR;
}
else {
int i;
int listlen = Jim_ListLength(interp, argv[2]);
objPtr = Jim_NewListObj(interp, NULL, 0);
for (i = 0; i < listlen; i++) {
Jim_Obj *valObj = Jim_ListGetIndex(interp, argv[2], i);
if (Jim_StringCompareLenObj(interp, argv[3], valObj, 0) == 0) {
Jim_ListAppendElement(interp, objPtr, valObj);
}
}
Jim_SetResult(interp, objPtr);
return JIM_OK;
}
case OPT_LONGEST:
if (argc != 4) {
Jim_WrongNumArgs(interp, 2, argv, "table string");
return JIM_ERR;
}
else if (Jim_ListLength(interp, argv[2])) {
const char *longeststr = NULL;
int longestlen = 0;
stringObj = argv[3];
int i;
int listlen = Jim_ListLength(interp, argv[2]);
for (i = 0; i < listlen; i++) {
Jim_Obj *valObj = Jim_ListGetIndex(interp, argv[2], i);
if (Jim_StringCompareLenObj(interp, stringObj, valObj, 0)) {
/* Does not begin with 'string' */
continue;
}
if (longeststr == NULL) {
longestlen = Jim_Utf8Length(interp, valObj);
longeststr = Jim_String(valObj);
}
else {
longestlen = JimStringCommonLength(longeststr, longestlen, Jim_String(valObj), Jim_Utf8Length(interp, valObj));
}
}
if (longeststr) {
Jim_SetResultString(interp, longeststr, longestlen);
}
return JIM_OK;
}
}
return JIM_ERR;
}
int Jim_tclprefixInit(Jim_Interp *interp)
{
if (Jim_PackageProvide(interp, "tclprefix", "1.0", JIM_ERRMSG)) {
return JIM_ERR;
}
Jim_CreateCommand(interp, "tcl::prefix", Jim_TclPrefixCoreCommand, NULL, NULL);
return JIM_OK;
}
|