home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / tclX6.5c / experimental / tcldbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-30  |  5.7 KB  |  207 lines

  1. /* 
  2.  * tcldbm.c --
  3.  *
  4.  *       dbm interface Tcl command.
  5.  *
  6.  *---------------------------------------------------------------------------
  7.  * Copyright 1992 Karl Lehenbauer & Mark Diekhans.
  8.  *
  9.  * Permission to use, copy, modify, and distribute this software and its
  10.  * documentation for any purpose and without fee is hereby granted, provided
  11.  * that the above copyright notice appear in all copies.  Karl Lehenbauer
  12.  * and Mark Diekhans make no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  */
  16.  
  17. #include "tclExtdInt.h"
  18. #include "tcl.h"
  19.  
  20. #include <dbm.h>
  21.  
  22. #define DBM_NOT_SEARCHING 0
  23. #define DBM_START_SEARCH 1
  24. #define DBM_SEARCHING 2
  25. #define DBM_SEARCH_COMPLETE 3
  26.  
  27.  
  28. /*
  29.  *----------------------------------------------------------------------
  30.  *
  31.  * Tcl_DbmCmd --
  32.  *
  33.  * Results:
  34.  *  Standard TCL results, may return the UNIX system error message.
  35.  *
  36.  *----------------------------------------------------------------------
  37.  */
  38. static int
  39. Tcl_DbmCmd (clientData, interp, argc, argv)
  40.     char        *clientData;
  41.     Tcl_Interp  *interp;
  42.     int          argc;
  43.     char       **argv;
  44. {
  45.     static int dbmSearchState = DBM_NOT_SEARCHING;
  46.     static datum key, value;
  47.  
  48.  
  49.     if ((argv[1][0] == 'i') && (strcmp(argv[1], "init") == 0))
  50.     {
  51.         int dbmInitVal;
  52.  
  53.         if (argc != 3) {
  54.             Tcl_AppendResult (interp, "bad # arg: ", argv[0],
  55.                 " init filename", (char *)NULL);
  56.             return TCL_ERROR;
  57.         }
  58.         dbmInitVal = dbminit (argv[2]);
  59.         dbmSearchState = DBM_START_SEARCH;
  60.         sprintf (interp->result, "%d", dbmInitVal);
  61.         return TCL_OK;
  62.     }
  63.     
  64.     if ((argv[1][0] == 'f') && (strcmp(argv[1], "fetch") == 0))
  65.     {
  66.         if ((argc < 3) || (argc > 4)) {
  67.             Tcl_AppendResult (interp, "bad # arg: ", argv[0],
  68.                 " fetch key [variable]", (char *)NULL);
  69.             return TCL_ERROR;
  70.         }
  71.         dbmSearchState = DBM_SEARCHING;
  72.         key.dptr = argv[2];
  73.         key.dsize = strlen(argv[2]);
  74.         value = fetch(key);
  75.  
  76.         if (value.dptr == NULL) {
  77.             if (argc == 3) {
  78.                 Tcl_AppendResult (interp, "dbm variable '", argv[2],
  79.                                   "doesn't exist.");
  80.                 return TCL_ERROR;
  81.             }
  82.             if (Tcl_SetVar (interp, argv[3], "", TCL_LEAVE_ERR_MSG) == NULL)
  83.                 return TCL_ERROR;
  84.             strcpy (interp->result, "0");
  85.             return TCL_OK;
  86.         }
  87.         value.dptr[value.dsize] = '\0';
  88.         if (argc == 3) {
  89.             Tcl_Return(interp, value.dptr, TCL_VOLATILE);
  90.             return TCL_OK;
  91.         }
  92.         if (Tcl_SetVar (interp, argv[3], value.dptr, TCL_LEAVE_ERR_MSG)
  93.                == NULL) return TCL_ERROR;
  94.         strcpy (interp->result, "1");
  95.         return TCL_OK;
  96.     }
  97.  
  98.     if ((argv[1][0] == 's') && (strcmp (argv[1], "store") == 0))
  99.     {
  100.         datum key, value;
  101.         int result;
  102.  
  103.         if (argc != 4) {
  104.             Tcl_AppendResult (interp, "bad # arg: ", argv[0],
  105.                " store key value", (char *)NULL);
  106.             return TCL_ERROR;
  107.         }
  108.         dbmSearchState = DBM_NOT_SEARCHING;
  109.         key.dptr = argv[2];
  110.         key.dsize = strlen(argv[2]);
  111.         value.dptr = argv[3];
  112.         value.dsize = strlen(argv[3]);
  113.         result = store(key, value);
  114.         sprintf(interp->result, "%d", result);
  115.         return TCL_OK;
  116.     }
  117.     
  118.     if ((argv[1][0] == 'd') && (strcmp(argv[1], "delete") == 0))
  119.     {
  120.         datum key;
  121.         int result;
  122.  
  123.         if (argc != 3) {
  124.             sprintf(interp->result, "bad # arg: dbm delete key");
  125.             return TCL_ERROR;
  126.         }
  127.         key.dptr = argv[2];
  128.         key.dsize = strlen(argv[2]);
  129.         result = delete(key);
  130.         sprintf(interp->result, "%d", result);
  131.         return TCL_OK;
  132.     }
  133. #define DBM_NOT_SEARCHING 0
  134. #define DBM_START_SEARCH 1
  135. #define DBM_SEARCHING 2
  136. #define DBM_SEARCH_COMPLETE 3
  137.  
  138.     if ((argv[1][0] == 's') && (strcmp(argv[1], "startsearch") == 0))
  139.     {
  140.         datum key;
  141.  
  142.         if (argc != 2) {
  143.             sprintf(interp->result, "bad # arg: dbm startsearch");
  144.             return TCL_ERROR;
  145.         }
  146.         dbmSearchState = DBM_START_SEARCH;
  147.         return TCL_OK;
  148.     }
  149.  
  150.     if ((argv[1][0] == 'n') && (strcmp(argv[1], "nextelement") == 0))
  151.     {
  152.     static datum searchkey;
  153.  
  154.         if (argc != 3) {
  155.             sprintf(interp->result, "bad # arg: dbm nextelement varname");
  156.             return TCL_ERROR;
  157.         }
  158.  
  159.         if (dbmSearchState == DBM_NOT_SEARCHING) {
  160.             Tcl_AppendResult("you must call 'dbm startsearch' before ",
  161.               "'dbm nextelement' with no intervening dbm operations");
  162.             return TCL_ERROR;
  163.         }
  164.  
  165.         if (dbmSearchState == DBM_START_SEARCH) 
  166.         {
  167.             dbmSearchState = DBM_SEARCHING;
  168.             searchkey = firstkey();
  169.         } else {
  170.             searchkey = nextkey(searchkey);
  171.         }
  172.  
  173.         if (searchkey.dptr == NULL) {
  174.             dbmSearchState = DBM_SEARCH_COMPLETE;
  175.             strcpy(interp->result, "0");
  176.             return TCL_OK;
  177.         }
  178.         searchkey.dptr[searchkey.dsize] = '\0';
  179.         strcpy(interp->result, "1");
  180.         Tcl_SetVar(interp, argv[2], searchkey.dptr, 0);
  181.         return TCL_OK;
  182.     }
  183.     
  184.     sprintf(interp->result,
  185.             "bad arg: dbm option must be \"init\", \"fetch\", \"store\", \"delete\", \"startsearch\" or \"nextelement\"");
  186.     return TCL_ERROR;
  187. }
  188.  
  189.  
  190. /*
  191.  *----------------------------------------------------------------------
  192.  *
  193.  * Tcl_InitDbmCmd --
  194.  *      Initializes the TCL dbm command.
  195.  *
  196.  *----------------------------------------------------------------------
  197.  */
  198. void
  199. Tcl_InitDbmCmd (interp)
  200.     Tcl_Interp *interp;
  201. {
  202.     Tcl_CreateCommand (interp, "dbm", Tcl_DbmCmd, (ClientData)NULL,
  203.                       (void (*)())NULL);
  204. }
  205.  
  206.  
  207.