#include <stdio.h>
#define Max_table 100
#define Table_name "vms_pp.trans"
#define Word_member \
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$"
static FILE *in,*out;
struct item {
char *name;
char *value;
};
static struct item name_table[Max_table];
static int defined_defined = 0;
main(argc,argv) int argc; char **argv; {
char buffer[1024];
if(argc != 3) {
fprintf(stderr,"usage: vms_pp infile outfile");
exit();
}
init_table();
if((in = fopen(argv[1],"r")) == NULL) {
fprintf(stderr,"vms_pp: unable to open file '%s'",argv[1]);
exit();
}
if((out = fopen(argv[2],"w")) == NULL) {
fprintf(stderr,"vms_pp: unable to create file '%s'",argv[2]);
exit();
}
while(fgets(buffer,1023,in) != NULL) {
process_line(buffer);
fputs(buffer,out);
}
}
static char *buy(str) char *str; {
char *temp;
if(!(temp = malloc(strlen(str)+1))) {
fprintf(stderr,"vms_pp: can't allocate memory");
exit();
}
strcpy(temp,str);
return temp;
}
static char *gather_word(ptr,word) char *ptr, *word;{
for(; strchr(Word_member,*ptr); ptr++,word++)
*word = *ptr;
*word = 0;
return ptr;
}
static char *skip_white(ptr) char *ptr; {
while(*ptr == ' ' || *ptr == '\t')
ptr++;
return ptr;
}
init_table() {
char buf[256],*ptr,word[128];
FILE *in;
int i;
if((in = fopen(Table_name,"r")) == NULL) {
fprintf(stderr,"vms_pp: can't open '%s'",Table_name);
exit();
}
for(i = 0; fgets(buf,255,in) != NULL;) {
ptr = skip_white(buf);
if(*ptr == '!')
continue;
ptr = gather_word(ptr,word);
if(*word == 0) {
fprintf(stderr,"vms_pp: bad input line '%s'\n",buf);
continue;
}
name_table[i].name = buy(word);
ptr = skip_white(ptr);
ptr = gather_word(ptr,word);
if(*word == 0) {
fprintf(stderr,"vms_pp: bad input line '%s'\n",buf);
continue;
}
name_table[i].value = buy(word);
i++;
}
for(; i < Max_table; i++)
name_table[i].name = 0;
}
process_line(buf) char *buf; {
char *in_ptr,*out_ptr;
char word[128],*ptr;
int len;
check_pp(buf);
for(in_ptr = out_ptr = buf; *in_ptr;) {
if(!strchr(Word_member,*in_ptr))
*out_ptr++ = *in_ptr++;
else {
in_ptr = gather_word(in_ptr,word);
if(strlen(word) > 31)
replace_word(word);
for(ptr = word; *ptr; ptr++,out_ptr++)
*out_ptr = *ptr;
}
}
*out_ptr = 0;
}
check_pp(buf) char *buf; {
char *ptr,*p;
char word[128];
ptr = skip_white(buf);
if(*ptr != '#')
return;
ptr = skip_white(++ptr);
ptr = gather_word(ptr,word);
if(!strcmp("undef",word)) {
ptr = skip_white(ptr);
ptr = gather_word(ptr,word);
fprintf(out,"#ifdef %s\n",word);
fputs(buf,out);
strcpy(buf,"#endif");
return;
}
if(!strcmp("if",word)) {
for(;;) {
ptr = strchr(ptr,'d');
if(!ptr)
return;
if(strchr(Word_member,*(ptr-1))){
ptr++; continue;
}
ptr = gather_word(ptr,word);
if(strcmp(word,"defined"))
continue;
ptr = skip_white(ptr);
if(*ptr != '(')
continue;
ptr++;
ptr = skip_white(ptr);
ptr = gather_word(ptr,word);
if(!*word)
continue;
fprintf(out,"#ifdef %s\n",word);
fprintf(out,"#define %s_VAL 1\n",word);
fprintf(out,"#else\n");
fprintf(out,"#define %s_VAL 0\n",word);
fprintf(out,"#endif\n");
if(!defined_defined) {
fprintf(out,"#define defined(XXX) XXX/**/_VAL\n");
defined_defined = 1;
}
}
}
}
replace_word(word) char *word; {
int i;
for(i = 0; i < Max_table && name_table[i].name; i++)
if(!strcmp(word,name_table[i].name)) {
strcpy(word,name_table[i].value);
return;
}
fprintf(stderr,"couldn't find '%s'\n",word);
}