comparison main.c @ 7:b8167949474a

make places work better
author David A. Holland
date Sun, 19 Dec 2010 19:08:24 -0500
parents 0601b6e8e53d
children 97243badae69
comparison
equal deleted inserted replaced
6:0601b6e8e53d 7:b8167949474a
53 53
54 //////////////////////////////////////////////////////////// 54 ////////////////////////////////////////////////////////////
55 // commandline macros 55 // commandline macros
56 56
57 struct commandline_macro { 57 struct commandline_macro {
58 unsigned column; 58 struct place *place;
59 const char *macro; 59 const char *macro;
60 const char *expansion; 60 const char *expansion;
61 }; 61 };
62 62
63 static struct array commandline_macros; 63 static struct array commandline_macros;
76 array_cleanup(&commandline_macros); 76 array_cleanup(&commandline_macros);
77 } 77 }
78 78
79 static 79 static
80 void 80 void
81 commandline_macro_add(unsigned column, 81 commandline_macro_add(const struct place *p,
82 const char *macro, const char *expansion) 82 const char *macro, const char *expansion)
83 { 83 {
84 struct commandline_macro *cm; 84 struct commandline_macro *cm;
85 85
86 cm = domalloc(sizeof(*cm)); 86 cm = domalloc(sizeof(*cm));
87 cm->column = column; 87 cm->place = place_clone(p);
88 cm->macro = macro; 88 cm->macro = macro;
89 cm->expansion = expansion; 89 cm->expansion = expansion;
90 } 90 }
91 91
92 static 92 static
93 void 93 void
94 commandline_def(unsigned column, char *str) 94 commandline_def(const struct place *p, char *str)
95 { 95 {
96 char *val; 96 char *val;
97 97
98 val = strchr(str, '='); 98 val = strchr(str, '=');
99 if (val != NULL) { 99 if (val != NULL) {
100 *val = '\0'; 100 *val = '\0';
101 val++; 101 val++;
102 } 102 }
103 commandline_macro_add(column, str, val ? val : "1"); 103 commandline_macro_add(p, str, val ? val : "1");
104 } 104 }
105 105
106 static 106 static
107 void 107 void
108 commandline_undef(unsigned column, char *str) 108 commandline_undef(const struct place *p, char *str)
109 { 109 {
110 commandline_macro_add(column, str, NULL); 110 commandline_macro_add(p, str, NULL);
111 } 111 }
112 112
113 static 113 static
114 void 114 void
115 apply_commandline_macros(void) 115 apply_commandline_macros(void)
116 { 116 {
117 struct commandline_macro *cm; 117 struct commandline_macro *cm;
118 struct place *p;
119 unsigned i, num; 118 unsigned i, num;
120 119
121 p = place_gettemporary();
122 num = array_num(&commandline_macros); 120 num = array_num(&commandline_macros);
123 for (i=0; i<num; i++) { 121 for (i=0; i<num; i++) {
124 cm = array_get(&commandline_macros, i); 122 cm = array_get(&commandline_macros, i);
125 if (cm->expansion != NULL) { 123 if (cm->expansion != NULL) {
126 place_setcommandline(p, cm->column); 124 macro_define(cm->place, cm->macro, cm->expansion);
127 macro_define(p, cm->macro, cm->expansion);
128 } else { 125 } else {
129 macro_undef(cm->macro); 126 macro_undef(cm->macro);
130 } 127 }
128 place_destroy(cm->place);
131 free(cm); 129 free(cm);
132 } 130 }
133 array_setsize(&commandline_macros, 0); 131 array_setsize(&commandline_macros, 0);
132 }
133
134 static
135 void
136 apply_builtin_macro(unsigned num, const char *name, const char *val)
137 {
138 struct place *p;
139
140 p = place_gettemporary();
141 place_setbuiltin(p, num);
142 macro_define(p, name, val);
134 place_puttemporary(p); 143 place_puttemporary(p);
135 } 144 }
136 145
137 static 146 static
138 void 147 void
139 apply_builtin_macro(const char *name, const char *val)
140 {
141 /* XXX distinguish builtin-place and commandline-place and nowhere */
142 macro_define(NULL, name, val);
143 }
144
145 static
146 void
147 apply_builtin_macros(void) 148 apply_builtin_macros(void)
148 { 149 {
150 unsigned n = 1;
151
149 #ifdef CONFIG_OS 152 #ifdef CONFIG_OS
150 apply_builtin_macro(CONFIG_OS, "1"); 153 apply_builtin_macro(n++, CONFIG_OS, "1");
151 #endif 154 #endif
152 #ifdef CONFIG_OS_2 155 #ifdef CONFIG_OS_2
153 apply_builtin_macro(CONFIG_OS_2, "1"); 156 apply_builtin_macro(n++, CONFIG_OS_2, "1");
154 #endif 157 #endif
155 158
156 #ifdef CONFIG_CPU 159 #ifdef CONFIG_CPU
157 apply_builtin_macro(CONFIG_CPU, "1"); 160 apply_builtin_macro(n++, CONFIG_CPU, "1");
158 #endif 161 #endif
159 #ifdef CONFIG_CPU_2 162 #ifdef CONFIG_CPU_2
160 apply_builtin_macro(CONFIG_CPU_2, "1"); 163 apply_builtin_macro(n++, CONFIG_CPU_2, "1");
161 #endif 164 #endif
162 165
163 #ifdef CONFIG_SIZE 166 #ifdef CONFIG_SIZE
164 apply_builtin_macro(CONFIG_SIZE, "1"); 167 apply_builtin_macro(n++, CONFIG_SIZE, "1");
165 #endif 168 #endif
166 #ifdef CONFIG_BINFMT 169 #ifdef CONFIG_BINFMT
167 apply_builtin_macro(CONFIG_BINFMT, "1"); 170 apply_builtin_macro(n++, CONFIG_BINFMT, "1");
168 #endif 171 #endif
169 172
170 #ifdef CONFIG_COMPILER 173 #ifdef CONFIG_COMPILER
171 apply_builtin_macro(CONFIG_COMPILER, VERSION_MAJOR); 174 apply_builtin_macro(n++, CONFIG_COMPILER, VERSION_MAJOR);
172 apply_builtin_macro(CONFIG_COMPILER_MINOR, VERSION_MINOR); 175 apply_builtin_macro(n++, CONFIG_COMPILER_MINOR, VERSION_MINOR);
173 apply_builtin_macro("__VERSION__", VERSION_LONG); 176 apply_builtin_macro(n++, "__VERSION__", VERSION_LONG);
174 #endif 177 #endif
175 } 178 }
176 179
177 //////////////////////////////////////////////////////////// 180 ////////////////////////////////////////////////////////////
178 // extra included files 181 // extra included files
179 182
180 struct commandline_file { 183 struct commandline_file {
184 struct place *place;
181 char *name; 185 char *name;
182 bool suppress_output; 186 bool suppress_output;
183 unsigned column;
184 }; 187 };
185 188
186 static struct array commandline_files; 189 static struct array commandline_files;
187 190
188 static 191 static
199 array_cleanup(&commandline_files); 202 array_cleanup(&commandline_files);
200 } 203 }
201 204
202 static 205 static
203 void 206 void
204 commandline_addfile(char *name, bool suppress_output, unsigned column) 207 commandline_addfile(const struct place *p, char *name, bool suppress_output)
205 { 208 {
206 struct commandline_file *cf; 209 struct commandline_file *cf;
207 210
208 cf = domalloc(sizeof(*cf)); 211 cf = domalloc(sizeof(*cf));
212 cf->place = place_clone(p);
209 cf->name = name; 213 cf->name = name;
210 cf->suppress_output = suppress_output; 214 cf->suppress_output = suppress_output;
211 cf->column = column;
212 array_add(&commandline_files, cf, NULL); 215 array_add(&commandline_files, cf, NULL);
213 } 216 }
214 217
215 static 218 static
216 void 219 void
217 commandline_addfile_output(unsigned column, char *name) 220 commandline_addfile_output(const struct place *p, char *name)
218 { 221 {
219 commandline_addfile(name, false, column); 222 commandline_addfile(p, name, false);
220 } 223 }
221 224
222 static 225 static
223 void 226 void
224 commandline_addfile_nooutput(unsigned column, char *name) 227 commandline_addfile_nooutput(const struct place *p, char *name)
225 { 228 {
226 commandline_addfile(name, true, column); 229 commandline_addfile(p, name, true);
227 } 230 }
228 231
229 static 232 static
230 void 233 void
231 read_commandline_files(void) 234 read_commandline_files(void)
232 { 235 {
233 struct commandline_file *cf; 236 struct commandline_file *cf;
234 unsigned i, num; 237 unsigned i, num;
235 bool save = false; 238 bool save = false;
236 struct place *p; 239
237
238 p = place_gettemporary();
239 num = array_num(&commandline_files); 240 num = array_num(&commandline_files);
240 for (i=0; i<num; i++) { 241 for (i=0; i<num; i++) {
241 cf = array_get(&commandline_files, i); 242 cf = array_get(&commandline_files, i);
242 place_setcommandline(p, cf->column);
243 if (cf->suppress_output) { 243 if (cf->suppress_output) {
244 save = mode.do_output; 244 save = mode.do_output;
245 mode.do_output = false; 245 mode.do_output = false;
246 file_readquote(p, cf->name); 246 file_readquote(cf->place, cf->name);
247 mode.do_output = save; 247 mode.do_output = save;
248 } else { 248 } else {
249 file_readquote(p, cf->name); 249 file_readquote(cf->place, cf->name);
250 } 250 }
251 place_destroy(cf->place);
251 free(cf); 252 free(cf);
252 } 253 }
253 place_puttemporary(p);
254 array_setsize(&commandline_files, 0); 254 array_setsize(&commandline_files, 0);
255 } 255 }
256 256
257 //////////////////////////////////////////////////////////// 257 ////////////////////////////////////////////////////////////
258 // include path accumulation 258 // include path accumulation
283 stringarray_cleanup(&incpath_late); 283 stringarray_cleanup(&incpath_late);
284 } 284 }
285 285
286 static 286 static
287 void 287 void
288 commandline_isysroot(unsigned column, char *dir) 288 commandline_isysroot(const struct place *p, char *dir)
289 { 289 {
290 (void)column; 290 (void)p;
291 sysroot = dir; 291 sysroot = dir;
292 } 292 }
293 293
294 static 294 static
295 void 295 void
298 stringarray_add(arr, s, NULL); 298 stringarray_add(arr, s, NULL);
299 } 299 }
300 300
301 static 301 static
302 void 302 void
303 commandline_addincpath_quote(unsigned column, char *dir) 303 commandline_addincpath_quote(const struct place *p, char *dir)
304 { 304 {
305 (void)column; 305 (void)p;
306 commandline_addincpath(&incpath_quote, dir); 306 commandline_addincpath(&incpath_quote, dir);
307 } 307 }
308 308
309 static 309 static
310 void 310 void
311 commandline_addincpath_user(unsigned column, char *dir) 311 commandline_addincpath_user(const struct place *p, char *dir)
312 { 312 {
313 (void)column; 313 (void)p;
314 commandline_addincpath(&incpath_user, dir); 314 commandline_addincpath(&incpath_user, dir);
315 } 315 }
316 316
317 static 317 static
318 void 318 void
319 commandline_addincpath_system(unsigned column, char *dir) 319 commandline_addincpath_system(const struct place *p, char *dir)
320 { 320 {
321 (void)column; 321 (void)p;
322 commandline_addincpath(&incpath_system, dir); 322 commandline_addincpath(&incpath_system, dir);
323 } 323 }
324 324
325 static 325 static
326 void 326 void
327 commandline_addincpath_late(unsigned column, char *dir) 327 commandline_addincpath_late(const struct place *p, char *dir)
328 { 328 {
329 (void)column; 329 (void)p;
330 commandline_addincpath(&incpath_late, dir); 330 commandline_addincpath(&incpath_late, dir);
331 } 331 }
332 332
333 static 333 static
334 void 334 void
394 394
395 static const char *commandline_prefix; 395 static const char *commandline_prefix;
396 396
397 static 397 static
398 void 398 void
399 commandline_setprefix(unsigned column, char *prefix) 399 commandline_setprefix(const struct place *p, char *prefix)
400 { 400 {
401 (void)column; 401 (void)p;
402 commandline_prefix = prefix; 402 commandline_prefix = prefix;
403 } 403 }
404 404
405 static 405 static
406 void 406 void
407 commandline_addincpath_user_withprefix(unsigned column, char *dir) 407 commandline_addincpath_user_withprefix(const struct place *p, char *dir)
408 { 408 {
409 char *s; 409 char *s;
410 410
411 if (commandline_prefix == NULL) { 411 if (commandline_prefix == NULL) {
412 warnx("-iprefix needed"); 412 warnx("-iprefix needed");
413 die(); 413 die();
414 } 414 }
415 s = dostrdup3(commandline_prefix, "/", dir); 415 s = dostrdup3(commandline_prefix, "/", dir);
416 freestringlater(s); 416 freestringlater(s);
417 commandline_addincpath_user(column, s); 417 commandline_addincpath_user(p, s);
418 } 418 }
419 419
420 static 420 static
421 void 421 void
422 commandline_addincpath_late_withprefix(unsigned column, char *dir) 422 commandline_addincpath_late_withprefix(const struct place *p, char *dir)
423 { 423 {
424 char *s; 424 char *s;
425 425
426 if (commandline_prefix == NULL) { 426 if (commandline_prefix == NULL) {
427 warnx("-iprefix needed"); 427 warnx("-iprefix needed");
428 die(); 428 die();
429 } 429 }
430 s = dostrdup3(commandline_prefix, "/", dir); 430 s = dostrdup3(commandline_prefix, "/", dir);
431 freestringlater(s); 431 freestringlater(s);
432 commandline_addincpath_late(column, s); 432 commandline_addincpath_late(p, s);
433 } 433 }
434 434
435 static 435 static
436 void 436 void
437 commandline_setstd(unsigned column, char *std) 437 commandline_setstd(const struct place *p, char *std)
438 { 438 {
439 (void)column; 439 (void)p;
440 440
441 if (!strcmp(std, "krc")) { 441 if (!strcmp(std, "krc")) {
442 return; 442 return;
443 } 443 }
444 warnx("Standard %s not supported by this preprocessor", std); 444 warnx("Standard %s not supported by this preprocessor", std);
445 die(); 445 die();
446 } 446 }
447 447
448 static 448 static
449 void 449 void
450 commandline_setlang(unsigned column, char *lang) 450 commandline_setlang(const struct place *p, char *lang)
451 { 451 {
452 (void)column; 452 (void)p;
453 453
454 if (!strcmp(lang, "c") || !strcmp(lang, "assembler-with-cpp")) { 454 if (!strcmp(lang, "c") || !strcmp(lang, "assembler-with-cpp")) {
455 return; 455 return;
456 } 456 }
457 warnx("Language %s not supported by this preprocessor", lang); 457 warnx("Language %s not supported by this preprocessor", lang);
461 //////////////////////////////////////////////////////////// 461 ////////////////////////////////////////////////////////////
462 // complex modes 462 // complex modes
463 463
464 static 464 static
465 void 465 void
466 commandline_iremap(unsigned column, char *str) 466 commandline_iremap(const struct place *p, char *str)
467 { 467 {
468 (void)column; 468 (void)p;
469 /* XXX */ 469 /* XXX */
470 (void)str; 470 (void)str;
471 warnx("-iremap not supported"); 471 warnx("-iremap not supported");
472 die(); 472 die();
473 } 473 }
474 474
475 static 475 static
476 void 476 void
477 commandline_tabstop(unsigned column, char *s) 477 commandline_tabstop(const struct place *p, char *s)
478 { 478 {
479 char *t; 479 char *t;
480 unsigned long val; 480 unsigned long val;
481 481
482 (void)column; 482 (void)p;
483 483
484 t = strchr(s, '='); 484 t = strchr(s, '=');
485 if (t == NULL) { 485 if (t == NULL) {
486 /* should not happen */ 486 /* should not happen */
487 warnx("Invalid tabstop"); 487 warnx("Invalid tabstop");
559 * depend 559 * depend
560 */ 560 */
561 561
562 static 562 static
563 void 563 void
564 commandline_setdependtarget(unsigned column, char *str) 564 commandline_setdependtarget(const struct place *p, char *str)
565 { 565 {
566 (void)column; 566 (void)p;
567 mode.depend_target = str; 567 mode.depend_target = str;
568 mode.depend_quote_target = false; 568 mode.depend_quote_target = false;
569 } 569 }
570 570
571 static 571 static
572 void 572 void
573 commandline_setdependtarget_quoted(unsigned column, char *str) 573 commandline_setdependtarget_quoted(const struct place *p, char *str)
574 { 574 {
575 (void)column; 575 (void)p;
576 mode.depend_target = str; 576 mode.depend_target = str;
577 mode.depend_quote_target = true; 577 mode.depend_quote_target = true;
578 } 578 }
579 579
580 static 580 static
581 void 581 void
582 commandline_setdependoutput(unsigned column, char *str) 582 commandline_setdependoutput(const struct place *p, char *str)
583 { 583 {
584 (void)column; 584 (void)p;
585 mode.depend_file = str; 585 mode.depend_file = str;
586 } 586 }
587 587
588 static 588 static
589 void 589 void
661 void (*func)(void); 661 void (*func)(void);
662 }; 662 };
663 663
664 struct prefix_option { 664 struct prefix_option {
665 const char *string; 665 const char *string;
666 void (*func)(unsigned column, char *); 666 void (*func)(const struct place *, char *);
667 }; 667 };
668 668
669 struct arg_option { 669 struct arg_option {
670 const char *string; 670 const char *string;
671 void (*func)(unsigned column, char *); 671 void (*func)(const struct place *, char *);
672 }; 672 };
673 673
674 static const struct flag_option flag_options[] = { 674 static const struct flag_option flag_options[] = {
675 { "C", &mode.output_retain_comments, true }, 675 { "C", &mode.output_retain_comments, true },
676 { "CC", &mode.output_retain_comments, true }, 676 { "CC", &mode.output_retain_comments, true },
777 return false; 777 return false;
778 } 778 }
779 779
780 static 780 static
781 bool 781 bool
782 check_prefix_option(unsigned column, char *opt) 782 check_prefix_option(const struct place *p, char *opt)
783 { 783 {
784 unsigned i; 784 unsigned i;
785 int r; 785 int r;
786 786
787 for (i=0; i<num_prefix_options; i++) { 787 for (i=0; i<num_prefix_options; i++) {
788 r = strncmp(opt, prefix_options[i].string, 788 r = strncmp(opt, prefix_options[i].string,
789 strlen(prefix_options[i].string)); 789 strlen(prefix_options[i].string));
790 if (r == 0) { 790 if (r == 0) {
791 prefix_options[i].func(column, opt); 791 prefix_options[i].func(p, opt);
792 return true; 792 return true;
793 } 793 }
794 if (r < 0) { 794 if (r < 0) {
795 break; 795 break;
796 } 796 }
798 return false; 798 return false;
799 } 799 }
800 800
801 static 801 static
802 bool 802 bool
803 check_arg_option(const char *opt, unsigned argcolumn, char *arg) 803 check_arg_option(const char *opt, const struct place *argplace, char *arg)
804 { 804 {
805 unsigned i; 805 unsigned i;
806 int r; 806 int r;
807 807
808 for (i=0; i<num_arg_options; i++) { 808 for (i=0; i<num_arg_options; i++) {
810 if (r == 0) { 810 if (r == 0) {
811 if (arg == NULL) { 811 if (arg == NULL) {
812 warnx("Option -%s requires an argument", opt); 812 warnx("Option -%s requires an argument", opt);
813 die(); 813 die();
814 } 814 }
815 arg_options[i].func(argcolumn, arg); 815 arg_options[i].func(argplace, arg);
816 return true; 816 return true;
817 } 817 }
818 if (r < 0) { 818 if (r < 0) {
819 break; 819 break;
820 } 820 }
902 int 902 int
903 main(int argc, char *argv[]) 903 main(int argc, char *argv[])
904 { 904 {
905 const char *inputfile = NULL; 905 const char *inputfile = NULL;
906 const char *outputfile = NULL; 906 const char *outputfile = NULL;
907 unsigned column, nextcolumn;
908 struct place *p; 907 struct place *p;
909 int i; 908 int i;
910 909
911 init(); 910 init();
912 911 p = place_gettemporary();
913 column = 1; 912
914 for (i=1; i<argc; i++, column = nextcolumn) { 913 for (i=1; i<argc; i++) {
915 if (argv[i][0] != '-') { 914 if (argv[i][0] != '-') {
916 break; 915 break;
917 } 916 }
918 nextcolumn = column + strlen(argv[i]) + 1; 917 place_setcommandline(p, i);
919 if (check_flag_option(argv[i]+1)) { 918 if (check_flag_option(argv[i]+1)) {
920 continue; 919 continue;
921 } 920 }
922 if (check_act_option(argv[i]+1)) { 921 if (check_act_option(argv[i]+1)) {
923 continue; 922 continue;
924 } 923 }
925 if (check_prefix_option(column, argv[i]+1)) { 924 if (check_prefix_option(p, argv[i]+1)) {
926 continue; 925 continue;
927 } 926 }
928 if (check_arg_option(argv[i]+1, nextcolumn, argv[i+1])) { 927 place_setcommandline(p, i+1);
929 column = nextcolumn; 928 if (check_arg_option(argv[i]+1, p, argv[i+1])) {
930 i++; 929 i++;
931 nextcolumn = strlen(argv[i]) + 1;
932 continue; 930 continue;
933 } 931 }
934 usage(); 932 usage();
935 } 933 }
936 if (i < argc) { 934 if (i < argc) {
947 945
948 loadincludepath(); 946 loadincludepath();
949 apply_builtin_macros(); 947 apply_builtin_macros();
950 apply_commandline_macros(); 948 apply_commandline_macros();
951 read_commandline_files(); 949 read_commandline_files();
952 p = place_gettemporary();
953 place_setnowhere(p); 950 place_setnowhere(p);
954 file_readabsolute(p, inputfile); 951 file_readabsolute(p, inputfile);
952
955 place_puttemporary(p); 953 place_puttemporary(p);
956
957 cleanup(); 954 cleanup();
958 if (complain_failed()) { 955 if (complain_failed()) {
959 return EXIT_FAILURE; 956 return EXIT_FAILURE;
960 } 957 }
961 return EXIT_SUCCESS; 958 return EXIT_SUCCESS;