home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v92.tgz / v92.tar / v92 / src / icont / trans.c < prev    next >
C/C++ Source or Header  |  1996-03-22  |  4KB  |  161 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 "tglobals.h"
  9. #include "tsym.h"
  10. #include "tree.h"
  11. #include "ttoken.h"
  12.  
  13. /*
  14.  * Prototypes.
  15.  */
  16.  
  17. hidden    novalue    trans1        Params((char *filename));
  18.  
  19. int tfatals;            /* number of fatal errors in file */
  20. int afatals;            /* total number of fatal errors */
  21. int nocode;            /* non-zero to suppress code generation */
  22. int in_line;            /* current input line number */
  23. int incol;            /* current input column number */
  24. int peekc;            /* one-character look ahead */
  25.  
  26. /*
  27.  * translate a number of files, returning an error count
  28.  */
  29. int trans(ifiles)
  30. char **ifiles;
  31.    {
  32.    tmalloc();            /* allocate memory for translation */
  33.  
  34.    afatals = 0;
  35.  
  36. #ifdef MultipleRuns
  37.    yylexinit();            /* initialize lexical analyser */
  38.    tcodeinit();            /* initialize code generator */
  39. #endif                    /* Multiple Runs */
  40.  
  41.    while (*ifiles) {
  42.       trans1(*ifiles++);    /* translate each file in turn */
  43.       afatals += tfatals;
  44.       }
  45.    tmfree();            /* free memory used for translation */
  46.  
  47.    /*
  48.     * Report information about errors and warnings and be correct about it.
  49.     */
  50.    if (afatals == 1)
  51.       fprintf(stderr, "1 error\n");
  52.    else if (afatals > 1)
  53.       fprintf(stderr, "%d errors\n", afatals);
  54.    else if (!silent)
  55.       fprintf(stderr, "No errors\n");
  56.  
  57.    return afatals;
  58.    }
  59.  
  60. /*
  61.  * translate one file.
  62.  */
  63. static novalue trans1(filename)
  64. char *filename;
  65. {
  66.    char oname1[MaxFileName];    /* buffer for constructing file name */
  67.    char oname2[MaxFileName];    /* buffer for constructing file name */
  68.  
  69.    tfatals = 0;            /* reset error counts */
  70.    nocode = 0;            /* allow code generation */
  71.    in_line = 1;            /* start with line 1, column 0 */
  72.    incol = 0;
  73.    peekc = 0;            /* clear character lookahead */
  74.  
  75.    if (!ppinit(filename,m4pre))
  76.       quitf("cannot open %s",filename);
  77.    ppdef("_INTERPRETED", "1");
  78.  
  79.    if (strcmp(filename,"-") == 0)
  80.       filename = "stdin";
  81.    if (!silent)
  82.       fprintf(stderr, "%s:\n",filename);
  83.  
  84.    if (pponly) {
  85.       ppecho();
  86.       return;
  87.       }
  88.  
  89. #ifndef VarTran
  90.    /*
  91.     * Form names for the .u1 and .u2 files and open them.
  92.     *  Write the ucode version number to the .u2 file.
  93.     */
  94.  
  95.    makename(oname1, TargetDir, filename, U1Suffix);
  96.  
  97. #if MVS || VM
  98. /*
  99.  * Even though the ucode data is all reasonable text characters, use
  100.  *  of text I/O may cause problems if a line is larger than LRECL.
  101.  *  This is likely to be true with any compiler, though the precise
  102.  *  disaster which results may vary.
  103.  *
  104.  * On CMS (and sometimes on MVS), empty files are not readable later.
  105.  *  Since the .U1 file may be empty, we start it off with an extra
  106.  *  blank (thrown away on input) to make sure there's something there.
  107.  */
  108.    codefile = fopen(oname1, WriteBinary);   /* avoid line splits */
  109.    if (codefile != NULL)
  110.       putc(' ', codefile);
  111. #else                    /* MVS || VM */
  112.    codefile = fopen(oname1, WriteText);
  113. #endif                    /* MVS || VM */
  114.  
  115.    if (codefile == NULL)
  116.       quitf("cannot create %s", oname1);
  117.  
  118.    makename(oname2, TargetDir, filename, U2Suffix);
  119.  
  120. #if MVS || VM
  121.    globfile = fopen(oname2, WriteBinary);
  122. #else                    /* MVS || VM */
  123.    globfile = fopen(oname2, WriteText);
  124. #endif                    /* MVS || VM */
  125.  
  126.    if (globfile == NULL)
  127.       quitf("cannot create %s", oname2);
  128.    writecheck(fprintf(globfile,"version\t%s\n",UVersion));
  129. #endif                    /* VarTran */
  130.  
  131.    tok_loc.n_file = filename;
  132.    in_line = 1;
  133.  
  134.    tminit();                /* Initialize data structures */
  135.    yyparse();                /* Parse the input */
  136.  
  137.    /*
  138.     * Close the output files.
  139.     */
  140.  
  141. #ifndef VarTran
  142.    if (fclose(codefile) != 0 || fclose(globfile) != 0)
  143.       quit("cannot close ucode file");
  144. #endif                    /* VarTran */
  145.  
  146.    if (tfatals) {
  147.       unlink(oname1);
  148.       unlink(oname2);
  149.       }
  150.    }
  151.  
  152. /*
  153.  * writecheck - check the return code from a stdio output operation
  154.  */
  155. novalue writecheck(rc)
  156. int rc;
  157.    {
  158.    if (rc < 0)
  159.       quit("cannot write to ucode file");
  160. }
  161.