home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / W / WWIVSOR.ZIP / MINIESM.C < prev    next >
C/C++ Source or Header  |  1995-04-25  |  6KB  |  243 lines

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