View Source

[modwtcl]
apreq.h
dist/
libapreq1/
mod_wtcl.c
mod_wtcl.h
setupExclude.txt
util/
win.def
win.mak
wt1.1/
WtAppTable.c
WtAppTable.h
WtBasicCmds.c
WtBasicCmds.h
WtClientRequest.c
WtClientRequest.h
WtCollection.c
WtCollection.h
WtCollectionCmds.c
WtCollectionCmds.h
WtContext.c
WtContext.h
WtContextEvents.c
WtContextEvents.h
WtCookie.c
WtCookie.h
WtDbSession.c
WtDbSession.h
WtExecute.c
WtExecute.h
WtHtmlEntities.c
WtHtmlEntities.h
WtInitCmds.c
WtInitCmds.h
WtMtTable.c
WtMtTable.h
WtMultiTable.c
WtMultiTable.h
WtOS.h
WtProcSession.c
WtProcSession.h
WtResponse.c
WtResponse.h
WtServerCmds.c
WtServerCmds.h
WtSession.c
WtSession.h
WtSettings.c
WtSettings.h
WtTable.c
WtTable.h
WtTableCmds.c
WtTableCmds.h
WtTableUtil.c
WtTableUtil.h
WtUpload.c
WtUpload.h
WtUtil.c
WtUtil.h
WtWebErrors.c
WtWebErrors.h
WtWindows.h
File: / archive / modwtcl / WtTableCmds.c

Lines Size Modified Created Owner MIME Types
834 22,438 2010/05/08 18:46:41 2011/06/13 15:35:15 BUILTIN\Administrators text/x-csrc

0001
/*
0002
 * Copyright 2001 Alexander Boverman and the original authors.
0003
 * 
0004
 * Licensed under the Apache License, Version 2.0 (the "License");
0005
 * you may not use this file except in compliance with the License.
0006
 * You may obtain a copy of the License at
0007
 * 
0008
 *      http://www.apache.org/licenses/LICENSE-2.0
0009
 * 
0010
 * Unless required by applicable law or agreed to in writing, software
0011
 * distributed under the License is distributed on an "AS IS" BASIS,
0012
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013
 * See the License for the specific language governing permissions and
0014
 * limitations under the License.
0015
 */
0016
0017
#include "WtTableCmds.h"
0018
#include "WtTable.h"
0019
#include "WtTableUtil.h"
0020
#include "WtUtil.h"
0021
#include "WtBasicCmds.h"
0022
0023
/* Create a custom command that operates on a table */
0024
0025
Tcl_Command WtCreateTableCmd(const char *cmdName,
0026
    Tcl_Obj **tblPtr, Tcl_ObjType *type, Tcl_Interp *interp)
0027
{
0028
  WtTableCmdData *tcd = (WtTableCmdData *)ckalloc(sizeof(WtTableCmdData));
0029
  tcd->tablePtr = tblPtr;
0030
  tcd->type = type;
0031
  return Tcl_CreateObjCommand(interp, cmdName, WtNamedTableCmd,
0032
    (ClientData)tcd, WtDeleteTableCmdProc);
0033
}
0034
0035
void WtDeleteTableCmdProc(ClientData clientData)
0036
{
0037
  if (clientData) {
0038
    ckfree(clientData);
0039
  }
0040
}
0041
0042
/* The custom table command procedure */
0043
0044
int WtNamedTableCmd(ClientData clientData, Tcl_Interp *interp,
0045
    int objc, Tcl_Obj *const objv[])
0046
{
0047
  int ret;
0048
  WtTableCmdData *tcd = (WtTableCmdData *)clientData;
0049
  WtTableMethodHelper(clientData, interp, objc, objv, &ret,
0050
    tcd->tablePtr, tcd->type, 0, 1);
0051
  return ret;
0052
}
0053
0054
/* Get a table variable */
0055
0056
int WtTblCmdGetVar(Tcl_Obj *var, Tcl_Obj **val,
0057
    Tcl_ObjType *type, Tcl_Interp *interp)
