"Fossies" - the Fresh Open Source Software Archive

Member "ncc-2.8/space.C" (11 Oct 2008, 8377 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.

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <limits.h>
    4 #include <string.h>
    5 #include <ctype.h>
    6 
    7 #include "global.h"
    8 #include "dbstree.h"
    9 #include "mem_pool.h"
   10 
   11 bool include_values = true;
   12 static int tprev_line = -1, *prev_line = &tprev_line;
   13 
   14 static earray<cfile_i>          Filez;
   15 static mem_pool<clines_i>       Linez;
   16 static mem_pool<int>            tmpCODE;
   17 static mem_pool<signed char>        TsInt8;
   18 static mem_pool<signed short int>   TsInt16;
   19 static mem_pool<signed long int>    TsInt32;
   20 static mem_pool<unsigned long int>  TuInt32;
   21 static mem_pool<double>         TFloat;
   22 static int              nreserved, indx;
   23 class symboltmp; class stringtmp;
   24 static dbsTree<symboltmp>       symtree;
   25 static dbsTree<stringtmp>       strtree;
   26 
   27 class symboltmp : public dbsNodeStr
   28 {
   29    public:
   30     symboltmp   *less, *more;
   31     symboltmp (int I) : dbsNodeStr (), ID (I) { symtree.addself (this); }
   32     unsigned int ID;
   33 };
   34 
   35 class stringtmp : public dbsNodeStr
   36 {
   37    public:
   38     stringtmp   *less, *more;
   39     stringtmp (int I) : dbsNodeStr (), ID (I) { strtree.addself (this); }
   40     unsigned int ID;
   41 };
   42 
   43 
   44 static void symtoarray (symboltmp *s)
   45 {
   46     if (s->ID >= SYMBASE)
   47         C_Syms [s->ID - SYMBASE] = s->Name;
   48     delete s;
   49 }
   50 
   51 static void strtoarray (stringtmp *s)
   52 {
   53     C_Strings [s->ID - STRINGBASE] = s->Name;
   54     delete s;
   55 }
   56 
   57 static char *string;
   58 static int enter_string ()
   59 {
   60 static  unsigned int SID = STRINGBASE;
   61     //DBS_STRQUERY = true_string (string);
   62     DBS_STRQUERY = string;
   63     stringtmp *S = (stringtmp*) strtree.dbsFind ();
   64     if (S) delete [] DBS_STRQUERY;
   65     else S = new stringtmp (SID++);
   66     return S->ID;
   67 }
   68 
   69 static int c_symbol (char *s, int len)
   70 {
   71 static  unsigned int SID = SYMBASE;
   72     DBS_STRQUERY = (char*) alloca (len + 1);
   73     strncpy (DBS_STRQUERY, s, len);
   74     DBS_STRQUERY [len] = 0;
   75     symboltmp *S = (symboltmp*) symtree.dbsFind ();
   76     if (S) return S->ID;
   77     DBS_STRQUERY = StrDup (DBS_STRQUERY);
   78     S = new symboltmp (SID++);
   79     return S->ID;
   80 }
   81 
   82 static int enter_float ()
   83 {
   84     int i;
   85     TFloat [i = TFloat.alloc ()] = strtod (CTok.p, NULL);
   86     return FLOATBASE + i;
   87 }
   88 
   89 static int character_constant ();
   90 static int enter_integer ()
   91 {
   92     long int is, iu;
   93     if (CTok.type == CCONSTANT)
   94         is = character_constant ();
   95     else is = strtol (CTok.p, NULL, 0);
   96 
   97     if (is == 0) return INT8BASE;
   98     if (is < 128 && is >= -128) {
   99         TsInt8 [iu = TsInt8.alloc ()] = is;
  100         return INT8BASE + iu;
  101     }
  102     if (is < 32768 && is >= -32768) {
  103         TsInt16 [iu = TsInt16.alloc ()] = is;
  104         return INT16BASE + iu;
  105     }
  106     if (is != LONG_MAX) {
  107         TsInt32 [iu = TsInt32.alloc ()] = is;
  108         return INT32BASE + iu;
  109     }
  110     TuInt32 [iu = TuInt32.alloc ()] = strtoul (CTok.p, NULL, 0);
  111     return UINT32BASE + iu;
  112 }
  113 
  114 static void string_constant (char *s, int l)
  115 {
  116     char *ns;
  117     int si;
  118 
  119     if (!include_strings) {
  120         string = "";
  121         return;
  122     }
  123 
  124     if (string) {
  125         ns = new char [(si = strlen (string) + l) + 1];
  126         strncat (strcpy (ns, string), s, l);
  127         ns [si] = 0;
  128         delete [] string;
  129         string = ns;
  130     } else {
  131         string = new char [l + 1];
  132         strncpy (string, s, l);
  133         string [l] = 0;
  134     }
  135 }
  136 
  137 static inline void _enter_token (int i)
  138 {
  139     if (i == RESERVED_auto || i == RESERVED_volatile
  140     || i == RESERVED_inline || i == RESERVED__Complex) return;
  141 
  142     if (i == RESERVED_double) i = RESERVED_float;
  143 
  144     tmpCODE [indx = tmpCODE.alloc ()] = i;
  145     if (*prev_line != CTok.at_line) {
  146         Linez [i = Linez.alloc ()].ftok = indx;
  147         Linez [i].line = *prev_line = CTok.at_line;
  148     }
  149 }
  150 
  151 void enter_token ()
  152 {
  153     int i;
  154 
  155     if (CTok.type == IDENT_DUMMY)
  156         i = c_symbol (CTok.p, CTok.len);
  157     else if (CTok.type == STRING) {
  158         string_constant (CTok.p, CTok.len);
  159         return;
  160     } else if (CTok.type == CONSTANT || CTok.type == CCONSTANT)
  161         i = (include_values) ? enter_integer () : INT8BASE;
  162     else if (CTok.type == FCONSTANT)
  163         i = (include_values) ? enter_float () : FLOATBASE;
  164     else i = CTok.type;
  165 
  166     if (string) {
  167         _enter_token ((include_strings) ? enter_string () : STRINGBASE);
  168         string = NULL;
  169     }
  170 
  171     _enter_token (i);
  172 }
  173 
  174 void enter_file_indicator (char *f)
  175 {
  176     int last;
  177 
  178     if (!f [0]) return;
  179 
  180     if (Filez.nr) {
  181         last = Filez.nr - 1;
  182         if (!strcmp (f, Filez.x [last].file)) return;
  183         if (Filez.x [last].indx == indx + 1) {
  184             delete [] Filez.x [last].file;
  185             Filez.x [last].file = StrDup (f);
  186             return;
  187         }
  188     }
  189 
  190     last = Filez.alloc ();
  191     Filez.x [last].indx = indx + 1;
  192     Filez.x [last].file = StrDup (f);
  193 }
  194 
  195 #define LOOKBUILTIN(x) \
  196     DBS_STRQUERY = #x;\
  197     S = (symboltmp*) symtree.dbsFind ();\
  198     ccbuiltins.bt ## x = (S) ? (int) S->ID : -1;
  199 
  200 static void used_builtins ()
  201 {
  202     symboltmp *S;
  203     LOOKBUILTIN (__func__);
  204     LOOKBUILTIN (__FUNCTION__);
  205     LOOKBUILTIN (__PRETTY_FUNCTION__);
  206     LOOKBUILTIN (__builtin_return_address);
  207     LOOKBUILTIN (__builtin_alloca);
  208 }
  209 
  210 void make_norm ()
  211 {
  212     if (string) _enter_token (enter_string ());
  213 
  214     used_builtins ();
  215     //
  216     C_Ntok = tmpCODE.nr ();
  217     CODE = new int [C_Ntok + 3];
  218     tmpCODE.copy (&CODE);
  219     CODE  [C_Ntok] = FORCEERROR;
  220     CODE  [C_Ntok + 1] = ';';
  221     CODE  [C_Ntok + 2] = FORCEERROR;
  222     tmpCODE.destroy ();
  223     //
  224     C_Syms = new char* [C_Nsyms = symtree.nnodes - nreserved];
  225     symtree.deltree (symtoarray);
  226     Filez.freeze ();
  227     C_Files = Filez.x;
  228     C_Nfiles = Filez.nr;
  229     C_Nlines = Linez.nr ();
  230     Linez.copy (&C_Lines);
  231     Linez.destroy ();
  232     C_Strings = new char* [C_Nstrings = strtree.nnodes];
  233     strtree.deltree (strtoarray);
  234     //
  235     TFloat.copy (&C_Floats);
  236     TFloat.destroy ();
  237     TsInt8.copy (&C_Chars);
  238     TsInt8.destroy ();
  239     TsInt16.copy (&C_Shortints);
  240     TsInt16.destroy ();
  241     TsInt32.copy (&C_Ints);
  242     TsInt32.destroy ();
  243     TuInt32.copy (&C_Unsigned);
  244     TuInt32.destroy ();
  245 }
  246 
  247 #define RESERVED(x) \
  248     DBS_STRQUERY = #x;\
  249     symtree.dbsFind ();\
  250     new symboltmp (RESERVED_ ## x); \
  251     ++nreserved;
  252 
  253 static void reserved_c ()
  254 {
  255     RESERVED(__inline__);
  256     RESERVED(__inline);
  257     RESERVED(inline);
  258     RESERVED(do);
  259     RESERVED(struct);
  260     RESERVED(case);
  261     RESERVED(for);
  262     RESERVED(short);
  263     RESERVED(union);
  264     RESERVED(sizeof);
  265     RESERVED(register);
  266     RESERVED(break);
  267     RESERVED(auto);
  268     RESERVED(continue);
  269     RESERVED(const);
  270     RESERVED(default);
  271     RESERVED(enum);
  272     RESERVED(else);
  273     RESERVED(extern);
  274     RESERVED(goto);
  275     RESERVED(if);
  276     RESERVED(long);
  277     RESERVED(return);
  278     RESERVED(signed);
  279     RESERVED(static);
  280     RESERVED(switch);
  281     RESERVED(typedef);
  282     RESERVED(unsigned);
  283     RESERVED(volatile);
  284     RESERVED(while);
  285     RESERVED(__asm__);
  286 #ifdef GNU_VIOLATIONS
  287     RESERVED(__typeof__);
  288     RESERVED(__label__);
  289     RESERVED(_Complex);
  290 #endif
  291 
  292     RESERVED(void);
  293     RESERVED(int);
  294     RESERVED(char);
  295     RESERVED(float);
  296     RESERVED(double);
  297 }
  298 
  299 Symbol intern_sym;
  300 
  301 void prepare ()
  302 {
  303     reserved_c ();
  304     symtree.dbsBalance ();
  305     indx = -1;
  306     if (!include_strings) {
  307         string = "";
  308         enter_string ();
  309     }
  310     string = NULL;
  311     TFloat [TFloat.alloc ()] = 0.0;
  312     // often have division by zero if all values zeroed
  313     TsInt8 [TsInt8.alloc ()] = include_values ? 0 : 1;
  314     intern_sym = c_symbol ("_@@@_", 5);
  315 }
  316 
  317 //******************************************************
  318 //  small utility
  319 //  evaluate number which is integer
  320 //******************************************************
  321 int getint (int token)
  322 {
  323     if (token < INT16BASE) return C_Chars [token - INT8BASE];
  324     if (token < INT32BASE) return C_Shortints [token - INT16BASE];
  325     if (token < UINT32BASE) return C_Ints [token - INT32BASE];
  326     return syntax_error ("Expected integer and got something else", "else");
  327 }
  328 
  329 //******************************************************
  330 //  small utility
  331 //  evaluate character constants
  332 //      a terrible deja-vu...
  333 //******************************************************
  334 
  335 const static char escc [] = "ntvbrfae";
  336 const static char esct [] = "\n\t\v\b\r\f\a\e";
  337 
  338 static char escape (char **s)
  339 {
  340     char i;
  341     (*s)++;
  342     if (**s >= '0' && **s < '8') {
  343         i = **s - '0';
  344         (*s)++;
  345         if (**s >= '0' && **s < '8') {
  346             i <<= 3;
  347             i = **s - '0';
  348             (*s)++;
  349             if (**s >= '0' && **s < '8') {
  350                 i <<= 3;
  351                 i += **s - '0';
  352             }
  353         }
  354         return i;
  355     }
  356     if (**s == 'x') {
  357         (*s)++;
  358         if (!isxdigit (**s)) warning ("bad hex character escape");
  359         i = (**s - '0') << 4;
  360         (*s)++;
  361         if (!isxdigit (**s)) warning ("bad hex character escape");
  362         return i + (**s - '0');
  363     }
  364     for (i = 0; i < (int) sizeof (escc) - 1; i++)
  365         if (**s == escc [(int)i]) return esct [(int)i];
  366     return **s;
  367 }
  368 
  369 static int character_constant ()
  370 {
  371     char *s = CTok.p;
  372     if (*s == '\'') {
  373         warning ("Empty character constant");
  374         return 0;
  375     }
  376     return (*s == '\\') ? escape (&s) : s [1];
  377 }
  378 
  379 #if 0
  380 static char *true_string (char *s)
  381 {
  382     char *e = s;
  383     char *d;
  384     char *tmp = d = (char*) alloca (strlen (s) + 5); // heuristic
  385 
  386     for (;*s; s++) *d++ = *s == '\\' ? escape (&s) : *s;
  387     *d = 0;
  388     delete [] e;
  389     return StrDup (tmp);
  390 }
  391 #endif