118
+ ��膩��鰹申鐃順�渇��膩��� 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
+ ��膩��鰹申鐃順�渇��膩��� 9 the Free Software Foundation; either version 1, or (at your option)
+ ��膩��鰹申鐃順�渇��膩��� 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
484
+ ��膩��鰹申鐃順�渇��膩��� 19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
118
+ ��膩��鰹申鐃順�渇��膩��� 20
+ ��膩��鰹申鐃順�渇��膩��� 21 *
+ ��膩��鰹申鐃順�渇��膩��� 22 * Usage:
+ ��膩��鰹申鐃順�渇��膩��� 23 * vms_pp infile outfile
+ ��膩��鰹申鐃順�渇��膩��� 24 * implicit inputs:
+ ��膩��鰹申鐃順�渇��膩��� 25 * The file "vms_pp.trans" has the names and their translations.
+ ��膩��鰹申鐃順�渇��膩��� 26 * description:
+ ��膩��鰹申鐃順�渇��膩��� 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.
+ ��膩��鰹申鐃順�渇��膩��� 30 *
+ ��膩��鰹申鐃順�渇��膩��� 31 * Additionally, the "#undef foo" construct is replaced with:
+ ��膩��鰹申鐃順�渇��膩��� 32 * #ifdef foo
+ ��膩��鰹申鐃順�渇��膩��� 33 * #undef foo
+ ��膩��鰹申鐃順�渇��膩��� 34 * #endif
+ ��膩��鰹申鐃順�渇��膩��� 35 *
+ ��膩��鰹申鐃順�渇��膩��� 36 * The construct #if defined(foo) is replaced with
+ ��膩��鰹申鐃順�渇��膩��� 37 * #ifdef foo
+ ��膩��鰹申鐃順�渇��膩��� 38 * #define foo_VAL 1
+ ��膩��鰹申鐃順�渇��膩��� 39 * #else
+ ��膩��鰹申鐃順�渇��膩��� 40 * #define foo_VAL 0
+ ��膩��鰹申鐃順�渇��膩��� 41 * #endif
+ ��膩��鰹申鐃順�渇��膩��� 42 * #define defined(XX) XX_val
+ ��膩��鰹申鐃順�渇��膩��� 43 * #if defined(foo)
+ ��膩��鰹申鐃順�渇��膩��� 44 *
+ ��膩��鰹申鐃順�渇��膩��� 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
+ ��膩��鰹申鐃順�渇��膩��� 48 * bomb.
+ ��膩��鰹申鐃順�渇��膩��� 49 */
+ ��膩��鰹申鐃順�渇��膩��� 50 #include <stdio.h>
+ ��膩��鰹申鐃順�渇��膩��� 51
+ ��膩��鰹申鐃順�渇��膩��� 52 #define Max_table 100
+ ��膩��鰹申鐃順�渇��膩��� 53 #define Table_name "vms_pp.trans"
+ ��膩��鰹申鐃順�渇��膩��� 54 #define Word_member \
+ ��膩��鰹申鐃順�渇��膩��� 55 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$"
+ ��膩��鰹申鐃順�渇��膩��� 56
+ ��膩��鰹申鐃順�渇��膩��� 57 static FILE *in,*out; /* read from, write to */
+ ��膩��鰹申鐃順�渇��膩��� 58 struct item { /* symbol table entries */
+ ��膩��鰹申鐃順�渇��膩��� 59 char *name;
+ ��膩��鰹申鐃順�渇��膩��� 60 char *value;
+ ��膩��鰹申鐃順�渇��膩��� 61 };
+ ��膩��鰹申鐃順�渇��膩��� 62 static struct item name_table[Max_table]; /* symbol table */
+ ��膩��鰹申鐃順�渇��膩��� 63 static int defined_defined = 0; /* small optimization */
+ ��膩��鰹申鐃順�渇��膩��� 64
+ ��膩��鰹申鐃順�渇��膩��� 65 main(argc,argv) int argc; char **argv; {
+ ��膩��鰹申鐃順�渇��膩��� 66 char buffer[1024];
+ ��膩��鰹申鐃順�渇��膩��� 67
+ ��膩��鰹申鐃順�渇��膩��� 68 if(argc != 3) { /* check argument count */
+ ��膩��鰹申鐃順�渇��膩��� 69 fprintf(stderr,"usage: vms_pp infile outfile");
+ ��膩��鰹申鐃順�渇��膩��� 70 exit();
+ ��膩��鰹申鐃順�渇��膩��� 71 }
+ ��膩��鰹申鐃順�渇��膩��� 72 init_table(); /* read in translation table */
+ ��膩��鰹申鐃順�渇��膩��� 73
+ ��膩��鰹申鐃順�渇��膩��� 74 /* open input and output files
+ ��膩��鰹申鐃順�渇��膩��� 75 */
+ ��膩��鰹申鐃順�渇��膩��� 76 if((in = fopen(argv[1],"r")) == NULL) {
+ ��膩��鰹申鐃順�渇��膩��� 77 fprintf(stderr,"vms_pp: unable to open file '%s'",argv[1]);
+ ��膩��鰹申鐃順�渇��膩��� 78 exit();
+ ��膩��鰹申鐃順�渇��膩��� 79 }
+ ��膩��鰹申鐃順�渇��膩��� 80 if((out = fopen(argv[2],"w")) == NULL) {
+ ��膩��鰹申鐃順�渇��膩��� 81 fprintf(stderr,"vms_pp: unable to create file '%s'",argv[2]);
+ ��膩��鰹申鐃順�渇��膩��� 82 exit();
+ ��膩��鰹申鐃順�渇��膩��� 83 }
+ ��膩��鰹申鐃順�渇��膩��� 84
+ ��膩��鰹申鐃順�渇��膩��� 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 */
+ ��膩��鰹申鐃順�渇��膩��� 88 }
+ ��膩��鰹申鐃順�渇��膩��� 89 }
+ ��膩��鰹申鐃順�渇��膩��� 90
+ ��膩��鰹申鐃順�渇��膩��� 91 /* buy - allocate and copy a string
+ ��膩��鰹申鐃順�渇��膩��� 92 */
+ ��膩��鰹申鐃順�渇��膩��� 93 static char *buy(str) char *str; {
+ ��膩��鰹申鐃順�渇��膩��� 94 char *temp;
+ ��膩��鰹申鐃順�渇��膩��� 95
+ ��膩��鰹申鐃順�渇��膩��� 96 if(!(temp = malloc(strlen(str)+1))) {
+ ��膩��鰹申鐃順�渇��膩��� 97 fprintf(stderr,"vms_pp: can't allocate memory");
+ ��膩��鰹申鐃順�渇��膩��� 98 exit();
+ ��膩��鰹申鐃順�渇��膩��� 99 }
+ ��膩��鰹申鐃順�渇��膩��� 100 strcpy(temp,str);
+ ��膩��鰹申鐃順�渇��膩��� 101 return temp;
+ ��膩��鰹申鐃順�渇��膩��� 102 }
+ ��膩��鰹申鐃順�渇��膩��� 103
+ ��膩��鰹申鐃順�渇��膩��� 104 /* gather_word - return a buffer full of the next word
+ ��膩��鰹申鐃順�渇��膩��� 105 */
+ ��膩��鰹申鐃順�渇��膩��� 106 static char *gather_word(ptr,word) char *ptr, *word;{
+ ��膩��鰹申鐃順�渇��膩��� 107 for(; strchr(Word_member,*ptr); ptr++,word++)
+ ��膩��鰹申鐃順�渇��膩��� 108 *word = *ptr;
+ ��膩��鰹申鐃順�渇��膩��� 109 *word = 0;
+ ��膩��鰹申鐃順�渇��膩��� 110 return ptr;
+ ��膩��鰹申鐃順�渇��膩��� 111 }
+ ��膩��鰹申鐃順�渇��膩��� 112
+ ��膩��鰹申鐃順�渇��膩��� 113 /* skip_white - skip white space
+ ��膩��鰹申鐃順�渇��膩��� 114 */
+ ��膩��鰹申鐃順�渇��膩��� 115 static char *skip_white(ptr) char *ptr; {
+ ��膩��鰹申鐃順�渇��膩��� 116 while(*ptr == ' ' || *ptr == '\t')
+ ��膩��鰹申鐃順�渇��膩��� 117 ptr++;
+ ��膩��鰹申鐃順�渇��膩��� 118 return ptr;
+ ��膩��鰹申鐃順�渇��膩��� 119 }
+ ��膩��鰹申鐃順�渇��膩��� 120
+ ��膩��鰹申鐃順�渇��膩��� 121 /* init_table - initialize translation table.
+ ��膩��鰹申鐃順�渇��膩��� 122 */
+ ��膩��鰹申鐃順�渇��膩��� 123 init_table() {
+ ��膩��鰹申鐃順�渇��膩��� 124 char buf[256],*ptr,word[128];
+ ��膩��鰹申鐃順�渇��膩��� 125 FILE *in;
+ ��膩��鰹申鐃順�渇��膩��� 126 int i;
+ ��膩��鰹申鐃順�渇��膩��� 127
+ ��膩��鰹申鐃順�渇��膩��� 128 if((in = fopen(Table_name,"r")) == NULL) { /* open file */
+ ��膩��鰹申鐃順�渇��膩��� 129 fprintf(stderr,"vms_pp: can't open '%s'",Table_name);
+ ��膩��鰹申鐃順�渇��膩��� 130 exit();
+ ��膩��鰹申鐃順�渇��膩��� 131 }
+ ��膩��鰹申鐃順�渇��膩��� 132 for(i = 0; fgets(buf,255,in) != NULL;) { /* loop through lines */
+ ��膩��鰹申鐃順�渇��膩��� 133 ptr = skip_white(buf);
+ ��膩��鰹申鐃順�渇��膩��� 134 if(*ptr == '!') /* skip comments */
+ ��膩��鰹申鐃順�渇��膩��� 135 continue;
+ ��膩��鰹申鐃順�渇��膩��� 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);
+ ��膩��鰹申鐃順�渇��膩��� 139 continue;
+ ��膩��鰹申鐃順�渇��膩��� 140 }
+ ��膩��鰹申鐃順�渇��膩��� 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);
+ ��膩��鰹申鐃順�渇��膩��� 146 continue;
+ ��膩��鰹申鐃順�渇��膩��� 147 }
+ ��膩��鰹申鐃順�渇��膩��� 148 name_table[i].value = buy(word); /* and the equivalent name */
+ ��膩��鰹申鐃順�渇��膩��� 149 i++; /* increment to next position */
+ ��膩��鰹申鐃順�渇��膩��� 150 }
+ ��膩��鰹申鐃順�渇��膩��� 151 for(; i < Max_table; i++) /* mark rest as unused */
+ ��膩��鰹申鐃順�渇��膩��� 152 name_table[i].name = 0;
+ ��膩��鰹申鐃順�渇��膩��� 153 }
+ ��膩��鰹申鐃順�渇��膩��� 154
+ ��膩��鰹申鐃順�渇��膩��� 155 /* process_line - do actual line processing
+ ��膩��鰹申鐃順�渇��膩��� 156 */
+ ��膩��鰹申鐃順�渇��膩��� 157 process_line(buf) char *buf; {
+ ��膩��鰹申鐃順�渇��膩��� 158 char *in_ptr,*out_ptr;
+ ��膩��鰹申鐃順�渇��膩��� 159 char word[128],*ptr;
+ ��膩��鰹申鐃順�渇��膩��� 160 int len;
+ ��膩��鰹申鐃順�渇��膩��� 161
+ ��膩��鰹申鐃順�渇��膩��� 162 check_pp(buf); /* check for preprocessor lines */
+ ��膩��鰹申鐃順�渇��膩��� 163
+ ��膩��鰹申鐃順�渇��膩��� 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++;
+ ��膩��鰹申鐃順�渇��膩��� 167 else {
+ ��膩��鰹申鐃順�渇��膩��� 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 */
+ ��膩��鰹申鐃順�渇��膩��� 172 *out_ptr = *ptr;
+ ��膩��鰹申鐃順�渇��膩��� 173 }
+ ��膩��鰹申鐃順�渇��膩��� 174 }
+ ��膩��鰹申鐃順�渇��膩��� 175 *out_ptr = 0;
+ ��膩��鰹申鐃順�渇��膩��� 176 }
+ ��膩��鰹申鐃順�渇��膩��� 177
+ ��膩��鰹申鐃順�渇��膩��� 178 /* check_pp - check for preprocessor lines
+ ��膩��鰹申鐃順�渇��膩��� 179 */
+ ��膩��鰹申鐃順�渇��膩��� 180 check_pp(buf) char *buf; {
+ ��膩��鰹申鐃順�渇��膩��� 181 char *ptr,*p;
+ ��膩��鰹申鐃順�渇��膩��� 182 char word[128];
+ ��膩��鰹申鐃順�渇��膩��� 183
+ ��膩��鰹申鐃順�渇��膩��� 184 ptr = skip_white(buf); /* skip white space */
+ ��膩��鰹申鐃順�渇��膩��� 185 if(*ptr != '#') /* is this a preprocessor line? */
+ ��膩��鰹申鐃順�渇��膩��� 186 return; /* no, just return */
+ ��膩��鰹申鐃順�渇��膩��� 187
+ ��膩��鰹申鐃順�渇��膩��� 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);
+ ��膩��鰹申鐃順�渇��膩��� 194 fputs(buf,out);
+ ��膩��鰹申鐃順�渇��膩��� 195 strcpy(buf,"#endif");
+ ��膩��鰹申鐃順�渇��膩��� 196 return;
+ ��膩��鰹申鐃順�渇��膩��� 197 }
+ ��膩��鰹申鐃順�渇��膩��� 198 if(!strcmp("if",word)) { /* check for if */
+ ��膩��鰹申鐃順�渇��膩��� 199 for(;;) {
+ ��膩��鰹申鐃順�渇��膩��� 200 ptr = strchr(ptr,'d'); /* look for d in defined */
+ ��膩��鰹申鐃順�渇��膩��� 201 if(!ptr) /* are we done? */
+ ��膩��鰹申鐃順�渇��膩��� 202 return;
+ ��膩��鰹申鐃順�渇��膩��� 203 if(strchr(Word_member,*(ptr-1))){ /* at beginning of word? */
+ ��膩��鰹申鐃順�渇��膩��� 204 ptr++; continue; /* no, continue looking */
+ ��膩��鰹申鐃順�渇��膩��� 205 }
+ ��膩��鰹申鐃順�渇��膩��� 206 ptr = gather_word(ptr,word); /* get the word */
+ ��膩��鰹申鐃順�渇��膩��� 207 if(strcmp(word,"defined")) /* skip if not defined */
+ ��膩��鰹申鐃順�渇��膩��� 208 continue;
+ ��膩��鰹申鐃順�渇��膩��� 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 */
+ ��膩��鰹申鐃順�渇��膩��� 216 continue;
+ ��膩��鰹申鐃順�渇��膩��� 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");
+ ��膩��鰹申鐃順�渇��膩��� 224 defined_defined = 1;
+ ��膩��鰹申鐃順�渇��膩��� 225 }
+ ��膩��鰹申鐃順�渇��膩��� 226 }
+ ��膩��鰹申鐃順�渇��膩��� 227 }
+ ��膩��鰹申鐃順�渇��膩��� 228 }
+ ��膩��鰹申鐃順�渇��膩��� 229
+ ��膩��鰹申鐃順�渇��膩��� 230 /* replace_word - look the word up in the table, and replace it
+ ��膩��鰹申鐃順�渇��膩��� 231 * if a match is found.
+ ��膩��鰹申鐃順�渇��膩��� 232 */
+ ��膩��鰹申鐃順�渇��膩��� 233 replace_word(word) char *word; {
+ ��膩��鰹申鐃順�渇��膩��� 234 int i;
+ ��膩��鰹申鐃順�渇��膩��� 235
+ ��膩��鰹申鐃順�渇��膩��� 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);
+ ��膩��鰹申鐃順�渇��膩��� 239 return;
+ ��膩��鰹申鐃順�渇��膩��� 240 }
+ ��膩��鰹申鐃順�渇��膩��� 241 fprintf(stderr,"couldn't find '%s'\n",word);
+ ��膩��鰹申鐃順�渇��膩��� 242 }