home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / icont / trans.c < prev    next >
C/C++ Source or Header  |  2002-01-18  |  3KB  |  138 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. static    void    trans1        (char *filename, char *tgtdir);
  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, tgtdir)
  30. char **ifiles;
  31. char *tgtdir;
  32.    {
  33.    tmalloc();            /* allocate memory for translation */
  34.  
  35.    afatals = 0;
  36.  
  37. #ifdef MultipleRuns
  38.    yylexinit();            /* initialize lexical analyser */
  39.    tcodeinit();            /* initialize code generator */
  40. #endif                    /* Multiple Runs */
  41.  
  42.    while (*ifiles) {
  43.       trans1(*ifiles++, tgtdir);    /* translate each file in turn */
  44.       afatals += tfatals;
  45.       }
  46.    tmfree();            /* free memory used for translation */
  47.  
  48.    /*
  49.     * Report information about errors and warnings and be correct about it.
  50.     */
  51.    if (afatals == 1)
  52.       report("1 error\n");
  53.    else if (afatals > 1) {
  54.       char tmp[12];
  55.       sprintf(tmp, "%d errors\n", afatals);
  56.       report(tmp);
  57.       }
  58.    else
  59.       report("No errors\n");
  60.  
  61.    return afatals;
  62.    }
  63.  
  64. /*
  65.  * translate one file.
  66.  */
  67. static void trans1(filename, tgtdir)
  68. char *filename, *tgtdir;
  69. {
  70.    char oname1[MaxFileName];    /* buffer for constructing file name */
  71.    char oname2[MaxFileName];    /* buffer for constructing file name */
  72.  
  73.    tfatals = 0;            /* reset error counts */
  74.    nocode = 0;            /* allow code generation */
  75.    in_line = 1;            /* start with line 1, column 0 */
  76.    incol = 0;
  77.    peekc = 0;            /* clear character lookahead */
  78.  
  79.    if (!ppinit(filename,lpath,m4pre))
  80.       quitf("cannot open %s",filename);
  81.  
  82.    if (strcmp(filename,"-") == 0)
  83.       filename = "stdin";
  84.  
  85.    report(filename);
  86.  
  87.    if (pponly) {
  88.       ppecho();
  89.       return;
  90.       }
  91.  
  92. #ifndef VarTran
  93.    /*
  94.     * Form names for the .u1 and .u2 files and open them.
  95.     *  Write the ucode version number to the .u2 file.
  96.     */
  97.    makename(oname1, tgtdir, filename, U1Suffix);
  98.    codefile = fopen(oname1, WriteText);
  99.    if (codefile == NULL)
  100.       quitf("cannot create %s", oname1);
  101.    makename(oname2, tgtdir, filename, U2Suffix);
  102.    globfile = fopen(oname2, WriteText);
  103.    if (globfile == NULL)
  104.       quitf("cannot create %s", oname2);
  105.    writecheck(fprintf(globfile,"version\t%s\n",UVersion));
  106. #endif                    /* VarTran */
  107.  
  108.    tok_loc.n_file = filename;
  109.    in_line = 1;
  110.  
  111.    tminit();                /* Initialize data structures */
  112.    yyparse();                /* Parse the input */
  113.  
  114.    /*
  115.     * Close the output files.
  116.     */
  117.  
  118. #ifndef VarTran
  119.    if (fclose(codefile) != 0 || fclose(globfile) != 0)
  120.       quit("cannot close ucode file");
  121. #endif                    /* VarTran */
  122.  
  123.    if (tfatals) {
  124.       remove(oname1);
  125.       remove(oname2);
  126.       }
  127.    }
  128.  
  129. /*
  130.  * writecheck - check the return code from a stdio output operation
  131.  */
  132. void writecheck(rc)
  133. int rc;
  134.    {
  135.    if (rc < 0)
  136.       quit("cannot write to ucode file");
  137. }
  138.