home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tools / clear_c / sel.c < prev    next >
C/C++ Source or Header  |  1989-12-07  |  2KB  |  70 lines

  1. /* sel.c */
  2.  
  3. #include <conio.h>
  4. #include <code.h>
  5.  
  6. /* This program illustrates of CODE.H definitons to make C code /*
  7. /* more readable. */
  8.  
  9. void trim(char *s)
  10. /* Trim white space from both ends of string s */
  11. BEGIN
  12.     char    *pc, *pce;
  13.  
  14.     for (pc = s; *pc AND (*pc == ' ' OR *pc == '\t'); pc++);
  15.     for (pce = pc; *pce > ' '; pce++);
  16.     *pce = 0;
  17.     strcpy(s, pc);
  18. END
  19.  
  20. cdecl main(int argc, char *argv[])
  21. BEGIN
  22.     char    s[80], *clrstr, *pc;
  23.     int     i, n;
  24.  
  25.     IF (argc < 2)
  26.         cputs("Usage:   sel \"Prompt\" [max_no]\r\n");
  27.         cputs("   Intended for use it batch files with ERRORLEVEL testing.\r\n");
  28.         cputs("   Will output the prompt and wait for the user to input\r\n");
  29.         cputs("   Y or N if max_no parameter is not present, or otherwise\r\n");
  30.         cputs("   expects the input of number from 1 to max_no. Exits with\r\n");
  31.         cputs("   errorlevel according to user input. Examples:\r\n");
  32.         cputs("         sel \"Enter your selection form 1 to 5 \" 5\r\n");
  33.         cputs("         sel \"Are you sure? (Y/N) \"\r\n");
  34.         cputs("   The last line returns errorlevel 0 for N and 1 for Y\r\n");
  35.         exit(255);
  36.     ENDIF
  37.     cputs(argv[1]);
  38.     clrstr = "\r                                                                              \r";
  39.     IF (argc > 2)               /* This is selection, get number 1..n */
  40.         n = atoi(argv[2]);
  41.         IF (n < 1 OR n > 25)
  42.             cputs("Error in select.");
  43.             exit(255);
  44.         ENDIF
  45.         REPEAT
  46.             s[0] = 78;
  47.             pc = cgets(s);
  48.             trim(pc);
  49.             i = atoi(pc);
  50.             IF (i<1 OR i>n)
  51.                 cputs(clrstr);
  52.                 cputs(argv[1]);
  53.             ENDIF
  54.         UNTIL (i > 0 AND i <= n);
  55.         exit(i);
  56.     ELSE                        /* This is Y/N prompt, get Y or N */
  57.         REPEAT
  58.             s[0] = 78;
  59.             pc = cgets(s);
  60.             trim(pc);
  61.             i = toupper(*pc);
  62.             IF (i!='N' AND i!='Y')
  63.                 cputs(clrstr);
  64.                 cputs(argv[1]);
  65.             ENDIF
  66.         UNTIL (i == 'N' OR i == 'Y');
  67.         exit (i=='Y');
  68.     ENDIF
  69. END
  70.