home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compiler / small_c / cb / sources / stp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-08-11  |  1.8 KB  |  82 lines

  1. /*
  2. ** stp.c -- setup a device configuration
  3. */
  4. #include <stdio.h>
  5. #define LINESZ   133        /* maximum input line size */
  6. #define FENCE    '|'        /* comment prefix */
  7. #define QUOTE    '"'        /* quote for strings */
  8. char ln[LINESZ], *lnptr;
  9. int view, debug, error;
  10. main(argc, argv) int argc, *argv; {
  11.   int i;
  12.   view = debug = NO;
  13.   i = 0;
  14.   while(getarg(++i, ln, 15, argc, argv) != EOF) {
  15.     if(*ln == '-') {
  16.       if(tolower(ln[1]) == 'v') {    /* view activity */
  17.         view = YES;
  18.         continue;
  19.         }
  20.       if(tolower(ln[1]) == 'd') {    /* debug mode - only show output */
  21.         debug = YES;
  22.         continue;
  23.         }
  24.       break;
  25.       }
  26.     fputs("usage: STP [-V] [-D]", stderr);
  27.     abort(7);
  28.     }
  29.   while(fgets(ln, LINESZ, stdin)) {
  30.     lnptr = ln;
  31.     error = NO;
  32.     while(*lnptr && *lnptr != FENCE && *lnptr != '\n') {
  33.       poll(YES);
  34.       if(*lnptr == QUOTE) dostring(); else dohex();
  35.       while(isspace(*++lnptr)) ;
  36.       }
  37.     if(debug) fputs("\n", stderr);
  38.     if(view || error) fputs(ln, stderr);
  39.     }
  40.   }
  41.  
  42. dostring() {
  43.   ++lnptr;
  44.   while(*lnptr >= ' ') {
  45.     if(*lnptr == QUOTE) {
  46.       ++lnptr;
  47.       if(*lnptr != QUOTE) return;
  48.       }
  49.     send(lnptr++);
  50.     }
  51.   fputs("\7\n- unterminated string in following line\n", stderr);
  52.   error = YES;
  53.   }
  54.  
  55. dohex() {
  56.   int i, hex;
  57.   char ch;
  58.   i = xtoi(lnptr, &hex);
  59.   if(i < 1 || i > 2 || !isspace(lnptr[i])) i = 0;
  60.   while(!isspace(*lnptr)) ++lnptr;
  61.   if(i == 0) {
  62.     fputs("\7\n- bad hex byte in following line\n", stderr);
  63.     error = YES;
  64.     return;
  65.     }
  66.   ch = hex;
  67.   send(&ch);
  68.   }
  69.  
  70. send(ptr) char *ptr; {
  71.   if(debug) {
  72.     int i;
  73.     char str[3];
  74.     i = *ptr & 255;
  75.     itox(i, str, 3);
  76.     fputs(str, stderr);
  77.     fputs(" ", stderr);
  78.     }
  79.   else write(stdout, ptr, 1);
  80.   }
  81.  
  82.