home *** CD-ROM | disk | FTP | other *** search
- /*
- m0strip.c
- */
- /* Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
-
- Distributed under the terms of the GNU General Public License
- version 2 of june 1991 as published by the Free Software
- Foundation, Inc.
-
- This file is part of M0.
-
- M0 is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY. No author or distributor accepts responsibility to anyone for
- the consequences of using it or for whether it serves any particular
- purpose or works at all, unless he says so in writing. Refer to the GNU
- General Public License for full details.
-
- Everyone is granted permission to copy, modify and redistribute M0, but
- only under the conditions described in the GNU General Public License.
- A copy of this license is supposed to have been given to you along with
- M0 so you can know your rights and responsibilities. It should be in a
- file named LICENSE. Among other things, the copyright notice and this
- notice must be preserved on all copies. */
-
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
-
- #include "copyrght.h"
-
- #define VERSION "0.11"
- #define MAXLINE 512
-
- enum {PUNCT, LOWER, UPPER, DIGIT, STRG}; /* used in last_char */
-
- char line[MAXLINE+1], last_char;
- FILE *fin, *fout;
- char infile[256];
- char includepath[256];
-
- char incl[] = "#include";
- char inclstr[] = "#includestring";
-
- extern FILE *myfopen(char *);
-
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- while (argc > 1 && argv[1][0] == '-') {
- argc--, argv++;
- switch(argv[0][1]) {
- case 'o':
- if (fout || argc < 2) usage();
- fout = fopen(argv[1], "w");
- if (!fout) {
- fprintf(stderr, "cannot open output file %s\n", argv[1]);
- exit(1);
- }
- break;
- case 'I':
- strcpy(includepath, argv[0]+2); break;
- default:
- usage();
- }
- }
-
- if (argc > 1) {
- fin = fopen(argv[1], "r");
- if (!fin) {
- fprintf(stderr, "cannot open input file %s\n", argv[1]);
- exit(1);
- }
- strcpy(infile, argv[1]);
- } else {
- fin = stdin;
- strcpy(infile, "--stdin--");
- }
-
- if (!fout)
- fout = stdout;
-
- readfile(fin, "stdin", 0);
- return 0;
- }
-
-
- print_error(char *fn, int linecount, char *err)
- {
- fprintf(stderr, "%s, line %d: %s\n", fn, linecount, err);
- exit(1);
- }
-
-
- readfile(FILE *f, char *fn, int quote)
- {
- int linecount = 0, i;
-
- for(;;) {
- if (!fgets(line, MAXLINE, f))
- break;
-
- linecount++;
- if (strncmp(inclstr, line, strlen(inclstr)) == 0) {
- char *cp = strtok(line+strlen(inclstr), " \t\n");
- FILE *f2 = myfopen(cp);
- if (!f2)
- print_error(fn, linecount, "cannot open input file");
- for (i=1; i < 2*quote; i++)
- fputc('\\', fout);
- fputc('\"', fout);
- last_char = PUNCT;
- readfile(f2, cp, quote+1);
- for (i=1; i < 2*quote; i++)
- fputc('\\', fout);
- fputc('\"', fout);
- last_char = PUNCT;
- fclose(f2);
- continue;
- }
- if (strncmp(incl, line, strlen(incl)) == 0) {
- char *cp = strtok(line+strlen(incl), " \t\n");
- FILE *f2 = myfopen(cp);
- if (!f2)
- print_error(fn, linecount, "cannot open input file");
- readfile(f2, cp, quote);
- fclose(f2);
- continue;
- }
- remove_comments(line, fn, linecount, 1);
- read_ascii(line, fn, linecount, quote);
- }
- }
-
-
- remove_comments(char *l, char *fn, int linecount, int rem_blanks)
- {
- char *s = l;
- int in_number = 0;
-
- for (;;) {
- switch (*s) {
- case '\n': *s = '\0'; goto bot;
- case '#':
- if (in_number) { s++; in_number--; break; }
- if (isdigit(*s)) { s++; in_number = 2; break; }
- *s = '\0'; goto bot;
- default: s++; break;
- }
- }
- bot:
- if (in_number)
- print_error(fn, linecount, "unterminated #base#digits# number");
- if (rem_blanks) {
- for (s--; s >= l && isspace(*s); s--);
- *(s+1) = '\0';
- for (s = l; isspace(*s); s++);
- if (s > l) {
- for (; *s; l++, s++)
- *l = *s;
- *l = '\0';
- }
- }
- return 0;
- }
-
-
- read_ascii(char *s, char *fn, int linecount, int quote)
- {
- char *cp;
- int i;
-
- if (*s == '\0') return;
-
- for (cp = s; *cp; s = cp) {
- if (isspace(*cp))
- cp++;
- else if (isdigit(*cp)) {
- for (cp++; isdigit(*cp); cp++);
- if (last_char == DIGIT)
- fputc(' ', fout);
- while (cp > s) {
- fputc(*s, fout);
- s++;
- }
- last_char = DIGIT;
- } else if (isupper(*cp)) {
- fputc(*cp, fout);
- cp++;
- last_char = UPPER;
- } else if (islower(*cp) || *cp == '_') {
- for (cp++; islower(*cp) || *cp == '_'; cp++);
- if (last_char == LOWER)
- fputc(' ', fout);
- while (cp > s) {
- fputc(*s, fout);
- s++;
- }
- last_char = LOWER;
- } else if (*cp == '\\') {
- for(cp++;;cp++) {
- if (*cp == '\0')
- print_error(fn, linecount, "unterminated hexstring");
- if (*cp == '\\')
- break;
- if ( !isxdigit(*cp) )
- print_error(fn, linecount, "invalid char in hexstring");
- }
- for (i=0; i <= 2*quote; i++)
- fputc('\\', fout);
- s++;
- while (cp > s) {
- fputc(*s, fout);
- s++;
- }
- for (i=0; i <= 2*quote; i++)
- fputc('\\', fout);
- cp++;
- last_char = PUNCT;
- } else if (*cp == '\"') {
- for(cp++;;cp++) {
- if (*cp == '\0')
- print_error(fn, linecount, "unterminated string");
- if (*cp == '\"')
- break;
- if (*cp == '\\' && *(cp+1) == '\"')
- cp++;
- }
- for (i=1; i < 2*quote; i++)
- fputc('\\', fout);
- fputc('\"', fout);
- s++;
- while (cp > s) {
- if (*s == '\"' || *s == '\\') {
- for (i=0; i < 2*quote; i++)
- fputc('\\', fout);
- }
- fputc(*s, fout);
- s++;
- }
- for (i=1; i < 2*quote; i++)
- fputc('\\', fout);
- fputc('\"', fout);
- cp++;
- last_char = PUNCT;
- } else {
- fputc(*cp, fout);
- cp++;
- last_char = PUNCT;
- }
- }
- }
-
-
- usage()
- {
- fprintf(stderr, "Strip blanks from M0 code, version %s (compiled %s at %s)\n",
- VERSION, __DATE__, __TIME__);
- fprintf(stderr, "%s\n", COPYRIGHT);
- fprintf(stderr, "usage: m0strip [options] [<infile>]\n");
- fprintf(stderr, "options:\n"
- "\t-o <outfile>\n"
- "\t-I<includepath>\n");
- exit(1);
- }
-
-
- FILE*
- myfopen(char *fn)
- {
- char path[512];
- FILE *f;
-
- f = fopen(fn, "r");
- if (f || includepath[0] == '\0' || strchr(fn, '/'))
- return f;
- if (includepath[strlen(includepath)-1] == '/')
- sprintf(path, "%s%s", includepath, fn);
- else
- sprintf(path, "%s/%s", includepath, fn);
- return fopen(path, "r");
- }
-