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)
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.
24 * vms_pp infile outfile
26 * The file "vms_pp.trans" has the names and their translations.
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:
37 * The construct #if defined(foo) is replaced with
43 * #define defined(XX) XX_val
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
54 #define Table_name "vms_pp.trans"
56 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$"
58 static FILE *in
,*out
; /* read from, write to */
59 struct item
{ /* symbol table entries */
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
; {
69 if(argc
!= 3) { /* check argument count */
70 fprintf(stderr
,"usage: vms_pp infile outfile");
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]);
81 if((out
= fopen(argv
[2],"w")) == NULL
) {
82 fprintf(stderr
,"vms_pp: unable to create file '%s'",argv
[2]);
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
; {
97 if(!(temp
= malloc(strlen(str
)+1))) {
98 fprintf(stderr
,"vms_pp: can't allocate memory");
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
++)
114 /* skip_white - skip white space
116 static char *skip_white(ptr
) char *ptr
; {
117 while(*ptr
== ' ' || *ptr
== '\t')
122 /* init_table - initialize translation table.
125 char buf
[256],*ptr
,word
[128];
129 if((in
= fopen(Table_name
,"r")) == NULL
) { /* open file */
130 fprintf(stderr
,"vms_pp: can't open '%s'",Table_name
);
133 for(i
= 0; fgets(buf
,255,in
) != NULL
;) { /* loop through lines */
134 ptr
= skip_white(buf
);
135 if(*ptr
== '!') /* skip comments */
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
);
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
);
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
;
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
++;
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 */
179 /* check_pp - check for preprocessor lines
181 check_pp(buf
) char *buf
; {
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
);
196 strcpy(buf
,"#endif");
199 if(!strcmp("if",word
)) { /* check for if */
201 ptr
= strchr(ptr
,'d'); /* look for d in defined */
202 if(!ptr
) /* are we done? */
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 */
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 */
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");
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
; {
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
);
242 fprintf(stderr
,"couldn't find '%s'\n",word
);