home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / WWIV2.ZIP / MINIESM.C < prev    next >
C/C++ Source or Header  |  1993-07-20  |  6KB  |  236 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1993 by Wayne Bell
  5.  
  6. Distribution of the source code for WWIV, in any form, modified or unmodified,
  7. without PRIOR, WRITTEN APPROVAL by the author, is expressly prohibited.
  8. Distribution of compiled versions of WWIV is limited to copies compiled BY
  9. THE AUTHOR.  Distribution of any copies of WWIV not compiled by the author
  10. is expressly prohibited.
  11.  
  12.  
  13. *****************************************************************************/
  14.  
  15.  
  16.  
  17. #include <ctype.h>
  18. #include "strings.c"
  19.  
  20. char *progname="miniesm";
  21.  
  22.  
  23. /****************************************************************************/
  24.  
  25. void parse_str(char *is, char *os)
  26. {
  27.   char escstr[20];
  28.   int escstrptr=0;
  29.   int escaped=0;
  30.   char ch;
  31.  
  32.  
  33.   while ((ch=*is++)!=0) {
  34.     switch(escaped) {
  35.       case 0:
  36.         if (ch=='\\') {
  37.           escaped=1;
  38.         } else {
  39.           *os++=ch;
  40.         }
  41.         break;
  42.       case 1:
  43.         if ((ch>='0') && (ch<='7')) {
  44.           escaped=2;
  45.           escstrptr=0;
  46.           escstr[escstrptr++]=ch;
  47.           escstr[escstrptr]=0;
  48.         } else if ((ch=='x') || (ch=='X')) {
  49.           escaped=3;
  50.           escstrptr=0;
  51.           escstr[escstrptr]=0;
  52.         } else {
  53.           switch(ch) {
  54.             case 'a': ch='\a'; break;
  55.             case 'b': ch='\b'; break;
  56.             case 'f': ch='\f'; break;
  57.             case 'n': ch='\n'; break;
  58.             case 'r': ch='\r'; break;
  59.             case 't': ch='\t'; break;
  60.             case 'v': ch='\v'; break;
  61.           }
  62.           *os++=ch;
  63.           escaped=0;
  64.         }
  65.         break;
  66.       case 2: /* octal */
  67.         if ((ch>='0') && (ch<='7') && (escstrptr<3)) {
  68.           escstr[escstrptr++]=ch;
  69.           escstr[escstrptr]=0;
  70.         } else {
  71.           is--;
  72.           escaped=0;
  73.           *os++=strtol(escstr, NULL, 8);
  74.         }
  75.         break;
  76.       case 3: /* hex */
  77.         if (isxdigit(ch) && (escstrptr<2)) {
  78.           escstr[escstrptr++]=ch;
  79.           escstr[escstrptr]=0;
  80.         } else {
  81.           is--;
  82.           escaped=0;
  83.           *os++=strtol(escstr, NULL, 16);
  84.         }
  85.         break;
  86.     }
  87.   }
  88.  
  89.   switch(escaped) {
  90.     case 2: /* octal */
  91.       *os++=strtol(escstr, NULL, 8);
  92.       break;
  93.     case 3: /* hex */
  94.       *os++=strtol(escstr, NULL, 16);
  95.       break;
  96.   }
  97.  
  98.   *os=0;
  99. }
  100.  
  101. /****************************************************************************/
  102.  
  103. void descr_str(FILE *f, char *s)
  104. {
  105.   putc('\"', f);
  106.   for (; *s; s++) {
  107.     if ((*s>=' ') && (*s<='~')) {
  108.       if ((*s=='"') || (*s=='\\'))
  109.         putc('\\', f);
  110.       putc(*s, f);
  111.     } else {
  112.       putc('\\', f);
  113.       switch(*s) {
  114.       case '\a': putc('a', f); break;
  115.       case '\b': putc('b', f); break;
  116.       case '\f': putc('f', f); break;
  117.       case '\n': putc('n', f); break;
  118.       case '\r': putc('r', f); break;
  119.       case '\t': putc('t', f); break;
  120.       case '\v': putc('v', f); break;
  121.       default: fprintf(f,"x%02.2x",((int)*s)&0xff); break;
  122.       }
  123.     }
  124.   }
  125.   putc('\"', f);
  126. }
  127.  
  128. /****************************************************************************/
  129.  
  130. void usage(void)
  131. {
  132.   printf("%s stringfn [opts]\n", progname);
  133.   printf("   -Anum      - Add num blank strings\n");
  134.   printf("   -Mnum      - Modify string #num\n");
  135.   printf("   -Lnum      - List string #num\n");
  136.  
  137.   exit(0);
  138. }
  139.  
  140. /****************************************************************************/
  141.  
  142. void main(int argc, char **argv)
  143. {
  144.   int i,i1,nums;
  145.   char *str_fn="";
  146.   char buf[256], buf1[300], *ss;
  147.  
  148.   if (argc<2)
  149.     usage();
  150.  
  151.   str_fn=argv[1];
  152.  
  153.   if (set_strings_fn(1, NULL, str_fn, 1)) {
  154.     printf("Couldn't open/create '%s'.\n");
  155.     exit(-1);
  156.   }
  157.  
  158.   nums=num_strings(1);
  159.  
  160.   printf("%s: %d strings\n\n",str_fn, nums);
  161.  
  162.   for (i=2; i<argc; i++) {
  163.     if (argv[i][0]!='-')
  164.       usage();
  165.  
  166.     switch(toupper(argv[i][1])) {
  167.       case 'A':
  168.         i1=atoi(argv[i]+2);
  169.         if (i1<1)
  170.           usage();
  171.         put_string(1, nums+i1, "");
  172.         printf("%s: Added %d blank entries\n",str_fn, i1);
  173.         nums=num_strings(1);
  174.         break;
  175.  
  176.       case 'L':
  177.         i1=atoi(argv[i]+2);
  178.         if (i1<1)
  179.           usage();
  180.         if (i1>nums) {
  181.           printf("%s: Only has %d string entries\n",str_fn, nums);
  182.     } else {
  183.      do {
  184.       ss=get_stringx(1,i1);
  185.       printf("String #%u: ", i1);
  186.       descr_str(stdout, ss);
  187.       printf("\n");
  188.       ++i1;
  189.        } while (i1<=nums);    }
  190.         break;
  191.  
  192.       case 'M':
  193.         i1=atoi(argv[i]+2);
  194.         if (i1<1)
  195.           usage();
  196.         if (i1>nums) {
  197.           printf("%s: Only has %d string entries\n",str_fn, nums);
  198.         } else {
  199.           ss=get_stringx(1,i1);
  200.           printf("String #%u: ", i1);
  201.           descr_str(stdout, ss);
  202.           printf("\n\nNew? ");
  203.           fgets(buf1, 295, stdin);
  204.           buf1[295]=0;
  205.           if (buf1[0]) {
  206.             ss=buf1+strlen(buf1)-1;
  207.             if (*ss=='\n');
  208.               *ss=0;
  209.  
  210.             if (buf1[0]) {
  211.               parse_str(buf1, buf);
  212.               printf("\n\nChange string #%u to:\n",i1);
  213.               descr_str(stdout, buf);
  214.               printf("\n\nSure? ");
  215.               fgets(buf1, 10, stdin);
  216.               if (toupper(buf1[0])=='Y') {
  217.                 put_string(1, i1, buf);
  218.                 printf("\nUpdated string #%u\n\n",i1);
  219.               }
  220.             }
  221.           }
  222.         }
  223.         break;
  224.  
  225.       default:
  226.         printf("Unknown argument: '%s'\n",argv[i]);
  227.         usage();
  228.     }
  229.  
  230.   }
  231.  
  232.   close_strfiles();
  233.  
  234.   exit(0);
  235. }
  236.