home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / icon / dos / src / icont / trans.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  5KB  |  194 lines

  1. /*
  2.  * trans.c - main control of the translation process.
  3.  */
  4.  
  5. #include "../h/gsupport.h"
  6. #include "tproto.h"
  7. #include "../h/version.h"
  8. #include "globals.h"
  9. #include "trans.h"
  10. #include "tsym.h"
  11. #include "tree.h"
  12. #include "token.h"
  13.  
  14. /*
  15.  * Prototypes.
  16.  */
  17.  
  18. hidden    FILE    *preprocess    Params((char *filename));
  19. hidden    novalue    trans1        Params((char *filename));
  20.  
  21. char *comfile = NULL;
  22.  
  23. int tfatals;            /* total number of fatal errors */
  24. int nocode;            /* non-zero to suppress code generation */
  25. int in_line;            /* current input line number */
  26. int incol;            /* current input column number */
  27. int peekc;            /* one-character look ahead */
  28.  
  29. FILE *srcfile;            /* current input file */
  30. FILE *codefile;            /* current ucode output file */
  31. FILE *globfile;            /* current global table output file */
  32.  
  33. /*
  34.  * translate a number of files, returning an error count
  35.  */
  36. int trans(ifiles)
  37. char **ifiles;
  38.    {
  39.    tmalloc();            /* allocate memory for translation */
  40.  
  41. #ifdef MultipleRuns
  42.    yylexinit();            /* initialize lexical analyser */
  43.    tcodeinit();            /* initialize code generator */
  44. #endif                    /* Multiple Runs */
  45.  
  46.    while (*ifiles)
  47.       trans1(*ifiles++);    /* translate each file in turn */
  48.    tmfree();            /* free memory used for translation */
  49.  
  50.    /*
  51.     * Report information about errors and warnings and be correct about it.
  52.     */
  53.    if (tfatals == 1)
  54.       fprintf(stderr, "1 error\n");
  55.    else if (tfatals > 1)
  56.       fprintf(stderr, "%d errors\n", tfatals);
  57.    else if (!silent)
  58.       fprintf(stderr, "No errors\n");
  59.  
  60.    return tfatals;
  61.    }
  62.  
  63. /*
  64.  * translate one file.
  65.  */
  66. static novalue trans1(filename)
  67. char *filename;
  68. {
  69.    char oname[MaxFileName];    /* buffer for constructing file names */
  70.  
  71.    comfile = filename;
  72.    tfatals = 0;    /* reset error counts */
  73.    nocode = 0;            /* allow code generation */
  74.    in_line = 1;            /* start with line 1, column 0 */
  75.    incol = 0;
  76.    peekc = 0;            /* clear character lookahead */
  77.  
  78.    if (m4pre)
  79.       srcfile = preprocess(filename);
  80.    else if (strcmp(filename,"-") == 0) {
  81.       srcfile = stdin;
  82.       filename = "stdin";
  83.       }
  84.    else
  85.       srcfile = fopen(filename,ReadText);
  86.    if (srcfile == NULL)
  87.       quitf("cannot open %s",filename);
  88.    if (!silent)
  89.       fprintf(stderr, "%s:\n",filename);
  90.  
  91. #ifndef VarTran
  92.    /*
  93.     * Form names for the .u1 and .u2 files and open them.
  94.     *  Write the ucode version number to the .u2 file.
  95.     */
  96.  
  97.    makename(oname, TargetDir, filename, U1Suffix);
  98.  
  99. #if MVS || VM
  100. /*
  101.  * Even though the ucode data is all reasonable text characters, use
  102.  *  of text I/O may cause problems if a line is larger than LRECL.
  103.  *  This is likely to be true with any compiler, though the precise
  104.  *  disaster which results may vary.
  105.  *
  106.  * On CMS (and sometimes on MVS), empty files are not readable later.
  107.  *  Since the .U1 file may be empty, we start it off with an extra
  108.  *  blank (thrown away on input) to make sure there's something there.
  109.  */
  110.    codefile = fopen(oname, WriteBinary);   /* avoid line splits */
  111.    if (codefile != NULL)
  112.       putc(' ', codefile);
  113. #else                    /* MVS || VM */
  114.    codefile = fopen(oname, WriteText);
  115. #endif                    /* MVS || VM */
  116.  
  117.    if (codefile == NULL)
  118.       quitf("cannot create %s", oname);
  119.  
  120.    makename(oname, TargetDir, filename, U2Suffix);
  121.  
  122. #if MVS || VM
  123.    globfile = fopen(oname, WriteBinary);
  124. #else                    /* MVS || VM */
  125.    globfile = fopen(oname, WriteText);
  126. #endif                    /* MVS || VM */
  127.  
  128.    if (globfile == NULL)
  129.       quitf("cannot create %s", oname);
  130.    writecheck(fprintf(globfile,"version\t%s\n",UVersion));
  131. #endif                    /* VarTran */
  132.  
  133.    tok_loc.n_file = filename;
  134.    in_line = 1;
  135.  
  136.    tminit();                /* Initialize data structures */
  137.    yyparse();                /* Parse the input */
  138.  
  139.    /*
  140.     * Close the output files and the input file.
  141.     */
  142.  
  143. #ifndef VarTran
  144.    if (fclose(codefile) != 0 || fclose(globfile) != 0)
  145.       quit("cannot close ucode file");
  146. #endif                    /* VarTran */
  147.  
  148.    if (!m4pre) 
  149.       fclose(srcfile);
  150.    /* "else" is below in conditional */
  151.  
  152. #if ARM || UNIX
  153.    else if (pclose(srcfile) != 0)
  154.       quit("m4 terminated abnormally");
  155. #endif                    /* ARM || UNIX */
  156.    }
  157.  
  158. /*
  159.  * writecheck - check the return code from a stdio output operation
  160.  */
  161. novalue writecheck(rc)
  162. int rc;
  163.    {
  164.    if (rc < 0)
  165.       quit("cannot write to ucode file");
  166.    }
  167.  
  168. /*
  169.  * open a pipe to the preprocessor.
  170.  */
  171. static FILE *preprocess(filename)
  172. char *filename;
  173. {
  174.  
  175. #if MACINTOSH
  176. #if MPW
  177. /* #pragma unused(filename) */
  178. #endif                    /* MPW */
  179. #endif                    /* MACINTOSH */
  180.  
  181. #if ARM || UNIX
  182.       {
  183.       FILE *f, *popen();
  184.       char *s = alloc((unsigned int)(4+strlen(filename)));
  185.       sprintf(s,"m4 %s",filename);
  186.       f = popen(s,ReadText);
  187.       free(s);
  188.       return f;
  189.       }
  190. #else                    /* ARM || UNIX */
  191.    return NULL;
  192. #endif                    /* ARM || UNIX */
  193. }
  194.