0058
{
0059
  int ok = 1;
0060
  *val = Tcl_ObjGetVar2(interp, var, NULL, 0);
0061
  if (*val && !WtConvertToTableObj(*val, type, interp)) {
0062
    ok = 0;
0063
    *val = NULL;
0064
  }
0065
  return ok;
0066
}
0067
0068
/* Get and unshare a table variable */
0069
0070
int WtTblCmdOwnVar(Tcl_Obj *var, Tcl_Obj **val, Tcl_ObjType *type,
0071
    int *created, Tcl_Interp *interp)
0072
{
0073
  int ok = 1;
0074
  Tcl_Obj *obj;
0075
0076
  *val = NULL;
0077
  *created = 0;
0078
0079
  obj = Tcl_ObjGetVar2(interp, var, NULL, 0);
0080
0081
  if (!obj) {
0082
    if (obj = WtNewTableObjWithType(type)) {
0083
      Tcl_IncrRefCount(obj);
0084
      *val = obj;
0085
      *created = 1;
0086
      ok = 1;
0087
    }
0088
  } else if (Tcl_IsShared(obj)) {
0089
    if (obj = Tcl_DuplicateObj(obj)) {
0090
      Tcl_IncrRefCount(obj);
0091
      if (WtConvertToTableObj(obj, type, interp)) {
0092
        *val = obj;
0093
        *created = 1;
0094
        ok = 1;
0095
      } else {
0096
        Tcl_DecrRefCount(obj);
0097
      }
0098
    }
0099
  } else if (WtConvertToTableObj(obj, type, interp)) {
0100
    *val = obj;
0101
    ok = 1;
0102
  }
0103
0104
  return ok;
0105
}
0106
0107
/* Set a table variable */
0108
0109
Tcl_Obj *WtTblCmdSetVar(Tcl_Obj *var, Tcl_Obj *val,
0110
    Tcl_Interp *interp)
0111
{
0112
  Tcl_Obj *ret;
0113
  Tcl_IncrRefCount(val);
0114
  ret = Tcl_ObjSetVar2(interp, var, NULL, val, 0);
0115
  Tcl_DecrRefCount(val);
0116
  return ret;
0117
}
0118
0119
/* Get a table value */
0120
0121
Tcl_Obj *WtTblCmdGetVal(Tcl_Obj *val, Tcl_ObjType *type,
0122
    Tcl_Interp *interp)
0123
{
0124
  return WtConvertToTableObj(val, type, interp) ? val : NULL;
0125
}
0126
0127
/* Helper to manage table objects. */
0128
0129
WtTableCmdStatus WtTableCmdHelper(ClientData clientData, Tcl_Interp *interp,
0130
    int objc, Tcl_Obj *const objv[], int *tclRet, Tcl_ObjType *type,
0131
    int readOnly, int appendUsage)
0132
{
0133
  int status = WT_TBL_CMD_DONE, ret = TCL_ERROR, error = 0;
0134
  int listLen, created = 0, size;
0135
  WtContext *w;
0136
  char *subCmd;
0137
  Tcl_Obj *tbl, *obj;
0138
0139
  w = WtGetAssocContext(interp);
0140
0141
  if (objc < 2) {
0142
    if (appendUsage) {
0143
      WtTableCmdUsageError(interp, objv[0], readOnly);
0144
    }
0145
0146
    *tclRet = TCL_ERROR;
0147
0148
    return WT_TBL_CMD_MISSING;
0149
  }
0150
0151
  subCmd = WtToString(objv[1]);
0152
0153
  if (!strcmp(subCmd, "create")) {
0154
0155
    /* create command */
0156
0157
    if (objc >= 2) {
0158
      tbl = WtNewTableObjWithElements(type, objc - 2, (const Tcl_Obj **)(objv + 2), interp);
0159
      if (tbl) {
0160
        Tcl_SetObjResult(interp, tbl);
0161
        ret = TCL_OK;
0162
      }
0163
    } else if (objc == 3) {
0164
      if (WtConvertToTableObj(objv[2], type, interp)) {
0165
        Tcl_SetObjResult(interp, objv[2]);
0166
        ret = TCL_OK;
0167
      }
0168
    } else {
0169
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0170
        WtToString(objv[0]), " ", WtToString(objv[1]), " ?items?", NULL);
0171
    }
0172
0173
  } else if (!strcmp(subCmd, "count")) {
0174
0175
    /* count command */
0176
0177
    if (objc != 3) {
0178
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0179
        WtToString(objv[0]), " ", WtToString(objv[1]), " table", NULL);
0180
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0181
      size = WtTableSize(tbl);
0182
      Tcl_SetObjResult(interp, Tcl_NewIntObj(size));
0183
      ret = TCL_OK;
0184
    }
