added NsTclGetNative function to get decoded versions of strings. Use that function to get filenames with ns_retutrnfile and ns_respond
| 1 | /* |
| 2 | * The contents of this file are subject to the AOLserver Public License |
| 3 | * Version 1.1 (the "License"); you may not use this file except in |
| 4 | * compliance with the License. You may obtain a copy of the License at |
| 5 | * http://aolserver.com/. |
| 6 | * |
| 7 | * Software distributed under the License is distributed on an "AS IS" |
| 8 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 9 | * the License for the specific language governing rights and limitations |
| 10 | * under the License. |
| 11 | * |
| 12 | * The Original Code is AOLserver Code and related documentation |
| 13 | * distributed by AOL. |
| 14 | * |
| 15 | * The Initial Developer of the Original Code is America Online, |
| 16 | * Inc. Portions created by AOL are Copyright (C) 1999 America Online, |
| 17 | * Inc. All Rights Reserved. |
| 18 | * |
| 19 | * Alternatively, the contents of this file may be used under the terms |
| 20 | * of the GNU General Public License (the "GPL"), in which case the |
| 21 | * provisions of GPL are applicable instead of those above. If you wish |
| 22 | * to allow use of your version of this file only under the terms of the |
| 23 | * GPL and not to allow others to use your version of this file under the |
| 24 | * License, indicate your decision by deleting the provisions above and |
| 25 | * replace them with the notice and other provisions required by the GPL. |
| 26 | * If you do not delete the provisions above, a recipient may use your |
| 27 | * version of this file under either the License or the GPL. |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | * tclmisc.c -- |
| 33 | * |
| 34 | * Implements a lot of Tcl API commands. |
| 35 | */ |
| 36 | |
| 37 | static const char *RCSID = "@(#) $Header: /cvsroot-fuse/aolserver/aolserver/nsd/tclmisc.c,v 1.35 2011/09/28 05:50:47 dvrsn Exp $, compiled: " __DATE__ " " __TIME__; |
| 38 | |
| 39 | #include "nsd.h" |
| 40 | |
| 41 | /* |
| 42 | * Local functions defined in this file |
| 43 | */ |
| 44 | |
| 45 | static int <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_WordEndsInSemi">WordEndsInSemi</a>(char *ip); |
| 46 | |
| 47 | |
| 48 | /* |
| 49 | *---------------------------------------------------------------------- |
| 50 | * |
| 51 | * <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclStripHtmlCmd">NsTclStripHtmlCmd</a> -- |
| 52 | * |
| 53 | * Implements ns_striphtml. |
| 54 | * |
| 55 | * Results: |
| 56 | * Tcl result. |
| 57 | * |
| 58 | * Side effects: |
| 59 | * See docs. |
| 60 | * |
| 61 | *---------------------------------------------------------------------- |
| 62 | */ |
| 63 | |
| 64 | int |
| 65 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclStripHtmlCmd">NsTclStripHtmlCmd</a>(ClientData dummy, Tcl_Interp *interp, int argc, char **argv) |
| 66 | { |
| 67 | int intag; /* flag to see if are we inside a tag */ |
| 68 | int intspec; /* flag to see if we are inside a special char */ |
| 69 | char *inString; /* copy of input string */ |
| 70 | char *inPtr; /* moving pointer to input string */ |
| 71 | char *outPtr; /* moving pointer to output string */ |
| 72 | |
| 73 | if (argc != 2) { |
| 74 | Tcl_AppendResult(interp, "wrong # of args: should be \"", |
| 75 | argv[0], " page\"", NULL); |
| 76 | return TCL_ERROR; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Make a copy of the input and point the moving and output ptrs to it. |
| 81 | */ |
| 82 | inString = ns_strdup(argv[1]); |
| 83 | inPtr = inString; |
| 84 | outPtr = inString; |
| 85 | intag = 0; |
| 86 | intspec = 0; |
| 87 | |
| 88 | while (*inPtr != '\0') { |
| 89 | |
| 90 | if (*inPtr == '<') { |
| 91 | intag = 1; |
| 92 | |
| 93 | } else if (intag && (*inPtr == '>')) { |
| 94 | /* inside a tag that closes */ |
| 95 | intag = 0; |
| 96 | |
| 97 | } else if (intspec && (*inPtr == ';')) { |
| 98 | /* inside a special character that closes */ |
| 99 | intspec = 0; |
| 100 | |
| 101 | } else if (!intag && !intspec) { |
| 102 | /* regular text */ |
| 103 | |
| 104 | if (*inPtr == '&') { |
| 105 | /* starting a new special character */ |
| 106 | intspec=<a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_WordEndsInSemi">WordEndsInSemi</a>(inPtr); |
| 107 | } |
| 108 | |
| 109 | if (!intspec) { |
| 110 | /* incr pointer only if we're not in something htmlish */ |
| 111 | *outPtr++ = *inPtr; |
| 112 | } |
| 113 | } |
| 114 | ++inPtr; |
| 115 | } |
| 116 | |
| 117 | /* null-terminator */ |
| 118 | *outPtr = '\0'; |
| 119 | |
| 120 | Tcl_SetResult(interp, inString, TCL_VOLATILE); |
| 121 | |
| 122 | ns_free(inString); |
| 123 | |
| 124 | return TCL_OK; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /* |
| 129 | *---------------------------------------------------------------------- |
| 130 | * |
| 131 | * <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclCryptObjCmd">NsTclCryptObjCmd</a> -- |
| 132 | * |
| 133 | * Implements ns_crypt as ObjCommand. |
| 134 | * |
| 135 | * Results: |
| 136 | * Tcl result. |
| 137 | * |
| 138 | * Side effects: |
| 139 | * See docs. |
| 140 | * |
| 141 | *---------------------------------------------------------------------- |
| 142 | */ |
| 143 | |
| 144 | int |
| 145 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclCryptObjCmd">NsTclCryptObjCmd</a>(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) |
| 146 | { |
| 147 | char buf[NS_ENCRYPT_BUFSIZE]; |
| 148 | |
| 149 | if (objc != 3) { |
| 150 | Tcl_WrongNumArgs(interp, 1, objv, "key salt"); |
| 151 | return TCL_ERROR; |
| 152 | } |
| 153 | Tcl_SetResult(interp, <a href="/cvs/aolserver/aolserver/nsd/crypt.c#A_Ns_Encrypt">Ns_Encrypt</a>(Tcl_GetString(objv[1]), Tcl_GetString(objv[2]), buf), TCL_VOLATILE); |
| 154 | |
| 155 | return TCL_OK; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /* |
| 160 | *---------------------------------------------------------------------- |
| 161 | * |
| 162 | * <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclHrefsCmd">NsTclHrefsCmd</a> -- |
| 163 | * |
| 164 | * Implements ns_hrefs. |
| 165 | * |
| 166 | * Results: |
| 167 | * Tcl result. |
| 168 | * |
| 169 | * Side effects: |
| 170 | * See docs. |
| 171 | * |
| 172 | *---------------------------------------------------------------------- |
| 173 | */ |
| 174 | |
| 175 | int |
| 176 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclHrefsCmd">NsTclHrefsCmd</a>(ClientData dummy, Tcl_Interp *interp, int argc, char **argv) |
| 177 | { |
| 178 | char *p, *s, *e, *he, save; |
| 179 | |
| 180 | if (argc != 2) { |
| 181 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 182 | argv[0], " html\"", (char *) NULL); |
| 183 | return TCL_ERROR; |
| 184 | } |
| 185 | |
| 186 | p = argv[1]; |
| 187 | while ((s = strchr(p, '<')) && (e = strchr(s, '>'))) { |
| 188 | ++s; |
| 189 | *e = '\0'; |
| 190 | while (*s && isspace(UCHAR(*s))) { |
| 191 | ++s; |
| 192 | } |
| 193 | if ((*s == 'a' || *s == 'A') && isspace(UCHAR(s[1]))) { |
| 194 | ++s; |
| 195 | while (*s) { |
| 196 | if (!strncasecmp(s, "href", 4)) { |
| 197 | s += 4; |
| 198 | while (*s && isspace(UCHAR(*s))) { |
| 199 | ++s; |
| 200 | } |
| 201 | if (*s == '=') { |
| 202 | ++s; |
| 203 | while (*s && isspace(UCHAR(*s))) { |
| 204 | ++s; |
| 205 | } |
| 206 | he = NULL; |
| 207 | if (*s == '\'' || *s == '"') { |
| 208 | he = strchr(s+1, *s); |
| 209 | ++s; |
| 210 | } |
| 211 | if (he == NULL) { |
| 212 | he = s; |
| 213 | while (!isspace(UCHAR(*he))) { |
| 214 | ++he; |
| 215 | } |
| 216 | } |
| 217 | save = *he; |
| 218 | *he = '\0'; |
| 219 | Tcl_AppendElement(interp, s); |
| 220 | *he = save; |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | if (*s == '\'' || *s == '\"') { |
| 225 | while (*s && (*s != '\'' || *s != '\"')) { |
| 226 | ++s; |
| 227 | } |
| 228 | continue; |
| 229 | } |
| 230 | ++s; |
| 231 | } |
| 232 | } |
| 233 | *e++ = '>'; |
| 234 | p = e; |
| 235 | } |
| 236 | |
| 237 | return TCL_OK; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /* |
| 242 | *---------------------------------------------------------------------- |
| 243 | * |
| 244 | * <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclLocalTimeObjCmd">NsTclLocalTimeObjCmd</a>, <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclGmTimeObjCmd">NsTclGmTimeObjCmd</a> -- |
| 245 | * |
| 246 | * Implements the ns_gmtime and ns_localtime commands. |
| 247 | * |
| 248 | * Results: |
| 249 | * Tcl result. |
| 250 | * |
| 251 | * Side effects: |
| 252 | * See docs. |
| 253 | * |
| 254 | *---------------------------------------------------------------------- |
| 255 | */ |
| 256 | |
| 257 | static int |
| 258 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_TmObjCmd">TmObjCmd</a>(ClientData isgmt, Tcl_Interp *interp, int objc, Tcl_Obj **objv) |
| 259 | { |
| 260 | time_t now; |
| 261 | struct tm *ptm; |
| 262 | Tcl_Obj *objPtr[9]; |
| 263 | |
| 264 | if (objc != 1) { |
| 265 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 266 | return TCL_ERROR; |
| 267 | } |
| 268 | now = time(NULL); |
| 269 | if (isgmt) { |
| 270 | ptm = ns_gmtime(&now); |
| 271 | } else { |
| 272 | ptm = ns_localtime(&now); |
| 273 | } |
| 274 | objPtr[0] = Tcl_NewIntObj(ptm->tm_sec); |
| 275 | objPtr[1] = Tcl_NewIntObj(ptm->tm_min); |
| 276 | objPtr[2] = Tcl_NewIntObj(ptm->tm_hour); |
| 277 | objPtr[3] = Tcl_NewIntObj(ptm->tm_mday); |
| 278 | objPtr[4] = Tcl_NewIntObj(ptm->tm_mon); |
| 279 | objPtr[5] = Tcl_NewIntObj(ptm->tm_year); |
| 280 | objPtr[6] = Tcl_NewIntObj(ptm->tm_wday); |
| 281 | objPtr[7] = Tcl_NewIntObj(ptm->tm_yday); |
| 282 | objPtr[8] = Tcl_NewIntObj(ptm->tm_isdst); |
| 283 | Tcl_SetListObj(Tcl_GetObjResult(interp), 9, objPtr); |
| 284 | return TCL_OK; |
| 285 | } |
| 286 | |
| 287 | int |
| 288 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclGmTimeObjCmd">NsTclGmTimeObjCmd</a>(ClientData dummy, Tcl_Interp *interp, int objc, Tcl_Obj **objv) |
| 289 | { |
| 290 | return <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_TmObjCmd">TmObjCmd</a>((ClientData) 1, interp, objc, objv); |
| 291 | } |
| 292 | |
| 293 | int |
| 294 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclLocalTimeObjCmd">NsTclLocalTimeObjCmd</a>(ClientData dummy, Tcl_Interp *interp, int objc, Tcl_Obj **objv) |
| 295 | { |
| 296 | return <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_TmObjCmd">TmObjCmd</a>(NULL, interp, objc, objv); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /* |
| 301 | *---------------------------------------------------------------------- |
| 302 | * |
| 303 | * <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclSleepObjCmd">NsTclSleepObjCmd</a> -- |
| 304 | * |
| 305 | * Tcl result. |
| 306 | * |
| 307 | * Results: |
| 308 | * See docs. |
| 309 | * |
| 310 | * Side effects: |
| 311 | * See docs. |
| 312 | * |
| 313 | *---------------------------------------------------------------------- |
| 314 | */ |
| 315 | |
| 316 | int |
| 317 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclSleepObjCmd">NsTclSleepObjCmd</a>(ClientData dummy, Tcl_Interp *interp, int objc, Tcl_Obj **objv) |
| 318 | { |
| 319 | Ns_Time time; |
| 320 | int ms; |
| 321 | |
| 322 | if (objc != 2) { |
| 323 | Tcl_WrongNumArgs(interp, 1, objv, "timespec"); |
| 324 | return TCL_ERROR; |
| 325 | } |
| 326 | if (<a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclGetTimeFromObj">Ns_TclGetTimeFromObj</a>(interp, objv[1], &time) != TCL_OK) { |
| 327 | return TCL_ERROR; |
| 328 | } |
| 329 | Ns_AdjTime(&time); |
| 330 | if (time.sec < 0 || (time.sec == 0 && time.usec < 0)) { |
| 331 | Tcl_AppendResult(interp, "invalid timespec: ", |
| 332 | Tcl_GetString(objv[1]), NULL); |
| 333 | return TCL_ERROR; |
| 334 | } |
| 335 | ms = time.sec * 1000 + time.usec / 1000; |
| 336 | Tcl_Sleep(ms); |
| 337 | return TCL_OK; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | /* |
| 342 | *---------------------------------------------------------------------- |
| 343 | * |
| 344 | * <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclHTUUEncodeObjCmd">NsTclHTUUEncodeObjCmd</a> -- |
| 345 | * |
| 346 | * Implements ns_uuencode as obj command. |
| 347 | * |
| 348 | * Results: |
| 349 | * Tcl result. |
| 350 | * |
| 351 | * Side effects: |
| 352 | * See docs. |
| 353 | * |
| 354 | *---------------------------------------------------------------------- |
| 355 | */ |
| 356 | |
| 357 | int |
| 358 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclHTUUEncodeObjCmd">NsTclHTUUEncodeObjCmd</a>(ClientData dummy, Tcl_Interp *interp, int objc, Tcl_Obj **objv) |
| 359 | { |
| 360 | char bufcoded[1 + (4 * 48) / 2]; |
| 361 | char *string; |
| 362 | int nbytes; |
| 363 | |
| 364 | if (objc != 2) { |
| 365 | Tcl_WrongNumArgs(interp, 1, objv, "string"); |
| 366 | return TCL_ERROR; |
| 367 | } |
| 368 | string = Tcl_GetStringFromObj(objv[1], &nbytes); |
| 369 | if (nbytes > 48) { |
| 370 | Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "invalid string \"", |
| 371 | string, "\": must be less than 48 characters", NULL); |
| 372 | return TCL_ERROR; |
| 373 | } |
| 374 | <a href="/cvs/aolserver/aolserver/nsd/uuencode.c#A_Ns_HtuuEncode">Ns_HtuuEncode</a>((unsigned char *) string, (size_t)nbytes, bufcoded); |
| 375 | Tcl_SetResult(interp, bufcoded, TCL_VOLATILE); |
| 376 | return TCL_OK; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | /* |
| 381 | *---------------------------------------------------------------------- |
| 382 | * |
| 383 | * HTUUDecodeObjcmd -- |
| 384 | * |
| 385 | * Implements ns_uudecode as obj command. |
| 386 | * |
| 387 | * Results: |
| 388 | * Tcl result. |
| 389 | * |
| 390 | * Side effects: |
| 391 | * See docs. |
| 392 | * |
| 393 | *---------------------------------------------------------------------- |
| 394 | */ |
| 395 | |
| 396 | int |
| 397 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclHTUUDecodeObjCmd">NsTclHTUUDecodeObjCmd</a>(ClientData dummy, Tcl_Interp *interp, int objc, Tcl_Obj **objv) |
| 398 | { |
| 399 | int n; |
| 400 | char *string, *decoded; |
| 401 | |
| 402 | if (objc != 2) { |
| 403 | Tcl_WrongNumArgs(interp, 1, objv, "string"); |
| 404 | return TCL_ERROR; |
| 405 | } |
| 406 | string = Tcl_GetStringFromObj(objv[1], &n); |
| 407 | n += 3; |
| 408 | decoded = ns_malloc((size_t)n); |
| 409 | n = <a href="/cvs/aolserver/aolserver/nsd/uuencode.c#A_Ns_HtuuDecode">Ns_HtuuDecode</a>(string, (unsigned char *) decoded, n); |
| 410 | decoded[n] = '\0'; |
| 411 | Tcl_SetResult(interp, decoded, (Tcl_FreeProc *) ns_free); |
| 412 | return TCL_OK; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /* |
| 417 | *---------------------------------------------------------------------- |
| 418 | * |
| 419 | * <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclTimeObjCmd">NsTclTimeObjCmd</a> -- |
| 420 | * |
| 421 | * Implements ns_time. |
| 422 | * |
| 423 | * Results: |
| 424 | * Tcl result. |
| 425 | * |
| 426 | * Side effects: |
| 427 | * See docs. |
| 428 | * |
| 429 | *---------------------------------------------------------------------- |
| 430 | */ |
| 431 | |
| 432 | int |
| 433 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclTimeObjCmd">NsTclTimeObjCmd</a>(ClientData dummy, Tcl_Interp *interp, int objc, Tcl_Obj **objv) |
| 434 | { |
| 435 | Ns_Time result, t1, t2; |
| 436 | static CONST char *opts[] = { |
| 437 | "adjust", "diff", "get", "incr", "make", "seconds", |
| 438 | "microseconds", NULL |
| 439 | }; |
| 440 | enum { |
| 441 | TAdjustIdx, TDiffIdx, TGetIdx, TIncrIdx, TMakeIdx, |
| 442 | TSecondsIdx, TMicroSecondsIdx |
| 443 | } _nsmayalias opt; |
| 444 | |
| 445 | if (objc < 2) { |
| 446 | Tcl_SetLongObj(Tcl_GetObjResult(interp), time(NULL)); |
| 447 | } else { |
| 448 | if (Tcl_GetIndexFromObj(interp, objv[1], opts, "option", 0, |
| 449 | (int *) &opt) != TCL_OK) { |
| 450 | return TCL_ERROR; |
| 451 | } |
| 452 | switch (opt) { |
| 453 | case TGetIdx: |
| 454 | Ns_GetTime(&result); |
| 455 | break; |
| 456 | |
| 457 | case TMakeIdx: |
| 458 | if (objc != 3 && objc != 4) { |
| 459 | Tcl_WrongNumArgs(interp, 2, objv, "sec ?usec?"); |
| 460 | return TCL_ERROR; |
| 461 | } |
| 462 | if (Tcl_GetLongFromObj(interp, objv[2], &result.sec) != TCL_OK) { |
| 463 | return TCL_ERROR; |
| 464 | } |
| 465 | if (objc == 3) { |
| 466 | result.usec = 0; |
| 467 | } else if (Tcl_GetLongFromObj(interp, objv[3], &result.usec) != TCL_OK) { |
| 468 | return TCL_ERROR; |
| 469 | } |
| 470 | break; |
| 471 | |
| 472 | case TIncrIdx: |
| 473 | if (objc != 4 && objc != 5) { |
| 474 | Tcl_WrongNumArgs(interp, 2, objv, "time sec ?usec?"); |
| 475 | return TCL_ERROR; |
| 476 | } |
| 477 | if (<a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclGetTimeFromObj">Ns_TclGetTimeFromObj</a>(interp, objv[2], &result) != TCL_OK || |
| 478 | Tcl_GetLongFromObj(interp, objv[3], &t2.sec) != TCL_OK) { |
| 479 | return TCL_ERROR; |
| 480 | } |
| 481 | if (objc == 4) { |
| 482 | t2.usec = 0; |
| 483 | } else if (Tcl_GetLongFromObj(interp, objv[4], &t2.usec) != TCL_OK) { |
| 484 | return TCL_ERROR; |
| 485 | } |
| 486 | Ns_IncrTime(&result, t2.sec, t2.usec); |
| 487 | break; |
| 488 | |
| 489 | case TDiffIdx: |
| 490 | if (objc != 4) { |
| 491 | Tcl_WrongNumArgs(interp, 2, objv, "time1 time2"); |
| 492 | return TCL_ERROR; |
| 493 | } |
| 494 | if (<a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclGetTimeFromObj">Ns_TclGetTimeFromObj</a>(interp, objv[2], &t1) != TCL_OK || |
| 495 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclGetTimeFromObj">Ns_TclGetTimeFromObj</a>(interp, objv[3], &t2) != TCL_OK) { |
| 496 | return TCL_ERROR; |
| 497 | } |
| 498 | Ns_DiffTime(&t1, &t2, &result); |
| 499 | break; |
| 500 | |
| 501 | case TAdjustIdx: |
| 502 | if (objc != 3) { |
| 503 | Tcl_WrongNumArgs(interp, 2, objv, "time"); |
| 504 | return TCL_ERROR; |
| 505 | } |
| 506 | if (<a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclGetTimeFromObj">Ns_TclGetTimeFromObj</a>(interp, objv[2], &result) != TCL_OK) { |
| 507 | return TCL_ERROR; |
| 508 | } |
| 509 | Ns_AdjTime(&result); |
| 510 | break; |
| 511 | |
| 512 | case TSecondsIdx: |
| 513 | case TMicroSecondsIdx: |
| 514 | if (objc != 3) { |
| 515 | Tcl_WrongNumArgs(interp, 2, objv, "time"); |
| 516 | return TCL_ERROR; |
| 517 | } |
| 518 | if (<a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclGetTimeFromObj">Ns_TclGetTimeFromObj</a>(interp, objv[2], &result) != TCL_OK) { |
| 519 | return TCL_ERROR; |
| 520 | } |
| 521 | Tcl_SetLongObj(Tcl_GetObjResult(interp), |
| 522 | opt == TSecondsIdx ? result.sec : result.usec); |
| 523 | return TCL_OK; |
| 524 | break; |
| 525 | } |
| 526 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclSetTimeObj">Ns_TclSetTimeObj</a>(Tcl_GetObjResult(interp), &result); |
| 527 | } |
| 528 | return TCL_OK; |
| 529 | } |
| 530 | |
| 531 | |
| 532 | /* |
| 533 | *---------------------------------------------------------------------- |
| 534 | * |
| 535 | * <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclStrftimeObjCmd">NsTclStrftimeObjCmd</a> -- |
| 536 | * |
| 537 | * Implements ns_fmttime. |
| 538 | * |
| 539 | * Results: |
| 540 | * Tcl result. |
| 541 | * |
| 542 | * Side effects: |
| 543 | * See docs. |
| 544 | * |
| 545 | *---------------------------------------------------------------------- |
| 546 | */ |
| 547 | |
| 548 | int |
| 549 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclStrftimeObjCmd">NsTclStrftimeObjCmd</a>(ClientData dummy, Tcl_Interp *interp, int objc, Tcl_Obj **objv) |
| 550 | { |
| 551 | char *fmt, buf[200]; |
| 552 | time_t time; |
| 553 | |
| 554 | if (objc != 2 && objc != 3) { |
| 555 | Tcl_WrongNumArgs(interp, 1, objv, "time ?fmt?"); |
| 556 | return TCL_ERROR; |
| 557 | } |
| 558 | if (Tcl_GetLongFromObj(interp, objv[1], &time) != TCL_OK) { |
| 559 | return TCL_ERROR; |
| 560 | } |
| 561 | if (objc > 2) { |
| 562 | fmt = Tcl_GetString(objv[2]); |
| 563 | } else { |
| 564 | fmt = "%c"; |
| 565 | } |
| 566 | if (strftime(buf, sizeof(buf), fmt, ns_localtime(&time)) == 0) { |
| 567 | Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "invalid time: ", |
| 568 | Tcl_GetString(objv[1]), NULL); |
| 569 | return TCL_ERROR; |
| 570 | } |
| 571 | Tcl_SetResult(interp, buf, TCL_VOLATILE); |
| 572 | return TCL_OK; |
| 573 | } |
| 574 | |
| 575 | |
| 576 | /* |
| 577 | *---------------------------------------------------------------------- |
| 578 | * |
| 579 | * <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclCrashCmd">NsTclCrashCmd</a> -- |
| 580 | * |
| 581 | * Crash the server to test exception handling. |
| 582 | * |
| 583 | * Results: |
| 584 | * None. |
| 585 | * |
| 586 | * Side effects: |
| 587 | * Server will segfault. |
| 588 | * |
| 589 | *---------------------------------------------------------------------- |
| 590 | */ |
| 591 | |
| 592 | int |
| 593 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_NsTclCrashCmd">NsTclCrashCmd</a>(ClientData dummy, Tcl_Interp *interp, int argc, char **argv) |
| 594 | { |
| 595 | char *death; |
| 596 | |
| 597 | death = NULL; |
| 598 | *death = 1; |
| 599 | |
| 600 | return TCL_ERROR; |
| 601 | } |
| 602 | |
| 603 | |
| 604 | /* |
| 605 | *---------------------------------------------------------------------- |
| 606 | * |
| 607 | * NsTclGetNative -- |
| 608 | * |
| 609 | * Gets a string from a Tcl_Obj in the system encoding, suitable |
| 610 | * for passing to system calls |
| 611 | * |
| 612 | * Results: |
| 613 | * native string |
| 614 | * |
| 615 | * Side effects: |
| 616 | * Memory is allocated. Caller should free result |
| 617 | * |
| 618 | *---------------------------------------------------------------------- |
| 619 | */ |
| 620 | |
| 621 | char * |
| 622 | NsTclGetNative(Tcl_Obj *objPtr) |
| 623 | { |
| 624 | Tcl_DString ds; |
| 625 | char *encoded, *native; |
| 626 | int len; |
| 627 | encoded=Tcl_GetStringFromObj(objPtr, &len); |
| 628 | native=ns_strdup(Tcl_UtfToExternalDString(NULL,encoded,len,&ds)); |
| 629 | Tcl_DStringFree(&ds); |
| 630 | return native; |
| 631 | } |
| 632 | |
| 633 | |
| 634 | |
| 635 | /* |
| 636 | *---------------------------------------------------------------------- |
| 637 | * |
| 638 | * <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_WordEndsInSemi">WordEndsInSemi</a> -- |
| 639 | * |
| 640 | * Does this word end in a semicolon or a space? |
| 641 | * |
| 642 | * Results: |
| 643 | * 1 if semi, 0 if space. |
| 644 | * |
| 645 | * Side effects: |
| 646 | * Undefined behavior if string does not end in null |
| 647 | * |
| 648 | *---------------------------------------------------------------------- |
| 649 | */ |
| 650 | |
| 651 | static int |
| 652 | <a href="/cvs/aolserver/aolserver/nsd/tclmisc.c#A_WordEndsInSemi">WordEndsInSemi</a>(char *ip) |
| 653 | { |
| 654 | if (ip == NULL) { |
| 655 | return 0; |
| 656 | } |
| 657 | /* advance past the first '&' so we can check for a second |
| 658 | (i.e. to handle "ben&jerry ") |
| 659 | */ |
| 660 | if (*ip == '&') { |
| 661 | ip++; |
| 662 | } |
| 663 | while((*ip != '\0') && (*ip != ' ') && (*ip != ';') && (*ip != '&')) { |
| 664 | ip++; |
| 665 | } |
| 666 | if (*ip == ';') { |
| 667 | return 1; |
| 668 | } else { |
| 669 | return 0; |
| 670 | } |
| 671 | } |
Copyright © 2010 Geeknet, Inc. All rights reserved. Terms of Use