home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part04 / m0strip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  6.0 KB  |  284 lines

  1. /*
  2.     m0strip.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28.  
  29. #include "copyrght.h"
  30.  
  31. #define VERSION        "0.11"
  32. #define MAXLINE        512
  33.  
  34. enum {PUNCT, LOWER, UPPER, DIGIT, STRG}; /* used in last_char */
  35.  
  36. char line[MAXLINE+1], last_char;
  37. FILE *fin, *fout;
  38. char infile[256];
  39. char includepath[256];
  40.  
  41. char incl[] = "#include";
  42. char inclstr[] = "#includestring";
  43.  
  44. extern FILE *myfopen(char *);
  45.  
  46.  
  47. main(argc, argv)
  48. int argc;
  49. char *argv[];
  50. {
  51.     while (argc > 1 && argv[1][0] == '-') {
  52.         argc--, argv++;
  53.         switch(argv[0][1]) {
  54.             case 'o':
  55.             if (fout || argc < 2) usage();
  56.             fout = fopen(argv[1], "w");
  57.             if (!fout) {
  58.                 fprintf(stderr, "cannot open output file %s\n", argv[1]);
  59.                 exit(1);
  60.             }
  61.             break;
  62.             case 'I':
  63.             strcpy(includepath, argv[0]+2); break;
  64.             default:
  65.             usage();
  66.         }
  67.     }
  68.  
  69.     if (argc > 1) {
  70.         fin = fopen(argv[1], "r");
  71.         if (!fin) {
  72.             fprintf(stderr, "cannot open input file %s\n", argv[1]);
  73.             exit(1);
  74.         }
  75.         strcpy(infile, argv[1]);
  76.     } else {
  77.         fin = stdin;
  78.         strcpy(infile, "--stdin--");
  79.     }
  80.  
  81.     if (!fout)
  82.         fout = stdout;
  83.  
  84.     readfile(fin, "stdin", 0);
  85.     return 0;
  86. }
  87.  
  88.  
  89. print_error(char *fn, int linecount, char *err)
  90. {
  91.     fprintf(stderr, "%s, line %d: %s\n", fn, linecount, err);
  92.     exit(1);
  93. }
  94.  
  95.  
  96. readfile(FILE *f, char *fn, int quote)
  97. {
  98.     int linecount = 0, i;
  99.  
  100.     for(;;) {
  101.         if (!fgets(line, MAXLINE, f))
  102.             break;
  103.  
  104.         linecount++;
  105.         if (strncmp(inclstr, line, strlen(inclstr)) == 0) {
  106.             char *cp = strtok(line+strlen(inclstr), " \t\n");
  107.             FILE *f2 = myfopen(cp);
  108.             if (!f2)
  109.                 print_error(fn, linecount, "cannot open input file");
  110.             for (i=1; i < 2*quote; i++)
  111.                 fputc('\\', fout);
  112.             fputc('\"', fout);
  113.             last_char = PUNCT;
  114.             readfile(f2, cp, quote+1);
  115.             for (i=1; i < 2*quote; i++)
  116.                 fputc('\\', fout);
  117.             fputc('\"', fout);
  118.             last_char = PUNCT;
  119.             fclose(f2);
  120.             continue;
  121.         }
  122.         if (strncmp(incl, line, strlen(incl)) == 0) {
  123.             char *cp = strtok(line+strlen(incl), " \t\n");
  124.             FILE *f2 = myfopen(cp);
  125.             if (!f2)
  126.                 print_error(fn, linecount, "cannot open input file");
  127.             readfile(f2, cp, quote);
  128.             fclose(f2);
  129.             continue;
  130.         }
  131.         remove_comments(line, fn, linecount, 1);
  132.         read_ascii(line, fn, linecount, quote);
  133.     }
  134. }
  135.  
  136.  
  137. remove_comments(char *l, char *fn, int linecount, int rem_blanks)
  138. {
  139.     char *s = l;
  140.     int in_number = 0;
  141.  
  142.     for (;;) {
  143.         switch (*s) {
  144.             case '\n': *s = '\0'; goto bot;
  145.             case '#':
  146.             if (in_number)      { s++; in_number--; break; }
  147.             if (isdigit(*s))  { s++; in_number = 2; break; }
  148.             *s = '\0'; goto bot;
  149.             default: s++; break;
  150.         }
  151.     }
  152. bot:
  153.     if (in_number)
  154.         print_error(fn, linecount, "unterminated #base#digits# number");
  155.     if (rem_blanks) {
  156.         for (s--; s >= l && isspace(*s); s--);
  157.         *(s+1) = '\0';
  158.         for (s = l; isspace(*s); s++);
  159.         if (s > l) {
  160.             for (; *s; l++, s++)
  161.                 *l = *s;
  162.             *l = '\0';
  163.         }
  164.     }
  165.     return 0;
  166. }
  167.  
  168.  
  169. read_ascii(char *s, char *fn, int linecount, int quote)
  170. {
  171.     char *cp;
  172.     int i;
  173.  
  174.     if (*s == '\0') return;
  175.  
  176.     for (cp = s; *cp; s = cp) {
  177.         if (isspace(*cp))
  178.             cp++;
  179.         else if (isdigit(*cp)) {
  180.             for (cp++; isdigit(*cp); cp++);
  181.             if (last_char == DIGIT)
  182.                 fputc(' ', fout);
  183.             while (cp > s) {
  184.                 fputc(*s, fout);
  185.                 s++;
  186.             }
  187.             last_char = DIGIT;
  188.         } else if (isupper(*cp)) {
  189.             fputc(*cp, fout);
  190.             cp++;
  191.             last_char = UPPER;
  192.         } else if (islower(*cp) || *cp == '_') {
  193.             for (cp++; islower(*cp) || *cp == '_'; cp++);
  194.             if (last_char == LOWER)
  195.                 fputc(' ', fout);
  196.             while (cp > s) {
  197.                 fputc(*s, fout);
  198.                 s++;
  199.             }
  200.             last_char = LOWER;
  201.         } else if (*cp == '\\') {
  202.             for(cp++;;cp++) {
  203.                 if (*cp == '\0')
  204.                     print_error(fn, linecount, "unterminated hexstring");
  205.                 if (*cp == '\\')
  206.                     break;
  207.                 if ( !isxdigit(*cp) )
  208.                     print_error(fn, linecount, "invalid char in hexstring");
  209.             }
  210.             for (i=0; i <= 2*quote; i++)
  211.                 fputc('\\', fout);
  212.             s++;
  213.             while (cp > s) {
  214.                 fputc(*s, fout);
  215.                 s++;
  216.             }
  217.             for (i=0; i <= 2*quote; i++)
  218.                 fputc('\\', fout);
  219.             cp++;
  220.             last_char = PUNCT;
  221.         } else if (*cp == '\"') {
  222.             for(cp++;;cp++) {
  223.                 if (*cp == '\0')
  224.                     print_error(fn, linecount, "unterminated string");
  225.                 if (*cp == '\"')
  226.                     break;
  227.                 if (*cp == '\\' && *(cp+1) == '\"')
  228.                     cp++;
  229.             }
  230.             for (i=1; i < 2*quote; i++)
  231.                 fputc('\\', fout);
  232.             fputc('\"', fout);
  233.             s++;
  234.             while (cp > s) {
  235.                 if (*s == '\"' || *s == '\\') {
  236.                     for (i=0; i < 2*quote; i++)
  237.                         fputc('\\', fout);
  238.                 }
  239.                 fputc(*s, fout);
  240.                 s++;
  241.             }
  242.             for (i=1; i < 2*quote; i++)
  243.                 fputc('\\', fout);
  244.             fputc('\"', fout);
  245.             cp++;
  246.             last_char = PUNCT;
  247.         } else {
  248.             fputc(*cp, fout);
  249.             cp++;
  250.             last_char = PUNCT;
  251.         }
  252.     }
  253. }
  254.  
  255.  
  256. usage()
  257. {
  258.     fprintf(stderr, "Strip blanks from M0 code, version %s (compiled %s at %s)\n",
  259.                 VERSION, __DATE__, __TIME__);
  260.     fprintf(stderr, "%s\n", COPYRIGHT);
  261.     fprintf(stderr,    "usage: m0strip [options] [<infile>]\n");
  262.     fprintf(stderr, "options:\n"
  263.             "\t-o <outfile>\n"
  264.             "\t-I<includepath>\n");
  265.     exit(1);
  266. }
  267.  
  268.  
  269. FILE*
  270. myfopen(char *fn)
  271. {
  272.     char path[512];
  273.     FILE *f;
  274.  
  275.     f = fopen(fn, "r");
  276.     if (f || includepath[0] == '\0' || strchr(fn, '/'))
  277.         return f;
  278.     if (includepath[strlen(includepath)-1] == '/')
  279.         sprintf(path, "%s%s", includepath, fn);
  280.     else
  281.         sprintf(path, "%s/%s", includepath, fn);
  282.     return fopen(path, "r");
  283. }
  284.