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)
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.
23 * vms_pp infile outfile
25 * The file "vms_pp.trans" has the names and their translations.
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:
36 * The construct #if defined(foo) is replaced with
42 * #define defined(XX) XX_val
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
53 #define Table_name "vms_pp.trans"
55 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$"
57 static FILE *in
,*out
; /* read from, write to */
58 struct item
{ /* symbol table entries */
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
; {
68 if(argc
!= 3) { /* check argument count */
69 fprintf(stderr
,"usage: vms_pp infile outfile");
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]);
80 if((out
= fopen(argv
[2],"w")) == NULL
) {
81 fprintf(stderr
,"vms_pp: unable to create file '%s'",argv
[2]);
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
; {
96 if(!(temp
= malloc(strlen(str
)+1))) {
97 fprintf(stderr
,"vms_pp: can't allocate memory");
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
++)
113 /* skip_white - skip white space
115 static char *skip_white(ptr
) char *ptr
; {
116 while(*ptr
== ' ' || *ptr
== '\t')
121 /* init_table - initialize translation table.
124 char buf
[256],*ptr
,word
[128];
128 if((in
= fopen(Table_name
,"r")) == NULL
) { /* open file */
129 fprintf(stderr
,"vms_pp: can't open '%s'",Table_name
);
132 for(i
= 0; fgets(buf
,255,in
) != NULL
;) { /* loop through lines */
133 ptr
= skip_white(buf
);
134 if(*ptr
== '!') /* skip comments */
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
);
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
);
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
;
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
++;
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 */
178 /* check_pp - check for preprocessor lines
180 check_pp(buf
) char *buf
; {
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
);
195 strcpy(buf
,"#endif");
198 if(!strcmp("if",word
)) { /* check for if */
200 ptr
= strchr(ptr
,'d'); /* look for d in defined */
201 if(!ptr
) /* are we done? */
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 */
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 */
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");
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
; {
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
);
241 fprintf(stderr
,"couldn't find '%s'\n",word
);