(easy-mmode-define-navigation):
[emacs.git] / src / vms-pp.c
blob9ac7a4966a9b7bbe6f2cb2860a3928013db603ba
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 2, 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, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
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.
32 * Additionally, the "#undef foo" construct is replaced with:
33 * #ifdef foo
34 * #undef foo
35 * #endif
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)
46 * This last construction only works on single line #if's and takes
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.
51 #include <stdio.h>
53 #define Max_table 100
54 #define Table_name "vms_pp.trans"
55 #define Word_member \
56 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$"
58 static FILE *in,*out; /* read from, write to */
59 struct item { /* symbol table entries */
60 char *name;
61 char *value;
63 static struct item name_table[Max_table]; /* symbol table */
64 static int defined_defined = 0; /* small optimization */
66 main(argc,argv) int argc; char **argv; {
67 char buffer[1024];
69 if(argc != 3) { /* check argument count */
70 fprintf(stderr,"usage: vms_pp infile outfile");
71 exit();
73 init_table(); /* read in translation table */
75 /* open input and output files
77 if((in = fopen(argv[1],"r")) == NULL) {
78 fprintf(stderr,"vms_pp: unable to open file '%s'",argv[1]);
79 exit();
81 if((out = fopen(argv[2],"w")) == NULL) {
82 fprintf(stderr,"vms_pp: unable to create file '%s'",argv[2]);
83 exit();
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 */
92 /* buy - allocate and copy a string
94 static char *buy(str) char *str; {
95 char *temp;
97 if(!(temp = malloc(strlen(str)+1))) {
98 fprintf(stderr,"vms_pp: can't allocate memory");
99 exit();
101 strcpy(temp,str);
102 return temp;
105 /* gather_word - return a buffer full of the next word
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;
114 /* skip_white - skip white space
116 static char *skip_white(ptr) char *ptr; {
117 while(*ptr == ' ' || *ptr == '\t')
118 ptr++;
119 return ptr;
122 /* init_table - initialize translation table.
124 init_table() {
125 char buf[256],*ptr,word[128];
126 FILE *in;
127 int i;
129 if((in = fopen(Table_name,"r")) == NULL) { /* open file */
130 fprintf(stderr,"vms_pp: can't open '%s'",Table_name);
131 exit();
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;
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;
149 name_table[i].value = buy(word); /* and the equivalent name */
150 i++; /* increment to next position */
152 for(; i < Max_table; i++) /* mark rest as unused */
153 name_table[i].name = 0;
156 /* process_line - do actual line processing
158 process_line(buf) char *buf; {
159 char *in_ptr,*out_ptr;
160 char word[128],*ptr;
161 int len;
163 check_pp(buf); /* check for preprocessor lines */
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;
176 *out_ptr = 0;
179 /* check_pp - check for preprocessor lines
181 check_pp(buf) char *buf; {
182 char *ptr,*p;
183 char word[128];
185 ptr = skip_white(buf); /* skip white space */
186 if(*ptr != '#') /* is this a preprocessor line? */
187 return; /* no, just return */
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;
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 */
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;
231 /* replace_word - look the word up in the table, and replace it
232 * if a match is found.
234 replace_word(word) char *word; {
235 int i;
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;
242 fprintf(stderr,"couldn't find '%s'\n",word);