"Fossies" - the Fresh Open Source Software Archive

Member "ncc-2.8/main.C" (11 Oct 2008, 5010 Bytes) of package /linux/privat/old/ncc-2.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "main.C" see the Fossies "Dox" file reference documentation.

    1 #include <sys/types.h>
    2 #include <sys/mman.h>
    3 #include <sys/stat.h>
    4 #include <fcntl.h>
    5 #include <sys/types.h>
    6 #include <stdlib.h>
    7 #include <string.h>
    8 #include <stdio.h>
    9 #include <unistd.h>
   10 #include <ctype.h>
   11 
   12 #include "global.h"
   13 
   14 
   15 extern bool quiet;
   16 class ncci * ncc;
   17 
   18 //
   19 int*        CODE;
   20 int     C_Ntok;
   21 char**      C_Syms;
   22 int     C_Nsyms;
   23 char**      C_Strings;
   24 int     C_Nstrings;
   25 cfile_i*    C_Files;
   26 int     C_Nfiles;
   27 clines_i*   C_Lines;
   28 int     C_Nlines;
   29 
   30 double*     C_Floats;
   31 signed char*    C_Chars;
   32 short int*  C_Shortints;
   33 long int*   C_Ints;
   34 unsigned long*  C_Unsigned;
   35 // *********** --------- ***********
   36 
   37 
   38 struct __builtins__ ccbuiltins;
   39 
   40 int syntax_error (int i, char *c)
   41 {
   42     if (c) fprintf (stderr, "ncc-error :  %s \n", c);
   43     debug ("syntax error:", i - 25, 50);
   44     exit (1);
   45 }
   46 
   47 int syntax_error (char *a1, char *a2)
   48 {
   49     fprintf (stderr, "ncc-error: %s %s\n", a1, a2);
   50     exit (1);
   51 }
   52 
   53 int syntax_error (int i, char *p, char *t)
   54 {
   55     fprintf (stderr, "ncc-error %s --> [%s]\n", p, t);
   56     debug ("syntax error:", i - 25, 50);
   57     exit (1);
   58 }
   59 
   60 static  int nhe = 0;
   61 void half_error (char *m1, char *m2)
   62 {
   63 #define STDDBG stderr
   64     if (m2) fprintf (STDDBG, "%s %s\n", m1, m2);
   65     else fprintf (STDDBG, "%s\n", m1);
   66     debug ("expression ncc-error:", ExpressionPtr - 15, 30);
   67     if (halt_on_error) { *(int*)0=0; }
   68     if (no_error)
   69         report_error ();
   70     else if (nhe++ > 20)
   71         syntax_error ("Maximum number of errors", "aborted");
   72     throw EXPR_ERROR ();
   73 }
   74 
   75 void warning (char *m1, char m2)
   76 {
   77     if (m2) fprintf (stderr, "warning:%s %c\n", m1, m2);
   78     else fprintf (stderr, "warning:%s\n", m1);
   79 }
   80 
   81 void yylex_open (char *file)
   82 {
   83     fprintf (stderr, "Opening preprocessed file %s\n", file);
   84     load_file C (file);
   85     if (C.success != ZC_OK) {
   86         fprintf (stderr, "Problems opening %s\n", file);
   87         exit (1);
   88     }
   89     yynorm (C.data, C.len);
   90 }
   91 
   92 extern void showdb ();
   93 
   94 static void set_cwd ()
   95 {
   96     char tmp [512];
   97     if (!getcwd (tmp, 512)) strcpy (tmp, "/TOO_BIG_PATH");
   98     cwd = StrDup (strcat (tmp, "/"));
   99 }
  100 
  101 int main (int argc, char **argv)
  102 {
  103     set_cwd ();
  104 
  105     // parse options and invoke preprocessor
  106     preproc (argc, argv);
  107 
  108     // initialize lexical analyser
  109     prepare ();
  110 
  111     // lexical analysis
  112     yylex_open (preprocfile);
  113 
  114     // unlink NCC.i if not -ncspp
  115     if (preprocfile == PREPROCESSOR_OUTPUT)
  116         unlink (preprocfile);
  117 
  118     // sum up into the big normalized array of tokens
  119     make_norm ();
  120 
  121     // strings are in the symbol table, so look for ncc keys
  122     ncc_keys ();
  123 
  124     // initialize syntactical analyser database
  125     init_cdb ();
  126 
  127     // syntactical analysis
  128     parse_C ();
  129 
  130     // object file useless ?
  131     if (nhe && !no_error) syntax_error ("Compilation errors", "in expressions");
  132 
  133     // final stuff
  134     ncc->finir ();
  135 
  136     // file-as-function calling functions defined in this file
  137     if (usage_only) functions_of_file ();
  138 
  139     // report structure declaration locations
  140     if (usage_only && report_structs) structs_of_file ();
  141 
  142     // print out what we learned from all this
  143     showdb ();
  144     fprintf (stderr,
  145         quiet ? "%i Tokens, %i symbols, %i expressions\n":
  146          "%i Tokens\n%i symbols\n%i expressions\n",
  147          C_Ntok, C_Nsyms, last_result);
  148 
  149 }
  150 
  151 char *StrDup (char *c)
  152 {
  153     return strcpy (new char [strlen (c) + 1], c);
  154 }
  155 
  156 char *StrDup (char *c, int i)
  157 {
  158     char *d = new char [i + 1];
  159     d [i] = 0;
  160     return strncpy (d, c, i);
  161 }
  162 
  163 #ifdef EFENCE
  164 // ###### Electric Fence ######
  165 // These activate efence on our C++ allocations.
  166 // #############################################
  167 
  168 void *operator new (size_t s)
  169 {
  170     return malloc (s);
  171 }
  172 
  173 void operator delete (void *p)
  174 {
  175     free (p);
  176 }
  177 
  178 void *operator new[] (size_t s)
  179 {
  180     return malloc (s);
  181 }
  182 
  183 void operator delete[] (void *p)
  184 {
  185     free (p);
  186 }
  187 
  188 #endif
  189 
  190 #ifdef _POSIX_MAPPED_FILES
  191 
  192 load_file::load_file (char *f)
  193 {
  194     data = NULL;
  195     success = ZC_NA;
  196     fd = -1;
  197 
  198     struct stat statbuf;
  199     if (stat (f, &statbuf) == -1) return;
  200     len = statbuf.st_size;
  201     if (len == -1 || (fd = open (f, O_RDONLY)) == -1) return;
  202 
  203     success = ((data = (char*) mmap (0, len, PROT_READ, MAP_PRIVATE, fd, 0))
  204          != MAP_FAILED) ? ZC_OK : ZC_FF;
  205 }
  206 
  207 load_file::~load_file ()
  208 {
  209     if (data && data != MAP_FAILED) munmap (data, len);
  210     if (fd != -1) close (fd);
  211 }
  212 
  213 #else
  214 
  215 load_file::load_file (char *f)
  216 {
  217     data = NULL;
  218     success = ZC_NA;
  219     fd = -1;
  220 
  221     struct stat statbuf;
  222     if (stat (f, &statbuf) == -1) return;
  223     len = statbuf.st_size;
  224     if (len == -1 || (fd = open (f, O_RDONLY)) == -1) return;
  225 
  226     data = (char*) malloc (len);
  227     success = (len = read (fd, data, len)) != -1 ? ZC_OK : ZC_FF;
  228 }
  229 
  230 load_file::~load_file ()
  231 {
  232     if (data) free (data);
  233     if (fd != -1) close (fd);
  234 }
  235 
  236 #endif
  237 //***********************************************************************
  238 //      definitions
  239 //***********************************************************************
  240 
  241 void intcpycat (int *d, const int *s1, const int *s2)
  242 {
  243     while ((*d++ = *s1++) != -1);
  244     d -= 1;
  245     while ((*d++ = *s2++) != -1);
  246 }
  247 
  248 int *intdup (int *i)
  249 {
  250     int *r = new int [intlen (i) + 1];
  251     intcpy (r, i);
  252     return r;
  253 }
  254 
  255 int intcmp (int *x, int *y)
  256 {
  257     while (*x == *y && *x != -1) x++, y++;
  258     return (*x < *y) ? -1 : (*x == *y) ? 0 : 1;
  259 }
  260 
  261 void intncpy (int *d, int *s, int l)
  262 {
  263     while (l--) *d++ = *s++;
  264 }