(src/Makefile, lib-src/Makefile): Delete ^L. Fix definition of $undefs.
[emacs.git] / src / vms-pp.c
blob2ff47d10daff3e0686c45ebefa7b717eb0a8b39f
1 /* vms_pp - preprocess emacs files in such a way that they can be
2 * compiled on VMS without warnings.
3 * Copyright (C) 1986 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 1, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Usage:
23 * vms_pp infile outfile
24 * implicit inputs:
25 * The file "vms_pp.trans" has the names and their translations.
26 * description:
27 * Vms_pp takes the input file and scans it, replacing the long
28 * names with shorter names according to the table read in from
29 * vms_pp.trans. The line is then written to the output file.
31 * Additionally, the "#undef foo" construct is replaced with:
32 * #ifdef foo
33 * #undef foo
34 * #endif
36 * The construct #if defined(foo) is replaced with
37 * #ifdef foo
38 * #define foo_VAL 1
39 * #else
40 * #define foo_VAL 0
41 * #endif
42 * #define defined(XX) XX_val
43 * #if defined(foo)
45 * This last contruction only works on single line #if's and takes
46 * advantage of a questionable C pre-processor trick. If there are
47 * comments within the #if, that contain "defined", then this will
48 * bomb.
50 #include <stdio.h>
52 #define Max_table 100
53 #define Table_name "vms_pp.trans"
54 #define Word_member \
55 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$"
57 static FILE *in,*out; /* read from, write to */
58 struct item { /* symbol table entries */
59 char *name;
60 char *value;
62 static struct item name_table[Max_table]; /* symbol table */
63 static int defined_defined = 0; /* small optimization */
65 main(argc,argv) int argc; char **argv; {
66 char buffer[1024];
68 if(argc != 3) { /* check argument count */
69 fprintf(stderr,"usage: vms_pp infile outfile");
70 exit();
72 init_table(); /* read in translation table */
74 /* open input and output files
76 if((in = fopen(argv[1],"r")) == NULL) {
77 fprintf(stderr,"vms_pp: unable to open file '%s'",argv[1]);
78 exit();
80 if((out = fopen(argv[2],"w")) == NULL) {
81 fprintf(stderr,"vms_pp: unable to create file '%s'",argv[2]);
82 exit();
85 while(fgets(buffer,1023,in) != NULL) { /* loop through buffer until end */
86 process_line(buffer); /* process the line */
87 fputs(buffer,out); /* write out the line */
91 /* buy - allocate and copy a string
93 static char *buy(str) char *str; {
94 char *temp;
96 if(!(temp = malloc(strlen(str)+1))) {
97 fprintf(stderr,"vms_pp: can't allocate memory");
98 exit();
100 strcpy(temp,str);
101 return temp;
104 /* gather_word - return a buffer full of the next word
106 static char *gather_word(ptr,word) char *ptr, *word;{
107 for(; strchr(Word_member,*ptr); ptr++,word++)
108 *word = *ptr;
109 *word = 0;
110 return ptr;
113 /* skip_white - skip white space
115 static char *skip_white(ptr) char *ptr; {
116 while(*ptr == ' ' || *ptr == '\t')
117 ptr++;
118 return ptr;
121 /* init_table - initialize translation table.
123 init_table() {
124 char buf[256],*ptr,word[128];
125 FILE *in;
126 int i;
128 if((in = fopen(Table_name,"r")) == NULL) { /* open file */
129 fprintf(stderr,"vms_pp: can't open '%s'",Table_name);
130 exit();
132 for(i = 0; fgets(buf,255,in) != NULL;) { /* loop through lines */
133 ptr = skip_white(buf);
134 if(*ptr == '!') /* skip comments */
135 continue;
136 ptr = gather_word(ptr,word); /* get long word */
137 if(*word == 0) { /* bad entry */
138 fprintf(stderr,"vms_pp: bad input line '%s'\n",buf);
139 continue;
141 name_table[i].name = buy(word); /* set up the name */
142 ptr = skip_white(ptr); /* skip white space */
143 ptr = gather_word(ptr,word); /* get equivalent name */
144 if(*word == 0) { /* bad entry */
145 fprintf(stderr,"vms_pp: bad input line '%s'\n",buf);
146 continue;
148 name_table[i].value = buy(word); /* and the equivalent name */
149 i++; /* increment to next position */
151 for(; i < Max_table; i++) /* mark rest as unused */
152 name_table[i].name = 0;
155 /* process_line - do actual line processing
157 process_line(buf) char *buf; {
158 char *in_ptr,*out_ptr;
159 char word[128],*ptr;
160 int len;
162 check_pp(buf); /* check for preprocessor lines */
164 for(in_ptr = out_ptr = buf; *in_ptr;) {
165 if(!strchr(Word_member,*in_ptr)) /* non alpha-numeric? just copy */
166 *out_ptr++ = *in_ptr++;
167 else {
168 in_ptr = gather_word(in_ptr,word); /* get the 'word' */
169 if(strlen(word) > 31) /* length is too long */
170 replace_word(word); /* replace the word */
171 for(ptr = word; *ptr; ptr++,out_ptr++) /* copy out the word */
172 *out_ptr = *ptr;
175 *out_ptr = 0;
178 /* check_pp - check for preprocessor lines
180 check_pp(buf) char *buf; {
181 char *ptr,*p;
182 char word[128];
184 ptr = skip_white(buf); /* skip white space */
185 if(*ptr != '#') /* is this a preprocessor line? */
186 return; /* no, just return */
188 ptr = skip_white(++ptr); /* skip white */
189 ptr = gather_word(ptr,word); /* get command word */
190 if(!strcmp("undef",word)) { /* undef? */
191 ptr = skip_white(ptr);
192 ptr = gather_word(ptr,word); /* get the symbol to undef */
193 fprintf(out,"#ifdef %s\n",word);
194 fputs(buf,out);
195 strcpy(buf,"#endif");
196 return;
198 if(!strcmp("if",word)) { /* check for if */
199 for(;;) {
200 ptr = strchr(ptr,'d'); /* look for d in defined */
201 if(!ptr) /* are we done? */
202 return;
203 if(strchr(Word_member,*(ptr-1))){ /* at beginning of word? */
204 ptr++; continue; /* no, continue looking */
206 ptr = gather_word(ptr,word); /* get the word */
207 if(strcmp(word,"defined")) /* skip if not defined */
208 continue;
209 ptr = skip_white(ptr); /* skip white */
210 if(*ptr != '(') /* look for open paren */
211 continue; /* error, continue */
212 ptr++; /* skip paren */
213 ptr = skip_white(ptr); /* more white skipping */
214 ptr = gather_word(ptr,word); /* get the thing to test */
215 if(!*word) /* null word is bad */
216 continue;
217 fprintf(out,"#ifdef %s\n",word); /* generate the code */
218 fprintf(out,"#define %s_VAL 1\n",word);
219 fprintf(out,"#else\n");
220 fprintf(out,"#define %s_VAL 0\n",word);
221 fprintf(out,"#endif\n");
222 if(!defined_defined) {
223 fprintf(out,"#define defined(XXX) XXX/**/_VAL\n");
224 defined_defined = 1;
230 /* replace_word - look the word up in the table, and replace it
231 * if a match is found.
233 replace_word(word) char *word; {
234 int i;
236 for(i = 0; i < Max_table && name_table[i].name; i++)
237 if(!strcmp(word,name_table[i].name)) {
238 strcpy(word,name_table[i].value);
239 return;
241 fprintf(stderr,"couldn't find '%s'\n",word);