home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD2.img / d4xx / d473 / cnewssrc / cnews_src.lzh / relay / amiga / read.c < prev    next >
C/C++ Source or Header  |  1990-12-30  |  2KB  |  71 lines

  1. /*
  2.  *    read -- reads a line of input and puts each word into variables
  3.  *
  4.  *    SYNTAX
  5.  *        read <var-list>
  6.  *
  7.  *    DESCRIPTION
  8.  *        Reads one line from stdin and puts each word encountered into
  9.  *        successive variables from the list provided on the command line.
  10.  *        Word delimiters are any whitespace characters.  Both single and
  11.  *        double quotes may surround a string of characters to indicate
  12.  *        that they should be considered one word, thus preventing the
  13.  *        whitespace from being interpreted.
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18. #include <exec/types.h>
  19. #include <dos/var.h>
  20. #include <pragma/dos.h>
  21.  
  22. void usage(int ier, char *fmt, ...)
  23. {
  24.     if (fmt && *fmt) {
  25.         va_list args;
  26.  
  27.         va_start(args, fmt);
  28.         vprintf(fmt, args);
  29.         va_end(args);
  30.     }
  31.     puts("Usage: read <var-list>");
  32.     exit( ier );
  33.     /* NOTREACHED */
  34. }
  35.  
  36. #define iseol(x)    ((x) == '\n')
  37.  
  38. int main(int argc, char **argv)
  39. {
  40.     static char buff[BUFSIZ];
  41.     char *bp, *start, delim;
  42.  
  43.     if (argc < 2)
  44.         usage(20, NULL);
  45.  
  46.     fgets(buff, sizeof(buff), stdin);
  47.     for (bp = buff; isspace(*bp); bp++)
  48.         ;
  49.     start = bp, delim = '\0';
  50.  
  51.     while (*bp) {
  52.         if (*bp == delim || (!delim && isspace(*bp))) {
  53.             if (argc-- > 2 || *bp == delim)
  54.                 *bp++ = '\0';
  55.             if (!SetVar(*++argv, start, -1L, LV_VAR | GVF_GLOBAL_ONLY))
  56.                 usage(20, "Couldn't set variable `%s'!\n", *argv);
  57.             if (argc == 1)
  58.                 break;
  59.             while (isspace(*bp))
  60.                 bp++;
  61.             delim = '\0';
  62.             start = bp;
  63.         } else if (bp == start && (*bp == '"' || *bp == '\'')) {
  64.             delim = *bp++;
  65.             start = bp;
  66.         } else
  67.             bp++;
  68.     }
  69.     return( feof(stdin) ? 5 : 0 );
  70. }
  71.