|
|
1.1 ! root 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 distributed in the hope that it will be useful, ! 8: but WITHOUT ANY WARRANTY. No author or distributor ! 9: accepts responsibility to anyone for the consequences of using it ! 10: or for whether it serves any particular purpose or works at all, ! 11: unless he says so in writing. Refer to the GNU Emacs General Public ! 12: License for full details. ! 13: ! 14: Everyone is granted permission to copy, modify and redistribute ! 15: GNU Emacs, but only under the conditions described in the ! 16: GNU Emacs General Public License. A copy of this license is ! 17: supposed to have been given to you along with GNU Emacs so you ! 18: can know your rights and responsibilities. It should be in a ! 19: file named COPYING. Among other things, the copyright notice ! 20: and this notice must be preserved on all copies. ! 21: ! 22: * ! 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. ! 31: * ! 32: * Additionally, the "#undef foo" construct is replaced with: ! 33: * #ifdef foo ! 34: * #undef foo ! 35: * #endif ! 36: * ! 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) ! 45: * ! 46: * This last contruction 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. ! 50: */ ! 51: #include <stdio.h> ! 52: ! 53: #define Max_table 100 ! 54: #define Table_name "vms_pp.trans" ! 55: #define Word_member \ ! 56: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$" ! 57: ! 58: static FILE *in,*out; /* read from, write to */ ! 59: struct item { /* symbol table entries */ ! 60: char *name; ! 61: char *value; ! 62: }; ! 63: static struct item name_table[Max_table]; /* symbol table */ ! 64: static int defined_defined = 0; /* small optimization */ ! 65: ! 66: main(argc,argv) int argc; char **argv; { ! 67: char buffer[1024]; ! 68: ! 69: if(argc != 3) { /* check argument count */ ! 70: fprintf(stderr,"usage: vms_pp infile outfile"); ! 71: exit(); ! 72: } ! 73: init_table(); /* read in translation table */ ! 74: ! 75: /* open input and output files ! 76: */ ! 77: if((in = fopen(argv[1],"r")) == NULL) { ! 78: fprintf(stderr,"vms_pp: unable to open file '%s'",argv[1]); ! 79: exit(); ! 80: } ! 81: if((out = fopen(argv[2],"w")) == NULL) { ! 82: fprintf(stderr,"vms_pp: unable to create file '%s'",argv[2]); ! 83: exit(); ! 84: } ! 85: ! 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 */ ! 89: } ! 90: } ! 91: ! 92: /* buy - allocate and copy a string ! 93: */ ! 94: static char *buy(str) char *str; { ! 95: char *temp; ! 96: ! 97: if(!(temp = malloc(strlen(str)+1))) { ! 98: fprintf(stderr,"vms_pp: can't allocate memory"); ! 99: exit(); ! 100: } ! 101: strcpy(temp,str); ! 102: return temp; ! 103: } ! 104: ! 105: /* gather_word - return a buffer full of the next word ! 106: */ ! 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; ! 112: } ! 113: ! 114: /* skip_white - skip white space ! 115: */ ! 116: static char *skip_white(ptr) char *ptr; { ! 117: while(*ptr == ' ' || *ptr == '\t') ! 118: ptr++; ! 119: return ptr; ! 120: } ! 121: ! 122: /* init_table - initialize translation table. ! 123: */ ! 124: init_table() { ! 125: char buf[256],*ptr,word[128]; ! 126: FILE *in; ! 127: int i; ! 128: ! 129: if((in = fopen(Table_name,"r")) == NULL) { /* open file */ ! 130: fprintf(stderr,"vms_pp: can't open '%s'",Table_name); ! 131: exit(); ! 132: } ! 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; ! 141: } ! 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; ! 148: } ! 149: name_table[i].value = buy(word); /* and the equivalent name */ ! 150: i++; /* increment to next position */ ! 151: } ! 152: for(; i < Max_table; i++) /* mark rest as unused */ ! 153: name_table[i].name = 0; ! 154: } ! 155: ! 156: /* process_line - do actual line processing ! 157: */ ! 158: process_line(buf) char *buf; { ! 159: char *in_ptr,*out_ptr; ! 160: char word[128],*ptr; ! 161: int len; ! 162: ! 163: check_pp(buf); /* check for preprocessor lines */ ! 164: ! 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; ! 174: } ! 175: } ! 176: *out_ptr = 0; ! 177: } ! 178: ! 179: /* check_pp - check for preprocessor lines ! 180: */ ! 181: check_pp(buf) char *buf; { ! 182: char *ptr,*p; ! 183: char word[128]; ! 184: ! 185: ptr = skip_white(buf); /* skip white space */ ! 186: if(*ptr != '#') /* is this a preprocessor line? */ ! 187: return; /* no, just return */ ! 188: ! 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; ! 198: } ! 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 */ ! 206: } ! 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; ! 226: } ! 227: } ! 228: } ! 229: } ! 230: ! 231: /* replace_word - look the word up in the table, and replace it ! 232: * if a match is found. ! 233: */ ! 234: replace_word(word) char *word; { ! 235: int i; ! 236: ! 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; ! 241: } ! 242: fprintf(stderr,"couldn't find '%s'\n",word); ! 243: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.