0185
0186
  } else if (!strcmp(subCmd, "has")) {
0187
0188
    /* has command */
0189
0190
    if (objc != 4) {
0191
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0192
        WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0193
        " table key", NULL);
0194
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0195
      Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
0196
        WtTableHas(tbl, objv[3])));
0197
      ret = TCL_OK;
0198
    }
0199
0200
  } else if (!strcmp(subCmd, "keys")) {
0201
0202
    /* keys command */
0203
0204
    if (objc != 3) {
0205
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0206
        WtToString(objv[0]), " ", WtToString(objv[1]), " table", NULL);
0207
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0208
      if (WtTableKeys(tbl, &obj, interp)) {
0209
        Tcl_SetObjResult(interp, obj);
0210
        ret = TCL_OK;
0211
      }
0212
    }
0213
0214
  } else if (!strcmp(subCmd, "values")) {
0215
0216
    /* values command */
0217
0218
    if (objc != 3) {
0219
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0220
        WtToString(objv[0]), " ", WtToString(objv[1]), " table", NULL);
0221
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0222
      if (WtTableValues(tbl, &obj, interp)) {
0223
        Tcl_SetObjResult(interp, obj);
0224
        ret = TCL_OK;
0225
      }
0226
    }
0227
0228
  } else if (!strcmp(subCmd, "get")) {
0229
0230
    /* get command */
0231
0232
    if (objc < 4 || objc > 5) {
0233
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0234
        WtToString(objv[0]), " ",
0235
        WtToString(objv[1]), " table key ?defaultValue?", NULL);
0236
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0237
      obj = WtTableGet(tbl, objv[3]);
0238
      if (obj) {
0239
        Tcl_SetObjResult(interp, obj);
0240
        ret = TCL_OK;
0241
      } else if (objc > 4) {
0242
        Tcl_SetObjResult(interp, objv[4]);
0243
        ret = TCL_OK;
0244
      } else {
0245
        Tcl_AppendResult(interp, "No entry with key \"",
0246
          WtToString(objv[3]), "\" found.", NULL);
0247
      }
0248
    }
0249
0250
  } else if (!strcmp(subCmd, "set") && !readOnly) {
0251
0252
    /* set command */
0253
0254
    if (objc != 5) {
0255
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0256
        WtToString(objv[0]), " ", WtToString(objv[1]),
0257
        " table key value", NULL);
0258
    } else if (WtTblCmdOwnVar(objv[2], &tbl, type, &created, interp)) {
0259
      WtTableSet(tbl, objv[3], objv[4]);
0260
      if (WtTblCmdSetVar(objv[2], tbl, interp)) {
0261
        Tcl_SetObjResult(interp, objv[4]);
0262
        ret = TCL_OK;
0263
      }
0264
    }
0265
0266
  } else if (!strcmp(subCmd, "setDefault") && !readOnly) {
0267
0268
    /* setDefault command */
0269
0270
    if (objc != 5) {
0271
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0272
        WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0273
        " table key value", NULL);
0274
    } else if (WtTblCmdGetVar(objv[2], &tbl, type, interp)) {
0275
      if (tbl && (obj = WtTableGet(tbl, objv[3]))) {
0276
        Tcl_SetObjResult(interp, obj);
0277
        ret = TCL_OK;
0278
      } else if (WtTblCmdOwnVar(objv[2], &tbl, type, &created, interp)) {
0279
        WtTableSet(tbl, objv[3], objv[4]);
0280
        if (WtTblCmdSetVar(objv[2], tbl, interp)) {
0281
          Tcl_SetObjResult(interp, objv[4]);
0282
          ret = TCL_OK;
0283
        }
0284
      }
0285
    }
