home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / fileutil / stuff2.arj / PARSEOPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  5.0 KB  |  190 lines

  1. /*
  2. Stuff 2.0.
  3.  
  4. Checksum: 2327246523 (verify with "brik")
  5.  
  6. (C) Copyright 1988 Rahul Dhesi.  Permission is granted to copy and
  7. distribute this file in modified or unmodified form, whether for
  8. noncommercial or commercial use, provided (a) this copyright notice
  9. is preserved, (b) no attempt is made to restrict redistribution of
  10. this file, and (c) this file is not distributed as part of any
  11. collection whose redistribution is restricted by a compilation
  12. copyright.
  13. */
  14.  
  15. /*
  16. parses a single option and stores in global array
  17. */
  18.  
  19. #include "stuff.h"
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #define     NEXT_ARG(i,j)  (++(i) < j ? i : insufargs())
  24.  
  25. request_rec *options[MAX_OPTS];
  26. char fmtstr[FMTSIZE];
  27. long size_limit = 0L;
  28.  
  29. int last_opt = -1;
  30. static void badopt (char *option);
  31. static int insufargs(void);
  32.  
  33. /* allocate a record */
  34. request_rec *getrec(int negate)
  35. {
  36.    request_rec *optr = (request_rec *) emalloc (sizeof (request_rec));
  37.    if (last_opt >= MAX_OPTS)
  38.       memerr();
  39.    options[++last_opt] = optr;
  40.    optr->negate = negate;
  41.    return optr;
  42. }
  43.  
  44. void parseopt (char **argv, int *i, int argc, int negate)
  45. {
  46.    FILE *infile;     /* to read @ files */
  47.    char *option;
  48.    request_rec *optr;
  49.  
  50.    if (*i >= argc)
  51.       (void) insufargs();
  52.    
  53.    option = argv[*i];
  54.    if (STRCMP (option,==,OPT_PRINT)) {
  55.       return;                       /* ignore print requests */
  56.    }
  57.  
  58.    if (STRCMP (option,==,"!"))   {
  59.       ++(*i);                             /* move to next option */
  60.       parseopt (argv, i, argc, NEGATE);   /* parse it with negation */
  61.       return;
  62.    }
  63.  
  64.    if (STRCMP(option,==,OPT_NAME)) {
  65.       optr = getrec (negate);
  66.       optr->choice = NAME;
  67.       strcpy (optr->info.name, argv[NEXT_ARG(*i,argc)]);
  68.  
  69. #define  LINE_SIZE   80
  70.       /* now, if name was preceded by @, it is actually a filename */
  71.       if (optr->info.name[0] == '@') {
  72.          int first_time = 1;
  73.          char this_line[LINE_SIZE];
  74.          infile = fopen (optr->info.name + 1, "r");
  75.          if (infile == NULL) {
  76.             fprintf (stderr, "Can't open %s, skipped\n", optr->info.name);
  77.             return;
  78.          }
  79.          while (1) {
  80.             char *lastpos;
  81.             do {
  82.                *this_line = '\0';
  83.                fgets (this_line, LINE_SIZE, infile);
  84.             } while (*this_line == '\0' && !feof(infile));/*skip null lines*/
  85.  
  86.             if (*this_line == '\0') {
  87.                break;
  88.             }
  89.  
  90.             lastpos = lastptr (this_line);
  91.             if (*lastpos == '\n')            /* remove trailing newline */
  92.                *lastpos = '\0';
  93.  
  94.             /* ugly code -- mem already allocated for first record */
  95.             if (!first_time) {
  96.                optr = getrec (negate);
  97.                optr->choice = NAME;
  98.             }
  99.  
  100.             first_time = 0;
  101.  
  102.             strcpy (optr->info.name, this_line);
  103.  
  104.             if (feof(infile))
  105.                break;
  106.          }
  107.          fclose (infile);
  108.       }
  109.  
  110.    } else if (STRCMP(option,==,OPT_MTIME)) {
  111.       optr = getrec (negate);
  112.       optr->choice = MTIME;
  113.       optr->info.mtime_info.choice = getcompare(argv[NEXT_ARG(*i,argc)]);
  114.       optr->info.mtime_info.value = getvalue(argv[*i]);
  115.    } else if (STRCMP(option,==,OPT_SIZE)) {
  116.       optr = getrec (negate);
  117.       optr->choice = SIZE;
  118.       optr->info.size_info.choice = getcompare(argv[NEXT_ARG(*i,argc)]);
  119.       optr->info.size_info.value = getvalue(argv[*i]);
  120.    } else if (STRCMP(option,==,OPT_NEWER)) {
  121.       optr = getrec (negate);
  122.       optr->choice = NEWER;
  123.       optr->info.mtime_info.value = filetime(argv[NEXT_ARG(*i,argc)]);
  124.    } else if (STRCMP(option,==,OPT_OLDER)) {
  125.       optr = getrec (negate);
  126.       optr->choice = OLDER;
  127.       optr->info.mtime_info.value = filetime(argv[NEXT_ARG(*i,argc)]);
  128.    } else if (STRCMP(option,==,OPT_MODIFIED)) {
  129.       optr = getrec (negate);
  130.       optr->choice = MODIFIED;
  131.    } else if (STRCMP(option,==,OPT_FORMAT)) {
  132.       strcpy(fmtstr,argv[NEXT_ARG(*i,argc)]);
  133.       strcat(fmtstr, "\n");
  134.    } else if (STRCMP(option,==,OPT_LIMIT)) {
  135.       size_limit = getvalue(argv[NEXT_ARG(*i,argc)]);
  136.    } else
  137.       badopt (option);
  138. }
  139. void badopt (char *option)
  140. {
  141.    fprintf (stderr, "stuff:  FATAL:  option [%s] is invalid\n", option);
  142.    exit(1);
  143. }
  144.  
  145. int insufargs(void)
  146. {
  147.    fprintf (stderr, "stuff:  FATAL:  missing argument\n");
  148.    exit(1);
  149. }
  150.  
  151. /*
  152. returns BIGGER, SMALLER, or SAME according as argument begins with
  153. +, -, or neither
  154.  */
  155. compare_pt getcompare (char *str)
  156. {
  157.    if (*str == '+')
  158.       return (BIGGER);
  159.    else if (*str == '-')
  160.       return (SMALLER);
  161.    return (SAME);
  162. }
  163.  
  164. #include <stdlib.h>
  165. /*
  166. changes text string into long integer
  167. */
  168. long getvalue (char *str)
  169. {
  170.    if (*str == '+' || *str == '-')
  171.       str++;
  172.    return (atol(str));
  173. }
  174.  
  175. void *emalloc (unsigned size)
  176. {
  177.    void *ptr;
  178.    ptr = malloc (size);
  179.    if (ptr == NULL) {
  180.       memerr();
  181.    }
  182.    return (ptr);
  183. }
  184.  
  185. void memerr(void)
  186. {
  187.    fprintf (stderr, "stuff: ran out of memory\n");
  188.    exit(1);
  189. }
  190.