00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "config.h"
00027 #include "system.h"
00028 #include "cpplib.h"
00029 #include "cpphash.h"
00030 #include "prefix.h"
00031 #include "intl.h"
00032 #include "mkdeps.h"
00033 #include "cppdefault.h"
00034 #ifdef SGI_MONGOOSE
00035
00036 #include "defaults.h"
00037 #endif
00038
00039
00040
00041
00042 #ifdef VMS
00043 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
00044 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
00045 #else
00046 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
00047 # define INO_T_EQ(A, B) 0
00048 # else
00049 # define INO_T_EQ(A, B) ((A) == (B))
00050 # endif
00051 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
00052 #endif
00053
00054
00055
00056
00057
00058 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
00059 struct pending_option
00060 {
00061 struct pending_option *next;
00062 const char *arg;
00063 cl_directive_handler handler;
00064 };
00065
00066
00067
00068
00069
00070 struct cpp_pending
00071 {
00072 struct pending_option *directive_head, *directive_tail;
00073
00074 struct search_path *quote_head, *quote_tail;
00075 struct search_path *brack_head, *brack_tail;
00076 struct search_path *systm_head, *systm_tail;
00077 struct search_path *after_head, *after_tail;
00078
00079 struct pending_option *imacros_head, *imacros_tail;
00080 struct pending_option *include_head, *include_tail;
00081 };
00082
00083 #ifdef __STDC__
00084 #define APPEND(pend, list, elt) \
00085 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
00086 else (pend)->list##_tail->next = (elt); \
00087 (pend)->list##_tail = (elt); \
00088 } while (0)
00089 #else
00090 #define APPEND(pend, list, elt) \
00091 do { if (!(pend)->list_head) (pend)->list_head = (elt); \
00092 else (pend)->list_tail->next = (elt); \
00093 (pend)->list_tail = (elt); \
00094 } while (0)
00095 #endif
00096
00097 static void path_include PARAMS ((cpp_reader *,
00098 char *, int));
00099 static void init_library PARAMS ((void));
00100 static void init_builtins PARAMS ((cpp_reader *));
00101 static void mark_named_operators PARAMS ((cpp_reader *));
00102 static void append_include_chain PARAMS ((cpp_reader *,
00103 char *, int, int));
00104 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
00105 struct search_path *,
00106 struct search_path **));
00107 static struct search_path * remove_dup_nonsys_dirs PARAMS ((cpp_reader *,
00108 struct search_path **,
00109 struct search_path *));
00110 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
00111 struct search_path **));
00112 static void merge_include_chains PARAMS ((cpp_reader *));
00113 static bool push_include PARAMS ((cpp_reader *,
00114 struct pending_option *));
00115 static void free_chain PARAMS ((struct pending_option *));
00116 static void init_standard_includes PARAMS ((cpp_reader *));
00117 static void read_original_filename PARAMS ((cpp_reader *));
00118 static void new_pending_directive PARAMS ((struct cpp_pending *,
00119 const char *,
00120 cl_directive_handler));
00121 static int parse_option PARAMS ((const char *));
00122 static void post_options PARAMS ((cpp_reader *));
00123
00124
00125
00126 enum { BRACKET = 0, SYSTEM, AFTER };
00127
00128
00129
00130
00131 #if HAVE_DESIGNATED_INITIALIZERS
00132
00133 #define init_trigraph_map()
00134 #define TRIGRAPH_MAP \
00135 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
00136
00137 #define END };
00138 #define s(p, v) [p] = v,
00139
00140 #else
00141
00142 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
00143 static void init_trigraph_map PARAMS ((void)) { \
00144 unsigned char *x = _cpp_trigraph_map;
00145
00146 #define END }
00147 #define s(p, v) x[p] = v;
00148
00149 #endif
00150
00151 TRIGRAPH_MAP
00152 s('=', '#') s(')', ']') s('!', '|')
00153 s('(', '[') s('\'', '^') s('>', '}')
00154 s('/', '\\') s('<', '{') s('-', '~')
00155 END
00156
00157 #undef s
00158 #undef END
00159 #undef TRIGRAPH_MAP
00160
00161 /* Given a colon-separated list of file names PATH,
00162 add all the names to the search path for include files. */
00163 static void
00164 path_include (pfile, list, path)
00165 cpp_reader *pfile;
00166 char *list;
00167 int path;
00168 {
00169 char *p, *q, *name;
00170
00171 p = list;
00172
00173 do
00174 {
00175 /* Find the end of this name. */
00176 q = p;
00177 while (*q != 0 && *q != PATH_SEPARATOR) q++;
00178 if (q == p)
00179 {
00180 /* An empty name in the path stands for the current directory. */
00181 name = (char *) xmalloc (2);
00182 name[0] = '.';
00183 name[1] = 0;
00184 }
00185 else
00186 {
00187 /* Otherwise use the directory that is named. */
00188 name = (char *) xmalloc (q - p + 1);
00189 memcpy (name, p, q - p);
00190 name[q - p] = 0;
00191 }
00192
00193 append_include_chain (pfile, name, path, path == SYSTEM);
00194
00195 /* Advance past this name. */
00196 if (*q == 0)
00197 break;
00198 p = q + 1;
00199 }
00200 while (1);
00201 }
00202
00203 /* Append DIR to include path PATH. DIR must be allocated on the
00204 heap; this routine takes responsibility for freeing it. CXX_AWARE
00205 is nonzero if the header contains extern "C" guards for C++,
00206 otherwise it is zero. */
00207 static void
00208 append_include_chain (pfile, dir, path, cxx_aware)
00209 cpp_reader *pfile;
00210 char *dir;
00211 int path;
00212 int cxx_aware;
00213 {
00214 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
00215 struct search_path *new;
00216 struct stat st;
00217 unsigned int len;
00218
00219 if (*dir == '\0')
00220 {
00221 free (dir);
00222 dir = xstrdup (".");
00223 }
00224 _cpp_simplify_pathname (dir);
00225
00226 if (stat (dir, &st))
00227 {
00228 /* Dirs that don't exist are silently ignored. */
00229 if (errno != ENOENT)
00230 cpp_errno (pfile, DL_ERROR, dir);
00231 else if (CPP_OPTION (pfile, verbose))
00232 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
00233 free (dir);
00234 return;
00235 }
00236
00237 if (!S_ISDIR (st.st_mode))
00238 {
00239 cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
00240 free (dir);
00241 return;
00242 }
00243
00244 len = strlen (dir);
00245 if (len > pfile->max_include_len)
00246 pfile->max_include_len = len;
00247
00248 new = (struct search_path *) xmalloc (sizeof (struct search_path));
00249 new->name = dir;
00250 new->len = len;
00251 INO_T_COPY (new->ino, st.st_ino);
00252 new->dev = st.st_dev;
00253
00254
00255
00256 if (path == SYSTEM || path == AFTER)
00257 new->sysp = cxx_aware ? 1 : 2;
00258 else
00259 new->sysp = 0;
00260 new->name_map = NULL;
00261 new->next = NULL;
00262
00263 switch (path)
00264 {
00265 case BRACKET: APPEND (pend, brack, new); break;
00266 case SYSTEM: APPEND (pend, systm, new); break;
00267 case AFTER: APPEND (pend, after, new); break;
00268 }
00269 }
00270
00271
00272
00273
00274
00275 static struct search_path *
00276 remove_dup_dir (pfile, prev, head_ptr)
00277 cpp_reader *pfile;
00278 struct search_path *prev;
00279 struct search_path **head_ptr;
00280 {
00281 struct search_path *cur;
00282
00283 if (prev != NULL)
00284 {
00285 cur = prev->next;
00286 prev->next = cur->next;
00287 }
00288 else
00289 {
00290 cur = *head_ptr;
00291 *head_ptr = cur->next;
00292 }
00293
00294 if (CPP_OPTION (pfile, verbose))
00295 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
00296
00297 free ((PTR) cur->name);
00298 free (cur);
00299
00300 return prev;
00301 }
00302
00303
00304
00305
00306
00307
00308 static struct search_path *
00309 remove_dup_nonsys_dirs (pfile, head_ptr, end)
00310 cpp_reader *pfile;
00311 struct search_path **head_ptr;
00312 struct search_path *end;
00313 {
00314 int sysdir = 0;
00315 struct search_path *prev = NULL, *cur, *other;
00316
00317 for (cur = *head_ptr; cur; cur = cur->next)
00318 {
00319 if (cur->sysp)
00320 {
00321 sysdir = 1;
00322 for (other = *head_ptr, prev = NULL;
00323 other != end;
00324 other = other ? other->next : *head_ptr)
00325 {
00326 if (!other->sysp
00327 && INO_T_EQ (cur->ino, other->ino)
00328 && cur->dev == other->dev)
00329 {
00330 other = remove_dup_dir (pfile, prev, head_ptr);
00331 if (CPP_OPTION (pfile, verbose))
00332 fprintf (stderr,
00333 _(" as it is a non-system directory that duplicates a system directory\n"));
00334 }
00335 prev = other;
00336 }
00337 }
00338 }
00339
00340 if (!sysdir)
00341 for (cur = *head_ptr; cur != end; cur = cur->next)
00342 prev = cur;
00343
00344 return prev;
00345 }
00346
00347
00348
00349
00350
00351 static struct search_path *
00352 remove_dup_dirs (pfile, head_ptr)
00353 cpp_reader *pfile;
00354 struct search_path **head_ptr;
00355 {
00356 struct search_path *prev = NULL, *cur, *other;
00357
00358 for (cur = *head_ptr; cur; cur = cur->next)
00359 {
00360 for (other = *head_ptr; other != cur; other = other->next)
00361 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
00362 {
00363 cur = remove_dup_dir (pfile, prev, head_ptr);
00364 break;
00365 }
00366 prev = cur;
00367 }
00368
00369 return prev;
00370 }
00371
00372
00373
00374
00375
00376
00377 static void
00378 merge_include_chains (pfile)
00379 cpp_reader *pfile;
00380 {
00381 struct search_path *quote, *brack, *systm, *qtail;
00382
00383 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
00384
00385 quote = pend->quote_head;
00386 brack = pend->brack_head;
00387 systm = pend->systm_head;
00388 qtail = pend->quote_tail;
00389
00390
00391 if (systm)
00392 pend->systm_tail->next = pend->after_head;
00393 else
00394 systm = pend->after_head;
00395
00396 if (brack)
00397 pend->brack_tail->next = systm;
00398 else
00399 brack = systm;
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414 remove_dup_nonsys_dirs (pfile, &brack, systm);
00415 remove_dup_dirs (pfile, &brack);
00416
00417 if (quote)
00418 {
00419 qtail = remove_dup_dirs (pfile, "e);
00420 qtail->next = brack;
00421
00422 qtail = remove_dup_nonsys_dirs (pfile, "e, brack);
00423
00424
00425 if (qtail && brack && INO_T_EQ (qtail->ino, brack->ino)
00426 && qtail->dev == brack->dev)
00427 brack = remove_dup_dir (pfile, qtail, "e);
00428 }
00429 else
00430 quote = brack;
00431
00432 CPP_OPTION (pfile, quote_include) = quote;
00433 CPP_OPTION (pfile, bracket_include) = brack;
00434 }
00435
00436
00437
00438 struct lang_flags
00439 {
00440 char c99;
00441 char cplusplus;
00442 char extended_numbers;
00443 char std;
00444 char dollars_in_ident;
00445 char cplusplus_comments;
00446 char digraphs;
00447 };
00448
00449
00450 static const struct lang_flags lang_defaults[] =
00451 {
00452 { 0, 0, 1, 0, 1, 1, 1 },
00453 { 1, 0, 1, 0, 1, 1, 1 },
00454 { 0, 0, 0, 1, 0, 0, 0 },
00455 { 0, 0, 0, 1, 0, 0, 1 },
00456 { 1, 0, 1, 1, 0, 1, 1 },
00457 { 0, 1, 1, 0, 1, 1, 1 },
00458 { 0, 1, 1, 1, 0, 1, 1 },
00459 { 0, 0, 1, 0, 0, 1, 0 }
00460 };
00461
00462
00463 void
00464 cpp_set_lang (pfile, lang)
00465 cpp_reader *pfile;
00466 enum c_lang lang;
00467 {
00468 const struct lang_flags *l = &lang_defaults[(int) lang];
00469
00470 CPP_OPTION (pfile, lang) = lang;
00471
00472 CPP_OPTION (pfile, c99) = l->c99;
00473 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
00474 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
00475 CPP_OPTION (pfile, std) = l->std;
00476 CPP_OPTION (pfile, trigraphs) = l->std;
00477 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
00478 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
00479 CPP_OPTION (pfile, digraphs) = l->digraphs;
00480 }
00481
00482 #ifdef HOST_EBCDIC
00483 static int opt_comp PARAMS ((const void *, const void *));
00484
00485
00486 static int
00487 opt_comp (p1, p2)
00488 const void *p1, *p2;
00489 {
00490 return strcmp (((struct cl_option *) p1)->opt_text,
00491 ((struct cl_option *) p2)->opt_text);
00492 }
00493 #endif
00494
00495
00496
00497 static void
00498 init_library ()
00499 {
00500 static int initialized = 0;
00501
00502 if (! initialized)
00503 {
00504 initialized = 1;
00505
00506 #ifdef HOST_EBCDIC
00507
00508
00509 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
00510 #endif
00511
00512
00513
00514
00515 init_trigraph_map ();
00516 }
00517 }
00518
00519
00520 cpp_reader *
00521 cpp_create_reader (lang)
00522 enum c_lang lang;
00523 {
00524 cpp_reader *pfile;
00525
00526
00527 init_library ();
00528
00529 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
00530
00531 cpp_set_lang (pfile, lang);
00532 CPP_OPTION (pfile, warn_import) = 1;
00533 CPP_OPTION (pfile, warn_multichar) = 1;
00534 CPP_OPTION (pfile, discard_comments) = 1;
00535 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
00536 CPP_OPTION (pfile, show_column) = 1;
00537 CPP_OPTION (pfile, tabstop) = 8;
00538 CPP_OPTION (pfile, operator_names) = 1;
00539 CPP_OPTION (pfile, warn_endif_labels) = 1;
00540 CPP_OPTION (pfile, warn_deprecated) = 1;
00541 CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99);
00542
00543 CPP_OPTION (pfile, pending) =
00544 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
00545
00546
00547
00548 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
00549 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
00550 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
00551 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
00552 CPP_OPTION (pfile, unsigned_char) = 0;
00553 CPP_OPTION (pfile, unsigned_wchar) = 1;
00554
00555
00556
00557 init_line_maps (&pfile->line_maps);
00558 pfile->line = 1;
00559
00560
00561 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
00562
00563
00564 pfile->avoid_paste.type = CPP_PADDING;
00565 pfile->avoid_paste.val.source = NULL;
00566 pfile->eof.type = CPP_EOF;
00567 pfile->eof.flags = 0;
00568
00569
00570 _cpp_init_tokenrun (&pfile->base_run, 250);
00571 pfile->cur_run = &pfile->base_run;
00572 pfile->cur_token = pfile->base_run.base;
00573
00574
00575 pfile->context = &pfile->base_context;
00576 pfile->base_context.macro = 0;
00577 pfile->base_context.prev = pfile->base_context.next = 0;
00578
00579
00580 pfile->a_buff = _cpp_get_buff (pfile, 0);
00581 pfile->u_buff = _cpp_get_buff (pfile, 0);
00582
00583
00584 _cpp_expand_op_stack (pfile);
00585
00586
00587 gcc_obstack_init (&pfile->buffer_ob);
00588
00589 _cpp_init_includes (pfile);
00590
00591 return pfile;
00592 }
00593
00594
00595
00596 void
00597 cpp_destroy (pfile)
00598 cpp_reader *pfile;
00599 {
00600 struct search_path *dir, *dirn;
00601 cpp_context *context, *contextn;
00602 tokenrun *run, *runn;
00603
00604 free_chain (CPP_OPTION (pfile, pending)->include_head);
00605 free (CPP_OPTION (pfile, pending));
00606 free (pfile->op_stack);
00607
00608 while (CPP_BUFFER (pfile) != NULL)
00609 _cpp_pop_buffer (pfile);
00610
00611 if (pfile->out.base)
00612 free (pfile->out.base);
00613
00614 if (pfile->macro_buffer)
00615 {
00616 free ((PTR) pfile->macro_buffer);
00617 pfile->macro_buffer = NULL;
00618 pfile->macro_buffer_len = 0;
00619 }
00620
00621 if (pfile->deps)
00622 deps_free (pfile->deps);
00623 obstack_free (&pfile->buffer_ob, 0);
00624
00625 _cpp_destroy_hashtable (pfile);
00626 _cpp_cleanup_includes (pfile);
00627
00628 _cpp_free_buff (pfile->a_buff);
00629 _cpp_free_buff (pfile->u_buff);
00630 _cpp_free_buff (pfile->free_buffs);
00631
00632 for (run = &pfile->base_run; run; run = runn)
00633 {
00634 runn = run->next;
00635 free (run->base);
00636 if (run != &pfile->base_run)
00637 free (run);
00638 }
00639
00640 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
00641 {
00642 dirn = dir->next;
00643 free ((PTR) dir->name);
00644 free (dir);
00645 }
00646
00647 for (context = pfile->base_context.next; context; context = contextn)
00648 {
00649 contextn = context->next;
00650 free (context);
00651 }
00652
00653 free_line_maps (&pfile->line_maps);
00654 free (pfile);
00655 }
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671 struct builtin
00672 {
00673 const uchar *name;
00674 unsigned short len;
00675 unsigned short value;
00676 };
00677
00678 #define B(n, t) { DSC(n), t }
00679 static const struct builtin builtin_array[] =
00680 {
00681 B("__TIME__", BT_TIME),
00682 B("__DATE__", BT_DATE),
00683 B("__FILE__", BT_FILE),
00684 B("__BASE_FILE__", BT_BASE_FILE),
00685 B("__LINE__", BT_SPECLINE),
00686 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
00687
00688
00689 B("_Pragma", BT_PRAGMA),
00690 B("__STDC__", BT_STDC),
00691 };
00692
00693 static const struct builtin operator_array[] =
00694 {
00695 B("and", CPP_AND_AND),
00696 B("and_eq", CPP_AND_EQ),
00697 B("bitand", CPP_AND),
00698 B("bitor", CPP_OR),
00699 B("compl", CPP_COMPL),
00700 B("not", CPP_NOT),
00701 B("not_eq", CPP_NOT_EQ),
00702 B("or", CPP_OR_OR),
00703 B("or_eq", CPP_OR_EQ),
00704 B("xor", CPP_XOR),
00705 B("xor_eq", CPP_XOR_EQ)
00706 };
00707 #undef B
00708
00709
00710 static void
00711 mark_named_operators (pfile)
00712 cpp_reader *pfile;
00713 {
00714 const struct builtin *b;
00715
00716 for (b = operator_array;
00717 b < (operator_array + ARRAY_SIZE (operator_array));
00718 b++)
00719 {
00720 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
00721 hp->flags |= NODE_OPERATOR;
00722 #ifdef SGI_MONGOOSE
00723 hp->value.operator_code = b->value;
00724 #else
00725 hp->value.operator = b->value;
00726 #endif
00727 }
00728 }
00729
00730
00731
00732 static void
00733 init_builtins (pfile)
00734 cpp_reader *pfile;
00735 {
00736 const struct builtin *b;
00737 size_t n = ARRAY_SIZE (builtin_array);
00738
00739 if (CPP_OPTION (pfile, traditional))
00740 n -= 2;
00741
00742 for(b = builtin_array; b < builtin_array + n; b++)
00743 {
00744 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
00745 hp->type = NT_MACRO;
00746 hp->flags |= NODE_BUILTIN | NODE_WARN;
00747 hp->value.builtin = b->value;
00748 }
00749
00750 if (CPP_OPTION (pfile, cplusplus))
00751 _cpp_define_builtin (pfile, "__cplusplus 1");
00752 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
00753 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
00754 else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
00755 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
00756 else if (CPP_OPTION (pfile, c99))
00757 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
00758
00759 if (CPP_OPTION (pfile, objc))
00760 _cpp_define_builtin (pfile, "__OBJC__ 1");
00761
00762 if (pfile->cb.register_builtins)
00763 (*pfile->cb.register_builtins) (pfile);
00764 }
00765
00766
00767 static void
00768 init_standard_includes (pfile)
00769 cpp_reader *pfile;
00770 {
00771 char *path;
00772 const struct default_include *p;
00773 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
00774
00775
00776
00777
00778
00779
00780
00781 GET_ENVIRONMENT (path, "CPATH");
00782 if (path != 0 && *path != 0)
00783 path_include (pfile, path, BRACKET);
00784
00785 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
00786 {
00787 case 0:
00788 GET_ENVIRONMENT (path, "C_INCLUDE_PATH");
00789 break;
00790 case 1:
00791 GET_ENVIRONMENT (path, "CPLUS_INCLUDE_PATH");
00792 break;
00793 case 2:
00794 GET_ENVIRONMENT (path, "OBJC_INCLUDE_PATH");
00795 break;
00796 case 3:
00797 GET_ENVIRONMENT (path, "OBJCPLUS_INCLUDE_PATH");
00798 break;
00799 }
00800 if (path != 0 && *path != 0)
00801 path_include (pfile, path, SYSTEM);
00802
00803
00804
00805 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
00806 {
00807
00808
00809 int default_len = cpp_GCC_INCLUDE_DIR_len;
00810 char *default_prefix = (char *) alloca (default_len + 1);
00811 int specd_len = strlen (specd_prefix);
00812
00813 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
00814 default_prefix[default_len] = '\0';
00815
00816 for (p = cpp_include_defaults; p->fname; p++)
00817 {
00818
00819 if (!p->cplusplus
00820 || (CPP_OPTION (pfile, cplusplus)
00821 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
00822 {
00823
00824 if (!strncmp (p->fname, default_prefix, default_len))
00825 {
00826
00827 int flen = strlen (p->fname);
00828 int this_len = specd_len + flen - default_len;
00829 char *str = (char *) xmalloc (this_len + 1);
00830 memcpy (str, specd_prefix, specd_len);
00831 memcpy (str + specd_len,
00832 p->fname + default_len,
00833 flen - default_len + 1);
00834
00835 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
00836 }
00837 }
00838 }
00839 }
00840
00841
00842 for (p = cpp_include_defaults; p->fname; p++)
00843 {
00844
00845 if (!p->cplusplus
00846 || (CPP_OPTION (pfile, cplusplus)
00847 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
00848 {
00849 char *str = update_path (p->fname, p->component);
00850 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
00851 }
00852 }
00853 }
00854
00855
00856
00857 static bool
00858 push_include (pfile, p)
00859 cpp_reader *pfile;
00860 struct pending_option *p;
00861 {
00862 cpp_token header;
00863
00864
00865
00866 header.type = CPP_STRING;
00867 header.val.str.text = (const unsigned char *) p->arg;
00868 header.val.str.len = strlen (p->arg);
00869
00870 pfile->line++;
00871
00872 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
00873 }
00874
00875
00876 static void
00877 free_chain (head)
00878 struct pending_option *head;
00879 {
00880 struct pending_option *next;
00881
00882 while (head)
00883 {
00884 next = head->next;
00885 free (head);
00886 head = next;
00887 }
00888 }
00889
00890
00891
00892 #if ENABLE_CHECKING
00893 static void sanity_checks PARAMS ((cpp_reader *));
00894 static void sanity_checks (pfile)
00895 cpp_reader *pfile;
00896 {
00897 cppchar_t test = 0;
00898 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
00899
00900
00901
00902 test--;
00903 if (test < 1)
00904 cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type");
00905
00906 if (CPP_OPTION (pfile, precision) > max_precision)
00907 cpp_error (pfile, DL_ICE,
00908 "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
00909 (unsigned long) max_precision,
00910 (unsigned long) CPP_OPTION (pfile, precision));
00911
00912 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
00913 cpp_error (pfile, DL_ICE,
00914 "CPP arithmetic must be at least as precise as a target int");
00915
00916 if (CPP_OPTION (pfile, char_precision) < 8)
00917 cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide");
00918
00919 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
00920 cpp_error (pfile, DL_ICE,
00921 "target wchar_t is narrower than target char");
00922
00923 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
00924 cpp_error (pfile, DL_ICE,
00925 "target int is narrower than target char");
00926
00927
00928 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
00929 cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character");
00930
00931 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
00932 cpp_error (pfile, DL_ICE,
00933 "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
00934 (unsigned long) BITS_PER_CPPCHAR_T,
00935 (unsigned long) CPP_OPTION (pfile, wchar_precision));
00936 }
00937 #else
00938 # define sanity_checks(PFILE)
00939 #endif
00940
00941
00942
00943
00944 void
00945 cpp_add_dependency_target (pfile, target, quote)
00946 cpp_reader *pfile;
00947 const char *target;
00948 int quote;
00949 {
00950 if (!pfile->deps)
00951 pfile->deps = deps_init ();
00952
00953 deps_add_target (pfile->deps, target, quote);
00954 }
00955
00956
00957
00958
00959
00960 const char *
00961 cpp_read_main_file (pfile, fname, table)
00962 cpp_reader *pfile;
00963 const char *fname;
00964 hash_table *table;
00965 {
00966 sanity_checks (pfile);
00967
00968 post_options (pfile);
00969
00970
00971
00972
00973 _cpp_init_hashtable (pfile, table);
00974
00975
00976 if (! CPP_OPTION (pfile, no_standard_includes))
00977 init_standard_includes (pfile);
00978
00979 merge_include_chains (pfile);
00980
00981
00982 if (CPP_OPTION (pfile, verbose))
00983 {
00984 struct search_path *l;
00985 fprintf (stderr, _("#include \"...\" search starts here:\n"));
00986 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
00987 {
00988 if (l == CPP_OPTION (pfile, bracket_include))
00989 fprintf (stderr, _("#include <...> search starts here:\n"));
00990 fprintf (stderr, " %s\n", l->name);
00991 }
00992 fprintf (stderr, _("End of search list.\n"));
00993 }
00994
00995 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
00996 {
00997 if (!pfile->deps)
00998 pfile->deps = deps_init ();
00999
01000
01001 deps_add_default_target (pfile->deps, fname);
01002 }
01003
01004
01005 if (!_cpp_read_file (pfile, fname))
01006 return NULL;
01007
01008
01009
01010
01011 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
01012
01013
01014
01015 if (CPP_OPTION (pfile, preprocessed))
01016 read_original_filename (pfile);
01017
01018 return pfile->map->to_file;
01019 }
01020
01021
01022
01023
01024
01025 static void
01026 read_original_filename (pfile)
01027 cpp_reader *pfile;
01028 {
01029 const cpp_token *token, *token1;
01030
01031
01032
01033 token = _cpp_lex_direct (pfile);
01034 if (token->type == CPP_HASH)
01035 {
01036 token1 = _cpp_lex_direct (pfile);
01037 _cpp_backup_tokens (pfile, 1);
01038
01039
01040 if (token1->type == CPP_NUMBER)
01041 {
01042 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
01043 return;
01044 }
01045 }
01046
01047
01048 _cpp_backup_tokens (pfile, 1);
01049 }
01050
01051
01052
01053
01054 void
01055 cpp_finish_options (pfile)
01056 cpp_reader *pfile;
01057 {
01058
01059 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
01060 mark_named_operators (pfile);
01061
01062
01063
01064 if (! CPP_OPTION (pfile, preprocessed))
01065 {
01066 struct pending_option *p;
01067
01068
01069 pfile->first_unused_line = (unsigned int) -1;
01070 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
01071 init_builtins (pfile);
01072 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
01073 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
01074 (*p->handler) (pfile, p->arg);
01075
01076
01077
01078
01079 for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
01080 if (push_include (pfile, p))
01081 cpp_scan_nooutput (pfile);
01082
01083 pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
01084 _cpp_maybe_push_include_file (pfile);
01085 }
01086
01087 pfile->first_unused_line = pfile->line;
01088
01089 free_chain (CPP_OPTION (pfile, pending)->imacros_head);
01090 free_chain (CPP_OPTION (pfile, pending)->directive_head);
01091 }
01092
01093
01094 void
01095 _cpp_maybe_push_include_file (pfile)
01096 cpp_reader *pfile;
01097 {
01098 if (pfile->next_include_file)
01099 {
01100 struct pending_option *head = *pfile->next_include_file;
01101
01102 while (head && !push_include (pfile, head))
01103 head = head->next;
01104
01105 if (head)
01106 pfile->next_include_file = &head->next;
01107 else
01108 {
01109
01110 _cpp_do_file_change (pfile, LC_RENAME,
01111 pfile->line_maps.maps[0].to_file, 1, 0);
01112
01113 pfile->next_include_file = NULL;
01114 }
01115 }
01116 }
01117
01118
01119
01120
01121
01122
01123
01124 int
01125 cpp_finish (pfile, deps_stream)
01126 cpp_reader *pfile;
01127 FILE *deps_stream;
01128 {
01129
01130 if (CPP_OPTION (pfile, warn_unused_macros))
01131 cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
01132
01133
01134
01135
01136
01137
01138 while (pfile->buffer)
01139 _cpp_pop_buffer (pfile);
01140
01141
01142 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
01143 && deps_stream && pfile->errors == 0)
01144 {
01145 deps_write (pfile->deps, deps_stream, 72);
01146
01147 if (CPP_OPTION (pfile, deps.phony_targets))
01148 deps_phony_targets (pfile->deps, deps_stream);
01149 }
01150
01151
01152 if (CPP_OPTION (pfile, print_include_names))
01153 _cpp_report_missing_guards (pfile);
01154
01155 return pfile->errors;
01156 }
01157
01158
01159 static void
01160 new_pending_directive (pend, text, handler)
01161 struct cpp_pending *pend;
01162 const char *text;
01163 cl_directive_handler handler;
01164 {
01165 struct pending_option *o = (struct pending_option *)
01166 xmalloc (sizeof (struct pending_option));
01167
01168 o->arg = text;
01169 o->next = NULL;
01170 o->handler = handler;
01171 APPEND (pend, directive, o);
01172 }
01173
01174
01175
01176
01177 #define no_ass N_("assertion missing after %s")
01178 #define no_dir N_("directory name missing after %s")
01179 #define no_fil N_("file name missing after %s")
01180 #define no_mac N_("macro name missing after %s")
01181 #define no_pth N_("path name missing after %s")
01182
01183
01184
01185 #define COMMAND_LINE_OPTIONS \
01186 DEF_OPT("A", no_ass, OPT_A) \
01187 DEF_OPT("D", no_mac, OPT_D) \
01188 DEF_OPT("I", no_dir, OPT_I) \
01189 DEF_OPT("U", no_mac, OPT_U) \
01190 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
01191 DEF_OPT("imacros", no_fil, OPT_imacros) \
01192 DEF_OPT("include", no_fil, OPT_include) \
01193 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
01194 DEF_OPT("isystem", no_dir, OPT_isystem) \
01195 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
01196 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore)
01197
01198 #define DEF_OPT(text, msg, code) code,
01199 enum opt_code
01200 {
01201 COMMAND_LINE_OPTIONS
01202 N_OPTS
01203 };
01204 #undef DEF_OPT
01205
01206 struct cl_option
01207 {
01208 const char *opt_text;
01209 const char *msg;
01210 size_t opt_len;
01211 enum opt_code opt_code;
01212 };
01213
01214 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
01215 #ifdef HOST_EBCDIC
01216 static struct cl_option cl_options[] =
01217 #else
01218 static const struct cl_option cl_options[] =
01219 #endif
01220 {
01221 COMMAND_LINE_OPTIONS
01222 };
01223 #undef DEF_OPT
01224 #undef COMMAND_LINE_OPTIONS
01225
01226
01227
01228
01229
01230
01231 static int
01232 parse_option (input)
01233 const char *input;
01234 {
01235 unsigned int md, mn, mx;
01236 size_t opt_len;
01237 int comp;
01238
01239 mn = 0;
01240 mx = N_OPTS;
01241
01242 while (mx > mn)
01243 {
01244 md = (mn + mx) / 2;
01245
01246 opt_len = cl_options[md].opt_len;
01247 comp = strncmp (input, cl_options[md].opt_text, opt_len);
01248
01249 if (comp > 0)
01250 mn = md + 1;
01251 else if (comp < 0)
01252 mx = md;
01253 else
01254 {
01255 if (input[opt_len] == '\0')
01256 return md;
01257
01258
01259
01260
01261
01262 mn = md + 1;
01263 if (cl_options[md].msg)
01264 {
01265
01266
01267
01268 mx = md;
01269 for (; mn < (unsigned int) N_OPTS; mn++)
01270 {
01271 opt_len = cl_options[mn].opt_len;
01272 if (strncmp (input, cl_options[mn].opt_text, opt_len))
01273 break;
01274 if (input[opt_len] == '\0')
01275 return mn;
01276 if (cl_options[mn].msg)
01277 mx = mn;
01278 }
01279 return mx;
01280 }
01281 }
01282 }
01283
01284 return -1;
01285 }
01286
01287
01288
01289
01290 int
01291 cpp_handle_option (pfile, argc, argv)
01292 cpp_reader *pfile;
01293 int argc;
01294 char **argv;
01295 {
01296 int i = 0;
01297 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
01298
01299 {
01300 enum opt_code opt_code;
01301 int opt_index;
01302 const char *arg = 0;
01303
01304
01305 opt_index = parse_option (&argv[i][1]);
01306 if (opt_index < 0)
01307 return i;
01308
01309 opt_code = cl_options[opt_index].opt_code;
01310 if (cl_options[opt_index].msg)
01311 {
01312 arg = &argv[i][cl_options[opt_index].opt_len + 1];
01313 if (arg[0] == '\0')
01314 {
01315 arg = argv[++i];
01316 if (!arg)
01317 {
01318 cpp_error (pfile, DL_ERROR,
01319 cl_options[opt_index].msg, argv[i - 1]);
01320 return argc;
01321 }
01322 }
01323 }
01324
01325 switch (opt_code)
01326 {
01327 case N_OPTS:
01328 break;
01329
01330 case OPT_D:
01331 new_pending_directive (pend, arg, cpp_define);
01332 break;
01333 case OPT_iprefix:
01334 CPP_OPTION (pfile, include_prefix) = arg;
01335 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
01336 break;
01337
01338 case OPT_A:
01339 if (arg[0] == '-')
01340 new_pending_directive (pend, arg + 1, cpp_unassert);
01341 else
01342 new_pending_directive (pend, arg, cpp_assert);
01343 break;
01344 case OPT_U:
01345 new_pending_directive (pend, arg, cpp_undef);
01346 break;
01347 case OPT_I:
01348 if (!strcmp (arg, "-"))
01349 {
01350
01351
01352
01353
01354
01355
01356 if (! CPP_OPTION (pfile, ignore_srcdir))
01357 {
01358 pend->quote_head = pend->brack_head;
01359 pend->quote_tail = pend->brack_tail;
01360 pend->brack_head = 0;
01361 pend->brack_tail = 0;
01362 CPP_OPTION (pfile, ignore_srcdir) = 1;
01363 }
01364 else
01365 {
01366 cpp_error (pfile, DL_ERROR, "-I- specified twice");
01367 return argc;
01368 }
01369 }
01370 else
01371 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
01372 break;
01373 case OPT_isystem:
01374
01375
01376 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
01377 break;
01378 case OPT_include:
01379 case OPT_imacros:
01380 {
01381 struct pending_option *o = (struct pending_option *)
01382 xmalloc (sizeof (struct pending_option));
01383 o->arg = arg;
01384 o->next = NULL;
01385
01386 if (opt_code == OPT_include)
01387 APPEND (pend, include, o);
01388 else
01389 APPEND (pend, imacros, o);
01390 }
01391 break;
01392 case OPT_iwithprefix:
01393
01394
01395
01396 case OPT_iwithprefixbefore:
01397
01398
01399 {
01400 char *fname;
01401 int len;
01402
01403 len = strlen (arg);
01404
01405 if (CPP_OPTION (pfile, include_prefix) != 0)
01406 {
01407 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
01408 fname = xmalloc (ipl + len + 1);
01409 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
01410 memcpy (fname + ipl, arg, len + 1);
01411 }
01412 else if (cpp_GCC_INCLUDE_DIR_len)
01413 {
01414 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
01415 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
01416 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
01417 }
01418 else
01419 fname = xstrdup (arg);
01420
01421 append_include_chain (pfile, fname,
01422 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
01423 }
01424 break;
01425 case OPT_idirafter:
01426
01427 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
01428 break;
01429 }
01430 }
01431 return i + 1;
01432 }
01433
01434
01435
01436
01437
01438 int
01439 cpp_handle_options (pfile, argc, argv)
01440 cpp_reader *pfile;
01441 int argc;
01442 char **argv;
01443 {
01444 int i;
01445 int strings_processed;
01446
01447 for (i = 0; i < argc; i += strings_processed)
01448 {
01449 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
01450 if (strings_processed == 0)
01451 break;
01452 }
01453
01454 return i;
01455 }
01456
01457 static void
01458 post_options (pfile)
01459 cpp_reader *pfile;
01460 {
01461
01462 if (CPP_OPTION (pfile, cplusplus))
01463 CPP_OPTION (pfile, warn_traditional) = 0;
01464
01465
01466
01467 if (CPP_OPTION (pfile, preprocessed))
01468 {
01469 pfile->state.prevent_expansion = 1;
01470 CPP_OPTION (pfile, traditional) = 0;
01471 }
01472
01473
01474 if (CPP_OPTION (pfile, traditional))
01475 CPP_OPTION (pfile, show_column) = 0;
01476 }