0286
0287
  } else if (!strcmp(subCmd, "lappend") && !readOnly) {
0288
0289
    /* lappend command */
0290
0291
    if (objc != 5) {
0292
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0293
        WtToString(objv[0]), " ", WtToString(objv[1]),
0294
        " table key value", NULL);
0295
    } else if (WtTblCmdOwnVar(objv[2], &tbl, type, &created, interp)) {
0296
      if (WtTableAppendToList(tbl, objv[3], objv[4], &obj, interp)) {
0297
        if (WtTblCmdSetVar(objv[2], tbl, interp)) {
0298
          Tcl_SetObjResult(interp, obj);
0299
          ret = TCL_OK;
0300
        }
0301
      }
0302
    }
0303
0304
  } else if (!strcmp(subCmd, "remove") && !readOnly) {
0305
0306
    /* remove command */
0307
0308
    if (objc != 4) {
0309
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0310
        WtToString(objv[0]), " ", WtToString(objv[1]), " table key", NULL);
0311
    } else if (WtTblCmdOwnVar(objv[2], &tbl, type, &created, interp)) {
0312
      WtTableRemove(tbl, objv[3]);
0313
      if (WtTblCmdSetVar(objv[2], tbl, interp)) {
0314
        ret = TCL_OK;
0315
      }
0316
    }
0317
0318
  } else if (!strcmp(subCmd, "removeList") && !readOnly) {
0319
0320
    /* removeList command */
0321
0322
    if (objc != 4) {
0323
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0324
        WtToString(objv[0]), " ", WtToString(objv[1]), " table list", NULL);
0325
    } else {
0326
      if (Tcl_ListObjLength(interp, objv[3], &listLen) != TCL_OK) {
0327
        Tcl_AppendResult(interp,
0328
          "Value must be a list. Usage: ", WtToString(objv[0]), " ",
0329
          WtToString(objv[1]), " table list", NULL);
0330
      } else if (WtTblCmdOwnVar(objv[2], &tbl, type, &created, interp)) {
0331
        if (WtTableRemoveList(tbl, objv[3], interp)) {
0332
          if (WtTblCmdSetVar(objv[2], tbl, interp)) {
0333
            ret = TCL_OK;
0334
          }
0335
        }
0336
      }
0337
    }
0338
0339
  } else if (!strcmp(subCmd, "items")) {
0340
0341
    /* items command */
0342
0343
    if (objc != 3) {
0344
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0345
        WtToString(objv[0]), " ", WtToString(objv[1]), " table", NULL);
0346
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0347
      Tcl_SetObjResult(interp, tbl);
0348
      ret = TCL_OK;
0349
    }
0350
0351
  } else if (!strcmp(subCmd, "list")) {
0352
0353
    /* list command */
0354
0355
    if (objc != 3) {
0356
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0357
        WtToString(objv[0]), " ", WtToString(objv[1]),
0358
        " table", NULL);
0359
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0360
      if (obj = WtTableToList(tbl, 0, interp)) {
0361
        Tcl_SetObjResult(interp, obj);
0362
        ret = TCL_OK;
0363
      }
0364
    }
0365
0366
  } else if (!strcmp(subCmd, "array")) {
0367
0368
    /* array command */
0369
0370
    if (objc != 4) {
0371
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0372
        WtToString(objv[0]), " ", WtToString(objv[1]),
0373
        " table arrayName", NULL);
0374
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0375
      if (WtTableToArray(interp, tbl, objv[3], 0)) {
0376
        ret = TCL_OK;
0377
      }
0378
    }
0379
0380
  } else if (!strcmp(subCmd, "copy")) {
0381
0382
    /* copy command */
0383
0384
    if (objc != 3) {
0385
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0386
        WtToString(objv[0]), " ", WtToString(objv[1]), " table", NULL);
0387
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0388
      if (obj = Tcl_DuplicateObj(tbl)) {
0389
        Tcl_SetObjResult(interp, obj);
0390
        ret = TCL_OK;
0391
      }
0392
    }
0393
0394
  } else if (!strcmp(subCmd, "clear") && !readOnly) {
0395
0396
    /* clear command */
0397
0398
    if (objc != 3) {
0399
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0400
        WtToString(objv[0]), " ", WtToString(objv[1]), " table", NULL);
0401
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0402
      WtClearTable(tbl);
0403
      ret = TCL_OK;
0404
    }
