comparison main.c @ 15:f6177d3ed5c2

handle directives
author David A. Holland
date Sun, 19 Dec 2010 21:42:01 -0500
parents 5045b9678bb0
children 76da41da923f
comparison
equal deleted inserted replaced
14:5045b9678bb0 15:f6177d3ed5c2
10 #include "utils.h" 10 #include "utils.h"
11 #include "array.h" 11 #include "array.h"
12 #include "mode.h" 12 #include "mode.h"
13 #include "place.h" 13 #include "place.h"
14 #include "files.h" 14 #include "files.h"
15 #include "directive.h"
15 #include "macro.h" 16 #include "macro.h"
16 17
17 struct mode mode = { 18 struct mode mode = {
18 .werror = false, 19 .werror = false,
19 20
55 //////////////////////////////////////////////////////////// 56 ////////////////////////////////////////////////////////////
56 // commandline macros 57 // commandline macros
57 58
58 struct commandline_macro { 59 struct commandline_macro {
59 struct place where; 60 struct place where;
61 struct place where2;
60 const char *macro; 62 const char *macro;
61 const char *expansion; 63 const char *expansion;
62 }; 64 };
63 65
64 static struct array commandline_macros; 66 static struct array commandline_macros;
77 array_cleanup(&commandline_macros); 79 array_cleanup(&commandline_macros);
78 } 80 }
79 81
80 static 82 static
81 void 83 void
82 commandline_macro_add(const struct place *p, 84 commandline_macro_add(const struct place *p, const char *macro,
83 const char *macro, const char *expansion) 85 const struct place *p2, const char *expansion)
84 { 86 {
85 struct commandline_macro *cm; 87 struct commandline_macro *cm;
86 88
87 cm = domalloc(sizeof(*cm)); 89 cm = domalloc(sizeof(*cm));
88 cm->where = *p; 90 cm->where = *p;
91 cm->where2 = *p2;
89 cm->macro = macro; 92 cm->macro = macro;
90 cm->expansion = expansion; 93 cm->expansion = expansion;
91 } 94 }
92 95
93 static 96 static
94 void 97 void
95 commandline_def(const struct place *p, char *str) 98 commandline_def(const struct place *p, char *str)
96 { 99 {
100 struct place p2;
97 char *val; 101 char *val;
98 102
99 val = strchr(str, '='); 103 val = strchr(str, '=');
100 if (val != NULL) { 104 if (val != NULL) {
101 *val = '\0'; 105 *val = '\0';
102 val++; 106 val++;
103 } 107 }
104 commandline_macro_add(p, str, val ? val : "1"); 108
109 if (val) {
110 p2 = *p;
111 p2.column += strlen(str);
112 } else {
113 place_setbuiltin(&p2, 1);
114 }
115 commandline_macro_add(p, str, &p2, val ? val : "1");
105 } 116 }
106 117
107 static 118 static
108 void 119 void
109 commandline_undef(const struct place *p, char *str) 120 commandline_undef(const struct place *p, char *str)
110 { 121 {
111 commandline_macro_add(p, str, NULL); 122 commandline_macro_add(p, str, p, NULL);
112 } 123 }
113 124
114 static 125 static
115 void 126 void
116 apply_commandline_macros(void) 127 apply_commandline_macros(void)
120 131
121 num = array_num(&commandline_macros); 132 num = array_num(&commandline_macros);
122 for (i=0; i<num; i++) { 133 for (i=0; i<num; i++) {
123 cm = array_get(&commandline_macros, i); 134 cm = array_get(&commandline_macros, i);
124 if (cm->expansion != NULL) { 135 if (cm->expansion != NULL) {
125 macro_define(&cm->where, cm->macro, cm->expansion); 136 macro_define(&cm->where, cm->macro,
137 &cm->where2, cm->expansion);
126 } else { 138 } else {
127 macro_undef(cm->macro); 139 macro_undef(cm->macro);
128 } 140 }
129 free(cm); 141 free(cm);
130 } 142 }
136 apply_builtin_macro(unsigned num, const char *name, const char *val) 148 apply_builtin_macro(unsigned num, const char *name, const char *val)
137 { 149 {
138 struct place p; 150 struct place p;
139 151
140 place_setbuiltin(&p, num); 152 place_setbuiltin(&p, num);
141 macro_define(&p, name, val); 153 macro_define(&p, name, &p, val);
142 } 154 }
143 155
144 static 156 static
145 void 157 void
146 apply_builtin_macros(void) 158 apply_builtin_macros(void)
857 commandline_macros_init(); 869 commandline_macros_init();
858 commandline_files_init(); 870 commandline_files_init();
859 871
860 place_init(); 872 place_init();
861 files_init(); 873 files_init();
874 directive_init();
862 } 875 }
863 876
864 static 877 static
865 void 878 void
866 cleanup(void) 879 cleanup(void)
867 { 880 {
868 unsigned i, num; 881 unsigned i, num;
869 882
883 directive_cleanup();
870 files_cleanup(); 884 files_cleanup();
871 place_cleanup(); 885 place_cleanup();
872 886
873 commandline_files_cleanup(); 887 commandline_files_cleanup();
874 commandline_macros_cleanup(); 888 commandline_macros_cleanup();