Back to index

radiance  4R0+20100331
expandarg.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: expandarg.c,v 2.9 2008/05/17 02:49:41 greg Exp $";
00003 #endif
00004 /*
00005  * Get additional command arguments from file or environment.
00006  *
00007  *  External symbols declared in rtio.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include <errno.h>
00013 #include <string.h>
00014 
00015 #include "rtio.h"
00016 #include "rtmisc.h"
00017 
00018 
00019 #define MAXARGEXP    4096          /* maximum argument expansion */
00020 
00021                      /* set the following to suit, -1 to disable */
00022 int    envexpchr = '$';            /* environment expansion character */
00023 int    filexpchr = '@';            /* file expansion character */
00024 
00025 
00026 int
00027 expandarg(acp, avp, n)             /* expand list at argument n */
00028 int    *acp;
00029 register char ***avp;
00030 int    n;
00031 {
00032        int    ace;
00033        char   *ave[MAXARGEXP];
00034        char   **newav;
00035                                    /* check argument */
00036        if (n >= *acp)
00037               return(0);
00038        errno = 0;    
00039        if ((*avp)[n][0] == filexpchr) {          /* file name */
00040               ace = wordfile(ave, (*avp)[n]+1);
00041               if (ace < 0)
00042                      return(-1);   /* no such file */
00043        } else if ((*avp)[n][0] == envexpchr) {          /* env. variable */
00044               ace = wordstring(ave, getenv((*avp)[n]+1));
00045               if (ace < 0)
00046                      return(-1);   /* no such variable */
00047        } else                                    /* regular argument */
00048               return(0);
00049                                    /* allocate new pointer list */
00050        newav = (char **)bmalloc((*acp+ace)*sizeof(char *));
00051        if (newav == NULL)
00052               return(-1);
00053                                    /* copy preceeding arguments */
00054        memcpy((void *)newav, (void *)*avp, n*sizeof(char *));
00055                                    /* copy expanded argument */
00056        memcpy((void *)(newav+n), (void *)ave, ace*sizeof(char *));
00057                                    /* copy trailing arguments + NULL */
00058        memcpy((void *)(newav+n+ace), (void *)(*avp+n+1), (*acp-n)*sizeof(char *));
00059                                    /* free old list */
00060        bfree((char *)*avp, (*acp+1)*sizeof(char *));
00061                                    /* assign new list */
00062        *acp += ace-1;
00063        *avp = newav;
00064        return(1);                  /* return success */
00065 }