0405
0406
  } else if (!strcmp(subCmd, "importVars")) {
0407
0408
    /* importVars command */
0409
0410
    if (objc != 3) {
0411
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0412
        WtToString(objv[0]), " ", WtToString(objv[1]), " table", NULL);
0413
    } else if (tbl = WtTblCmdGetVal(objv[2], type, interp)) {
0414
      if (WtImportTableVars(tbl, interp)) {
0415
        ret = TCL_OK;
0416
      }
0417
    }
0418
0419
  } else {
0420
0421
    /* unknown command */
0422
0423
    status = WT_TBL_CMD_NOT_FOUND;
0424
0425
    if (appendUsage) {
0426
      WtTableCmdUsageError(interp, objv[0], readOnly);
0427
    }
0428
0429
  }
0430
0431
  *tclRet = ret;
0432
0433
  if (created && tbl) {
0434
    Tcl_DecrRefCount(tbl);
0435
  }
0436
0437
  return status;
0438
}
0439
0440
void WtTableCmdUsageError(Tcl_Interp *interp, Tcl_Obj *cmd, int readOnly)
0441
{
0442
  Tcl_Obj *interpRes = Tcl_GetObjResult(interp);
0443
  Tcl_AppendResult(interp, wtBadUsagePrefix2, NULL);
0444
  WtTableCmdAppendUsage(interp, cmd, readOnly);
0445
}
0446
0447
void WtTableCmdAppendUsage(Tcl_Interp *interp, Tcl_Obj *cmd, int readOnly)
0448
{
0449
  Tcl_Obj *res = Tcl_GetObjResult(interp);
0450
  char *cmdStr = WtToString(cmd);
0451
0452
  if (readOnly) {
0453
    Tcl_AppendStringsToObj(res,
0454
      cmdStr, " create ?items?\n",
0455
      cmdStr, " count table\n",
0456
      cmdStr, " has table key\n",
0457
      cmdStr, " keys table\n",
0458
      cmdStr, " values table\n",
0459
      cmdStr, " get table key ?defaultValue?\n",
0460
      cmdStr, " items table\n",
0461
      cmdStr, " list table\n",
0462
      cmdStr, " array table arrayName\n",
0463
      cmdStr, " copy table\n",
0464
      NULL);
0465
  } else {
0466
    Tcl_AppendStringsToObj(res,
0467
      cmdStr, " create ?items?\n",
0468
      cmdStr, " count\n",
0469
      cmdStr, " has table key\n",
0470
      cmdStr, " keys table\n",
0471
      cmdStr, " values table\n",
0472
      cmdStr, " get table key ?defaultValue?\n",
0473
      cmdStr, " set table key value\n",
0474
      cmdStr, " setDefault table key value\n",
0475
      cmdStr, " append table key value\n",
0476
      cmdStr, " add table key value\n",
0477
      cmdStr, " remove table key\n",
0478
      cmdStr, " removeList table keys\n",
0479
      cmdStr, " items table\n",
0480
      cmdStr, " list table\n",
0481
      cmdStr, " array table arrayName\n",
0482
      cmdStr, " copy table\n",
0483
      cmdStr, " clear table\n",
0484
      NULL);
0485
  }
0486
}
0487
0488
int WtTableCmd(ClientData clientData, Tcl_Interp *interp,
0489
    int objc, Tcl_Obj *const objv[])
0490
{
0491
  int ret;
0492
  WtTableCmdHelper(clientData, interp, objc, objv, &ret, &WtTableType, 0, 1);
0493
  return ret;
0494
}
0495
0496
int WtCaselessTableCmd(ClientData clientData, Tcl_Interp *interp,
0497
    int objc, Tcl_Obj *const objv[])
0498
{
0499
  int ret;
0500
  WtTableCmdHelper(clientData, interp, objc, objv, &ret,
0501
    &WtCaselessTableType, 0, 1);
0502
  return ret;
0503
}
0504
0505
/* Helper to manage table objects. Implements the "object method args"
0506
   command style. */
