Select one of the symbols to view example projects that use it.
 
Outline
#include <jim.h>
#include "utf8.h"
JimStringCommonLength(const char *, int, const char *, int)
JimStringComparePrefix(Jim_Interp *, Jim_Obj *, Jim_Obj *)
Jim_TclPrefixCoreCommand(Jim_Interp *, int, Jim_Obj *const *)
Jim_tclprefixInit(Jim_Interp *)
Files
loading...
SourceVuDevelopment ToolsOpenOCDjimtcl/jim-tclprefix.c
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * 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; }if (c1 != c2) { ... } maxlen++; }while (charlen1-- && charlen2--) { ... } return maxlen; }{ ... } /* * Like Jim_StringCompareObj() except only matches as much as the length of firstObjPtr. * So "abc" matches "abcdef" but "abcdef" does not match "abc". *//* ... */ int JimStringComparePrefix(Jim_Interp *interp, Jim_Obj *firstObjPtr, Jim_Obj *secondObjPtr) { /* We do this the easy way by creating a (possibly) shorter version of secondObjPtr */ int l1 = Jim_Utf8Length(interp, firstObjPtr); const char *s2 = Jim_String(secondObjPtr); int l2 = Jim_Utf8Length(interp, secondObjPtr); Jim_Obj *objPtr; int ret; if (l2 > l1) { objPtr = Jim_NewStringObjUtf8(interp, s2, l1); }if (l2 > l1) { ... } else { objPtr = secondObjPtr; }else { ... } Jim_IncrRefCount(objPtr); ret = Jim_StringCompareObj(interp, firstObjPtr, objPtr, 0); Jim_DecrRefCount(interp, objPtr); return ret; }{ ... } /* [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 (argc < 2) { ... } 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; const char *message = "option"; 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; }if (argc < 4) { ... } 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_EXACT: case OPT_MATCH_ERROR: if (++i == argc) { Jim_SetResultString(interp, "missing error options", -1); return JIM_ERR; }if (++i == argc) { ... } errorObj = argv[i]; if (Jim_Length(errorObj) % 2) { Jim_SetResultString(interp, "error options must have an even number of elements", -1); return JIM_ERR; }if (Jim_Length(errorObj) % 2) { ... } break; case OPT_MATCH_ERROR: case OPT_MATCH_MESSAGE: if (++i == argc) { Jim_SetResultString(interp, "missing message", -1); return JIM_ERR; }if (++i == argc) { ... } message = Jim_String(argv[i]); break;case OPT_MATCH_MESSAGE: }switch (matchoption) { ... } }for (i = 2; i < argc; i++) { ... } /* 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); }for (i = 0; i < tablesize; i++) { ... } table[i] = NULL; ret = Jim_GetEnum(interp, stringObj, table, &i, message, flags); Jim_Free(table); if (ret == JIM_OK) { Jim_ListIndex(interp, tableObj, i, &objPtr, JIM_NONE); Jim_SetResult(interp, objPtr); return JIM_OK; }if (ret == JIM_OK) { ... } if (tablesize == 0) { Jim_SetResultFormatted(interp, "bad %s \"%#s\": no valid options", message, stringObj); return JIM_ERR; }if (tablesize == 0) { ... } if (errorObj) { if (Jim_Length(errorObj) == 0) { Jim_SetEmptyResult(interp); return JIM_OK; }if (Jim_Length(errorObj) == 0) { ... } /* 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); }if (errorObj) { ... } return JIM_ERR; ...} case OPT_MATCH: case OPT_ALL: if (argc != 4) { Jim_WrongNumArgs(interp, 2, argv, "table string"); return JIM_ERR; }if (argc != 4) { ... } 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 (JimStringComparePrefix(interp, argv[3], valObj) == 0) { Jim_ListAppendElement(interp, objPtr, valObj); }if (JimStringComparePrefix(interp, argv[3], valObj) == 0) { ... } }for (i = 0; i < listlen; i++) { ... } Jim_SetResult(interp, objPtr); return JIM_OK; }else { ... } case OPT_ALL: case OPT_LONGEST: if (argc != 4) { Jim_WrongNumArgs(interp, 2, argv, "table string"); return JIM_ERR; }if (argc != 4) { ... } else if (Jim_ListLength(interp, argv[2])) { const char *longeststr = NULL; int longestlen = 0; int i; int listlen = Jim_ListLength(interp, argv[2]); stringObj = argv[3]; for (i = 0; i < listlen; i++) { Jim_Obj *valObj = Jim_ListGetIndex(interp, argv[2], i); if (JimStringComparePrefix(interp, stringObj, valObj)) { /* Does not begin with 'string' */ continue; }if (JimStringComparePrefix(interp, stringObj, valObj)) { ... } if (longeststr == NULL) { longestlen = Jim_Utf8Length(interp, valObj); longeststr = Jim_String(valObj); }if (longeststr == NULL) { ... } else { longestlen = JimStringCommonLength(longeststr, longestlen, Jim_String(valObj), Jim_Utf8Length(interp, valObj)); }else { ... } }for (i = 0; i < listlen; i++) { ... } if (longeststr) { Jim_SetResultString(interp, longeststr, longestlen); }if (longeststr) { ... } return JIM_OK; }else if (Jim_ListLength(interp, argv[2])) { ... } case OPT_LONGEST: }switch (option) { ... } return JIM_ERR; /* Cannot ever get here */ }{ ... } int Jim_tclprefixInit(Jim_Interp *interp) { Jim_PackageProvideCheck(interp, "tclprefix"); Jim_CreateCommand(interp, "tcl::prefix", Jim_TclPrefixCoreCommand, NULL, NULL); return JIM_OK; }{ ... }
Details
Show:
from
Types: Columns:
Click anywhere in the source to view detailed information here...