forked from aolserver/nsmemcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsmemfunc.h
executable file
·276 lines (253 loc) · 7.94 KB
/
nsmemfunc.h
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://mozilla.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU General Public License (the "GPL"), in which case the
* provisions of GPL are applicable instead of those above. If you wish
* to allow use of your version of this file only under the terms of the
* GPL and not to allow others to use your version of this file under the
* License, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the GPL.
* If you do not delete the provisions above, a recipient may use your
* version of this file under either the License or the GPL.
*/
/* Added By Majid [email protected]
* Following structure and functions have been copied from tclobjv.c
* http://naviserver.cvs.sourceforge.net/naviserver/naviserver/nsd/tclobjv.c?revision=1.17&view=markup
*/
/*
* The following struct describes how to process an option
* or argument passed to a Tcl command.
*/
struct Ns_ObjvSpec;
typedef int (Ns_ObjvProc) (struct Ns_ObjvSpec *spec, Tcl_Interp *interp,
int *objcPtr, Tcl_Obj *CONST objv[]);
typedef struct Ns_ObjvTable {
char *key;
int value;
} Ns_ObjvTable;
typedef struct Ns_ObjvSpec {
char *key;
Ns_ObjvProc *proc;
void *dest;
void *arg;
} Ns_ObjvSpec;
/*
*----------------------------------------------------------------------
*
* Ns_ObjvInt, Ns_ObjvLong, Ns_ObjvWideInt, Ns_ObjvDouble --
*
* Consume exactly one argument, returning it's value into dest.
*
* Results:
* TCL_OK or TCL_ERROR;
*
* Side effects:
* Argument maybe converted to type specific internal rep.
*
*----------------------------------------------------------------------
*/
int Ns_ObjvInt(Ns_ObjvSpec *spec, Tcl_Interp *interp, int *objcPtr,Tcl_Obj *CONST objv[])
{
int *dest = spec->dest;
if (*objcPtr > 0 && Tcl_GetIntFromObj(interp, objv[0], dest) == TCL_OK) {
*objcPtr -= 1;
return TCL_OK;
}
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* Ns_ObjvString --
*
* Consume exactly one argument, returning a pointer to it's
* cstring into *spec->dest.
*
* If spec->arg is != NULL it is assumed to be a pointer to an
* int and the returned string length will be left in it.
*
* Results:
* TCL_OK or TCL_ERROR.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int Ns_ObjvString(Ns_ObjvSpec *spec, Tcl_Interp *interp, int *objcPtr,Tcl_Obj *CONST objv[])
{
char **dest = spec->dest;
if (*objcPtr > 0) {
*dest = Tcl_GetStringFromObj(objv[0], (int *) spec->arg);
*objcPtr -= 1;
return TCL_OK;
}
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* Ns_ObjvBreak --
*
* Handle '--' option/argument separator.
*
* Results:
* Always TCL_BREAK.
*
* Side effects:
* Option processing will end successfully, argument processing
* will begin.
*
*----------------------------------------------------------------------
*/
int Ns_ObjvBreak(Ns_ObjvSpec *spec, Tcl_Interp *interp,int *objcPtr, Tcl_Obj *CONST objv[])
{
return TCL_BREAK;
}
/*
*----------------------------------------------------------------------
*
* Ns_ObjvBool --
*
* If spec->arg is 0 consume exactly one argument and attempt
* conversion to a boolean value. Otherwise, spec->arg is treated
* as an int and placed into spec->dest with zero args consumed.
*
* Results:
* TCL_OK or TCL_ERROR.
*
* Side effects:
* Next Tcl object maybe converted to boolean type.
*
*----------------------------------------------------------------------
*/
int Ns_ObjvBool(Ns_ObjvSpec *spec, Tcl_Interp *interp, int *objcPtr,Tcl_Obj *CONST objv[])
{
int *dest = spec->dest;
if (spec->arg) {
*dest = (int) spec->arg;
return TCL_OK;
}
if (*objcPtr > 0 && Tcl_GetBooleanFromObj(interp, objv[0], dest) == TCL_OK) {
*objcPtr -= 1;
return TCL_OK;
}
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* WrongNumArgs --
*
* Leave a usage message in the interpreters result.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void WrongNumArgs(Ns_ObjvSpec *optSpec, Ns_ObjvSpec *argSpec, Tcl_Interp *interp,int objc, Tcl_Obj *CONST objv[])
{
Ns_ObjvSpec *specPtr;
Ns_DString ds;
char *p;
Ns_DStringInit(&ds);
if (optSpec != NULL) {
for (specPtr = optSpec; specPtr->key != NULL; ++specPtr) {
if (STREQ(specPtr->key, "--")) {
Ns_DStringAppend(&ds, "?--? ");
} else if (specPtr->proc == &Ns_ObjvBool && specPtr->arg != NULL) {
Ns_DStringPrintf(&ds, "?%s? ", specPtr->key);
} else {
p = specPtr->key;
if (*specPtr->key == '-') {
++p;
}
Ns_DStringPrintf(&ds, "?%s %s? ", specPtr->key, p);
}
}
}
if (argSpec != NULL) {
for (specPtr = argSpec; specPtr->key != NULL; ++specPtr) {
Ns_DStringPrintf(&ds, "%s%s ", specPtr->key,
(*specPtr->key == '?') ? "?" : "");
}
}
Ns_DStringTrunc(&ds, Ns_DStringLength(&ds) - 1);
Tcl_WrongNumArgs(interp, objc, objv, ds.string);
Ns_DStringFree(&ds);
}
/*
*----------------------------------------------------------------------
*
* Ns_ParseObjv --
*
* Process objv acording to given option and arg specs.
*
* Results:
* NS_OK or NS_ERROR.
*
* Side effects:
* Depends on the Ns_ObjvTypeProcs which run.
*
*----------------------------------------------------------------------
*/
int Ns_ParseObjv(Ns_ObjvSpec *optSpec, Ns_ObjvSpec *argSpec, Tcl_Interp *interp,int offset, int objc, Tcl_Obj *CONST objv[])
{
Ns_ObjvSpec *specPtr = NULL;
int optIndex, status, remain = (objc - offset);
if (optSpec && optSpec->key) {
while (remain > 0) {
if (Tcl_GetIndexFromObjStruct(NULL, objv[objc - remain], optSpec,
sizeof(Ns_ObjvSpec), "option",
TCL_EXACT, &optIndex) != TCL_OK) {
break;
}
--remain;
specPtr = optSpec + optIndex;
status = specPtr->proc(specPtr, interp, &remain,
objv + (objc - remain));
if (status == TCL_BREAK) {
break;
} else if (status != TCL_OK) {
return NS_ERROR;
}
}
}
if (argSpec == NULL) {
if (remain > 0) {
badargs:
WrongNumArgs(optSpec, argSpec, interp, offset, objv);
return NS_ERROR;
}
return NS_OK;
}
for (specPtr = argSpec; specPtr->key != NULL; specPtr++) {
if (remain == 0) {
if (specPtr->key[0] != '?') {
goto badargs; /* Too few args. */
}
return NS_OK;
}
if (specPtr->proc(specPtr, interp, &remain, objv + (objc - remain))
!= TCL_OK) {
return NS_ERROR;
}
}
if (remain > 0) {
goto badargs; /* Too many args. */
}
return NS_OK;
}