0507
0508
WtTableCmdStatus WtTableMethodHelper(ClientData clientData, Tcl_Interp *interp,
0509
    int objc, Tcl_Obj *const objv[], int *tclRet, Tcl_Obj **tblPtr,
0510
    Tcl_ObjType *type, int readOnly, int appendUsage)
0511
{
0512
  int status = WT_TBL_CMD_DONE, ret = TCL_ERROR, error = 0;
0513
  int listLen;
0514
  WtContext *w;
0515
  char *subCmd;
0516
  Tcl_Obj *obj, *tbl;
0517
0518
  w = WtGetAssocContext(interp);
0519
0520
  if (objc < 2) {
0521
    if (appendUsage) {
0522
      WtTableMethodUsageError(interp, objv[0], readOnly);
0523
    }
0524
0525
    *tclRet = TCL_ERROR;
0526
0527
    return WT_TBL_CMD_MISSING;
0528
  }
0529
0530
  subCmd = WtToString(objv[1]);
0531
  tbl = *tblPtr;
0532
0533
  if (!strcmp(subCmd, "count")) {
0534
0535
    /* count command */
0536
0537
    if (objc != 2) {
0538
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0539
        WtToString(objv[0]), " ", WtToString(objv[1]), NULL);
0540
    } else if (WtConvertToTableObj(tbl, type, interp)) {
0541
      Tcl_SetObjResult(interp, Tcl_NewIntObj(WtTableSize(tbl)));
0542
      ret = TCL_OK;
0543
    }
0544
0545
  } else if (!strcmp(subCmd, "has")) {
0546
0547
    /* has command */
0548
0549
    if (objc != 3) {
0550
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0551
        WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0552
        " key", NULL);
0553
    } else if (WtConvertToTableObj(tbl, type, interp)) {
0554
      Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
0555
        WtTableHas(tbl, objv[2])));
0556
      ret = TCL_OK;
0557
    }
0558
0559
  } else if (!strcmp(subCmd, "keys")) {
0560
0561
    /* keys command */
0562
0563
    if (objc != 2) {
0564
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0565
        WtToString(objv[0]), " ", WtToString(objv[1]), NULL);
0566
    } else if (WtConvertToTableObj(tbl, type, interp)) {
0567
      if (WtTableKeys(tbl, &obj, interp)) {
0568
        Tcl_SetObjResult(interp, obj);
0569
        ret = TCL_OK;
0570
      }
0571
    }
0572
0573
  } else if (!strcmp(subCmd, "values")) {
0574
0575
    /* values command */
0576
0577
    if (objc != 2) {
0578
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0579
        WtToString(objv[0]), " ", WtToString(objv[1]), NULL);
0580
    } else if (WtConvertToTableObj(tbl, type, interp)) {
0581
      if (WtTableValues(tbl, &obj, interp)) {
0582
        Tcl_SetObjResult(interp, obj);
0583
        ret = TCL_OK;
0584
      }
0585
    }
0586
0587
  } else if (!strcmp(subCmd, "get")) {
0588
0589
    /* get command */
0590
0591
    if (objc < 3 || objc > 4) {
0592
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0593
        WtToString(objv[0]), " ",
0594
        WtToString(objv[1]), " key ?defaultValue?", NULL);
0595
    } else if (WtConvertToTableObj(tbl, type, interp)) {
0596
      obj = WtTableGet(tbl, objv[2]);
0597
      if (obj) {
0598
        Tcl_SetObjResult(interp, obj);
0599
        ret = TCL_OK;
0600
      } else if (objc > 3) {
0601
        Tcl_SetObjResult(interp, objv[3]);
0602
        ret = TCL_OK;
0603
      } else {
0604
        Tcl_AppendResult(interp, "No entry with key \"",
0605
          WtToString(objv[2]), "\" found.", NULL);
0606
      }
0607
    }
0608
0609
  } else if (!strcmp(subCmd, "set") && !readOnly) {
0610
0611
    /* set command */
0612
0613
    if (objc != 4) {
0614
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0615
        WtToString(objv[0]), " ", WtToString(objv[1]),
0616
        " key value", NULL);
0617
    } else if (WtOwnTableObj(tblPtr, type, interp)) {
0618
      tbl = *tblPtr;
0619
      WtTableSet(tbl, objv[2], objv[3]);
0620
      Tcl_SetObjResult(interp, objv[3]);
0621
      ret = TCL_OK;
0622
    }
