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