home *** CD-ROM | disk | FTP | other *** search
- /* (C) Tim Graves 20th April 1994
- This code is supplied AS IS. no warrantee either expressed or implied
- is provided. This code may be freeley modified and modified as long as my
- origional authorship is acknowledged.
-
- Tim Graves
- Sun Microsystems
-
- */
- /* Handle command line variables for the psion comd cmd language */
- #include <stdio.h>
- #include "psion.h"
- #include "psvar.h"
- #include <malloc.h>
- static char vsn[]="@(#) psvar.c 3.2@(#)";
- extern int debugcall ;
-
- /* the base of the variable stack */
- struct varstack * varstackbase = NULL ;
-
- struct varstack * curlevel = NULL ;
-
- initvars ()
- {
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: initvars()\n") ;
- /* calloc the first stack entry */
- varstackbase = (struct varstack *) calloc(1, sizeof(struct varstack)) ;
-
- /* setup the current pointer */
- curlevel = varstackbase ;
-
- /* setup the next / previous pointers */
- curlevel->prevlevel = NULL ;
- curlevel->nextlevel = NULL ;
-
- /* setup the pointer to the first item */
-
- curlevel->firstitem = NULL ;
-
- }
-
- upvarlevel()
- {
- struct varstack * newlev ;
- /* create the new level */
-
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: upvarlevel()\n") ;
- newlev = (struct varstack *) calloc(1, sizeof(struct varstack)) ;
-
- /* setup the old level */
- curlevel->nextlevel = newlev ;
-
- /* setup the current level */
- newlev->nextlevel = NULL ;
- newlev->prevlevel = curlevel ;
- newlev->firstitem = NULL ;
-
- /* set the current level to be the new one */
- curlevel = newlev ;
- /* copy the existing vars across */
- copyuplev() ;
- }
-
- copyuplev()
- {
- struct varitem *from ;
-
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: copyuplev ()\n") ;
- /* get the start of the previous variable linked list */
- from = curlevel->prevlevel->firstitem ;
-
- while (from != NULL)
- {
- addvar(from->varname, from->varval) ;
- from = from -> nextvar ;
- }
- }
-
- downvarlevel()
- {
- struct varstack *oldptr ;
- /* first free the variable list */
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: downvarlevel () \n") ;
- clearvarlevel() ;
-
- /* check to see if we are at the bottom of the stack, if so
- return */
- if (curlevel == varstackbase)
- return ;
-
- /* then unravel the pointers */
- oldptr = curlevel ;
- curlevel = curlevel->prevlevel ;
- curlevel->nextlevel = NULL ;
-
- /* free the space */
- free(oldptr) ;
- }
-
- clearvarlevel()
- {
- struct varitem * curitem ;
- struct varitem * nextitem ;
-
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: clearvarlevel () \n") ;
- /* init the list */
- curitem = curlevel->firstitem ;
-
- /* free the list */
- while (curitem != NULL)
- {
- nextitem = curitem->nextvar ;
- free(curitem->varname) ;
- free(curitem->varval) ;
- free(curitem) ;
- curitem = nextitem ;
- }
- }
-
- addvar(name, value)
- char * name, *value ;
- {
- struct varitem *tmp ;
-
- /* ensure that we are not duplicating a variable */
- delvar(name) ;
-
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: addvar (name = %s, value = %s)\n", name, value) ;
- /* create the new items memory */
- tmp = (struct varitem *) calloc (1, sizeof (struct varitem)) ;
-
- /* insert it at the head of the list */
- tmp->nextvar = curlevel-> firstitem ;
- curlevel-> firstitem = tmp ;
-
- /* setup the memory for the data sets */
- tmp->varname = (char * ) calloc(1, strlen(name) + 1) ;
- tmp->varval = (char * ) calloc(1, strlen(value) + 1) ;
- strcpy(tmp->varname, name) ;
- strcpy(tmp->varval, value) ;
- }
-
- char * findvar(name)
- char * name ;
- {
- struct varitem * curvar ;
- /* scan along the list of current variables to see if they match the
- given variable, if they do return the pointer */
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: findvar (name = %s)\n", name) ;
- curvar = curlevel->firstitem ;
-
- while (curvar != NULL)
- {
- if (strcmp(curvar->varname, name) == 0)
- return(curvar->varval) ;
- /* move to the next value */
- curvar = curvar->nextvar ;
- }
- /* var not found, return NULL */
- return(NULL) ;
- }
-
- dumpvars()
- {
- struct varitem * curvar ;
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: dumpvars()\n" );
- /* scan allong the list and print each variable name */
- printf("Vars\n") ;
- curvar = curlevel -> firstitem ;
- while (curvar != NULL)
- {
- printf("%s = %s\n", curvar->varname, curvar->varval) ;
- curvar = curvar -> nextvar ;
- }
- printf("End\n") ;
- }
-
- delvar(name)
- char * name ;
- {
- struct varitem *prev, *cur, *next ;
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: delvar (name = %s)\n", name) ;
- /* scan along the list and unlink and then free the first variable that matches
- this will be the one as addvar ensures that the variable doesnot exist before
- adding it
- special case is if the first variable is what we are dealing with */
-
- cur = curlevel->firstitem ;
-
- /* no variables in the list return */
- if (cur == NULL)
- return ;
-
- /* special case the one we want is at the start */
- if (strcmp(cur->varname, name) == 0)
- {
- curlevel->firstitem = cur->nextvar ;
- free(cur->varname) ;
- free(cur->varval) ;
- free (cur) ;
- return ;
- }
-
- /* move into the list and set things up */
- prev = cur ;
- cur = cur->nextvar ;
-
- /* general case, this code also handles the end case */
- while(cur != NULL)
- {
- if (strcmp(cur->varname, name) == 0)
- {
- prev->nextvar = cur->nextvar ;
- free(cur->varval) ;
- free(cur->varname) ;
- free(cur) ;
- return ;
- }
- /* not the one we want move allong */
- prev = cur ;
- cur = cur -> nextvar ;
- }
- }
-
- char *extractenv(var, env)
- char * env[] , *var;
- {
- int len ;
-
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: extractenv (var = %s, env = OMITED)\n", var) ;
- len = strlen(var) ;
-
- while (*env != NULL)
- {
- if (strncmp( *env, var, len) == 0)
- return (*env) ;
- env ++ ;
- }
- return(NULL) ;
- }
-
- loadvar(var, name, env)
- char * env[] , *var, *name ;
- {
- char * envent ;
- char * val ;
- char tmpstr [1000] ;
- int ctr ;
-
- if (debugcall >= VARCALLDEBUG)
- fprintf(stderr, "CALL: loadvar (var = %s, name = %s env = OMITED)\n", var, name) ;
- envent = extractenv(var, env) ;
- if (envent == NULL)
- return(FALSE) ;
-
- /* take a copy of the entry */
- strcpy(tmpstr, envent) ;
- /* locate the = sign */
- if ((val = strchr(tmpstr, '=')) == NULL)
- return(FALSE) ;
-
- /* replace the = with `\0` and move along one */
- val[0] = '\0' ;
- val ++ ;
- addvar(name, val) ;
- return(TRUE) ;
- }
-
-