Commit | Line | Data |
---|---|---|
dcfdbac7 JB |
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. | |
4 | ||
5 | This file is part of GNU Emacs. | |
6 | ||
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 | |
7c938215 | 9 | the Free Software Foundation; either version 2, or (at your option) |
dcfdbac7 JB |
10 | any later version. |
11 | ||
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. | |
16 | ||
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 | |
3b7ad313 EN |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. | |
dcfdbac7 JB |
21 | |
22 | * | |
23 | * Usage: | |
24 | * vms_pp infile outfile | |
25 | * implicit inputs: | |
26 | * The file "vms_pp.trans" has the names and their translations. | |
27 | * description: | |
28 | * Vms_pp takes the input file and scans it, replacing the long | |
29 | * names with shorter names according to the table read in from | |
30 | * vms_pp.trans. The line is then written to the output file. | |
31 | * | |
32 | * Additionally, the "#undef foo" construct is replaced with: | |
33 | * #ifdef foo | |
34 | * #undef foo | |
35 | * #endif | |
36 | * | |
37 | * The construct #if defined(foo) is replaced with | |
38 | * #ifdef foo | |
39 | * #define foo_VAL 1 | |
40 | * #else | |
41 | * #define foo_VAL 0 | |
42 | * #endif | |
43 | * #define defined(XX) XX_val | |
44 | * #if defined(foo) | |
45 | * | |
8e6208c5 | 46 | * This last construction only works on single line #if's and takes |
dcfdbac7 JB |
47 | * advantage of a questionable C pre-processor trick. If there are |
48 | * comments within the #if, that contain "defined", then this will | |
49 | * bomb. | |
50 | */ | |
51 | #include <stdio.h> | |
52 | ||
53 | #define Max_table 100 | |
54 | #define Table_name "vms_pp.trans" | |
55 | #define Word_member \ | |
56 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$" | |
57 | ||
58 | static FILE *in,*out; /* read from, write to */ | |
59 | struct item { /* symbol table entries */ | |
60 | char *name; | |
61 | char *value; | |
62 | }; | |
63 | static struct item name_table[Max_table]; /* symbol table */ | |
64 | static int defined_defined = 0; /* small optimization */ | |
65 | ||
66 | main(argc,argv) int argc; char **argv; { | |
67 | char buffer[1024]; | |
68 | ||
69 | if(argc != 3) { /* check argument count */ | |
70 | fprintf(stderr,"usage: vms_pp infile outfile"); | |
71 | exit(); | |
72 | } | |
73 | init_table(); /* read in translation table */ | |
74 | ||
75 | /* open input and output files | |
76 | */ | |
77 | if((in = fopen(argv[1],"r")) == NULL) { | |
78 | fprintf(stderr,"vms_pp: unable to open file '%s'",argv[1]); | |
79 | exit(); | |
80 | } | |
81 | if((out = fopen(argv[2],"w")) == NULL) { | |
82 | fprintf(stderr,"vms_pp: unable to create file '%s'",argv[2]); | |
83 | exit(); | |
84 | } | |
85 | ||
86 | while(fgets(buffer,1023,in) != NULL) { /* loop through buffer until end */ | |
87 | process_line(buffer); /* process the line */ | |
88 | fputs(buffer,out); /* write out the line */ | |
89 | } | |
90 | } | |
91 | ||
92 | /* buy - allocate and copy a string | |
93 | */ | |
94 | static char *buy(str) char *str; { | |
95 | char *temp; | |
96 | ||
97 | if(!(temp = malloc(strlen(str)+1))) { | |
98 | fprintf(stderr,"vms_pp: can't allocate memory"); | |
99 | exit(); | |
100 | } | |
101 | strcpy(temp,str); | |
102 | return temp; | |
103 | } | |
104 | ||
105 | /* gather_word - return a buffer full of the next word | |
106 | */ | |
107 | static char *gather_word(ptr,word) char *ptr, *word;{ | |
108 | for(; strchr(Word_member,*ptr); ptr++,word++) | |
109 | *word = *ptr; | |
110 | *word = 0; | |
111 | return ptr; | |
112 | } | |
113 | ||
114 | /* skip_white - skip white space | |
115 | */ | |
116 | static char *skip_white(ptr) char *ptr; { | |
117 | while(*ptr == ' ' || *ptr == '\t') | |
118 | ptr++; | |
119 | return ptr; | |
120 | } | |
121 | ||
122 | /* init_table - initialize translation table. | |
123 | */ | |
124 | init_table() { | |
125 | char buf[256],*ptr,word[128]; | |
126 | FILE *in; | |
127 | int i; | |
128 | ||
129 | if((in = fopen(Table_name,"r")) == NULL) { /* open file */ | |
130 | fprintf(stderr,"vms_pp: can't open '%s'",Table_name); | |
131 | exit(); | |
132 | } | |
133 | for(i = 0; fgets(buf,255,in) != NULL;) { /* loop through lines */ | |
134 | ptr = skip_white(buf); | |
135 | if(*ptr == '!') /* skip comments */ | |
136 | continue; | |
137 | ptr = gather_word(ptr,word); /* get long word */ | |
138 | if(*word == 0) { /* bad entry */ | |
139 | fprintf(stderr,"vms_pp: bad input line '%s'\n",buf); | |
140 | continue; | |
141 | } | |
142 | name_table[i].name = buy(word); /* set up the name */ | |
143 | ptr = skip_white(ptr); /* skip white space */ | |
144 | ptr = gather_word(ptr,word); /* get equivalent name */ | |
145 | if(*word == 0) { /* bad entry */ | |
146 | fprintf(stderr,"vms_pp: bad input line '%s'\n",buf); | |
147 | continue; | |
148 | } | |
149 | name_table[i].value = buy(word); /* and the equivalent name */ | |
150 | i++; /* increment to next position */ | |
151 | } | |
152 | for(; i < Max_table; i++) /* mark rest as unused */ | |
153 | name_table[i].name = 0; | |
154 | } | |
155 | ||
156 | /* process_line - do actual line processing | |
157 | */ | |
158 | process_line(buf) char *buf; { | |
159 | char *in_ptr,*out_ptr; | |
160 | char word[128],*ptr; | |
161 | int len; | |
162 | ||
163 | check_pp(buf); /* check for preprocessor lines */ | |
164 | ||
165 | for(in_ptr = out_ptr = buf; *in_ptr;) { | |
166 | if(!strchr(Word_member,*in_ptr)) /* non alpha-numeric? just copy */ | |
167 | *out_ptr++ = *in_ptr++; | |
168 | else { | |
169 | in_ptr = gather_word(in_ptr,word); /* get the 'word' */ | |
170 | if(strlen(word) > 31) /* length is too long */ | |
171 | replace_word(word); /* replace the word */ | |
172 | for(ptr = word; *ptr; ptr++,out_ptr++) /* copy out the word */ | |
173 | *out_ptr = *ptr; | |
174 | } | |
175 | } | |
176 | *out_ptr = 0; | |
177 | } | |
178 | ||
179 | /* check_pp - check for preprocessor lines | |
180 | */ | |
181 | check_pp(buf) char *buf; { | |
182 | char *ptr,*p; | |
183 | char word[128]; | |
184 | ||
185 | ptr = skip_white(buf); /* skip white space */ | |
186 | if(*ptr != '#') /* is this a preprocessor line? */ | |
187 | return; /* no, just return */ | |
188 | ||
189 | ptr = skip_white(++ptr); /* skip white */ | |
190 | ptr = gather_word(ptr,word); /* get command word */ | |
191 | if(!strcmp("undef",word)) { /* undef? */ | |
192 | ptr = skip_white(ptr); | |
193 | ptr = gather_word(ptr,word); /* get the symbol to undef */ | |
194 | fprintf(out,"#ifdef %s\n",word); | |
195 | fputs(buf,out); | |
196 | strcpy(buf,"#endif"); | |
197 | return; | |
198 | } | |
199 | if(!strcmp("if",word)) { /* check for if */ | |
200 | for(;;) { | |
201 | ptr = strchr(ptr,'d'); /* look for d in defined */ | |
202 | if(!ptr) /* are we done? */ | |
203 | return; | |
204 | if(strchr(Word_member,*(ptr-1))){ /* at beginning of word? */ | |
205 | ptr++; continue; /* no, continue looking */ | |
206 | } | |
207 | ptr = gather_word(ptr,word); /* get the word */ | |
208 | if(strcmp(word,"defined")) /* skip if not defined */ | |
209 | continue; | |
210 | ptr = skip_white(ptr); /* skip white */ | |
211 | if(*ptr != '(') /* look for open paren */ | |
212 | continue; /* error, continue */ | |
213 | ptr++; /* skip paren */ | |
214 | ptr = skip_white(ptr); /* more white skipping */ | |
215 | ptr = gather_word(ptr,word); /* get the thing to test */ | |
216 | if(!*word) /* null word is bad */ | |
217 | continue; | |
218 | fprintf(out,"#ifdef %s\n",word); /* generate the code */ | |
219 | fprintf(out,"#define %s_VAL 1\n",word); | |
220 | fprintf(out,"#else\n"); | |
221 | fprintf(out,"#define %s_VAL 0\n",word); | |
222 | fprintf(out,"#endif\n"); | |
223 | if(!defined_defined) { | |
224 | fprintf(out,"#define defined(XXX) XXX/**/_VAL\n"); | |
225 | defined_defined = 1; | |
226 | } | |
227 | } | |
228 | } | |
229 | } | |
230 | ||
231 | /* replace_word - look the word up in the table, and replace it | |
232 | * if a match is found. | |
233 | */ | |
234 | replace_word(word) char *word; { | |
235 | int i; | |
236 | ||
237 | for(i = 0; i < Max_table && name_table[i].name; i++) | |
238 | if(!strcmp(word,name_table[i].name)) { | |
239 | strcpy(word,name_table[i].value); | |
240 | return; | |
241 | } | |
242 | fprintf(stderr,"couldn't find '%s'\n",word); | |
243 | } |