comparison main.c @ 11:b9d50e786322

simplify places
author David A. Holland
date Sun, 19 Dec 2010 19:30:24 -0500
parents 800f3a560a3b
children 5045b9678bb0
comparison
equal deleted inserted replaced
10:800f3a560a3b 11:b9d50e786322
54 54
55 //////////////////////////////////////////////////////////// 55 ////////////////////////////////////////////////////////////
56 // commandline macros 56 // commandline macros
57 57
58 struct commandline_macro { 58 struct commandline_macro {
59 struct place *place; 59 struct place where;
60 const char *macro; 60 const char *macro;
61 const char *expansion; 61 const char *expansion;
62 }; 62 };
63 63
64 static struct array commandline_macros; 64 static struct array commandline_macros;
83 const char *macro, const char *expansion) 83 const char *macro, const char *expansion)
84 { 84 {
85 struct commandline_macro *cm; 85 struct commandline_macro *cm;
86 86
87 cm = domalloc(sizeof(*cm)); 87 cm = domalloc(sizeof(*cm));
88 cm->place = place_clone(p); 88 cm->where = *p;
89 cm->macro = macro; 89 cm->macro = macro;
90 cm->expansion = expansion; 90 cm->expansion = expansion;
91 } 91 }
92 92
93 static 93 static
120 120
121 num = array_num(&commandline_macros); 121 num = array_num(&commandline_macros);
122 for (i=0; i<num; i++) { 122 for (i=0; i<num; i++) {
123 cm = array_get(&commandline_macros, i); 123 cm = array_get(&commandline_macros, i);
124 if (cm->expansion != NULL) { 124 if (cm->expansion != NULL) {
125 macro_define(cm->place, cm->macro, cm->expansion); 125 macro_define(&cm->where, cm->macro, cm->expansion);
126 } else { 126 } else {
127 macro_undef(cm->macro); 127 macro_undef(cm->macro);
128 } 128 }
129 place_destroy(cm->place);
130 free(cm); 129 free(cm);
131 } 130 }
132 array_setsize(&commandline_macros, 0); 131 array_setsize(&commandline_macros, 0);
133 } 132 }
134 133
135 static 134 static
136 void 135 void
137 apply_builtin_macro(unsigned num, const char *name, const char *val) 136 apply_builtin_macro(unsigned num, const char *name, const char *val)
138 { 137 {
139 struct place *p; 138 struct place p;
140 139
141 p = place_gettemporary(); 140 place_setbuiltin(&p, num);
142 place_setbuiltin(p, num); 141 macro_define(&p, name, val);
143 macro_define(p, name, val);
144 place_puttemporary(p);
145 } 142 }
146 143
147 static 144 static
148 void 145 void
149 apply_builtin_macros(void) 146 apply_builtin_macros(void)
180 177
181 //////////////////////////////////////////////////////////// 178 ////////////////////////////////////////////////////////////
182 // extra included files 179 // extra included files
183 180
184 struct commandline_file { 181 struct commandline_file {
185 struct place *place; 182 struct place where;
186 char *name; 183 char *name;
187 bool suppress_output; 184 bool suppress_output;
188 }; 185 };
189 186
190 static struct array commandline_files; 187 static struct array commandline_files;
208 commandline_addfile(const struct place *p, char *name, bool suppress_output) 205 commandline_addfile(const struct place *p, char *name, bool suppress_output)
209 { 206 {
210 struct commandline_file *cf; 207 struct commandline_file *cf;
211 208
212 cf = domalloc(sizeof(*cf)); 209 cf = domalloc(sizeof(*cf));
213 cf->place = place_clone(p); 210 cf->where = *p;
214 cf->name = name; 211 cf->name = name;
215 cf->suppress_output = suppress_output; 212 cf->suppress_output = suppress_output;
216 array_add(&commandline_files, cf, NULL); 213 array_add(&commandline_files, cf, NULL);
217 } 214 }
218 215
242 for (i=0; i<num; i++) { 239 for (i=0; i<num; i++) {
243 cf = array_get(&commandline_files, i); 240 cf = array_get(&commandline_files, i);
244 if (cf->suppress_output) { 241 if (cf->suppress_output) {
245 save = mode.do_output; 242 save = mode.do_output;
246 mode.do_output = false; 243 mode.do_output = false;
247 file_readquote(cf->place, cf->name); 244 file_readquote(&cf->where, cf->name);
248 mode.do_output = save; 245 mode.do_output = save;
249 } else { 246 } else {
250 file_readquote(cf->place, cf->name); 247 file_readquote(&cf->where, cf->name);
251 } 248 }
252 place_destroy(cf->place);
253 free(cf); 249 free(cf);
254 } 250 }
255 array_setsize(&commandline_files, 0); 251 array_setsize(&commandline_files, 0);
256 } 252 }
257 253
905 int 901 int
906 main(int argc, char *argv[]) 902 main(int argc, char *argv[])
907 { 903 {
908 const char *inputfile = NULL; 904 const char *inputfile = NULL;
909 const char *outputfile = NULL; 905 const char *outputfile = NULL;
910 struct place *p; 906 struct place cmdplace;
911 int i; 907 int i;
912 908
913 init(); 909 init();
914 p = place_gettemporary();
915 910
916 for (i=1; i<argc; i++) { 911 for (i=1; i<argc; i++) {
917 if (argv[i][0] != '-') { 912 if (argv[i][0] != '-') {
918 break; 913 break;
919 } 914 }
920 place_setcommandline(p, i); 915 place_setcommandline(&cmdplace, i);
921 if (check_flag_option(argv[i]+1)) { 916 if (check_flag_option(argv[i]+1)) {
922 continue; 917 continue;
923 } 918 }
924 if (check_act_option(argv[i]+1)) { 919 if (check_act_option(argv[i]+1)) {
925 continue; 920 continue;
926 } 921 }
927 if (check_prefix_option(p, argv[i]+1)) { 922 if (check_prefix_option(&cmdplace, argv[i]+1)) {
928 continue; 923 continue;
929 } 924 }
930 place_setcommandline(p, i+1); 925 place_setcommandline(&cmdplace, i+1);
931 if (check_arg_option(argv[i]+1, p, argv[i+1])) { 926 if (check_arg_option(argv[i]+1, &cmdplace, argv[i+1])) {
932 i++; 927 i++;
933 continue; 928 continue;
934 } 929 }
935 usage(); 930 usage();
936 } 931 }
948 943
949 loadincludepath(); 944 loadincludepath();
950 apply_builtin_macros(); 945 apply_builtin_macros();
951 apply_commandline_macros(); 946 apply_commandline_macros();
952 read_commandline_files(); 947 read_commandline_files();
953 place_setnowhere(p); 948 place_setnowhere(&cmdplace);
954 file_readabsolute(p, inputfile); 949 file_readabsolute(&cmdplace, inputfile);
955 950
956 place_puttemporary(p);
957 cleanup(); 951 cleanup();
958 if (complain_failed()) { 952 if (complain_failed()) {
959 return EXIT_FAILURE; 953 return EXIT_FAILURE;
960 } 954 }
961 return EXIT_SUCCESS; 955 return EXIT_SUCCESS;