home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / comms / x_sun_psio / code / psvar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-30  |  6.1 KB  |  279 lines

  1. /* (C) Tim Graves 20th April 1994
  2.    This code is supplied AS IS. no warrantee either expressed or implied 
  3.    is provided. This code may be freeley modified and modified as long as my
  4.    origional authorship is acknowledged. 
  5.    
  6.    Tim Graves
  7.     Sun Microsystems
  8.      
  9.      */
  10. /* Handle command line variables for the psion comd cmd language */
  11. #include <stdio.h>
  12. #include "psion.h"
  13. #include "psvar.h"
  14. #include <malloc.h>
  15. static char vsn[]="@(#) psvar.c 3.2@(#)";
  16. extern int debugcall ;
  17.  
  18. /* the base of the variable stack */
  19. struct varstack * varstackbase = NULL ;
  20.  
  21. struct varstack * curlevel = NULL ;
  22.  
  23. initvars ()
  24. {
  25.     if (debugcall >= VARCALLDEBUG)
  26.         fprintf(stderr, "CALL: initvars()\n") ;
  27.     /* calloc the first stack entry */
  28.     varstackbase = (struct varstack *) calloc(1, sizeof(struct varstack)) ;
  29.  
  30.     /* setup the current pointer */
  31.     curlevel = varstackbase ;
  32.  
  33.     /* setup the next / previous pointers */
  34.     curlevel->prevlevel = NULL ;
  35.     curlevel->nextlevel = NULL ;
  36.  
  37.     /* setup the pointer to the first item */
  38.  
  39.     curlevel->firstitem = NULL ;
  40.  
  41. }
  42.  
  43. upvarlevel()
  44. {
  45.     struct varstack * newlev ;
  46.     /* create the new level */
  47.  
  48.     if (debugcall >= VARCALLDEBUG)
  49.         fprintf(stderr, "CALL: upvarlevel()\n") ;
  50.     newlev = (struct varstack *) calloc(1, sizeof(struct varstack)) ;
  51.  
  52.     /* setup the old level */
  53.     curlevel->nextlevel = newlev ;
  54.  
  55.     /* setup the current level */
  56.     newlev->nextlevel = NULL ;
  57.     newlev->prevlevel = curlevel ;
  58.     newlev->firstitem = NULL ;
  59.  
  60.     /* set the current level to be the new one */
  61.     curlevel = newlev ;
  62.     /* copy the existing vars across */
  63.     copyuplev() ;
  64. }
  65.  
  66. copyuplev()
  67. {
  68.     struct varitem *from ;
  69.  
  70.     if (debugcall >= VARCALLDEBUG)
  71.         fprintf(stderr, "CALL: copyuplev ()\n") ;
  72.     /* get the start of the previous variable linked list */
  73.     from = curlevel->prevlevel->firstitem ;
  74.  
  75.     while (from != NULL)
  76.     {
  77.         addvar(from->varname, from->varval) ;
  78.         from = from -> nextvar ;
  79.     }
  80. }
  81.  
  82. downvarlevel()
  83. {
  84.     struct varstack *oldptr ;
  85.     /* first free the variable list */
  86.     if (debugcall >= VARCALLDEBUG)
  87.         fprintf(stderr, "CALL: downvarlevel () \n") ;
  88.     clearvarlevel() ;
  89.  
  90.     /* check to see if we are at the bottom of the stack, if so
  91.        return */
  92.     if (curlevel == varstackbase)
  93.         return ;
  94.  
  95.     /* then unravel the pointers */
  96.     oldptr = curlevel ;
  97.     curlevel = curlevel->prevlevel ;
  98.     curlevel->nextlevel = NULL ;
  99.  
  100.     /* free the space */
  101.     free(oldptr) ;
  102. }
  103.  
  104. clearvarlevel()
  105. {
  106.     struct varitem * curitem ;
  107.     struct varitem * nextitem ;
  108.  
  109.     if (debugcall >= VARCALLDEBUG)
  110.         fprintf(stderr, "CALL: clearvarlevel () \n") ;
  111.     /* init the list */
  112.     curitem = curlevel->firstitem ;
  113.  
  114.     /* free the list */
  115.     while (curitem != NULL)
  116.     {
  117.         nextitem = curitem->nextvar ;
  118.         free(curitem->varname) ;
  119.         free(curitem->varval) ;
  120.         free(curitem) ;
  121.         curitem = nextitem ;
  122.     }
  123. }
  124.  
  125. addvar(name, value)
  126. char * name, *value ;
  127. {
  128.     struct varitem *tmp ;
  129.  
  130.     /* ensure that we are not duplicating a variable */
  131.     delvar(name) ;
  132.  
  133.     if (debugcall >= VARCALLDEBUG)
  134.         fprintf(stderr, "CALL: addvar (name = %s, value = %s)\n", name, value) ;
  135.     /* create the new items memory */
  136.     tmp = (struct varitem *) calloc (1, sizeof (struct varitem)) ;
  137.  
  138.     /* insert it at the head of the list */
  139.     tmp->nextvar = curlevel-> firstitem ;
  140.     curlevel-> firstitem = tmp ;
  141.  
  142.     /* setup the memory for the data sets */
  143.     tmp->varname = (char * ) calloc(1, strlen(name) + 1) ;
  144.     tmp->varval = (char * ) calloc(1, strlen(value) + 1) ;
  145.     strcpy(tmp->varname, name) ;
  146.     strcpy(tmp->varval, value) ;
  147. }
  148.  
  149. char * findvar(name) 
  150. char * name ;
  151. {
  152.     struct varitem * curvar ;
  153.     /* scan along the list of current variables to see if they match the
  154.        given variable, if they do return the pointer */
  155.     if (debugcall >= VARCALLDEBUG)
  156.         fprintf(stderr, "CALL: findvar (name = %s)\n", name) ;
  157.     curvar = curlevel->firstitem ;
  158.  
  159.     while (curvar != NULL) 
  160.     {
  161.         if (strcmp(curvar->varname, name) == 0)
  162.             return(curvar->varval) ;
  163.         /* move to the next value */
  164.         curvar = curvar->nextvar ;
  165.     }
  166.     /* var not found, return NULL */
  167.     return(NULL) ;
  168. }
  169.  
  170. dumpvars()
  171. {
  172.     struct varitem * curvar ;
  173.     if (debugcall >= VARCALLDEBUG)
  174.         fprintf(stderr, "CALL: dumpvars()\n" ); 
  175.     /* scan allong the list and print each variable name */
  176.     printf("Vars\n") ;
  177.     curvar = curlevel -> firstitem ;
  178.     while (curvar != NULL) 
  179.     {
  180.         printf("%s = %s\n", curvar->varname, curvar->varval) ;
  181.         curvar = curvar -> nextvar ;
  182.     }
  183.     printf("End\n") ;
  184. }
  185.  
  186. delvar(name)
  187. char * name ;
  188. {
  189.     struct varitem *prev, *cur, *next ;
  190.     if (debugcall >= VARCALLDEBUG)
  191.         fprintf(stderr, "CALL: delvar (name = %s)\n", name) ;
  192.     /* scan along the list and unlink and then free the first variable that matches
  193.        this will be the one as addvar ensures that the variable doesnot exist before
  194.        adding it 
  195.        special case is if the first variable is what we are dealing with */
  196.  
  197.     cur = curlevel->firstitem ;
  198.  
  199.     /* no variables in the list return */
  200.     if (cur == NULL)
  201.         return ;
  202.  
  203.     /* special case the one we want is at the start */
  204.     if (strcmp(cur->varname, name) == 0)
  205.     {
  206.         curlevel->firstitem = cur->nextvar ;
  207.         free(cur->varname) ;
  208.         free(cur->varval) ;
  209.         free (cur) ;
  210.         return ;
  211.     }
  212.  
  213.     /* move into the list and set things up */
  214.     prev = cur ;
  215.     cur = cur->nextvar ;
  216.  
  217.     /* general case, this code also handles the end case */
  218.     while(cur != NULL)
  219.     {
  220.         if (strcmp(cur->varname, name) == 0)
  221.         {
  222.             prev->nextvar = cur->nextvar ;
  223.             free(cur->varval) ;
  224.             free(cur->varname) ;
  225.             free(cur) ;
  226.             return ;
  227.         }
  228.         /* not the one we want move allong */
  229.         prev = cur ;
  230.         cur = cur -> nextvar ;
  231.     }
  232. }
  233.  
  234. char *extractenv(var, env)
  235. char * env[] , *var;
  236. {
  237.     int len  ;
  238.  
  239.     if (debugcall >= VARCALLDEBUG)
  240.         fprintf(stderr, "CALL: extractenv (var = %s, env = OMITED)\n", var) ;
  241.     len = strlen(var) ;
  242.  
  243.     while (*env != NULL)
  244.     {
  245.         if (strncmp( *env, var, len) == 0)
  246.             return (*env) ;
  247.         env ++ ;
  248.     }
  249.     return(NULL) ;
  250. }
  251.  
  252. loadvar(var, name, env)
  253. char * env[] , *var, *name ;
  254. {
  255.     char * envent ;
  256.     char * val ;
  257.     char tmpstr [1000] ;
  258.     int ctr ;
  259.  
  260.     if (debugcall >= VARCALLDEBUG)
  261.         fprintf(stderr, "CALL: loadvar (var = %s, name = %s env = OMITED)\n", var, name) ;
  262.     envent = extractenv(var, env) ;
  263.     if (envent == NULL)
  264.         return(FALSE) ;
  265.     
  266.     /* take a copy of the entry */
  267.     strcpy(tmpstr, envent) ;
  268.     /* locate the = sign */
  269.     if ((val = strchr(tmpstr, '=')) == NULL)
  270.         return(FALSE) ;
  271.     
  272.     /* replace the = with `\0` and move along one */
  273.     val[0] = '\0' ;
  274.     val ++ ;
  275.     addvar(name, val) ;
  276.     return(TRUE) ;
  277. }
  278.  
  279.