home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / vms-pp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-01  |  7.0 KB  |  245 lines

  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 XEmacs.
  6.  
  7. XEmacs is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with XEmacs; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Synched up with: Not synched with FSF. */
  22.  
  23.  *
  24.  * Usage:
  25.  *    vms_pp infile outfile
  26.  * implicit inputs:
  27.  *    The file "vms_pp.trans" has the names and their translations.
  28.  * description:
  29.  *    Vms_pp takes the input file and scans it, replacing the long
  30.  *    names with shorter names according to the table read in from
  31.  *    vms_pp.trans. The line is then written to the output file.
  32.  *
  33.  *    Additionally, the "#undef foo" construct is replaced with:
  34.  *        #ifdef foo
  35.  *        #undef foo
  36.  *        #endif
  37.  *
  38.  *    The construct #if defined(foo) is replaced with
  39.  *        #ifdef foo
  40.  *        #define foo_VAL 1
  41.  *        #else
  42.  *        #define foo_VAL 0
  43.  *        #endif
  44.  *        #define defined(XX) XX_val
  45.  *        #if defined(foo)
  46.  *
  47.  *    This last contruction only works on single line #if's and takes
  48.  *    advantage of a questionable C pre-processor trick. If there are
  49.  *    comments within the #if, that contain "defined", then this will
  50.  *    bomb.
  51.  */
  52. #include <stdio.h>
  53.  
  54. #define Max_table 100
  55. #define Table_name "vms_pp.trans"
  56. #define Word_member \
  57. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$"
  58.  
  59. static   FILE *in,*out;            /* read from, write to */
  60. struct item {                /* symbol table entries */
  61.   char *name;
  62.   char *value;
  63. };
  64. static struct item name_table[Max_table]; /* symbol table */
  65. static int defined_defined = 0;        /* small optimization */
  66.  
  67. main(argc,argv) int argc; char **argv; {
  68.   char buffer[1024];
  69.  
  70.   if(argc != 3) {            /* check argument count */
  71.     fprintf(stderr,"usage: vms_pp infile outfile");
  72.     exit();
  73.   }
  74.   init_table();                /* read in translation table */
  75.  
  76. /* open input and output files
  77.  */
  78.   if((in = fopen(argv[1],"r")) == NULL) {
  79.     fprintf(stderr,"vms_pp: unable to open file '%s'",argv[1]);
  80.     exit();
  81.   }
  82.   if((out = fopen(argv[2],"w")) == NULL) {
  83.     fprintf(stderr,"vms_pp: unable to create file '%s'",argv[2]);
  84.     exit();
  85.   }
  86.  
  87.   while(fgets(buffer,1023,in) != NULL) { /* loop through buffer until end */
  88.     process_line(buffer);        /* process the line */
  89.     fputs(buffer,out);            /* write out the line */
  90.   }
  91. }
  92.  
  93. /* buy - allocate and copy a string
  94.  */
  95. static char *buy(str) char *str; {
  96.   char *temp;
  97.  
  98.   if(!(temp = malloc(strlen(str)+1))) {
  99.     fprintf(stderr,"vms_pp: can't allocate memory");
  100.     exit();
  101.   }
  102.   strcpy(temp,str);
  103.   return temp;
  104. }
  105.  
  106. /* gather_word - return a buffer full of the next word
  107.  */
  108. static char *gather_word(ptr,word) char *ptr, *word;{
  109.   for(; strchr(Word_member,*ptr); ptr++,word++)
  110.     *word = *ptr;
  111.   *word = 0;
  112.   return ptr;
  113. }
  114.  
  115. /* skip_white - skip white space
  116.  */
  117. static char *skip_white(ptr) char *ptr; {
  118.   while(*ptr == ' ' || *ptr == '\t')
  119.     ptr++;
  120.   return ptr;
  121. }
  122.  
  123. /* init_table - initialize translation table.
  124.  */
  125. init_table() {
  126.   char buf[256],*ptr,word[128];
  127.   FILE *in;
  128.   int i;
  129.  
  130.   if((in = fopen(Table_name,"r")) == NULL) { /* open file */
  131.     fprintf(stderr,"vms_pp: can't open '%s'",Table_name);
  132.     exit();
  133.   }
  134.   for(i = 0; fgets(buf,255,in) != NULL;) { /* loop through lines */
  135.     ptr = skip_white(buf);
  136.     if(*ptr == '!')            /* skip comments */
  137.       continue;
  138.     ptr = gather_word(ptr,word);    /* get long word */
  139.     if(*word == 0) {            /* bad entry */
  140.       fprintf(stderr,"vms_pp: bad input line '%s'\n",buf);
  141.       continue;
  142.     }
  143.     name_table[i].name = buy(word);    /* set up the name */
  144.     ptr = skip_white(ptr);        /* skip white space */
  145.     ptr = gather_word(ptr,word);    /* get equivalent name */
  146.     if(*word == 0) {            /* bad entry */
  147.       fprintf(stderr,"vms_pp: bad input line '%s'\n",buf);
  148.       continue;
  149.     }
  150.     name_table[i].value = buy(word);    /* and the equivalent name */
  151.     i++;                /* increment to next position */
  152.   }
  153.   for(; i < Max_table; i++)        /* mark rest as unused */
  154.     name_table[i].name = 0;
  155. }
  156.  
  157. /* process_line - do actual line processing
  158.  */
  159. process_line(buf) char *buf; {
  160.   char *in_ptr,*out_ptr;
  161.   char word[128],*ptr;
  162.   int len;
  163.  
  164.   check_pp(buf);            /* check for preprocessor lines */
  165.  
  166.   for(in_ptr = out_ptr = buf; *in_ptr;) {
  167.     if(!strchr(Word_member,*in_ptr))    /* non alpha-numeric? just copy */
  168.       *out_ptr++ = *in_ptr++;
  169.     else {
  170.       in_ptr = gather_word(in_ptr,word); /* get the 'word' */
  171.       if(strlen(word) > 31)        /* length is too long */
  172.     replace_word(word);        /* replace the word */
  173.       for(ptr = word; *ptr; ptr++,out_ptr++) /* copy out the word */
  174.       *out_ptr = *ptr;
  175.     }
  176.   }
  177.   *out_ptr = 0;
  178. }
  179.  
  180. /* check_pp - check for preprocessor lines
  181.  */
  182. check_pp(buf) char *buf; {
  183.   char *ptr,*p;
  184.   char word[128];
  185.  
  186.   ptr = skip_white(buf);        /* skip white space */
  187.   if(*ptr != '#')            /* is this a preprocessor line? */
  188.     return;                /* no, just return */
  189.  
  190.   ptr = skip_white(++ptr);        /* skip white */
  191.   ptr = gather_word(ptr,word);        /* get command word */
  192.   if(!strcmp("undef",word)) {        /* undef? */
  193.     ptr = skip_white(ptr);
  194.     ptr = gather_word(ptr,word);    /* get the symbol to undef */
  195.     fprintf(out,"#ifdef %s\n",word);
  196.     fputs(buf,out);
  197.     strcpy(buf,"#endif");
  198.     return;
  199.   }
  200.   if(!strcmp("if",word)) {        /* check for if */
  201.     for(;;) {
  202.       ptr = strchr(ptr,'d');        /* look for d in defined */
  203.       if(!ptr)                /* are we done? */
  204.     return;
  205.       if(strchr(Word_member,*(ptr-1))){    /* at beginning of word? */
  206.     ptr++; continue;        /* no, continue looking */
  207.       }
  208.       ptr = gather_word(ptr,word);    /* get the word */
  209.       if(strcmp(word,"defined"))    /* skip if not defined */
  210.     continue;
  211.       ptr = skip_white(ptr);        /* skip white */
  212.       if(*ptr != '(')            /* look for open paren */
  213.     continue;            /* error, continue */
  214.       ptr++;                /* skip paren */
  215.       ptr = skip_white(ptr);        /* more white skipping */
  216.       ptr = gather_word(ptr,word);    /* get the thing to test */
  217.       if(!*word)            /* null word is bad */
  218.     continue;
  219.       fprintf(out,"#ifdef %s\n",word);    /* generate the code */
  220.       fprintf(out,"#define %s_VAL 1\n",word);
  221.       fprintf(out,"#else\n");
  222.       fprintf(out,"#define %s_VAL 0\n",word);
  223.       fprintf(out,"#endif\n");
  224.       if(!defined_defined) {
  225.     fprintf(out,"#define defined(XXX) XXX/**/_VAL\n");
  226.     defined_defined = 1;
  227.       }
  228.     }
  229.   }
  230. }
  231.  
  232. /* replace_word - look the word up in the table, and replace it
  233.  *          if a match is found.
  234.  */
  235. replace_word(word) char *word; {
  236.   int i;
  237.  
  238.   for(i = 0; i < Max_table && name_table[i].name; i++)
  239.     if(!strcmp(word,name_table[i].name)) {
  240.       strcpy(word,name_table[i].value);
  241.       return;
  242.     }
  243.   fprintf(stderr,"couldn't find '%s'\n",word);
  244. }
  245.