home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 156_01 / args.c < prev    next >
Text File  |  1985-08-21  |  3KB  |  107 lines

  1. /*    name...
  2.         args
  3.  
  4.     purpose...
  5.         to fetch arguments from the command line
  6.  
  7.     note...
  8.         Delete the memory allocation (DW directive)
  9.         for stdin and stdout before assembly.
  10.  
  11.     history...
  12.     26 Jul 84  Not opening files until entire command line
  13.         has been scanned & arguments stored. ">>"
  14.         implemented (appends to existing file).
  15.     26 Jul 84  No longer redirecting output to CONSOLE or
  16.         LIST, or input to CONSOLE. args[] is declared
  17.         globally.
  18.     from ddj n 74 p 62, by Jan-Henrik Johansson
  19. */
  20. #include iolib.h
  21.  
  22. #define EOF -1
  23. #define NULL 0
  24. #define CR 13
  25. #define SPACE 32
  26. #define BELL 7
  27.  
  28. int stdin,    /* input unit # - external (in IOLIB) */
  29. stdout,        /* output unit # - external (in IOLIB) */
  30. argcnt,        /* # arguments on command line */
  31. argv[10];    /* pointers to arguments in args[] */
  32. char *args;    /* stored arguments */
  33.  
  34. getarg(n,s,size) /* places in s the n-th argument (assumes s 
  35.             has "size" bytes). Returns s.    */
  36. int n; char *s; int size;
  37. {    char *str;
  38.     int i;
  39.  
  40.     if(n<0|n>=argcnt) return EOF;
  41.     i=0;
  42.     str=argv[n];
  43.     while(i<size-1)
  44.         {if(str[i]==NULL) break;
  45.         s[i]=str[i];
  46.         i++;
  47.         }
  48.     s[i]=NULL;
  49.     return s;
  50. }
  51. setargs()            /* fetch arguments */
  52. {    int error; /* # errors encountered */
  53.     char *inname,*outname, /* file names from command line */
  54.     *count,    /* *count is # characters in command line */
  55.     *ptr,    /* *ptr is next character in command line */
  56.     *lastc,    /* points to last character in command line */
  57.     *mode,    /* mode for output file */
  58.     *next;    /* where the next byte goes into args[] */
  59.  
  60.     count=128;    /* CP/M command buffer */
  61.     ptr=count+1;
  62.     lastc=ptr+*count;
  63.     *lastc=SPACE;        /* place a sentinal */
  64.     args=alloc(*count);    /* allocate the buffer */
  65.     argv[0]=args;
  66.     args[1]=NULL;
  67.     next=args+2;
  68.     argcnt=1;
  69.     inname=outname=error=0;
  70.     while(++ptr<lastc)
  71.         {if(*ptr==SPACE) continue;
  72.         if(*ptr == '<')        /* redirect input */
  73.             {while(*++ptr==SPACE){}
  74.             inname=next;
  75.             }
  76.         else if(*ptr == '>')    /* redirect output */
  77.             {if(ptr[1]=='>') {++ptr; *mode='a';}
  78.             else *mode='w';
  79.             while(*++ptr==SPACE){}
  80.             outname=next;
  81.             }
  82.         else            /* argument */
  83.             {argv[argcnt++]=next;
  84.             }
  85.         while(*ptr!=SPACE) *next++=*ptr++;
  86.         *next++=NULL;
  87.         }
  88.     if(inname)
  89.         {stdin=fopen(inname,"r");
  90.         if(stdin==0)
  91.             {putc("\n Can't open ",STDERR);
  92.             putc(inname,STDERR);
  93.             ++error;
  94.             }
  95.         }
  96.     if(outname)
  97.         {stdout=fopen(outname,mode);
  98.         if(stdout==0)
  99.             {putc("\n Can't open ",STDERR);
  100.             putc(outname,STDERR);
  101.             ++error;
  102.             }
  103.         }
  104.     args[0]='*';        /* place 0-th argument */
  105.     if(error) exit(BELL);
  106. }
  107.