0623
0624
  } else if (!strcmp(subCmd, "setDefault") && !readOnly) {
0625
0626
    /* setDefault command */
0627
0628
    if (objc != 4) {
0629
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0630
        WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0631
        " key value", NULL);
0632
    } else if (WtConvertToTableObj(tbl, type, interp)) {
0633
      obj = WtTableGet(tbl, objv[2]);
0634
      if (obj) {
0635
        Tcl_SetObjResult(interp, obj);
0636
        ret = TCL_OK;
0637
      } else if (WtOwnTableObj(tblPtr, type, interp)) {
0638
        tbl = *tblPtr;
0639
        WtTableSet(tbl, objv[2], objv[3]);
0640
        Tcl_SetObjResult(interp, objv[3]);
0641
        ret = TCL_OK;
0642
      }
0643
    }
0644
0645
  } else if (!strcmp(subCmd, "add") && !readOnly) {
0646
0647
    /* add command */
0648
0649
    if (objc != 4) {
0650
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0651
        WtToString(objv[0]), " ", WtToString(objv[1]),
0652
        " key value", NULL);
0653
    } else if (WtOwnTableObj(tblPtr, type, interp)) {
0654
      tbl = *tblPtr;
0655
      if (WtTableAppendToList(tbl, objv[2], objv[3], &obj, interp)) {
0656
        Tcl_SetObjResult(interp, obj);
0657
        ret = TCL_OK;
0658
      }
0659
    }
0660
0661
  } else if (!strcmp(subCmd, "remove") && !readOnly) {
0662
0663
    /* remove command */
0664
0665
    if (objc != 3) {
0666
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0667
        WtToString(objv[0]), " ", WtToString(objv[1]), " key", NULL);
0668
    } else if (WtOwnTableObj(tblPtr, type, interp)) {
0669
      tbl = *tblPtr;
0670
      WtTableRemove(tbl, objv[2]);
0671
      ret = TCL_OK;
0672
    }
0673
0674
  } else if (!strcmp(subCmd, "removeList") && !readOnly) {
0675
0676
    /* removeList command */
0677
0678
    if (objc != 3) {
0679
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0680
        WtToString(objv[0]), " ", WtToString(objv[1]), " list", NULL);
0681
    } else {
0682
      if (Tcl_ListObjLength(interp, objv[2], &listLen) != TCL_OK) {
0683
        Tcl_AppendResult(interp,
0684
          "Value must be a list. Usage: ",
0685
          WtToString(objv[0]), " ", WtToString(objv[1]), " list", NULL);
0686
      } else if (WtOwnTableObj(tblPtr, type, interp)) {
0687
        tbl = *tblPtr;
0688
        if (WtTableRemoveList(tbl, objv[2], interp)) {
0689
          ret = TCL_OK;
0690
        }
0691
      }
0692
    }
0693
0694
  } else if (!strcmp(subCmd, "items")) {
0695
0696
    /* items command */
0697
0698
    if (objc != 2) {
0699
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0700
        WtToString(objv[0]), " ", WtToString(objv[1]), NULL);
0701
    } else if (WtConvertToTableObj(tbl, type, interp)) {
0702
      Tcl_SetObjResult(interp, tbl);
0703
      ret = TCL_OK;
0704
    }
0705
0706
  } else if (!strcmp(subCmd, "list")) {
0707
0708
    /* list command */
0709
0710
    if (objc != 2) {
0711
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0712
        WtToString(objv[0]), " ", WtToString(objv[1]), NULL);
0713
    } else if (WtConvertToTableObj(tbl, type, interp)) {
0714
      if (obj = WtTableToList(tbl, 0, interp)) {
0715
        Tcl_SetObjResult(interp, obj);
0716
        ret = TCL_OK;
0717
      }
0718
    }
