Mercurial > ~dholland > hg > tradcpp > index.cgi
comparison main.c @ 6:0601b6e8e53d
checkpoint - can find files
author | David A. Holland |
---|---|
date | Sun, 19 Dec 2010 18:55:51 -0500 |
parents | 7c489c73d62b |
children | b8167949474a |
comparison
equal
deleted
inserted
replaced
5:7c489c73d62b | 6:0601b6e8e53d |
---|---|
3 #include <stdlib.h> | 3 #include <stdlib.h> |
4 #include <string.h> | 4 #include <string.h> |
5 #include <errno.h> | 5 #include <errno.h> |
6 #include <err.h> | 6 #include <err.h> |
7 | 7 |
8 #include "inlinedefs.h" // XXX | |
9 #include "version.h" | 8 #include "version.h" |
10 #include "config.h" | 9 #include "config.h" |
11 #include "utils.h" | 10 #include "utils.h" |
12 #include "array.h" | 11 #include "array.h" |
13 #include "mode.h" | 12 #include "mode.h" |
54 | 53 |
55 //////////////////////////////////////////////////////////// | 54 //////////////////////////////////////////////////////////// |
56 // commandline macros | 55 // commandline macros |
57 | 56 |
58 struct commandline_macro { | 57 struct commandline_macro { |
58 unsigned column; | |
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(const char *macro, const char *expansion) | 81 commandline_macro_add(unsigned column, |
82 const char *macro, const char *expansion) | |
82 { | 83 { |
83 struct commandline_macro *cm; | 84 struct commandline_macro *cm; |
84 | 85 |
85 cm = domalloc(sizeof(*cm)); | 86 cm = domalloc(sizeof(*cm)); |
87 cm->column = column; | |
86 cm->macro = macro; | 88 cm->macro = macro; |
87 cm->expansion = expansion; | 89 cm->expansion = expansion; |
88 } | 90 } |
89 | 91 |
90 static | 92 static |
91 void | 93 void |
92 commandline_def(char *str) | 94 commandline_def(unsigned column, char *str) |
93 { | 95 { |
94 char *val; | 96 char *val; |
95 | 97 |
96 val = strchr(str, '='); | 98 val = strchr(str, '='); |
97 if (val != NULL) { | 99 if (val != NULL) { |
98 *val = '\0'; | 100 *val = '\0'; |
99 val++; | 101 val++; |
100 } | 102 } |
101 commandline_macro_add(str, val ? val : "1"); | 103 commandline_macro_add(column, str, val ? val : "1"); |
102 } | 104 } |
103 | 105 |
104 static | 106 static |
105 void | 107 void |
106 commandline_undef(char *str) | 108 commandline_undef(unsigned column, char *str) |
107 { | 109 { |
108 commandline_macro_add(str, NULL); | 110 commandline_macro_add(column, str, NULL); |
109 } | 111 } |
110 | 112 |
111 static | 113 static |
112 void | 114 void |
113 apply_commandline_macros(void) | 115 apply_commandline_macros(void) |
114 { | 116 { |
115 struct commandline_macro *cm; | 117 struct commandline_macro *cm; |
118 struct place *p; | |
116 unsigned i, num; | 119 unsigned i, num; |
117 | 120 |
121 p = place_gettemporary(); | |
118 num = array_num(&commandline_macros); | 122 num = array_num(&commandline_macros); |
119 for (i=0; i<num; i++) { | 123 for (i=0; i<num; i++) { |
120 cm = array_get(&commandline_macros, i); | 124 cm = array_get(&commandline_macros, i); |
121 if (cm->expansion != NULL) { | 125 if (cm->expansion != NULL) { |
122 macro_define(NULL, cm->macro, cm->expansion); | 126 place_setcommandline(p, cm->column); |
127 macro_define(p, cm->macro, cm->expansion); | |
123 } else { | 128 } else { |
124 macro_undef(cm->macro); | 129 macro_undef(cm->macro); |
125 } | 130 } |
126 free(cm); | 131 free(cm); |
127 } | 132 } |
128 array_setsize(&commandline_macros, 0); | 133 array_setsize(&commandline_macros, 0); |
134 place_puttemporary(p); | |
129 } | 135 } |
130 | 136 |
131 static | 137 static |
132 void | 138 void |
133 apply_builtin_macro(const char *name, const char *val) | 139 apply_builtin_macro(const char *name, const char *val) |
172 // extra included files | 178 // extra included files |
173 | 179 |
174 struct commandline_file { | 180 struct commandline_file { |
175 char *name; | 181 char *name; |
176 bool suppress_output; | 182 bool suppress_output; |
183 unsigned column; | |
177 }; | 184 }; |
178 | 185 |
179 static struct array commandline_files; | 186 static struct array commandline_files; |
180 | 187 |
181 static | 188 static |
192 array_cleanup(&commandline_files); | 199 array_cleanup(&commandline_files); |
193 } | 200 } |
194 | 201 |
195 static | 202 static |
196 void | 203 void |
197 commandline_addfile(char *name, bool suppress_output) | 204 commandline_addfile(char *name, bool suppress_output, unsigned column) |
198 { | 205 { |
199 struct commandline_file *cf; | 206 struct commandline_file *cf; |
200 | 207 |
201 cf = domalloc(sizeof(*cf)); | 208 cf = domalloc(sizeof(*cf)); |
202 cf->name = name; | 209 cf->name = name; |
203 cf->suppress_output = suppress_output; | 210 cf->suppress_output = suppress_output; |
211 cf->column = column; | |
204 array_add(&commandline_files, cf, NULL); | 212 array_add(&commandline_files, cf, NULL); |
205 } | 213 } |
206 | 214 |
207 static | 215 static |
208 void | 216 void |
209 commandline_addfile_output(char *name) | 217 commandline_addfile_output(unsigned column, char *name) |
210 { | 218 { |
211 commandline_addfile(name, false); | 219 commandline_addfile(name, false, column); |
212 } | 220 } |
213 | 221 |
214 static | 222 static |
215 void | 223 void |
216 commandline_addfile_nooutput(char *name) | 224 commandline_addfile_nooutput(unsigned column, char *name) |
217 { | 225 { |
218 commandline_addfile(name, true); | 226 commandline_addfile(name, true, column); |
219 } | 227 } |
220 | 228 |
221 static | 229 static |
222 void | 230 void |
223 read_commandline_files(void) | 231 read_commandline_files(void) |
224 { | 232 { |
225 struct commandline_file *cf; | 233 struct commandline_file *cf; |
226 unsigned i, num; | 234 unsigned i, num; |
227 bool save = false; | 235 bool save = false; |
228 | 236 struct place *p; |
237 | |
238 p = place_gettemporary(); | |
229 num = array_num(&commandline_files); | 239 num = array_num(&commandline_files); |
230 for (i=0; i<num; i++) { | 240 for (i=0; i<num; i++) { |
231 cf = array_get(&commandline_files, i); | 241 cf = array_get(&commandline_files, i); |
242 place_setcommandline(p, cf->column); | |
232 if (cf->suppress_output) { | 243 if (cf->suppress_output) { |
233 save = mode.do_output; | 244 save = mode.do_output; |
234 mode.do_output = false; | 245 mode.do_output = false; |
235 files_read(NULL, cf->name); | 246 file_readquote(p, cf->name); |
236 mode.do_output = save; | 247 mode.do_output = save; |
237 } else { | 248 } else { |
238 files_read(NULL, cf->name); | 249 file_readquote(p, cf->name); |
239 } | 250 } |
240 free(cf); | 251 free(cf); |
241 } | 252 } |
253 place_puttemporary(p); | |
242 array_setsize(&commandline_files, 0); | 254 array_setsize(&commandline_files, 0); |
243 } | 255 } |
244 | 256 |
245 //////////////////////////////////////////////////////////// | 257 //////////////////////////////////////////////////////////// |
246 // include path accumulation | 258 // include path accumulation |
271 stringarray_cleanup(&incpath_late); | 283 stringarray_cleanup(&incpath_late); |
272 } | 284 } |
273 | 285 |
274 static | 286 static |
275 void | 287 void |
276 commandline_isysroot(char *dir) | 288 commandline_isysroot(unsigned column, char *dir) |
277 { | 289 { |
290 (void)column; | |
278 sysroot = dir; | 291 sysroot = dir; |
279 } | 292 } |
280 | 293 |
281 static | 294 static |
282 void | 295 void |
285 stringarray_add(arr, s, NULL); | 298 stringarray_add(arr, s, NULL); |
286 } | 299 } |
287 | 300 |
288 static | 301 static |
289 void | 302 void |
290 commandline_addincpath_quote(char *dir) | 303 commandline_addincpath_quote(unsigned column, char *dir) |
291 { | 304 { |
305 (void)column; | |
292 commandline_addincpath(&incpath_quote, dir); | 306 commandline_addincpath(&incpath_quote, dir); |
293 } | 307 } |
294 | 308 |
295 static | 309 static |
296 void | 310 void |
297 commandline_addincpath_user(char *dir) | 311 commandline_addincpath_user(unsigned column, char *dir) |
298 { | 312 { |
313 (void)column; | |
299 commandline_addincpath(&incpath_user, dir); | 314 commandline_addincpath(&incpath_user, dir); |
300 } | 315 } |
301 | 316 |
302 static | 317 static |
303 void | 318 void |
304 commandline_addincpath_system(char *dir) | 319 commandline_addincpath_system(unsigned column, char *dir) |
305 { | 320 { |
321 (void)column; | |
306 commandline_addincpath(&incpath_system, dir); | 322 commandline_addincpath(&incpath_system, dir); |
307 } | 323 } |
308 | 324 |
309 static | 325 static |
310 void | 326 void |
311 commandline_addincpath_late(char *dir) | 327 commandline_addincpath_late(unsigned column, char *dir) |
312 { | 328 { |
329 (void)column; | |
313 commandline_addincpath(&incpath_late, dir); | 330 commandline_addincpath(&incpath_late, dir); |
314 } | 331 } |
315 | 332 |
316 static | 333 static |
317 void | 334 void |
377 | 394 |
378 static const char *commandline_prefix; | 395 static const char *commandline_prefix; |
379 | 396 |
380 static | 397 static |
381 void | 398 void |
382 commandline_setprefix(char *prefix) | 399 commandline_setprefix(unsigned column, char *prefix) |
383 { | 400 { |
401 (void)column; | |
384 commandline_prefix = prefix; | 402 commandline_prefix = prefix; |
385 } | 403 } |
386 | 404 |
387 static | 405 static |
388 void | 406 void |
389 commandline_addincpath_user_withprefix(char *dir) | 407 commandline_addincpath_user_withprefix(unsigned column, char *dir) |
390 { | 408 { |
391 char *s; | 409 char *s; |
392 | 410 |
393 if (commandline_prefix == NULL) { | 411 if (commandline_prefix == NULL) { |
394 warnx("-iprefix needed"); | 412 warnx("-iprefix needed"); |
395 die(); | 413 die(); |
396 } | 414 } |
397 s = dostrdup3(commandline_prefix, "/", dir); | 415 s = dostrdup3(commandline_prefix, "/", dir); |
398 freestringlater(s); | 416 freestringlater(s); |
399 commandline_addincpath_user(s); | 417 commandline_addincpath_user(column, s); |
400 } | 418 } |
401 | 419 |
402 static | 420 static |
403 void | 421 void |
404 commandline_addincpath_late_withprefix(char *dir) | 422 commandline_addincpath_late_withprefix(unsigned column, char *dir) |
405 { | 423 { |
406 char *s; | 424 char *s; |
407 | 425 |
408 if (commandline_prefix == NULL) { | 426 if (commandline_prefix == NULL) { |
409 warnx("-iprefix needed"); | 427 warnx("-iprefix needed"); |
410 die(); | 428 die(); |
411 } | 429 } |
412 s = dostrdup3(commandline_prefix, "/", dir); | 430 s = dostrdup3(commandline_prefix, "/", dir); |
413 freestringlater(s); | 431 freestringlater(s); |
414 commandline_addincpath_late(s); | 432 commandline_addincpath_late(column, s); |
415 } | 433 } |
416 | 434 |
417 static | 435 static |
418 void | 436 void |
419 commandline_setstd(char *std) | 437 commandline_setstd(unsigned column, char *std) |
420 { | 438 { |
439 (void)column; | |
440 | |
421 if (!strcmp(std, "krc")) { | 441 if (!strcmp(std, "krc")) { |
422 return; | 442 return; |
423 } | 443 } |
424 warnx("Standard %s not supported by this preprocessor", std); | 444 warnx("Standard %s not supported by this preprocessor", std); |
425 die(); | 445 die(); |
426 } | 446 } |
427 | 447 |
428 static | 448 static |
429 void | 449 void |
430 commandline_setlang(char *lang) | 450 commandline_setlang(unsigned column, char *lang) |
431 { | 451 { |
452 (void)column; | |
453 | |
432 if (!strcmp(lang, "c") || !strcmp(lang, "assembler-with-cpp")) { | 454 if (!strcmp(lang, "c") || !strcmp(lang, "assembler-with-cpp")) { |
433 return; | 455 return; |
434 } | 456 } |
435 warnx("Language %s not supported by this preprocessor", lang); | 457 warnx("Language %s not supported by this preprocessor", lang); |
436 die(); | 458 die(); |
439 //////////////////////////////////////////////////////////// | 461 //////////////////////////////////////////////////////////// |
440 // complex modes | 462 // complex modes |
441 | 463 |
442 static | 464 static |
443 void | 465 void |
444 commandline_iremap(char *str) | 466 commandline_iremap(unsigned column, char *str) |
445 { | 467 { |
468 (void)column; | |
446 /* XXX */ | 469 /* XXX */ |
470 (void)str; | |
447 warnx("-iremap not supported"); | 471 warnx("-iremap not supported"); |
448 die(); | 472 die(); |
449 } | 473 } |
450 | 474 |
451 static | 475 static |
452 void | 476 void |
453 commandline_tabstop(char *s) | 477 commandline_tabstop(unsigned column, char *s) |
454 { | 478 { |
455 char *t; | 479 char *t; |
456 unsigned long val; | 480 unsigned long val; |
481 | |
482 (void)column; | |
457 | 483 |
458 t = strchr(s, '='); | 484 t = strchr(s, '='); |
459 if (t == NULL) { | 485 if (t == NULL) { |
460 /* should not happen */ | 486 /* should not happen */ |
461 warnx("Invalid tabstop"); | 487 warnx("Invalid tabstop"); |
533 * depend | 559 * depend |
534 */ | 560 */ |
535 | 561 |
536 static | 562 static |
537 void | 563 void |
538 commandline_setdependtarget(char *str) | 564 commandline_setdependtarget(unsigned column, char *str) |
539 { | 565 { |
566 (void)column; | |
540 mode.depend_target = str; | 567 mode.depend_target = str; |
541 mode.depend_quote_target = false; | 568 mode.depend_quote_target = false; |
542 } | 569 } |
543 | 570 |
544 static | 571 static |
545 void | 572 void |
546 commandline_setdependtarget_quoted(char *str) | 573 commandline_setdependtarget_quoted(unsigned column, char *str) |
547 { | 574 { |
575 (void)column; | |
548 mode.depend_target = str; | 576 mode.depend_target = str; |
549 mode.depend_quote_target = true; | 577 mode.depend_quote_target = true; |
550 } | 578 } |
551 | 579 |
552 static | 580 static |
553 void | 581 void |
554 commandline_setdependoutput(char *str) | 582 commandline_setdependoutput(unsigned column, char *str) |
555 { | 583 { |
584 (void)column; | |
556 mode.depend_file = str; | 585 mode.depend_file = str; |
557 } | 586 } |
558 | 587 |
559 static | 588 static |
560 void | 589 void |
632 void (*func)(void); | 661 void (*func)(void); |
633 }; | 662 }; |
634 | 663 |
635 struct prefix_option { | 664 struct prefix_option { |
636 const char *string; | 665 const char *string; |
637 void (*func)(char *); | 666 void (*func)(unsigned column, char *); |
638 }; | 667 }; |
639 | 668 |
640 struct arg_option { | 669 struct arg_option { |
641 const char *string; | 670 const char *string; |
642 void (*func)(char *); | 671 void (*func)(unsigned column, char *); |
643 }; | 672 }; |
644 | 673 |
645 static const struct flag_option flag_options[] = { | 674 static const struct flag_option flag_options[] = { |
646 { "C", &mode.output_retain_comments, true }, | 675 { "C", &mode.output_retain_comments, true }, |
647 { "CC", &mode.output_retain_comments, true }, | 676 { "CC", &mode.output_retain_comments, true }, |
748 return false; | 777 return false; |
749 } | 778 } |
750 | 779 |
751 static | 780 static |
752 bool | 781 bool |
753 check_prefix_option(char *opt) | 782 check_prefix_option(unsigned column, char *opt) |
754 { | 783 { |
755 unsigned i; | 784 unsigned i; |
756 int r; | 785 int r; |
757 | 786 |
758 for (i=0; i<num_prefix_options; i++) { | 787 for (i=0; i<num_prefix_options; i++) { |
759 r = strncmp(opt, prefix_options[i].string, | 788 r = strncmp(opt, prefix_options[i].string, |
760 strlen(prefix_options[i].string)); | 789 strlen(prefix_options[i].string)); |
761 if (r == 0) { | 790 if (r == 0) { |
762 prefix_options[i].func(opt); | 791 prefix_options[i].func(column, opt); |
763 return true; | 792 return true; |
764 } | 793 } |
765 if (r < 0) { | 794 if (r < 0) { |
766 break; | 795 break; |
767 } | 796 } |
769 return false; | 798 return false; |
770 } | 799 } |
771 | 800 |
772 static | 801 static |
773 bool | 802 bool |
774 check_arg_option(const char *opt, char *arg) | 803 check_arg_option(const char *opt, unsigned argcolumn, char *arg) |
775 { | 804 { |
776 unsigned i; | 805 unsigned i; |
777 int r; | 806 int r; |
778 | 807 |
779 for (i=0; i<num_arg_options; i++) { | 808 for (i=0; i<num_arg_options; i++) { |
781 if (r == 0) { | 810 if (r == 0) { |
782 if (arg == NULL) { | 811 if (arg == NULL) { |
783 warnx("Option -%s requires an argument", opt); | 812 warnx("Option -%s requires an argument", opt); |
784 die(); | 813 die(); |
785 } | 814 } |
786 arg_options[i].func(arg); | 815 arg_options[i].func(argcolumn, arg); |
787 return true; | 816 return true; |
788 } | 817 } |
789 if (r < 0) { | 818 if (r < 0) { |
790 break; | 819 break; |
791 } | 820 } |
824 static | 853 static |
825 void | 854 void |
826 init(void) | 855 init(void) |
827 { | 856 { |
828 stringarray_init(&freestrings); | 857 stringarray_init(&freestrings); |
858 | |
829 incpath_init(); | 859 incpath_init(); |
830 commandline_macros_init(); | 860 commandline_macros_init(); |
831 commandline_files_init(); | 861 commandline_files_init(); |
862 | |
863 files_init(); | |
832 } | 864 } |
833 | 865 |
834 static | 866 static |
835 void | 867 void |
836 cleanup(void) | 868 cleanup(void) |
837 { | 869 { |
838 unsigned i, num; | 870 unsigned i, num; |
871 | |
872 files_cleanup(); | |
839 | 873 |
840 commandline_files_cleanup(); | 874 commandline_files_cleanup(); |
841 commandline_macros_cleanup(); | 875 commandline_macros_cleanup(); |
842 incpath_cleanup(); | 876 incpath_cleanup(); |
843 | 877 |
868 int | 902 int |
869 main(int argc, char *argv[]) | 903 main(int argc, char *argv[]) |
870 { | 904 { |
871 const char *inputfile = NULL; | 905 const char *inputfile = NULL; |
872 const char *outputfile = NULL; | 906 const char *outputfile = NULL; |
907 unsigned column, nextcolumn; | |
908 struct place *p; | |
873 int i; | 909 int i; |
874 | 910 |
875 init(); | 911 init(); |
876 | 912 |
877 for (i=1; i<argc; i++) { | 913 column = 1; |
914 for (i=1; i<argc; i++, column = nextcolumn) { | |
878 if (argv[i][0] != '-') { | 915 if (argv[i][0] != '-') { |
879 break; | 916 break; |
880 } | 917 } |
918 nextcolumn = column + strlen(argv[i]) + 1; | |
881 if (check_flag_option(argv[i]+1)) { | 919 if (check_flag_option(argv[i]+1)) { |
882 continue; | 920 continue; |
883 } | 921 } |
884 if (check_act_option(argv[i]+1)) { | 922 if (check_act_option(argv[i]+1)) { |
885 continue; | 923 continue; |
886 } | 924 } |
887 if (check_prefix_option(argv[i]+1)) { | 925 if (check_prefix_option(column, argv[i]+1)) { |
888 continue; | 926 continue; |
889 } | 927 } |
890 if (check_arg_option(argv[i]+1, argv[i+1])) { | 928 if (check_arg_option(argv[i]+1, nextcolumn, argv[i+1])) { |
929 column = nextcolumn; | |
891 i++; | 930 i++; |
931 nextcolumn = strlen(argv[i]) + 1; | |
892 continue; | 932 continue; |
893 } | 933 } |
894 usage(); | 934 usage(); |
895 } | 935 } |
896 if (i < argc) { | 936 if (i < argc) { |
907 | 947 |
908 loadincludepath(); | 948 loadincludepath(); |
909 apply_builtin_macros(); | 949 apply_builtin_macros(); |
910 apply_commandline_macros(); | 950 apply_commandline_macros(); |
911 read_commandline_files(); | 951 read_commandline_files(); |
912 files_read(NULL, inputfile); | 952 p = place_gettemporary(); |
953 place_setnowhere(p); | |
954 file_readabsolute(p, inputfile); | |
955 place_puttemporary(p); | |
913 | 956 |
914 cleanup(); | 957 cleanup(); |
958 if (complain_failed()) { | |
959 return EXIT_FAILURE; | |
960 } | |
915 return EXIT_SUCCESS; | 961 return EXIT_SUCCESS; |
916 } | 962 } |