0719
0720
  } else if (!strcmp(subCmd, "array")) {
0721
0722
    /* array command */
0723
0724
    if (objc != 3) {
0725
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0726
        WtToString(objv[0]), " ", WtToString(objv[1]), " arrayName",
0727
        NULL);
0728
    } else if (WtConvertToTableObj(tbl, type, interp)) {
0729
      if (WtTableToArray(interp, tbl, objv[2], 0)) {
0730
        ret = TCL_OK;
0731
      }
0732
    }
0733
0734
  } else if (!strcmp(subCmd, "copy")) {
0735
0736
    /* copy command */
0737
0738
    if (WtConvertToTableObj(tbl, type, interp)) {
0739
      Tcl_SetObjResult(interp, Tcl_DuplicateObj(tbl));
0740
      ret = TCL_OK;
0741
    }
0742
0743
  } else if (!strcmp(subCmd, "clear") && !readOnly) {
0744
0745
    /* clear command */
0746
0747
    if (WtOwnTableObj(tblPtr, type, interp)) {
0748
      tbl = *tblPtr;
0749
      WtClearTable(tbl);
0750
      ret = TCL_OK;
0751
    }
0752
0753
  } else if (!strcmp(subCmd, "importVars")) {
0754
0755
    /* importVars command */
0756
0757
    if (objc != 2) {
0758
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0759
        WtToString(objv[0]), " ", WtToString(objv[1]),
0760
        NULL);
0761
    } else if (WtImportTableVars(tbl, interp)) {
0762
      ret = TCL_OK;
0763
    }
0764
0765
  } else {
0766
0767
    /* unknown command */
0768
0769
    status = WT_TBL_CMD_NOT_FOUND;
0770
0771
    if (appendUsage) {
0772
      WtTableMethodUsageError(interp, objv[0], readOnly);
0773
    }
0774
0775
  }
0776
0777
  *tclRet = ret;
0778
0779
  return status;
0780
}
0781
0782
void WtTableMethodUsageError(Tcl_Interp *interp, Tcl_Obj *cmd, int readOnly)
0783
{
0784
  Tcl_Obj *interpRes = Tcl_GetObjResult(interp);
0785
  Tcl_AppendResult(interp, wtBadUsagePrefix2, NULL);
0786
  WtTableMethodAppendUsage(interp, cmd, readOnly);
0787
}
0788
0789
void WtTableMethodAppendUsage(Tcl_Interp *interp, Tcl_Obj *cmd, int readOnly)
0790
{
0791
  Tcl_Obj *res = Tcl_GetObjResult(interp);
0792
  char *cmdStr = WtToString(cmd);
0793
0794
  if (readOnly) {
0795
    Tcl_AppendStringsToObj(res,
0796
      cmdStr, " count\n",
0797
      cmdStr, " has key\n",
0798
      cmdStr, " keys\n",
0799
      cmdStr, " values\n",
0800
      cmdStr, " get key ?defaultValue?\n",
0801
      cmdStr, " items\n",
0802
      cmdStr, " list\n",
0803
      cmdStr, " array arrayName\n",
0804
      cmdStr, " copy\n",
0805
      cmdStr, " importVars\n",
0806
      NULL);
0807
  } else {
0808
    Tcl_AppendStringsToObj(res,
0809
      cmdStr, " count\n",
0810
      cmdStr, " has key\n",
0811
      cmdStr, " keys\n",
0812
      cmdStr, " values\n",
0813
      cmdStr, " get key ?defaultValue?\n",
0814
      cmdStr, " set key value\n",
0815
      cmdStr, " setDefault key value\n",
0816
      cmdStr, " append key value\n",
0817
      cmdStr, " add key value\n",
0818
      cmdStr, " remove key\n",
0819
      cmdStr, " removeList keys\n",
0820
      cmdStr, " items\n",
0821
      cmdStr, " list\n",
0822
      cmdStr, " array arrayName\n",
0823
      cmdStr, " copy\n",
0824
      cmdStr, " clear\n",
0825
      cmdStr, " importVars\n",
0826
      NULL);
0827
  }
0828
}
0829
0830
void WtInitTableCommands(Tcl_Interp *interp)
0831
{
0832
  Tcl_CreateObjCommand(interp, "table", WtTableCmd, NULL, NULL);
0833
  Tcl_CreateObjCommand(interp, "ciTable", WtCaselessTableCmd, NULL, NULL);
0834
}