| @@ -43,566 +43,594 @@ | | | @@ -43,566 +43,594 @@ |
43 | #include <config.h> | | 43 | #include <config.h> |
44 | #endif | | 44 | #endif |
45 | | | 45 | |
46 | #include <X11/Xlib.h> | | 46 | #include <X11/Xlib.h> |
47 | #include <X11/Xutil.h> | | 47 | #include <X11/Xutil.h> |
48 | #include <X11/Xatom.h> | | 48 | #include <X11/Xatom.h> |
49 | #include <X11/Xos.h> | | 49 | #include <X11/Xos.h> |
50 | #include <X11/Xmu/SysUtil.h> | | 50 | #include <X11/Xmu/SysUtil.h> |
51 | #include <stdio.h> | | 51 | #include <stdio.h> |
52 | #include <ctype.h> | | 52 | #include <ctype.h> |
53 | #include <errno.h> | | 53 | #include <errno.h> |
54 | #include <stdlib.h> | | 54 | #include <stdlib.h> |
55 | #include <stdarg.h> | | 55 | #include <stdarg.h> |
| | | 56 | #include <stdint.h> |
56 | | | 57 | |
57 | #ifdef NEED_SYS_PARAM_H | | 58 | #ifdef NEED_SYS_PARAM_H |
58 | # include <sys/param.h> /* defines MAXHOSTNAMELEN on BSD & Linux */ | | 59 | # include <sys/param.h> /* defines MAXHOSTNAMELEN on BSD & Linux */ |
59 | #endif | | 60 | #endif |
60 | | | 61 | |
61 | #ifdef NEED_NETDB_H | | 62 | #ifdef NEED_NETDB_H |
62 | # include <netdb.h> /* defines MAXHOSTNAMELEN on Solaris */ | | 63 | # include <netdb.h> /* defines MAXHOSTNAMELEN on Solaris */ |
63 | #endif | | 64 | #endif |
64 | | | 65 | |
65 | #define SCREEN_RESOURCES "SCREEN_RESOURCES" | | 66 | #define SCREEN_RESOURCES "SCREEN_RESOURCES" |
66 | | | 67 | |
67 | #ifndef CPP | | 68 | #ifndef CPP |
68 | #ifdef __UNIXOS2__ | | | |
69 | /* expected to be in path */ | | | |
70 | #define CPP "cpp" | | | |
71 | #else | | | |
72 | #define CPP "/usr/lib/cpp" | | 69 | #define CPP "/usr/lib/cpp" |
73 | #endif /* __UNIXOS2__ */ | | 70 | #endif /* CPP */ |
74 | #endif /* CPP */ | | | |
75 | | | 71 | |
76 | #define INIT_BUFFER_SIZE 10000 | | 72 | #define INIT_BUFFER_SIZE 10000 |
77 | #define INIT_ENTRY_SIZE 500 | | 73 | #define INIT_ENTRY_SIZE 500 |
78 | | | 74 | |
79 | #define RALL 0 | | 75 | #define RALL 0 |
80 | #define RGLOBAL 1 | | 76 | #define RGLOBAL 1 |
81 | #define RSCREEN 2 | | 77 | #define RSCREEN 2 |
82 | #define RSCREENS 3 | | 78 | #define RSCREENS 3 |
83 | | | 79 | |
84 | #define OPSYMBOLS 0 | | 80 | #define OPSYMBOLS 0 |
85 | #define OPQUERY 1 | | 81 | #define OPQUERY 1 |
86 | #define OPREMOVE 2 | | 82 | #define OPREMOVE 2 |
87 | #define OPEDIT 3 | | 83 | #define OPEDIT 3 |
88 | #define OPLOAD 4 | | 84 | #define OPLOAD 4 |
89 | #define OPMERGE 5 | | 85 | #define OPMERGE 5 |
90 | #define OPOVERRIDE 6 | | 86 | #define OPOVERRIDE 6 |
91 | | | 87 | |
92 | #define RESOURCE_PROPERTY_NAME "RESOURCE_MANAGER" | | 88 | #define BACKUP_SUFFIX ".bak" /* for editing */ |
93 | #define BACKUP_SUFFIX ".bak" /* for editting */ | | | |
94 | | | 89 | |
95 | typedef struct _Entry { | | 90 | typedef struct _Entry { |
96 | char *tag, *value; | | 91 | char *tag, *value; |
97 | int lineno; | | 92 | int lineno; |
98 | Bool usable; | | 93 | Bool usable; |
99 | } Entry; | | 94 | } Entry; |
| | | 95 | |
100 | typedef struct _Buffer { | | 96 | typedef struct _Buffer { |
101 | char *buff; | | 97 | char *buff; |
102 | int room, used; | | 98 | size_t room, used; |
103 | } Buffer; | | 99 | } Buffer; |
| | | 100 | |
104 | typedef struct _Entries { | | 101 | typedef struct _Entries { |
105 | Entry *entry; | | 102 | Entry *entry; |
106 | int room, used; | | 103 | size_t room, used; |
107 | } Entries; | | 104 | } Entries; |
108 | | | 105 | |
109 | /* dynamically allocated strings */ | | 106 | /* dynamically allocated strings */ |
110 | #define CHUNK_SIZE 4096 | | 107 | #define CHUNK_SIZE 4096 |
111 | typedef struct _String { | | 108 | typedef struct _String { |
112 | char *val; | | 109 | char *val; |
113 | int room, used; | | 110 | size_t room, used; |
114 | } String; | | 111 | } String; |
115 | | | 112 | |
116 | static char *ProgramName; | | 113 | static char *ProgramName; |
117 | static Bool quiet = False; | | 114 | static Bool quiet = False; |
118 | static char tmpname[32]; | | 115 | static char tmpname[32]; |
119 | static char *filename = NULL; | | 116 | static char *filename = NULL; |
120 | #ifdef PATHETICCPP | | 117 | #ifdef PATHETICCPP |
121 | static Bool need_real_defines = False; | | 118 | static Bool need_real_defines = False; |
122 | static char tmpname2[32]; | | 119 | static char tmpname2[32]; |
123 | #endif | | 120 | #endif |
124 | #ifdef WIN32 | | 121 | #ifdef WIN32 |
125 | static char tmpname3[32]; | | 122 | static char tmpname3[32]; |
126 | #endif | | 123 | #endif |
127 | static int oper = OPLOAD; | | 124 | static int oper = OPLOAD; |
128 | static char *editFile = NULL; | | 125 | static char *editFile = NULL; |
129 | static const char *cpp_program = NULL; | | 126 | static const char *cpp_program = NULL; |
130 | static const char* const cpp_locations[] = { CPP }; | | 127 | static const char * const cpp_locations[] = { CPP }; |
131 | static const char *backup_suffix = BACKUP_SUFFIX; | | 128 | static const char *backup_suffix = BACKUP_SUFFIX; |
132 | static Bool dont_execute = False; | | 129 | static Bool dont_execute = False; |
133 | static String defines; | | 130 | static String defines; |
134 | static int defines_base; | | 131 | static size_t defines_base; |
135 | #define MAX_CMD_DEFINES 512 | | 132 | #define MAX_CMD_DEFINES 512 |
136 | static char *cmd_defines[MAX_CMD_DEFINES]; | | 133 | static char *cmd_defines[MAX_CMD_DEFINES]; |
137 | static int num_cmd_defines = 0; | | 134 | static int num_cmd_defines = 0; |
138 | static String includes; | | 135 | static String includes; |
139 | static Display *dpy; | | 136 | static Display *dpy; |
140 | static Buffer buffer; | | 137 | static Buffer buffer; |
141 | static Entries newDB; | | 138 | static Entries newDB; |
142 | | | 139 | |
143 | static void fatal(const char *, ...) _X_ATTRIBUTE_PRINTF(1,2) _X_NORETURN; | | 140 | static void fatal(const char *, ...) |
144 | static void addstring ( String *arg, const char *s ); | | 141 | _X_ATTRIBUTE_PRINTF(1, 2)_X_NORETURN _X_COLD; |
145 | static void addescapedstring ( String *arg, const char *s ); | | 142 | static void addstring(String *arg, const char *s); |
146 | static void addtokstring ( String *arg, const char *s ); | | 143 | static void addescapedstring(String *arg, const char *s); |
147 | static void FormatEntries ( Buffer *b, Entries *entries ); | | 144 | static void addtokstring(String *arg, const char *s); |
148 | static void StoreProperty ( Display *display, Window root, Atom res_prop ); | | 145 | static void FormatEntries(Buffer *b, Entries * entries); |
149 | static void Process ( int scrno, Bool doScreen, Bool execute ); | | 146 | static void StoreProperty(Display *display, Window root, Atom res_prop); |
150 | static void ShuffleEntries ( Entries *db, Entries *dbs, int num ); | | 147 | static void Process(int scrno, Bool doScreen, Bool execute); |
151 | static void ReProcess ( int scrno, Bool doScreen ); | | 148 | static void ShuffleEntries(Entries *db, Entries *dbs, unsigned int num); |
| | | 149 | static void ReProcess(int scrno, Bool doScreen); |
152 | | | 150 | |
153 | #ifndef HAVE_ASPRINTF | | 151 | #ifndef HAVE_ASPRINTF |
154 | /* sprintf variant found in newer libc's which allocates string to print to */ | | 152 | /* sprintf variant found in newer libc's which allocates string to print to */ |
155 | static int _X_ATTRIBUTE_PRINTF(2,3) | | 153 | static int _X_ATTRIBUTE_PRINTF(2, 3) |
156 | asprintf(char ** ret, const char *format, ...) | | 154 | asprintf(char **ret, const char *format, ...) |
157 | { | | 155 | { |
158 | char buf[256]; | | 156 | char buf[256]; |
159 | int len; | | 157 | int len; |
160 | va_list ap; | | 158 | va_list ap; |
161 | | | 159 | |
162 | va_start(ap, format); | | 160 | va_start(ap, format); |
163 | len = vsnprintf(buf, sizeof(buf), format, ap); | | 161 | len = vsnprintf(buf, sizeof(buf), format, ap); |
164 | va_end(ap); | | 162 | va_end(ap); |
165 | | | 163 | |
166 | if (len < 0) | | 164 | if (len < 0) |
167 | return -1; | | 165 | return -1; |
168 | | | 166 | |
169 | if (len < sizeof(buf)) | | 167 | if (len < sizeof(buf)) { |
170 | { | | 168 | *ret = strdup(buf); |
171 | *ret = strdup(buf); | | | |
172 | } | | 169 | } |
173 | else | | 170 | else { |
174 | { | | 171 | *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */ |
175 | *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */ | | 172 | if (*ret != NULL) { |
176 | if (*ret != NULL) | | 173 | va_start(ap, format); |
177 | { | | 174 | len = vsnprintf(*ret, len + 1, format, ap); |
178 | va_start(ap, format); | | 175 | va_end(ap); |
179 | len = vsnprintf(*ret, len + 1, format, ap); | | 176 | if (len < 0) { |
180 | va_end(ap); | | 177 | free(*ret); |
181 | if (len < 0) { | | 178 | *ret = NULL; |
182 | free(*ret); | | 179 | } |
183 | *ret = NULL; | | 180 | } |
184 | } | | | |
185 | } | | | |
186 | } | | 181 | } |
187 | | | 182 | |
188 | if (*ret == NULL) | | 183 | if (*ret == NULL) |
189 | return -1; | | 184 | return -1; |
190 | | | 185 | |
191 | return len; | | 186 | return len; |
192 | } | | 187 | } |
193 | #endif /* HAVE_ASPRINTF */ | | 188 | #endif /* HAVE_ASPRINTF */ |
| | | 189 | |
| | | 190 | #ifndef HAVE_REALLOCARRAY |
| | | 191 | /* overflow checking realloc API from OpenBSD libc */ |
| | | 192 | static inline void * |
| | | 193 | reallocarray(void *optr, size_t n, size_t s) |
| | | 194 | { |
| | | 195 | if (n > 0 && (SIZE_MAX / n) < s) |
| | | 196 | return NULL; |
| | | 197 | return realloc(optr, n * s); |
| | | 198 | } |
| | | 199 | #endif |
| | | 200 | |
| | | 201 | # define mallocarray(n, s) reallocarray(NULL, n, s) |
194 | | | 202 | |
195 | static void | | 203 | static void |
196 | InitBuffer(Buffer *b) | | 204 | InitBuffer(Buffer *b) |
197 | { | | 205 | { |
198 | b->room = INIT_BUFFER_SIZE; | | 206 | b->room = INIT_BUFFER_SIZE; |
199 | b->used = 0; | | 207 | b->used = 0; |
200 | b->buff = malloc(INIT_BUFFER_SIZE*sizeof(char)); | | 208 | b->buff = mallocarray(INIT_BUFFER_SIZE, sizeof(char)); |
| | | 209 | if (b->buff == NULL) |
| | | 210 | fatal("%s: Can't allocate memory in %s\n", ProgramName, __func__); |
201 | } | | 211 | } |
202 | | | 212 | |
203 | #ifdef notyet | | 213 | #ifdef notyet |
204 | static void | | 214 | static void |
205 | FreeBuffer(Buffer *b) | | 215 | FreeBuffer(Buffer *b) |
206 | { | | 216 | { |
207 | free(b->buff); | | 217 | free(b->buff); |
208 | } | | 218 | } |
209 | #endif | | 219 | #endif |
210 | | | 220 | |
211 | static void | | 221 | static void |
212 | AppendToBuffer(Buffer *b, const char *str, int len) | | 222 | AppendToBuffer(Buffer *b, const char *str, size_t len) |
213 | { | | 223 | { |
214 | while (b->used + len > b->room) { | | 224 | while (b->used + len > b->room) { |
215 | b->buff = realloc(b->buff, 2*b->room*(sizeof(char))); | | 225 | b->buff = reallocarray(b->buff, b->room, 2 * sizeof(char)); |
216 | b->room *= 2; | | 226 | if (b->buff == NULL) |
| | | 227 | fatal("%s: Can't allocate memory in %s\n", ProgramName, __func__); |
| | | 228 | b->room *= 2; |
217 | } | | 229 | } |
218 | strncpy(b->buff + b->used, str, len); | | 230 | strncpy(b->buff + b->used, str, len); |
219 | b->used += len; | | 231 | b->used += len; |
220 | } | | 232 | } |
221 | | | 233 | |
222 | static void | | 234 | static void |
223 | InitEntries(Entries *e) | | 235 | InitEntries(Entries *e) |
224 | { | | 236 | { |
225 | e->room = INIT_ENTRY_SIZE; | | 237 | e->room = INIT_ENTRY_SIZE; |
226 | e->used = 0; | | 238 | e->used = 0; |
227 | e->entry = malloc(INIT_ENTRY_SIZE*sizeof(Entry)); | | 239 | e->entry = mallocarray(INIT_ENTRY_SIZE, sizeof(Entry)); |
| | | 240 | if (e->entry == NULL) |
| | | 241 | fatal("%s: Can't allocate memory in %s\n", ProgramName, __func__); |
| | | 242 | |
228 | } | | 243 | } |
229 | | | 244 | |
230 | static void | | 245 | static void |
231 | FreeEntries(Entries *e) | | 246 | FreeEntries(Entries *e) |
232 | { | | 247 | { |
233 | register int i; | | 248 | size_t i; |
234 | | | 249 | |
235 | for (i = 0; i < e->used; i++) { | | 250 | for (i = 0; i < e->used; i++) { |
236 | if (e->entry[i].usable) { | | 251 | if (e->entry[i].usable) { |
237 | free(e->entry[i].tag); | | 252 | free(e->entry[i].tag); |
238 | free(e->entry[i].value); | | 253 | free(e->entry[i].value); |
239 | } | | 254 | } |
240 | } | | 255 | } |
241 | free((char *)e->entry); | | 256 | free(e->entry); |
242 | } | | 257 | } |
243 | | | 258 | |
244 | static void | | 259 | static void |
245 | AddEntry(Entries *e, Entry *entry) | | 260 | AddEntry(Entries *e, Entry *entry) |
246 | { | | 261 | { |
247 | register int n; | | 262 | size_t n; |
248 | | | 263 | |
249 | for (n = 0; n < e->used; n++) { | | 264 | for (n = 0; n < e->used; n++) { |
250 | if (!strcmp(e->entry[n].tag, entry->tag)) { | | 265 | if (!strcmp(e->entry[n].tag, entry->tag)) { |
251 | /* overwrite old entry */ | | 266 | /* overwrite old entry */ |
252 | if (e->entry[n].lineno && !quiet) { | | 267 | if (e->entry[n].lineno && !quiet) { |
253 | fprintf (stderr, | | 268 | fprintf(stderr, |
254 | "%s: \"%s\" on line %d overrides entry on line %d\n", | | 269 | "%s: \"%s\" on line %d overrides entry on line %d\n", |
255 | ProgramName, entry->tag, entry->lineno, | | 270 | ProgramName, entry->tag, entry->lineno, |
256 | e->entry[n].lineno); | | 271 | e->entry[n].lineno); |
257 | } | | 272 | } |
258 | free(e->entry[n].tag); | | 273 | free(e->entry[n].tag); |
259 | free(e->entry[n].value); | | 274 | free(e->entry[n].value); |
260 | entry->usable = True; | | 275 | entry->usable = True; |
261 | e->entry[n] = *entry; | | 276 | e->entry[n] = *entry; |
262 | return; /* ok to leave, now there's only one of each tag in db */ | | 277 | return; /* ok to leave, now there's only one of each tag in db */ |
263 | } | | 278 | } |
264 | } | | 279 | } |
265 | | | 280 | |
266 | if (e->used == e->room) { | | 281 | if (e->used == e->room) { |
267 | e->entry = realloc(e->entry, 2 * e->room * (sizeof(Entry))); | | 282 | e->entry = reallocarray(e->entry, e->room, 2 * sizeof(Entry)); |
268 | e->room *= 2; | | 283 | if (e->entry == NULL) |
| | | 284 | fatal("%s: Can't allocate memory in %s\n", ProgramName, __func__); |
| | | 285 | e->room *= 2; |
269 | } | | 286 | } |
270 | entry->usable = True; | | 287 | entry->usable = True; |
271 | e->entry[e->used++] = *entry; | | 288 | e->entry[e->used++] = *entry; |
272 | } | | 289 | } |
273 | | | 290 | |
274 | | | | |
275 | static int | | 291 | static int |
276 | CompareEntries(const void *e1, const void *e2) | | 292 | CompareEntries(const void *e1, const void *e2) |
277 | { | | 293 | { |
278 | return strcmp(((const Entry *)e1)->tag, ((const Entry *)e2)->tag); | | 294 | return strcmp(((const Entry *) e1)->tag, ((const Entry *) e2)->tag); |
279 | } | | 295 | } |
280 | | | 296 | |
281 | static void | | 297 | static void |
282 | AppendEntryToBuffer(Buffer *b, Entry *entry) | | 298 | AppendEntryToBuffer(Buffer *b, Entry *entry) |
283 | { | | 299 | { |
284 | AppendToBuffer(b, entry->tag, strlen(entry->tag)); | | 300 | AppendToBuffer(b, entry->tag, strlen(entry->tag)); |
285 | AppendToBuffer(b, ":\t", 2); | | 301 | AppendToBuffer(b, ":\t", 2); |
286 | AppendToBuffer(b, entry->value, strlen(entry->value)); | | 302 | AppendToBuffer(b, entry->value, strlen(entry->value)); |
287 | AppendToBuffer(b, "\n", 1); | | 303 | AppendToBuffer(b, "\n", 1); |
288 | } | | 304 | } |
289 | | | 305 | |
290 | /* | | 306 | /* |
291 | * Return the position of the first unescaped occurrence of dest in string. | | 307 | * Return the position of the first unescaped occurrence of dest in string. |
292 | * If lines is non-null, return the number of newlines skipped over. | | 308 | * If lines is non-null, return the number of newlines skipped over. |
293 | */ | | 309 | */ |
294 | static const char * | | 310 | static const char * |
295 | FindFirst(const char *string, char dest, int *lines) | | 311 | FindFirst(const char *string, char dest, int *lines) |
296 | { | | 312 | { |
297 | if (lines) | | 313 | if (lines) |
298 | *lines = 0; | | 314 | *lines = 0; |
299 | for (;;) { | | 315 | for (;;) { |
300 | if (*string == '\0') | | 316 | if (*string == '\0') |
301 | return NULL; | | 317 | return NULL; |
302 | if (*string == '\\') { | | 318 | if (*string == '\\') { |
303 | if (*++string == '\0') | | 319 | if (*++string == '\0') |
304 | return NULL; | | 320 | return NULL; |
305 | } else if (*string == dest) | | 321 | } |
306 | return string; | | 322 | else if (*string == dest) |
307 | if (*string == '\n' && lines) | | 323 | return string; |
308 | (*lines)++; | | 324 | if (*string == '\n' && lines) |
309 | string++; | | 325 | (*lines)++; |
| | | 326 | string++; |
310 | } | | 327 | } |
311 | } | | 328 | } |
312 | | | 329 | |
313 | static void | | 330 | static void |
314 | GetEntries(Entries *entries, Buffer *buff, int bequiet) | | 331 | GetEntries(Entries *entries, Buffer *buff, int bequiet) |
315 | { | | 332 | { |
316 | const char *line, *colon, *temp, *str; | | 333 | const char *line, *colon, *temp, *str; |
317 | Entry entry; | | 334 | Entry entry; |
318 | register int length; | | 335 | size_t length; |
319 | int lineno = 0; | | 336 | int lineno = 0; |
320 | int lines_skipped; | | 337 | int lines_skipped; |
321 | | | 338 | |
322 | str = buff->buff; | | 339 | str = buff->buff; |
323 | if (!str) return; | | 340 | if (!str) |
324 | for ( ; str < buff->buff + buff->used; | | 341 | return; |
325 | str = line + 1, lineno += lines_skipped) { | | 342 | for (; str < buff->buff + buff->used; |
326 | line = FindFirst(str, '\n', &lines_skipped); | | 343 | str = line + 1, lineno += lines_skipped) { |
327 | lineno++; | | 344 | line = FindFirst(str, '\n', &lines_skipped); |
328 | if (!line) | | 345 | lineno++; |
329 | line = buff->buff + buff->used; | | 346 | if (!line) |
330 | if (*str == '!') | | 347 | line = buff->buff + buff->used; |
331 | continue; | | 348 | if (*str == '!') |
332 | if (*str == '\n') | | 349 | continue; |
333 | continue; | | 350 | if (*str == '\n') |
334 | if (!bequiet && *str == '#') { | | 351 | continue; |
335 | int dummy; | | 352 | if (!bequiet && *str == '#') { |
336 | if (sscanf (str, "# %d", &dummy) == 1 || | | 353 | int dummy; |
337 | sscanf (str, "# line %d", &dummy) == 1) | | 354 | |
338 | lineno = dummy - 1; | | 355 | if (sscanf(str, "# %d", &dummy) == 1 || |
339 | continue; | | 356 | sscanf(str, "# line %d", &dummy) == 1) |
340 | } | | 357 | lineno = dummy - 1; |
341 | for (temp = str; | | 358 | continue; |
342 | *temp && *temp != '\n' && isascii(*temp) && isspace(*temp); | | 359 | } |
343 | temp++) ; | | 360 | for (temp = str; |
344 | if (!*temp || *temp == '\n') continue; | | 361 | *temp && *temp != '\n' && isascii(*temp) && isspace(*temp); |
345 | | | 362 | temp++); |
346 | colon = FindFirst(str, ':', NULL); | | 363 | if (!*temp || *temp == '\n') |
347 | if (!colon || colon > line) { | | 364 | continue; |
348 | if (!bequiet && !quiet) | | 365 | |
349 | fprintf (stderr, | | 366 | colon = FindFirst(str, ':', NULL); |
350 | "%s: colon missing on line %d, ignoring line\n", | | 367 | if (!colon || colon > line) { |
351 | ProgramName, lineno); | | 368 | if (!bequiet && !quiet) |
352 | continue; | | 369 | fprintf(stderr, |
353 | } | | 370 | "%s: colon missing on line %d, ignoring line\n", |
354 | | | 371 | ProgramName, lineno); |
355 | /* strip leading and trailing blanks from name and store result */ | | 372 | continue; |
356 | while (*str == ' ' || *str == '\t') | | 373 | } |
357 | str++; | | 374 | |
358 | length = colon - str; | | 375 | /* strip leading and trailing blanks from name and store result */ |
359 | while (length && (str[length-1] == ' ' || str[length-1] == '\t')) | | 376 | while (*str == ' ' || *str == '\t') |
360 | length--; | | 377 | str++; |
361 | entry.tag = malloc(length + 1); | | 378 | length = colon - str; |
362 | strncpy(entry.tag, str, length); | | 379 | while (length && (str[length - 1] == ' ' || str[length - 1] == '\t')) |
363 | entry.tag[length] = '\0'; | | 380 | length--; |
364 | | | 381 | entry.tag = malloc(length + 1); |
365 | /* strip leading and trailing blanks from value and store result */ | | 382 | strncpy(entry.tag, str, length); |
366 | colon++; | | 383 | entry.tag[length] = '\0'; |
367 | while (*colon == ' ' || *colon == '\t') | | 384 | |
368 | colon++; | | 385 | /* strip leading and trailing blanks from value and store result */ |
369 | length = line - colon; | | 386 | colon++; |
370 | entry.value = malloc(length + 1); | | 387 | while (*colon == ' ' || *colon == '\t') |
371 | strncpy(entry.value, colon, length); | | 388 | colon++; |
372 | entry.value[length] = '\0'; | | 389 | length = line - colon; |
373 | entry.lineno = bequiet ? 0 : lineno; | | 390 | entry.value = malloc(length + 1); |
| | | 391 | strncpy(entry.value, colon, length); |
| | | 392 | entry.value[length] = '\0'; |
| | | 393 | entry.lineno = bequiet ? 0 : lineno; |
374 | | | 394 | |
375 | AddEntry(entries, &entry); | | 395 | AddEntry(entries, &entry); |
376 | } | | 396 | } |
377 | } | | 397 | } |
378 | | | 398 | |
379 | static void | | 399 | static void |
380 | GetEntriesString(Entries *entries, char *str) | | 400 | GetEntriesString(Entries *entries, char *str) |
381 | { | | 401 | { |
382 | Buffer buff; | | 402 | Buffer buff; |
383 | | | 403 | |
384 | if (str && *str) { | | 404 | if (str && *str) { |
385 | buff.buff = str; | | 405 | buff.buff = str; |
386 | buff.used = strlen(str); | | 406 | buff.used = strlen(str); |
387 | GetEntries(entries, &buff, 1); | | 407 | GetEntries(entries, &buff, 1); |
388 | } | | 408 | } |
389 | } | | 409 | } |
390 | | | 410 | |
391 | static void | | 411 | static void |
392 | ReadFile(Buffer *b, FILE *input) | | 412 | ReadFile(Buffer *b, FILE *input) |
393 | { | | 413 | { |
394 | char buf[BUFSIZ + 1]; | | 414 | char buf[BUFSIZ + 1]; |
395 | register int bytes; | | 415 | size_t bytes; |
396 | | | 416 | |
397 | b->used = 0; | | 417 | b->used = 0; |
398 | while (!feof(input) && (bytes = fread(buf, 1, BUFSIZ, input)) > 0) { | | 418 | while (!feof(input) && (bytes = fread(buf, 1, BUFSIZ, input)) > 0) { |
399 | #ifdef WIN32 | | 419 | #ifdef WIN32 |
400 | char *p; | | 420 | char *p; |
401 | buf[bytes] = '\0'; | | 421 | |
402 | for (p = buf; p = strchr(p, '\r'); ) { | | 422 | buf[bytes] = '\0'; |
403 | if (p[-1] == '\\' && p[1] == '\n') { | | 423 | for (p = buf; p = strchr(p, '\r');) { |
404 | bytes -= 3; | | 424 | if (p[-1] == '\\' && p[1] == '\n') { |
405 | strcpy(p - 1, p + 2); | | 425 | bytes -= 3; |
406 | } | | 426 | strcpy(p - 1, p + 2); |
407 | } | | 427 | } |
| | | 428 | } |
408 | #endif | | 429 | #endif |
409 | AppendToBuffer(b, buf, bytes); | | 430 | AppendToBuffer(b, buf, bytes); |
410 | } | | 431 | } |
411 | AppendToBuffer(b, "", 1); | | 432 | AppendToBuffer(b, "", 1); |
412 | } | | 433 | } |
413 | | | 434 | |
414 | static void | | 435 | static void |
415 | AddDef(String *buff, const char *title, const char *value) | | 436 | AddDef(String *buff, const char *title, const char *value) |
416 | { | | 437 | { |
417 | #ifdef PATHETICCPP | | 438 | #ifdef PATHETICCPP |
418 | if (need_real_defines) { | | 439 | if (need_real_defines) { |
419 | addstring(buff, "\n#define "); | | 440 | addstring(buff, "\n#define "); |
420 | addtokstring(buff, title); | | 441 | addtokstring(buff, title); |
421 | if (value && (value[0] != '\0')) { | | 442 | if (value && (value[0] != '\0')) { |
422 | addstring(buff, " "); | | 443 | addstring(buff, " "); |
423 | addstring(buff, value); | | 444 | addstring(buff, value); |
424 | } | | 445 | } |
425 | return; | | 446 | return; |
426 | } | | 447 | } |
427 | #endif | | 448 | #endif |
428 | if (buff->used) { | | 449 | if (buff->used) { |
429 | if (oper == OPSYMBOLS) | | 450 | if (oper == OPSYMBOLS) |
430 | addstring(buff, "\n-D"); | | 451 | addstring(buff, "\n-D"); |
431 | else | | 452 | else |
432 | addstring(buff, " -D"); | | 453 | addstring(buff, " -D"); |
433 | } else | | 454 | } |
434 | addstring(buff, "-D"); | | 455 | else |
| | | 456 | addstring(buff, "-D"); |
435 | addtokstring(buff, title); | | 457 | addtokstring(buff, title); |
436 | if (value && (value[0] != '\0')) { | | 458 | if (value && (value[0] != '\0')) { |
437 | addstring(buff, "="); | | 459 | addstring(buff, "="); |
438 | addescapedstring(buff, value); | | 460 | addescapedstring(buff, value); |
439 | } | | 461 | } |
440 | } | | 462 | } |
441 | | | 463 | |
442 | static void | | 464 | static void |
443 | AddSimpleDef(String *buff, const char *title) | | 465 | AddSimpleDef(String *buff, const char *title) |
444 | { | | 466 | { |
445 | AddDef(buff, title, (char *)NULL); | | 467 | AddDef(buff, title, (char *) NULL); |
446 | } | | 468 | } |
447 | | | 469 | |
448 | static void | | 470 | static void |
449 | AddDefQ(String *buff, const char *title, const char *value) | | 471 | AddDefQ(String *buff, const char *title, const char *value) |
450 | { | | 472 | { |
451 | #ifdef PATHETICCPP | | 473 | #ifdef PATHETICCPP |
452 | if (need_real_defines) | | 474 | if (need_real_defines) |
453 | AddDef(buff, title, value); | | 475 | AddDef(buff, title, value); |
454 | else | | 476 | else |
455 | #endif | | 477 | #endif |
456 | if (value && (value[0] != '\0')) { | | 478 | if (value && (value[0] != '\0')) { |
457 | AddSimpleDef(buff, title); | | 479 | AddSimpleDef(buff, title); |
458 | addstring(buff, "=\""); | | 480 | addstring(buff, "=\""); |
459 | addescapedstring(buff, value); | | 481 | addescapedstring(buff, value); |
460 | addstring(buff, "\""); | | 482 | addstring(buff, "\""); |
461 | } else | | 483 | } |
462 | AddDef(buff, title, NULL); | | 484 | else |
| | | 485 | AddDef(buff, title, NULL); |
463 | } | | 486 | } |
464 | | | 487 | |
465 | static void | | 488 | static void |
466 | AddNum(String *buff, const char *title, int value) | | 489 | AddNum(String *buff, const char *title, int value) |
467 | { | | 490 | { |
468 | char num[20]; | | 491 | char num[20]; |
| | | 492 | |
469 | snprintf(num, sizeof(num), "%d", value); | | 493 | snprintf(num, sizeof(num), "%d", value); |
470 | AddDef(buff, title, num); | | 494 | AddDef(buff, title, num); |
471 | } | | 495 | } |
472 | | | 496 | |
473 | static void | | 497 | static void |
474 | AddDefTok(String *buff, const char *prefix, char *title) | | 498 | AddDefTok(String *buff, const char *prefix, char *title) |
475 | { | | 499 | { |
476 | char name[512]; | | 500 | char name[512]; |
477 | | | 501 | |
478 | snprintf(name, sizeof(name), "%s%s", prefix, title); | | 502 | snprintf(name, sizeof(name), "%s%s", prefix, title); |
479 | AddSimpleDef(buff, name); | | 503 | AddSimpleDef(buff, name); |
480 | } | | 504 | } |
481 | | | 505 | |
482 | static void | | 506 | static void |
483 | AddDefHostname(String *buff, const char *title, const char *value) | | 507 | AddDefHostname(String *buff, const char *title, const char *value) |
484 | { | | 508 | { |
485 | char *s; | | 509 | char *s; |
486 | char name[512]; | | 510 | char name[512]; |
487 | char c; | | 511 | char c; |
488 | | | 512 | |
489 | strncpy (name, value, sizeof(name)-1); | | 513 | strncpy(name, value, sizeof(name) - 1); |
490 | name[sizeof(name)-1] = '\0'; | | 514 | name[sizeof(name) - 1] = '\0'; |
491 | for (s = name; (c = *s); s++) { | | 515 | for (s = name; (c = *s); s++) { |
492 | if (!isalpha(c) && !isdigit(c) && c != '_' && c != '.' && c != ':' && c != '-') | | 516 | if (!isalpha(c) && !isdigit(c) && |
493 | *s = '_'; | | 517 | c != '_' && c != '.' && c != ':' && c != '-') |
| | | 518 | *s = '_'; |
494 | } | | 519 | } |
495 | AddDef(buff, title, name); | | 520 | AddDef(buff, title, name); |
496 | } | | 521 | } |
497 | | | 522 | |
498 | static void | | 523 | static void |
499 | AddUndef(String *buff, const char *title) | | 524 | AddUndef(String *buff, const char *title) |
500 | { | | 525 | { |
501 | #ifdef PATHETICCPP | | 526 | #ifdef PATHETICCPP |
502 | if (need_real_defines) { | | 527 | if (need_real_defines) { |
503 | addstring(buff, "\n#undef "); | | 528 | addstring(buff, "\n#undef "); |
504 | addstring(buff, title); | | 529 | addstring(buff, title); |
505 | return; | | 530 | return; |
506 | } | | 531 | } |
507 | #endif | | 532 | #endif |
508 | if (buff->used) { | | 533 | if (buff->used) { |
509 | if (oper == OPSYMBOLS) | | 534 | if (oper == OPSYMBOLS) |
510 | addstring(buff, "\n-U"); | | 535 | addstring(buff, "\n-U"); |
511 | else | | 536 | else |
512 | addstring(buff, " -U"); | | 537 | addstring(buff, " -U"); |
513 | } else | | 538 | } |
514 | addstring(buff, "-U"); | | 539 | else |
| | | 540 | addstring(buff, "-U"); |
515 | addtokstring(buff, title); | | 541 | addtokstring(buff, title); |
516 | } | | 542 | } |
517 | | | 543 | |
518 | static void | | 544 | static void |
519 | DoCmdDefines(String *buff) | | 545 | DoCmdDefines(String *buff) |
520 | { | | 546 | { |
521 | int i; | | 547 | int i; |
522 | char *arg, *val; | | 548 | char *arg, *val; |
523 | | | 549 | |
524 | for (i = 0; i < num_cmd_defines; i++) { | | 550 | for (i = 0; i < num_cmd_defines; i++) { |
525 | arg = cmd_defines[i]; | | 551 | arg = cmd_defines[i]; |
526 | if (arg[1] == 'D') { | | 552 | if (arg[1] == 'D') { |
527 | val = strchr(arg, '='); | | 553 | val = strchr(arg, '='); |
528 | if (val) { | | 554 | if (val) { |
529 | *val = '\0'; | | 555 | *val = '\0'; |
530 | AddDefQ(buff, arg + 2, val + 1); | | 556 | AddDefQ(buff, arg + 2, val + 1); |
531 | *val = '='; | | 557 | *val = '='; |
532 | } else | | 558 | } |
533 | AddSimpleDef(buff, arg + 2); | | 559 | else |
534 | } else if (arg[1] == 'U') { | | 560 | AddSimpleDef(buff, arg + 2); |
535 | AddUndef(buff, arg + 2); | | 561 | } |
536 | } else if (!strcmp(arg, "-undef") && oper != OPSYMBOLS) { | | 562 | else if (arg[1] == 'U') { |
537 | addstring(buff, " -undef"); | | 563 | AddUndef(buff, arg + 2); |
538 | } | | 564 | } |
| | | 565 | else if (!strcmp(arg, "-undef") && oper != OPSYMBOLS) { |
| | | 566 | addstring(buff, " -undef"); |
| | | 567 | } |
539 | } | | 568 | } |
540 | } | | 569 | } |
541 | | | 570 | |
542 | static int | | 571 | static int |
543 | Resolution(int pixels, int mm) | | 572 | Resolution(int pixels, int mm) |
544 | { | | 573 | { |
545 | if (mm == 0) | | 574 | if (mm == 0) |
546 | return 0; | | 575 | return 0; |
547 | else | | 576 | else |
548 | return ((pixels * 100000 / mm) + 50) / 100; | | 577 | return ((pixels * 100000 / mm) + 50) / 100; |
549 | } | | 578 | } |
550 | | | 579 | |
551 | | | | |
552 | static void | | 580 | static void |
553 | DoDisplayDefines(Display *display, String *defs, char *host) | | 581 | DoDisplayDefines(Display *display, String *defs, char *host) |
554 | { | | 582 | { |
555 | #ifndef MAXHOSTNAMELEN | | 583 | #ifndef MAXHOSTNAMELEN |
556 | #define MAXHOSTNAMELEN 255 | | 584 | #define MAXHOSTNAMELEN 255 |
557 | #endif | | 585 | #endif |
558 | char client[MAXHOSTNAMELEN], server[MAXHOSTNAMELEN], *colon; | | 586 | char client[MAXHOSTNAMELEN], server[MAXHOSTNAMELEN], *colon; |
559 | char **extnames; | | 587 | char **extnames; |
560 | int n; | | 588 | int n; |
561 | | | 589 | |
562 | XmuGetHostname(client, MAXHOSTNAMELEN); | | 590 | XmuGetHostname(client, MAXHOSTNAMELEN); |
563 | strncpy(server, XDisplayName(host), sizeof(server)); | | 591 | strncpy(server, XDisplayName(host), sizeof(server)); |
564 | server[sizeof(server) - 1] = '\0'; | | 592 | server[sizeof(server) - 1] = '\0'; |
565 | /* search for final colon to skip over any embedded colons in IPv6 | | 593 | /* search for final colon to skip over any embedded colons in IPv6 |
566 | numeric address forms */ | | 594 | numeric address forms */ |
567 | colon = strrchr(server, ':'); | | 595 | colon = strrchr(server, ':'); |
568 | n = 0; | | 596 | n = 0; |
569 | if (colon) { | | 597 | if (colon) { |
570 | /* remove extra colon if there are exactly two, since it indicates | | 598 | /* remove extra colon if there are exactly two, since it indicates |
571 | DECnet. Three colons is an IPv6 address ending in :: though. */ | | 599 | DECnet. Three colons is an IPv6 address ending in :: though. */ |
572 | if ((colon > server) && (*(colon-1) == ':') && | | 600 | if ((colon > server) && (*(colon - 1) == ':') && |
573 | ( ((colon - 1) == server) || (*(colon-2) != ':') ) ) { | | 601 | (((colon - 1) == server) || (*(colon - 2) != ':'))) { |
574 | *(colon-1) = ':'; | | 602 | *(colon - 1) = ':'; |
575 | } | | 603 | } |
576 | *colon++ = '\0'; | | 604 | *colon++ = '\0'; |
577 | sscanf(colon, "%d", &n); | | 605 | sscanf(colon, "%d", &n); |
578 | } | | 606 | } |
579 | if (!*server || !strcmp(server, "unix") || !strcmp(server, "localhost")) | | 607 | if (!*server || !strcmp(server, "unix") || !strcmp(server, "localhost")) |
580 | strcpy(server, client); | | 608 | strcpy(server, client); |
581 | AddDefHostname(defs, "HOST", server); /* R3 compatibility */ | | 609 | AddDefHostname(defs, "HOST", server); /* R3 compatibility */ |
582 | AddDefHostname(defs, "SERVERHOST", server); | | 610 | AddDefHostname(defs, "SERVERHOST", server); |
583 | AddDefTok(defs, "SRVR_", server); | | 611 | AddDefTok(defs, "SRVR_", server); |
584 | AddNum(defs, "DISPLAY_NUM", n); | | 612 | AddNum(defs, "DISPLAY_NUM", n); |
585 | AddDefHostname(defs, "CLIENTHOST", client); | | 613 | AddDefHostname(defs, "CLIENTHOST", client); |
586 | AddDefTok(defs, "CLNT_", client); | | 614 | AddDefTok(defs, "CLNT_", client); |
587 | AddNum(defs, "VERSION", ProtocolVersion(display)); | | 615 | AddNum(defs, "VERSION", ProtocolVersion(display)); |
588 | AddNum(defs, "REVISION", ProtocolRevision(display)); | | 616 | AddNum(defs, "REVISION", ProtocolRevision(display)); |
589 | AddDefQ(defs, "VENDOR", ServerVendor(display)); | | 617 | AddDefQ(defs, "VENDOR", ServerVendor(display)); |
590 | AddDefTok(defs, "VNDR_", ServerVendor(display)); | | 618 | AddDefTok(defs, "VNDR_", ServerVendor(display)); |
591 | AddNum(defs, "RELEASE", VendorRelease(display)); | | 619 | AddNum(defs, "RELEASE", VendorRelease(display)); |
592 | AddNum(defs, "NUM_SCREENS", ScreenCount(display)); | | 620 | AddNum(defs, "NUM_SCREENS", ScreenCount(display)); |
593 | extnames = XListExtensions(display, &n); | | 621 | extnames = XListExtensions(display, &n); |
594 | while (--n >= 0) | | 622 | while (--n >= 0) |
595 | AddDefTok(defs, "EXT_", extnames[n]); | | 623 | AddDefTok(defs, "EXT_", extnames[n]); |
596 | XFreeExtensionList(extnames); | | 624 | XFreeExtensionList(extnames); |
597 | } | | 625 | } |
598 | | | 626 | |
599 | static const char *ClassNames[] = { | | 627 | static const char *ClassNames[] = { |
600 | "StaticGray", | | 628 | "StaticGray", |
601 | "GrayScale", | | 629 | "GrayScale", |
602 | "StaticColor", | | 630 | "StaticColor", |
603 | "PseudoColor", | | 631 | "PseudoColor", |
604 | "TrueColor", | | 632 | "TrueColor", |
605 | "DirectColor" | | 633 | "DirectColor" |
606 | }; | | 634 | }; |
607 | | | 635 | |
608 | #define NUM_CLASS_NAMES (int)(sizeof(ClassNames) / sizeof(ClassNames[0])) | | 636 | #define NUM_CLASS_NAMES (int)(sizeof(ClassNames) / sizeof(ClassNames[0])) |
| @@ -613,835 +641,885 @@ DoScreenDefines(Display *display, int sc | | | @@ -613,835 +641,885 @@ DoScreenDefines(Display *display, int sc |
613 | Screen *screen; | | 641 | Screen *screen; |
614 | Visual *visual; | | 642 | Visual *visual; |
615 | XVisualInfo vinfo, *vinfos; | | 643 | XVisualInfo vinfo, *vinfos; |
616 | int nv, i, j; | | 644 | int nv, i, j; |
617 | char name[50]; | | 645 | char name[50]; |
618 | | | 646 | |
619 | screen = ScreenOfDisplay(display, scrno); | | 647 | screen = ScreenOfDisplay(display, scrno); |
620 | visual = DefaultVisualOfScreen(screen); | | 648 | visual = DefaultVisualOfScreen(screen); |
621 | vinfo.screen = scrno; | | 649 | vinfo.screen = scrno; |
622 | vinfos = XGetVisualInfo(display, VisualScreenMask, &vinfo, &nv); | | 650 | vinfos = XGetVisualInfo(display, VisualScreenMask, &vinfo, &nv); |
623 | AddNum(defs, "SCREEN_NUM", scrno); | | 651 | AddNum(defs, "SCREEN_NUM", scrno); |
624 | AddNum(defs, "WIDTH", screen->width); | | 652 | AddNum(defs, "WIDTH", screen->width); |
625 | AddNum(defs, "HEIGHT", screen->height); | | 653 | AddNum(defs, "HEIGHT", screen->height); |
626 | AddNum(defs, "X_RESOLUTION", Resolution(screen->width,screen->mwidth)); | | 654 | AddNum(defs, "X_RESOLUTION", Resolution(screen->width, screen->mwidth)); |
627 | AddNum(defs, "Y_RESOLUTION", Resolution(screen->height,screen->mheight)); | | 655 | AddNum(defs, "Y_RESOLUTION", Resolution(screen->height, screen->mheight)); |
628 | AddNum(defs, "PLANES", DisplayPlanes(display, scrno)); | | 656 | AddNum(defs, "PLANES", DisplayPlanes(display, scrno)); |
629 | AddNum(defs, "BITS_PER_RGB", visual->bits_per_rgb); | | 657 | AddNum(defs, "BITS_PER_RGB", visual->bits_per_rgb); |
630 | if (visual->class >= 0 && visual->class < NUM_CLASS_NAMES) { | | 658 | if (visual->class >= 0 && visual->class < NUM_CLASS_NAMES) { |
631 | AddDefQ(defs, "CLASS", ClassNames[visual->class]); | | 659 | AddDefQ(defs, "CLASS", ClassNames[visual->class]); |
632 | snprintf(name, sizeof(name), "CLASS_%s", ClassNames[visual->class]); | | 660 | snprintf(name, sizeof(name), "CLASS_%s", ClassNames[visual->class]); |
633 | AddNum(defs, name, (int)visual->visualid); | | 661 | AddNum(defs, name, (int) visual->visualid); |
634 | } | | 662 | } |
635 | else { | | 663 | else { |
636 | fprintf(stderr, | | 664 | fprintf(stderr, |
637 | "%s: unknown visual type %d for default visual id 0x%lx\n", | | 665 | "%s: unknown visual type %d for default visual id 0x%lx\n", |
638 | ProgramName, visual->class, visual->visualid); | | 666 | ProgramName, visual->class, visual->visualid); |
639 | } | | 667 | } |
640 | switch(visual->class) { | | 668 | switch (visual->class) { |
641 | case StaticColor: | | 669 | case StaticColor: |
642 | case PseudoColor: | | 670 | case PseudoColor: |
643 | case TrueColor: | | 671 | case TrueColor: |
644 | case DirectColor: | | 672 | case DirectColor: |
645 | AddSimpleDef(defs, "COLOR"); | | 673 | AddSimpleDef(defs, "COLOR"); |
646 | break; | | 674 | break; |
647 | } | | 675 | } |
648 | for (i = 0; i < nv; i++) { | | 676 | for (i = 0; i < nv; i++) { |
649 | for (j = i; --j >= 0; ) { | | 677 | for (j = i; --j >= 0;) { |
650 | if (vinfos[j].class == vinfos[i].class && | | 678 | if (vinfos[j].class == vinfos[i].class && |
651 | vinfos[j].depth == vinfos[i].depth) | | 679 | vinfos[j].depth == vinfos[i].depth) |
652 | break; | | 680 | break; |
653 | } | | 681 | } |
654 | if (j < 0) { | | 682 | if (j < 0) { |
655 | if (vinfos[i].class >= 0 && vinfos[i].class < NUM_CLASS_NAMES) { | | 683 | if (vinfos[i].class >= 0 && vinfos[i].class < NUM_CLASS_NAMES) { |
656 | snprintf(name, sizeof(name), "CLASS_%s_%d", | | 684 | snprintf(name, sizeof(name), "CLASS_%s_%d", |
657 | ClassNames[vinfos[i].class], vinfos[i].depth); | | 685 | ClassNames[vinfos[i].class], vinfos[i].depth); |
658 | AddNum(defs, name, (int)vinfos[i].visualid); | | 686 | AddNum(defs, name, (int) vinfos[i].visualid); |
659 | } | | 687 | } |
660 | else { | | 688 | else { |
661 | fprintf(stderr, | | 689 | fprintf(stderr, |
662 | "%s: unknown visual type %d for visual id 0x%lx\n", | | 690 | "%s: unknown visual type %d for visual id 0x%lx\n", |
663 | ProgramName, vinfos[i].class, vinfos[i].visualid); | | 691 | ProgramName, vinfos[i].class, vinfos[i].visualid); |
664 | } | | 692 | } |
665 | } | | 693 | } |
666 | } | | 694 | } |
667 | XFree((char *)vinfos); | | 695 | XFree(vinfos); |
668 | } | | 696 | } |
669 | | | 697 | |
670 | static Entry * | | 698 | static Entry * |
671 | FindEntry(Entries *db, Buffer *b) | | 699 | FindEntry(Entries *db, Buffer *b) |
672 | { | | 700 | { |
673 | int i; | | 701 | size_t i; |
674 | register Entry *e; | | 702 | register Entry *e; |
675 | Entries phoney; | | 703 | Entries phoney; |
676 | Entry entry; | | 704 | Entry entry; |
677 | | | 705 | |
678 | entry.usable = False; | | 706 | entry.usable = False; |
679 | entry.tag = NULL; | | 707 | entry.tag = NULL; |
680 | entry.value = NULL; | | 708 | entry.value = NULL; |
681 | phoney.used = 0; | | 709 | phoney.used = 0; |
682 | phoney.room = 1; | | 710 | phoney.room = 1; |
683 | phoney.entry = &entry; | | 711 | phoney.entry = &entry; |
684 | GetEntries(&phoney, b, 1); | | 712 | GetEntries(&phoney, b, 1); |
685 | if (phoney.used < 1) | | 713 | if (phoney.used < 1) |
686 | return NULL; | | 714 | return NULL; |
687 | for (i = 0; i < db->used; i++) { | | 715 | for (i = 0; i < db->used; i++) { |
688 | e = &db->entry[i]; | | 716 | e = &db->entry[i]; |
689 | if (!e->usable) | | 717 | if (!e->usable) |
690 | continue; | | 718 | continue; |
691 | if (strcmp(e->tag, entry.tag)) | | 719 | if (strcmp(e->tag, entry.tag)) |
692 | continue; | | 720 | continue; |
693 | e->usable = False; | | 721 | e->usable = False; |
694 | if (strcmp(e->value, entry.value)) | | 722 | if (strcmp(e->value, entry.value)) |
695 | return e; | | 723 | return e; |
696 | return NULL; | | 724 | return NULL; |
697 | } | | 725 | } |
698 | return NULL; | | 726 | return NULL; |
699 | } | | 727 | } |
700 | | | 728 | |
701 | static void | | 729 | static void |
702 | EditFile(Entries *new, FILE *in, FILE *out) | | 730 | EditFile(Entries *new, FILE *in, FILE *out) |
703 | { | | 731 | { |
704 | Buffer b; | | 732 | Buffer b; |
705 | char buff[BUFSIZ]; | | 733 | char buff[BUFSIZ]; |
706 | register Entry *e; | | 734 | register Entry *e; |
707 | register char *c; | | 735 | register char *c; |
708 | int i; | | 736 | size_t i; |
709 | | | 737 | |
710 | InitBuffer(&b); | | 738 | InitBuffer(&b); |
711 | while (in) { | | 739 | while (in) { |
712 | b.used = 0; | | 740 | b.used = 0; |
713 | while (1) { | | 741 | while (1) { |
714 | buff[0] ='\0'; | | 742 | buff[0] = '\0'; |
715 | if (!fgets(buff, BUFSIZ, in)) | | 743 | if (!fgets(buff, BUFSIZ, in)) |
716 | goto cleanup; | | 744 | goto cleanup; |
717 | AppendToBuffer(&b, buff, strlen(buff)); | | 745 | AppendToBuffer(&b, buff, strlen(buff)); |
718 | c = &b.buff[b.used - 1]; | | 746 | c = &b.buff[b.used - 1]; |
719 | if ((*(c--) == '\n') && (b.used == 1 || *c != '\\')) | | 747 | if ((*(c--) == '\n') && (b.used == 1 || *c != '\\')) |
720 | break; | | 748 | break; |
721 | } | | 749 | } |
722 | if ((e = FindEntry(new, &b))) | | 750 | if ((e = FindEntry(new, &b))) |
723 | fprintf(out, "%s:\t%s\n", e->tag, e->value); | | 751 | fprintf(out, "%s:\t%s\n", e->tag, e->value); |
724 | else | | 752 | else |
725 | fwrite(b.buff, 1, b.used, out); | | 753 | fwrite(b.buff, 1, b.used, out); |
726 | } | | 754 | } |
727 | cleanup: | | 755 | cleanup: |
728 | for (i = 0; i < new->used; i++) { | | 756 | for (i = 0; i < new->used; i++) { |
729 | e = &new->entry[i]; | | 757 | e = &new->entry[i]; |
730 | if (e->usable) | | 758 | if (e->usable) |
731 | fprintf(out, "%s:\t%s\n", e->tag, e->value); | | 759 | fprintf(out, "%s:\t%s\n", e->tag, e->value); |
732 | } | | 760 | } |
733 | } | | 761 | } |
734 | | | 762 | |
735 | static void _X_NORETURN | | 763 | static void _X_NORETURN _X_COLD |
736 | Syntax (void) | | 764 | Syntax(const char *errmsg) |
737 | { | | 765 | { |
738 | fprintf (stderr, | | 766 | if (errmsg != NULL) |
739 | "usage: %s [-options ...] [filename]\n\n" | | 767 | fprintf(stderr, "%s: %s\n", ProgramName, errmsg); |
740 | "where options include:\n" | | 768 | |
741 | " -help print this help message\n" | | 769 | fprintf(stderr, |
742 | " -version print the program version\n" | | 770 | "usage: %s [-options ...] [filename]\n\n" |
743 | " -display host:dpy display to use\n" | | 771 | "where options include:\n" |
744 | " -all do all resources [default]\n" | | 772 | " -help print this help message\n" |
745 | " -global do screen-independent resources\n" | | 773 | " -version print the program version\n" |
746 | " -screen do screen-specific resources for one screen\n" | | 774 | " -display host:dpy display to use\n" |
747 | " -screens do screen-specific resources for all screens\n" | | 775 | " -all do all resources [default]\n" |
748 | " -n show but don't do changes\n" | | 776 | " -global do screen-independent resources\n" |
749 | " -cpp filename preprocessor to use [%s]\n" | | 777 | " -screen do screen-specific resources for one screen\n" |
750 | " -nocpp do not use a preprocessor\n" | | 778 | " -screens do screen-specific resources for all screens\n" |
751 | " -query query resources\n" | | 779 | " -n show but don't do changes\n" |
752 | " -load load resources from file [default]\n" | | 780 | " -cpp filename preprocessor to use [%s]\n" |
753 | " -override add in resources from file\n" | | 781 | " -nocpp do not use a preprocessor\n" |
754 | " -merge merge resources from file & sort\n" | | 782 | " -query query resources\n" |
755 | " -edit filename edit resources into file\n" | | 783 | " -load load resources from file [default]\n" |
756 | " -backup string backup suffix for -edit [%s]\n" | | 784 | " -override add in resources from file\n" |
757 | " -symbols show preprocessor symbols\n" | | 785 | " -merge merge resources from file & sort\n" |
758 | " -remove remove resources\n" | | 786 | " -edit filename edit resources into file\n" |
759 | " -retain avoid server reset (avoid using this)\n" | | 787 | " -backup string backup suffix for -edit [%s]\n" |
760 | " -quiet don't warn about duplicates\n" | | 788 | " -symbols show preprocessor symbols\n" |
761 | " -Dname[=value], -Uname, -Idirectory passed to preprocessor\n" | | 789 | " -remove remove resources\n" |
762 | "\n" | | 790 | " -retain avoid server reset (avoid using this)\n" |
763 | "A - or no input filename represents stdin.\n", | | 791 | " -quiet don't warn about duplicates\n" |
764 | ProgramName, cpp_program ? cpp_program : "", BACKUP_SUFFIX); | | 792 | " -Dname[=value], -Uname, -Idirectory passed to preprocessor\n" |
765 | exit (1); | | 793 | "\n" |
| | | 794 | "A - or no input filename represents stdin.\n", |
| | | 795 | ProgramName, cpp_program ? cpp_program : "", BACKUP_SUFFIX); |
| | | 796 | exit(1); |
766 | } | | 797 | } |
767 | | | 798 | |
768 | /* | | 799 | /* |
769 | * The following is a hack until XrmParseCommand is ready. It determines | | 800 | * The following is a hack until XrmParseCommand is ready. It determines |
770 | * whether or not the given string is an abbreviation of the arg. | | 801 | * whether or not the given string is an abbreviation of the arg. |
771 | */ | | 802 | */ |
772 | | | 803 | |
773 | static Bool | | 804 | static Bool |
774 | isabbreviation(const char *arg, const char *s, int minslen) | | 805 | isabbreviation(const char *arg, const char *s, size_t minslen) |
775 | { | | 806 | { |
776 | int arglen; | | 807 | size_t arglen; |
777 | int slen; | | 808 | size_t slen; |
778 | | | 809 | |
779 | /* exact match */ | | 810 | /* exact match */ |
780 | if (!strcmp (arg, s)) return (True); | | 811 | if (!strcmp(arg, s)) |
| | | 812 | return (True); |
781 | | | 813 | |
782 | arglen = strlen (arg); | | 814 | arglen = strlen(arg); |
783 | slen = strlen (s); | | 815 | slen = strlen(s); |
784 | | | 816 | |
785 | /* too long or too short */ | | 817 | /* too long or too short */ |
786 | if (slen >= arglen || slen < minslen) return (False); | | 818 | if (slen >= arglen || slen < minslen) |
| | | 819 | return (False); |
787 | | | 820 | |
788 | /* abbreviation */ | | 821 | /* abbreviation */ |
789 | if (strncmp (arg, s, slen) == 0) return (True); | | 822 | if (strncmp(arg, s, slen) == 0) |
| | | 823 | return (True); |
790 | | | 824 | |
791 | /* bad */ | | 825 | /* bad */ |
792 | return (False); | | 826 | return (False); |
793 | } | | 827 | } |
794 | | | 828 | |
795 | static void | | 829 | static void |
796 | addstring(String *arg, const char *s) | | 830 | addstring(String *arg, const char *s) |
797 | { | | 831 | { |
798 | if(arg->used + strlen(s) + 1 >= arg->room) { | | 832 | if (arg->used + strlen(s) + 1 >= arg->room) { |
799 | if(arg->val) | | 833 | if (arg->val) |
800 | arg->val = realloc(arg->val, arg->room + CHUNK_SIZE); | | 834 | arg->val = realloc(arg->val, arg->room + CHUNK_SIZE); |
801 | else | | 835 | else |
802 | arg->val = malloc(arg->room + CHUNK_SIZE); | | 836 | arg->val = malloc(arg->room + CHUNK_SIZE); |
803 | if(arg->val == NULL) | | 837 | if (arg->val == NULL) |
804 | fatal("%s: Not enough memory\n", ProgramName); | | 838 | fatal("%s: Not enough memory\n", ProgramName); |
805 | arg->room += CHUNK_SIZE; | | 839 | arg->room += CHUNK_SIZE; |
806 | } | | 840 | } |
807 | if(arg->used) | | 841 | if (arg->used) |
808 | strcat(arg->val, s); | | 842 | strcat(arg->val, s); |
809 | else | | 843 | else |
810 | strcpy(arg->val, s); | | 844 | strcpy(arg->val, s); |
811 | arg->used += strlen(s); | | 845 | arg->used += strlen(s); |
812 | } | | 846 | } |
813 | | | 847 | |
814 | static void | | 848 | static void |
815 | addescapedstring(String *arg, const char *s) | | 849 | addescapedstring(String *arg, const char *s) |
816 | { | | 850 | { |
817 | char copy[512], *c; | | 851 | char copy[512], *c; |
818 | | | 852 | |
819 | for (c = copy; *s && c < ©[sizeof(copy)-1]; s++) { | | 853 | for (c = copy; *s && c < ©[sizeof(copy) - 1]; s++) { |
820 | switch (*s) { | | 854 | switch (*s) { |
821 | case '"': case '\'': case '`': | | 855 | case '"': |
822 | case '$': case '\\': | | 856 | case '\'': |
823 | *c++ = '_'; | | 857 | case '`': |
824 | break; | | 858 | case '$': |
825 | default: | | 859 | case '\\': |
826 | *c++ = *s; | | 860 | *c++ = '_'; |
827 | } | | 861 | break; |
| | | 862 | default: |
| | | 863 | *c++ = *s; |
| | | 864 | } |
828 | } | | 865 | } |
829 | *c = 0; | | 866 | *c = 0; |
830 | addstring (arg, copy); | | 867 | addstring(arg, copy); |
831 | } | | 868 | } |
832 | | | 869 | |
833 | static void | | 870 | static void |
834 | addtokstring(String *arg, const char *s) | | 871 | addtokstring(String *arg, const char *s) |
835 | { | | 872 | { |
836 | char copy[512], *c; | | 873 | char copy[512], *c; |
837 | | | 874 | |
838 | for (c = copy; *s && c < ©[sizeof(copy)-1]; s++) { | | 875 | for (c = copy; *s && c < ©[sizeof(copy) - 1]; s++) { |
839 | if (!isalpha(*s) && !isdigit(*s) && *s != '_') | | 876 | if (!isalpha(*s) && !isdigit(*s) && *s != '_') |
840 | *c++ = '_'; | | 877 | *c++ = '_'; |
841 | else | | 878 | else |
842 | *c++ = *s; | | 879 | *c++ = *s; |
843 | } | | 880 | } |
844 | *c = 0; | | 881 | *c = 0; |
845 | addstring (arg, copy); | | 882 | addstring(arg, copy); |
846 | } | | 883 | } |
847 | | | 884 | |
848 | | | 885 | |
849 | int | | 886 | int |
850 | main(int argc, char *argv[]) | | 887 | main(int argc, char *argv[]) |
851 | { | | 888 | { |
852 | int i; | | 889 | int i; |
853 | char *displayname = NULL; | | 890 | char *displayname = NULL; |
854 | int whichResources = RALL; | | 891 | int whichResources = RALL; |
855 | int retainProp = 0; | | 892 | int retainProp = 0; |
856 | FILE *fp = NULL; | | 893 | FILE *fp = NULL; |
857 | Bool need_newline; | | 894 | Bool need_newline; |
858 | | | 895 | |
859 | ProgramName = argv[0]; | | 896 | ProgramName = argv[0]; |
860 | | | 897 | |
861 | defines.room = defines.used = includes.room = includes.used = 0; | | 898 | defines.room = defines.used = includes.room = includes.used = 0; |
862 | | | 899 | |
863 | /* initialize the includes String struct */ | | 900 | /* initialize the includes String struct */ |
864 | addstring(&includes, ""); | | 901 | addstring(&includes, ""); |
865 | | | 902 | |
866 | /* Pick the default cpp to use. This needs to be done before | | 903 | /* Pick the default cpp to use. This needs to be done before |
867 | * we parse the command line in order to honor -nocpp which sets | | 904 | * we parse the command line in order to honor -nocpp which sets |
868 | * it back to NULL. | | 905 | * it back to NULL. |
869 | */ | | 906 | */ |
870 | if (cpp_program == NULL) { | | 907 | if (cpp_program == NULL) { |
871 | int number_of_elements | | 908 | int number_of_elements |
872 | = (sizeof cpp_locations) / (sizeof cpp_locations[0]); | | 909 | = (sizeof cpp_locations) / (sizeof cpp_locations[0]); |
873 | int j; | | 910 | int j; |
874 | | | 911 | |
875 | for (j = 0; j < number_of_elements; j++) { | | 912 | for (j = 0; j < number_of_elements; j++) { |
876 | char *end, *dup; | | 913 | char *end, *dup; |
877 | /* cut off arguments */ | | 914 | |
878 | dup = strdup(cpp_locations[j]); | | 915 | /* cut off arguments */ |
879 | end = strchr(dup,' '); | | 916 | dup = strdup(cpp_locations[j]); |
880 | if (end) | | 917 | end = strchr(dup, ' '); |
881 | *end = '\0'; | | 918 | if (end) |
882 | if (access(dup, X_OK) == 0) { | | 919 | *end = '\0'; |
883 | cpp_program = cpp_locations[j]; | | 920 | if (access(dup, X_OK) == 0) { |
884 | free(dup); | | 921 | cpp_program = cpp_locations[j]; |
885 | break; | | 922 | free(dup); |
886 | } | | 923 | break; |
887 | free(dup); | | 924 | } |
888 | } | | 925 | free(dup); |
| | | 926 | } |
889 | } | | 927 | } |
890 | | | 928 | |
891 | /* needs to be replaced with XrmParseCommand */ | | 929 | /* needs to be replaced with XrmParseCommand */ |
892 | | | 930 | |
893 | for (i = 1; i < argc; i++) { | | 931 | for (i = 1; i < argc; i++) { |
894 | char *arg = argv[i]; | | 932 | char *arg = argv[i]; |
895 | | | 933 | |
896 | if (arg[0] == '-') { | | 934 | if (arg[0] == '-') { |
897 | if (arg[1] == '\0') { | | 935 | if (arg[1] == '\0') { |
898 | filename = NULL; | | 936 | filename = NULL; |
899 | continue; | | 937 | continue; |
900 | } else if (isabbreviation ("-help", arg, 2)) { | | 938 | } |
901 | Syntax (); | | 939 | else if (isabbreviation("-help", arg, 2)) { |
902 | /* doesn't return */ | | 940 | Syntax(NULL); |
903 | } else if (isabbreviation ("-version", arg, 2)) { | | 941 | /* doesn't return */ |
904 | printf("%s\n", PACKAGE_STRING); | | 942 | } |
905 | exit(0); | | 943 | else if (isabbreviation("-version", arg, 2)) { |
906 | } else if (isabbreviation ("-display", arg, 2)) { | | 944 | printf("%s\n", PACKAGE_STRING); |
907 | if (++i >= argc) Syntax (); | | 945 | exit(0); |
908 | displayname = argv[i]; | | 946 | } |
909 | continue; | | 947 | else if (isabbreviation("-display", arg, 2)) { |
910 | } else if (isabbreviation ("-geometry", arg, 3)) { | | 948 | if (++i >= argc) |
911 | if (++i >= argc) Syntax (); | | 949 | Syntax("-display requires an argument"); |
912 | /* ignore geometry */ | | 950 | displayname = argv[i]; |
913 | continue; | | 951 | continue; |
914 | } else if (isabbreviation ("-cpp", arg, 2)) { | | 952 | } |
915 | if (++i >= argc) Syntax (); | | 953 | else if (isabbreviation("-geometry", arg, 3)) { |
916 | cpp_program = argv[i]; | | 954 | if (++i >= argc) |
917 | continue; | | 955 | Syntax("-geometry requires an argument"); |
918 | } else if (!strcmp ("-n", arg)) { | | 956 | /* ignore geometry */ |
919 | dont_execute = True; | | 957 | continue; |
920 | continue; | | 958 | } |
921 | } else if (isabbreviation ("-nocpp", arg, 3)) { | | 959 | else if (isabbreviation("-cpp", arg, 2)) { |
922 | cpp_program = NULL; | | 960 | if (++i >= argc) |
923 | continue; | | 961 | Syntax("-cpp requires an argument"); |
924 | } else if (isabbreviation ("-query", arg, 2)) { | | 962 | cpp_program = argv[i]; |
925 | oper = OPQUERY; | | 963 | continue; |
926 | continue; | | 964 | } |
927 | } else if (isabbreviation ("-load", arg, 2)) { | | 965 | else if (!strcmp("-n", arg)) { |
928 | oper = OPLOAD; | | 966 | dont_execute = True; |
929 | continue; | | 967 | continue; |
930 | } else if (isabbreviation ("-merge", arg, 2)) { | | 968 | } |
931 | oper = OPMERGE; | | 969 | else if (isabbreviation("-nocpp", arg, 3)) { |
932 | continue; | | 970 | cpp_program = NULL; |
933 | } else if (isabbreviation ("-override", arg, 2)) { | | 971 | continue; |
934 | oper = OPOVERRIDE; | | 972 | } |
935 | continue; | | 973 | else if (isabbreviation("-query", arg, 2)) { |
936 | } else if (isabbreviation ("-symbols", arg, 3)) { | | 974 | oper = OPQUERY; |
937 | oper = OPSYMBOLS; | | 975 | continue; |
938 | continue; | | 976 | } |
939 | } else if (isabbreviation ("-remove", arg, 4)) { | | 977 | else if (isabbreviation("-load", arg, 2)) { |
940 | oper = OPREMOVE; | | 978 | oper = OPLOAD; |
941 | continue; | | 979 | continue; |
942 | } else if (isabbreviation ("-edit", arg, 2)) { | | 980 | } |
943 | if (++i >= argc) Syntax (); | | 981 | else if (isabbreviation("-merge", arg, 2)) { |
944 | oper = OPEDIT; | | 982 | oper = OPMERGE; |
945 | editFile = argv[i]; | | 983 | continue; |
946 | continue; | | 984 | } |
947 | } else if (isabbreviation ("-backup", arg, 2)) { | | 985 | else if (isabbreviation("-override", arg, 2)) { |
948 | if (++i >= argc) Syntax (); | | 986 | oper = OPOVERRIDE; |
949 | backup_suffix = argv[i]; | | 987 | continue; |
950 | continue; | | 988 | } |
951 | } else if (isabbreviation ("-all", arg, 2)) { | | 989 | else if (isabbreviation("-symbols", arg, 3)) { |
952 | whichResources = RALL; | | 990 | oper = OPSYMBOLS; |
953 | continue; | | 991 | continue; |
954 | } else if (isabbreviation ("-global", arg, 3)) { | | 992 | } |
955 | whichResources = RGLOBAL; | | 993 | else if (isabbreviation("-remove", arg, 4)) { |
956 | continue; | | 994 | oper = OPREMOVE; |
957 | } else if (isabbreviation ("-screen", arg, 3)) { | | 995 | continue; |
958 | whichResources = RSCREEN; | | 996 | } |
959 | continue; | | 997 | else if (isabbreviation("-edit", arg, 2)) { |
960 | } else if (!strcmp ("-screens", arg)) { | | 998 | if (++i >= argc) |
961 | whichResources = RSCREENS; | | 999 | Syntax("-edit requires an argument"); |
962 | continue; | | 1000 | oper = OPEDIT; |
963 | } else if (isabbreviation ("-retain", arg, 4)) { | | 1001 | editFile = argv[i]; |
964 | retainProp = 1; | | 1002 | continue; |
965 | continue; | | 1003 | } |
966 | } else if (isabbreviation ("-quiet", arg, 2)) { | | 1004 | else if (isabbreviation("-backup", arg, 2)) { |
967 | quiet = True; | | 1005 | if (++i >= argc) |
968 | continue; | | 1006 | Syntax("-backup requires an argument"); |
969 | } else if (arg[1] == 'I') { | | 1007 | backup_suffix = argv[i]; |
970 | addstring(&includes, " "); | | 1008 | continue; |
971 | addescapedstring(&includes, arg); | | 1009 | } |
972 | continue; | | 1010 | else if (isabbreviation("-all", arg, 2)) { |
973 | } else if (arg[1] == 'U' || arg[1] == 'D') { | | 1011 | whichResources = RALL; |
974 | if (num_cmd_defines < MAX_CMD_DEFINES) { | | 1012 | continue; |
975 | cmd_defines[num_cmd_defines++] = arg; | | 1013 | } |
976 | } else { | | 1014 | else if (isabbreviation("-global", arg, 3)) { |
977 | fatal("%s: Too many -U/-D arguments\n", ProgramName); | | 1015 | whichResources = RGLOBAL; |
978 | } | | 1016 | continue; |
979 | continue; | | 1017 | } |
980 | } else if (!strcmp ("-undef", arg)) { | | 1018 | else if (isabbreviation("-screen", arg, 3)) { |
981 | if (num_cmd_defines < MAX_CMD_DEFINES) { | | 1019 | whichResources = RSCREEN; |
982 | cmd_defines[num_cmd_defines++] = "-undef"; | | 1020 | continue; |
983 | } else { | | 1021 | } |
984 | fatal("%s: Too many cpp arguments\n", ProgramName); | | 1022 | else if (!strcmp("-screens", arg)) { |
985 | } | | 1023 | whichResources = RSCREENS; |
986 | continue; | | 1024 | continue; |
987 | } | | 1025 | } |
988 | Syntax (); | | 1026 | else if (isabbreviation("-retain", arg, 4)) { |
989 | } else if (arg[0] == '=') | | 1027 | retainProp = 1; |
990 | continue; | | 1028 | continue; |
991 | else | | 1029 | } |
992 | filename = arg; | | 1030 | else if (isabbreviation("-quiet", arg, 2)) { |
993 | } /* end for */ | | 1031 | quiet = True; |
| | | 1032 | continue; |
| | | 1033 | } |
| | | 1034 | else if (arg[1] == 'I') { |
| | | 1035 | addstring(&includes, " "); |
| | | 1036 | addescapedstring(&includes, arg); |
| | | 1037 | continue; |
| | | 1038 | } |
| | | 1039 | else if (arg[1] == 'U' || arg[1] == 'D') { |
| | | 1040 | if (num_cmd_defines < MAX_CMD_DEFINES) { |
| | | 1041 | cmd_defines[num_cmd_defines++] = arg; |
| | | 1042 | } |
| | | 1043 | else { |
| | | 1044 | fatal("%s: Too many -U/-D arguments\n", ProgramName); |
| | | 1045 | } |
| | | 1046 | continue; |
| | | 1047 | } |
| | | 1048 | else if (!strcmp("-undef", arg)) { |
| | | 1049 | if (num_cmd_defines < MAX_CMD_DEFINES) { |
| | | 1050 | cmd_defines[num_cmd_defines++] = "-undef"; |
| | | 1051 | } |
| | | 1052 | else { |
| | | 1053 | fatal("%s: Too many cpp arguments\n", ProgramName); |
| | | 1054 | } |
| | | 1055 | continue; |
| | | 1056 | } |
| | | 1057 | fprintf(stderr, "%s: unrecognized argument '%s'\n", |
| | | 1058 | ProgramName, arg); |
| | | 1059 | Syntax(NULL); |
| | | 1060 | } |
| | | 1061 | else if (arg[0] == '=') |
| | | 1062 | continue; |
| | | 1063 | else |
| | | 1064 | filename = arg; |
| | | 1065 | } /* end for */ |
994 | | | 1066 | |
995 | #ifndef WIN32 | | 1067 | #ifndef WIN32 |
996 | while ((i = open("/dev/null", O_RDONLY)) < 3) | | 1068 | while ((i = open("/dev/null", O_RDONLY)) < 3) |
997 | ; /* make sure later freopen won't clobber things */ | | 1069 | ; /* make sure later freopen won't clobber things */ |
998 | (void) close(i); | | 1070 | (void) close(i); |
999 | #endif | | 1071 | #endif |
1000 | /* Open display */ | | 1072 | /* Open display */ |
1001 | if (!(dpy = XOpenDisplay (displayname))) | | 1073 | if (!(dpy = XOpenDisplay(displayname))) |
1002 | fatal("%s: Can't open display '%s'\n", ProgramName, | | 1074 | fatal("%s: Can't open display '%s'\n", ProgramName, |
1003 | XDisplayName (displayname)); | | 1075 | XDisplayName(displayname)); |
1004 | | | 1076 | |
1005 | if (whichResources == RALL && ScreenCount(dpy) == 1) | | 1077 | if (whichResources == RALL && ScreenCount(dpy) == 1) |
1006 | whichResources = RGLOBAL; | | 1078 | whichResources = RGLOBAL; |
1007 | | | 1079 | |
1008 | #ifdef PATHETICCPP | | 1080 | #ifdef PATHETICCPP |
1009 | if (cpp_program && | | 1081 | if (cpp_program && |
1010 | (oper == OPLOAD || oper == OPMERGE || oper == OPOVERRIDE)) { | | 1082 | (oper == OPLOAD || oper == OPMERGE || oper == OPOVERRIDE)) { |
1011 | need_real_defines = True; | | 1083 | need_real_defines = True; |
1012 | #ifdef WIN32 | | 1084 | #ifdef WIN32 |
1013 | strcpy(tmpname2, "xrdbD_XXXXXX"); | | 1085 | strcpy(tmpname2, "xrdbD_XXXXXX"); |
1014 | strcpy(tmpname3, "\\temp\\xrdbD_XXXXXX"); | | 1086 | strcpy(tmpname3, "\\temp\\xrdbD_XXXXXX"); |
1015 | #else | | | |
1016 | #ifdef __UNIXOS2__ | | | |
1017 | { char *tmpdir=getenv("TMP"); | | | |
1018 | if (!tmpdir) tmpdir="/"; | | | |
1019 | sprintf(tmpname2, "%s/xrdbD_XXXXXX",tmpdir); | | | |
1020 | } | | | |
1021 | #else | | 1087 | #else |
1022 | strcpy(tmpname2, "/tmp/xrdbD_XXXXXX"); | | 1088 | strcpy(tmpname2, "/tmp/xrdbD_XXXXXX"); |
1023 | #endif | | | |
1024 | #endif | | 1089 | #endif |
1025 | (void) mktemp(tmpname2); | | 1090 | (void) mktemp(tmpname2); |
1026 | } | | 1091 | } |
1027 | #endif | | 1092 | #endif |
1028 | | | 1093 | |
1029 | if (!filename && | | 1094 | if (!filename && |
1030 | #ifdef PATHETICCPP | | 1095 | #ifdef PATHETICCPP |
1031 | need_real_defines | | 1096 | need_real_defines |
1032 | #else | | 1097 | #else |
1033 | (oper == OPLOAD || oper == OPMERGE || oper == OPOVERRIDE) && | | 1098 | (oper == OPLOAD || oper == OPMERGE || oper == OPOVERRIDE) && |
1034 | (whichResources == RALL || whichResources == RSCREENS) | | 1099 | (whichResources == RALL || whichResources == RSCREENS) |
1035 | #endif | | 1100 | #endif |
1036 | ) { | | 1101 | ) { |
1037 | char inputbuf[1024]; | | 1102 | char inputbuf[1024]; |
| | | 1103 | |
1038 | #ifdef WIN32 | | 1104 | #ifdef WIN32 |
1039 | strcpy(tmpname, "\\temp\\xrdb_XXXXXX"); | | 1105 | strcpy(tmpname, "\\temp\\xrdb_XXXXXX"); |
1040 | #else | | | |
1041 | #ifdef __UNIXOS2__ | | | |
1042 | { char *tmpdir=getenv("TMP"); | | | |
1043 | if (!tmpdir) tmpdir="/"; | | | |
1044 | sprintf(tmpname, "%s/xrdb_XXXXXX",tmpdir); | | | |
1045 | } | | | |
1046 | #else | | 1106 | #else |
1047 | strcpy(tmpname, "/tmp/xrdb_XXXXXX"); | | 1107 | strcpy(tmpname, "/tmp/xrdb_XXXXXX"); |
1048 | #endif | | | |
1049 | #endif | | 1108 | #endif |
1050 | #ifndef HAVE_MKSTEMP | | 1109 | #ifndef HAVE_MKSTEMP |
1051 | (void) mktemp(tmpname); | | 1110 | (void) mktemp(tmpname); |
1052 | filename = tmpname; | | 1111 | filename = tmpname; |
1053 | fp = fopen(filename, "w"); | | 1112 | fp = fopen(filename, "w"); |
1054 | #else | | 1113 | #else |
1055 | { | | 1114 | { |
1056 | int fd = mkstemp(tmpname); | | 1115 | int fd = mkstemp(tmpname); |
1057 | filename = tmpname; | | 1116 | |
1058 | fp = fdopen(fd, "w"); | | 1117 | filename = tmpname; |
1059 | } | | 1118 | fp = fdopen(fd, "w"); |
1060 | #endif /* MKSTEMP */ | | 1119 | } |
1061 | if (!fp) | | 1120 | #endif /* MKSTEMP */ |
1062 | fatal("%s: Failed to open temp file: %s\n", ProgramName, | | 1121 | if (!fp) |
1063 | filename); | | 1122 | fatal("%s: Failed to open temp file: %s\n", ProgramName, filename); |
1064 | while (fgets(inputbuf, sizeof(inputbuf), stdin) != NULL) | | 1123 | while (fgets(inputbuf, sizeof(inputbuf), stdin) != NULL) |
1065 | fputs(inputbuf, fp); | | 1124 | fputs(inputbuf, fp); |
1066 | fclose(fp); | | 1125 | fclose(fp); |
1067 | } | | 1126 | } |
1068 | | | 1127 | |
1069 | DoDisplayDefines(dpy, &defines, displayname); | | 1128 | DoDisplayDefines(dpy, &defines, displayname); |
1070 | defines_base = defines.used; | | 1129 | defines_base = defines.used; |
1071 | need_newline = (oper == OPQUERY || oper == OPSYMBOLS || | | 1130 | need_newline = (oper == OPQUERY || oper == OPSYMBOLS || |
1072 | (dont_execute && oper != OPREMOVE)); | | 1131 | (dont_execute && oper != OPREMOVE)); |
1073 | InitBuffer(&buffer); | | 1132 | InitBuffer(&buffer); |
1074 | if (whichResources == RGLOBAL) | | 1133 | if (whichResources == RGLOBAL) |
1075 | Process(DefaultScreen(dpy), False, True); | | 1134 | Process(DefaultScreen(dpy), False, True); |
1076 | else if (whichResources == RSCREEN) | | 1135 | else if (whichResources == RSCREEN) |
1077 | Process(DefaultScreen(dpy), True, True); | | 1136 | Process(DefaultScreen(dpy), True, True); |
1078 | else if (whichResources == RSCREENS || | | 1137 | else if (whichResources == RSCREENS || |
1079 | (oper != OPLOAD && oper != OPMERGE && oper != OPOVERRIDE)) { | | 1138 | (oper != OPLOAD && oper != OPMERGE && oper != OPOVERRIDE)) { |
1080 | if (whichResources == RALL && oper != OPSYMBOLS) { | | 1139 | if (whichResources == RALL && oper != OPSYMBOLS) { |
1081 | if (need_newline) | | 1140 | if (need_newline) |
1082 | printf("! screen-independent resources\n"); | | 1141 | printf("! screen-independent resources\n"); |
1083 | Process(0, False, True); | | 1142 | Process(0, False, True); |
1084 | if (need_newline) | | 1143 | if (need_newline) |
1085 | printf("\n"); | | 1144 | printf("\n"); |
1086 | } | | 1145 | } |
1087 | for (i = 0; i < ScreenCount(dpy); i++) { | | 1146 | for (i = 0; i < ScreenCount(dpy); i++) { |
1088 | if (need_newline) { | | 1147 | if (need_newline) { |
1089 | if (oper == OPSYMBOLS) | | 1148 | if (oper == OPSYMBOLS) |
1090 | printf("# screen %d symbols\n", i); | | 1149 | printf("# screen %d symbols\n", i); |
1091 | else { | | 1150 | else { |
1092 | printf("! screen %d resources\n", i); | | 1151 | printf("! screen %d resources\n", i); |
1093 | printf("#if SCREEN_NUM == %d\n", i); | | 1152 | printf("#if SCREEN_NUM == %d\n", i); |
1094 | } | | 1153 | } |
1095 | } | | 1154 | } |
1096 | Process(i, True, True); | | 1155 | Process(i, True, True); |
1097 | if (need_newline) { | | 1156 | if (need_newline) { |
1098 | if (oper != OPSYMBOLS) | | 1157 | if (oper != OPSYMBOLS) |
1099 | printf("#endif\n"); | | 1158 | printf("#endif\n"); |
1100 | if (i+1 != ScreenCount(dpy)) | | 1159 | if (i + 1 != ScreenCount(dpy)) |
1101 | printf("\n"); | | 1160 | printf("\n"); |
1102 | } | | 1161 | } |
1103 | } | | 1162 | } |
1104 | } | | 1163 | } |
1105 | else { | | 1164 | else { |
1106 | Entries *dbs; | | 1165 | Entries *dbs; |
1107 | | | 1166 | |
1108 | dbs = malloc(ScreenCount(dpy) * sizeof(Entries)); | | 1167 | dbs = mallocarray(ScreenCount(dpy), sizeof(Entries)); |
1109 | for (i = 0; i < ScreenCount(dpy); i++) { | | 1168 | if (dbs == NULL) |
1110 | Process(i, True, False); | | 1169 | fatal("%s: Can't allocate memory in %s\n", ProgramName, __func__); |
1111 | dbs[i] = newDB; | | 1170 | for (i = 0; i < ScreenCount(dpy); i++) { |
1112 | } | | 1171 | Process(i, True, False); |
1113 | InitEntries(&newDB); | | 1172 | dbs[i] = newDB; |
1114 | if (oper == OPMERGE || oper == OPOVERRIDE) | | 1173 | } |
1115 | GetEntriesString(&newDB, XResourceManagerString(dpy)); | | 1174 | InitEntries(&newDB); |
1116 | ShuffleEntries(&newDB, dbs, ScreenCount(dpy)); | | 1175 | if (oper == OPMERGE || oper == OPOVERRIDE) |
1117 | if (need_newline) | | 1176 | GetEntriesString(&newDB, XResourceManagerString(dpy)); |
1118 | printf("! screen-independent resources\n"); | | 1177 | ShuffleEntries(&newDB, dbs, (unsigned) ScreenCount(dpy)); |
1119 | ReProcess(0, False); | | 1178 | if (need_newline) |
1120 | if (need_newline) | | 1179 | printf("! screen-independent resources\n"); |
1121 | printf("\n"); | | 1180 | ReProcess(0, False); |
1122 | for (i = 0; i < ScreenCount(dpy); i++) { | | 1181 | if (need_newline) |
1123 | newDB = dbs[i]; | | 1182 | printf("\n"); |
1124 | if (need_newline) { | | 1183 | for (i = 0; i < ScreenCount(dpy); i++) { |
1125 | printf("! screen %d resources\n", i); | | 1184 | newDB = dbs[i]; |
1126 | printf("#if SCREEN_NUM == %d\n", i); | | 1185 | if (need_newline) { |
1127 | } | | 1186 | printf("! screen %d resources\n", i); |
1128 | ReProcess(i, True); | | 1187 | printf("#if SCREEN_NUM == %d\n", i); |
1129 | if (need_newline) { | | 1188 | } |
1130 | printf("#endif\n"); | | 1189 | ReProcess(i, True); |
1131 | if (i+1 != ScreenCount(dpy)) | | 1190 | if (need_newline) { |
1132 | printf("\n"); | | 1191 | printf("#endif\n"); |
1133 | } | | 1192 | if (i + 1 != ScreenCount(dpy)) |
1134 | } | | 1193 | printf("\n"); |
| | | 1194 | } |
| | | 1195 | } |
1135 | } | | 1196 | } |
1136 | | | 1197 | |
1137 | if (fp) | | 1198 | if (fp) |
1138 | unlink(filename); | | 1199 | unlink(filename); |
1139 | if (retainProp) | | 1200 | if (retainProp) |
1140 | XSetCloseDownMode(dpy, RetainPermanent); | | 1201 | XSetCloseDownMode(dpy, RetainPermanent); |
1141 | XCloseDisplay(dpy); | | 1202 | XCloseDisplay(dpy); |
1142 | exit (0); | | 1203 | exit(0); |
1143 | } | | 1204 | } |
1144 | | | 1205 | |
1145 | | | 1206 | |
1146 | static void | | 1207 | static void |
1147 | FormatEntries(Buffer *b, Entries *entries) | | 1208 | FormatEntries(Buffer *b, Entries *entries) |
1148 | { | | 1209 | { |
1149 | register int i; | | 1210 | size_t i; |
1150 | | | 1211 | |
1151 | b->used = 0; | | 1212 | b->used = 0; |
1152 | if (!entries->used) | | 1213 | if (!entries->used) |
1153 | return; | | 1214 | return; |
1154 | if (oper == OPMERGE) | | 1215 | if (oper == OPMERGE) |
1155 | qsort(entries->entry, entries->used, sizeof(Entry), | | 1216 | qsort(entries->entry, entries->used, sizeof(Entry), CompareEntries); |
1156 | CompareEntries); | | | |
1157 | for (i = 0; i < entries->used; i++) { | | 1217 | for (i = 0; i < entries->used; i++) { |
1158 | if (entries->entry[i].usable) | | 1218 | if (entries->entry[i].usable) |
1159 | AppendEntryToBuffer(b, &entries->entry[i]); | | 1219 | AppendEntryToBuffer(b, &entries->entry[i]); |
1160 | } | | 1220 | } |
1161 | } | | 1221 | } |
1162 | | | 1222 | |
1163 | static void | | 1223 | static void |
1164 | StoreProperty(Display *display, Window root, Atom res_prop) | | 1224 | StoreProperty(Display *display, Window root, Atom res_prop) |
1165 | { | | 1225 | { |
1166 | int len = buffer.used; | | 1226 | size_t len = buffer.used; |
1167 | int mode = PropModeReplace; | | 1227 | int mode = PropModeReplace; |
1168 | unsigned char *buf = (unsigned char *)buffer.buff; | | 1228 | unsigned char *buf = (unsigned char *) buffer.buff; |
1169 | int max = (XMaxRequestSize(display) << 2) - 28; | | 1229 | size_t max = ((unsigned) XMaxRequestSize(display) << 2) - 28; |
1170 | | | 1230 | |
1171 | if (len > max) { | | 1231 | if (len > max) { |
1172 | XGrabServer(display); | | 1232 | XGrabServer(display); |
1173 | do { | | 1233 | do { |
1174 | XChangeProperty(display, root, res_prop, XA_STRING, 8, mode, buf, max); | | 1234 | XChangeProperty(display, root, res_prop, XA_STRING, 8, mode, buf, |
1175 | buf += max; | | 1235 | (int) max); |
1176 | len -= max; | | 1236 | buf += max; |
1177 | mode = PropModeAppend; | | 1237 | len -= max; |
1178 | } while (len > max); | | 1238 | mode = PropModeAppend; |
| | | 1239 | } while (len > max); |
1179 | } | | 1240 | } |
1180 | XChangeProperty(display, root, res_prop, XA_STRING, 8, mode, buf, len); | | 1241 | XChangeProperty(display, root, res_prop, XA_STRING, 8, mode, buf, |
| | | 1242 | (int) len); |
1181 | if (mode != PropModeReplace) | | 1243 | if (mode != PropModeReplace) |
1182 | XUngrabServer(display); | | 1244 | XUngrabServer(display); |
1183 | } | | 1245 | } |
1184 | | | 1246 | |
1185 | static void | | 1247 | static void |
1186 | Process(int scrno, Bool doScreen, Bool execute) | | 1248 | Process(int scrno, Bool doScreen, Bool execute) |
1187 | { | | 1249 | { |
1188 | char *xdefs; | | 1250 | char *xdefs; |
1189 | Window root; | | 1251 | Window root; |
1190 | Atom res_prop; | | 1252 | Atom res_prop; |
1191 | FILE *input, *output; | | 1253 | FILE *input, *output; |
1192 | char *cmd; | | 1254 | char *cmd; |
1193 | | | 1255 | |
1194 | defines.val[defines_base] = '\0'; | | 1256 | defines.val[defines_base] = '\0'; |
1195 | defines.used = defines_base; | | 1257 | defines.used = defines_base; |
1196 | buffer.used = 0; | | 1258 | buffer.used = 0; |
1197 | InitEntries(&newDB); | | 1259 | InitEntries(&newDB); |
1198 | DoScreenDefines(dpy, scrno, &defines); | | 1260 | DoScreenDefines(dpy, scrno, &defines); |
1199 | DoCmdDefines(&defines); | | 1261 | DoCmdDefines(&defines); |
1200 | if (doScreen) { | | 1262 | if (doScreen) { |
1201 | xdefs = XScreenResourceString (ScreenOfDisplay(dpy, scrno)); | | 1263 | xdefs = XScreenResourceString(ScreenOfDisplay(dpy, scrno)); |
1202 | root = RootWindow(dpy, scrno); | | 1264 | root = RootWindow(dpy, scrno); |
1203 | res_prop = XInternAtom(dpy, SCREEN_RESOURCES, False); | | 1265 | res_prop = XInternAtom(dpy, SCREEN_RESOURCES, False); |
1204 | } else { | | 1266 | } |
1205 | xdefs = XResourceManagerString (dpy); | | 1267 | else { |
1206 | root = RootWindow(dpy, 0); | | 1268 | xdefs = XResourceManagerString(dpy); |
1207 | res_prop = XA_RESOURCE_MANAGER; | | 1269 | root = RootWindow(dpy, 0); |
| | | 1270 | res_prop = XA_RESOURCE_MANAGER; |
1208 | } | | 1271 | } |
1209 | if (oper == OPSYMBOLS) { | | 1272 | if (oper == OPSYMBOLS) { |
1210 | printf ("%s\n", defines.val); | | 1273 | printf("%s\n", defines.val); |
1211 | } else if (oper == OPQUERY) { | | 1274 | } |
1212 | if (xdefs) | | 1275 | else if (oper == OPQUERY) { |
1213 | printf ("%s", xdefs); /* fputs broken in SunOS 4.0 */ | | 1276 | if (xdefs) |
1214 | } else if (oper == OPREMOVE) { | | 1277 | printf("%s", xdefs); /* fputs broken in SunOS 4.0 */ |
1215 | if (xdefs) | | 1278 | } |
1216 | XDeleteProperty(dpy, root, res_prop); | | 1279 | else if (oper == OPREMOVE) { |
1217 | } else if (oper == OPEDIT) { | | 1280 | if (xdefs) |
1218 | char template[100], old[100]; | | 1281 | XDeleteProperty(dpy, root, res_prop); |
| | | 1282 | } |
| | | 1283 | else if (oper == OPEDIT) { |
| | | 1284 | char template[100], old[100]; |
1219 | | | 1285 | |
1220 | input = fopen(editFile, "r"); | | 1286 | input = fopen(editFile, "r"); |
1221 | snprintf(template, sizeof(template), "%sXXXXXX", editFile); | | 1287 | snprintf(template, sizeof(template), "%sXXXXXX", editFile); |
1222 | #ifndef HAVE_MKSTEMP | | 1288 | #ifndef HAVE_MKSTEMP |
1223 | (void) mktemp(template); | | 1289 | (void) mktemp(template); |
1224 | output = fopen(template, "w"); | | 1290 | output = fopen(template, "w"); |
1225 | #else | | 1291 | #else |
1226 | { | | 1292 | { |
1227 | int fd = mkstemp(template); | | 1293 | int fd = mkstemp(template); |
1228 | output = fd != -1 ? fdopen(fd, "w") : NULL; | | 1294 | |
1229 | } | | 1295 | output = fd != -1 ? fdopen(fd, "w") : NULL; |
1230 | #endif | | 1296 | } |
1231 | if (!output) | | 1297 | #endif |
1232 | fatal("%s: can't open temporary file '%s'\n", ProgramName, template); | | 1298 | if (!output) |
1233 | GetEntriesString(&newDB, xdefs); | | 1299 | fatal("%s: can't open temporary file '%s'\n", ProgramName, |
1234 | EditFile(&newDB, input, output); | | 1300 | template); |
1235 | if (input) | | 1301 | GetEntriesString(&newDB, xdefs); |
1236 | fclose(input); | | 1302 | EditFile(&newDB, input, output); |
1237 | fclose(output); | | 1303 | if (input) |
1238 | snprintf(old, sizeof(old), "%s%s", editFile, backup_suffix); | | 1304 | fclose(input); |
1239 | if (dont_execute) { /* then write to standard out */ | | 1305 | fclose(output); |
1240 | char buf[BUFSIZ]; | | 1306 | snprintf(old, sizeof(old), "%s%s", editFile, backup_suffix); |
1241 | int n; | | 1307 | if (dont_execute) { /* then write to standard out */ |
1242 | | | 1308 | char buf[BUFSIZ]; |
1243 | output = fopen (template, "r"); | | 1309 | size_t n; |
1244 | if (output) { | | 1310 | |
1245 | while ((n = fread (buf, 1, sizeof buf, output)) > 0) { | | 1311 | output = fopen(template, "r"); |
1246 | fwrite (buf, 1, n, stdout); | | 1312 | if (output) { |
1247 | } | | 1313 | while ((n = fread(buf, 1, sizeof buf, output)) > 0) { |
1248 | fclose (output); | | 1314 | fwrite(buf, 1, n, stdout); |
1249 | } | | 1315 | } |
1250 | unlink (template); | | 1316 | fclose(output); |
1251 | } else { | | 1317 | } |
1252 | rename (editFile, old); | | 1318 | unlink(template); |
1253 | if (rename (template, editFile)) | | 1319 | } |
1254 | fatal("%s: can't rename file '%s' to '%s'\n", ProgramName, | | 1320 | else { |
1255 | template, editFile); | | 1321 | rename(editFile, old); |
1256 | } | | 1322 | if (rename(template, editFile)) |
1257 | } else { | | 1323 | fatal("%s: can't rename file '%s' to '%s'\n", ProgramName, |
1258 | const char *cpp_addflags = ""; | | 1324 | template, editFile); |
1259 | | | 1325 | } |
1260 | if (oper == OPMERGE || oper == OPOVERRIDE) | | 1326 | } |
1261 | GetEntriesString(&newDB, xdefs); | | 1327 | else { |
1262 | | | 1328 | const char *cpp_addflags = ""; |
1263 | /* Add -P flag only if using cpp, not another preprocessor */ | | 1329 | |
1264 | if (cpp_program) { | | 1330 | if (oper == OPMERGE || oper == OPOVERRIDE) |
1265 | const char *cp = strstr(cpp_program, "cpp"); | | 1331 | GetEntriesString(&newDB, xdefs); |
1266 | | | 1332 | |
1267 | if (cp && ((cp[3] == '\0') || cp[3] == ' ')) | | 1333 | /* Add -P flag only if using cpp, not another preprocessor */ |
1268 | cpp_addflags = "-P"; | | 1334 | if (cpp_program) { |
1269 | } | | 1335 | const char *cp = strstr(cpp_program, "cpp"); |
| | | 1336 | |
| | | 1337 | if (cp && ((cp[3] == '\0') || cp[3] == ' ')) |
| | | 1338 | cpp_addflags = "-P"; |
| | | 1339 | } |
1270 | #ifdef PATHETICCPP | | 1340 | #ifdef PATHETICCPP |
1271 | if (need_real_defines) { | | 1341 | if (need_real_defines) { |
1272 | #ifdef WIN32 | | 1342 | #ifdef WIN32 |
1273 | if (!(input = fopen(tmpname2, "w"))) | | 1343 | if (!(input = fopen(tmpname2, "w"))) |
1274 | fatal("%s: can't open file '%s'\n", ProgramName, tmpname2); | | 1344 | fatal("%s: can't open file '%s'\n", ProgramName, tmpname2); |
1275 | fputs(defines.val, input); | | 1345 | fputs(defines.val, input); |
1276 | fprintf(input, "\n#include \"%s\"\n", filename); | | 1346 | fprintf(input, "\n#include \"%s\"\n", filename); |
1277 | fclose(input); | | 1347 | fclose(input); |
1278 | (void) mktemp(tmpname3); | | 1348 | (void) mktemp(tmpname3); |
1279 | if (asprintf(&cmd, "%s %s %s %s > %s", cpp_program, cpp_addflags, | | 1349 | if (asprintf(&cmd, "%s %s %s %s > %s", cpp_program, cpp_addflags, |
1280 | includes.val, tmpname2, tmpname3) == -1) | | 1350 | includes.val, tmpname2, tmpname3) == -1) |
1281 | fatal("%s: Out of memory\n", ProgramName); | | 1351 | fatal("%s: Out of memory\n", ProgramName); |
1282 | if (system(cmd) < 0) | | 1352 | if (system(cmd) < 0) |
1283 | fatal("%s: cannot run '%s'\n", ProgramName, cmd); | | 1353 | fatal("%s: cannot run '%s'\n", ProgramName, cmd); |
1284 | free(cmd); | | 1354 | free(cmd); |
1285 | if (!(input = fopen(tmpname3, "r"))) | | 1355 | if (!(input = fopen(tmpname3, "r"))) |
1286 | fatal("%s: can't open file '%s'\n", ProgramName, tmpname3); | | 1356 | fatal("%s: can't open file '%s'\n", ProgramName, tmpname3); |
1287 | #else | | 1357 | #else |
1288 | if (!freopen(tmpname2, "w+", stdin)) | | 1358 | if (!freopen(tmpname2, "w+", stdin)) |
1289 | fatal("%s: can't open file '%s'\n", ProgramName, tmpname2); | | 1359 | fatal("%s: can't open file '%s'\n", ProgramName, tmpname2); |
1290 | fputs(defines.val, stdin); | | 1360 | fputs(defines.val, stdin); |
1291 | fprintf(stdin, "\n#include \"%s\"\n", filename); | | 1361 | fprintf(stdin, "\n#include \"%s\"\n", filename); |
1292 | fflush(stdin); | | 1362 | fflush(stdin); |
1293 | fseek(stdin, 0, 0); | | 1363 | fseek(stdin, 0, SEEK_SET); |
1294 | if (asprintf(&cmd, "%s %s %s", cpp_program, cpp_addflags, | | 1364 | if (asprintf(&cmd, "%s %s %s", cpp_program, cpp_addflags, |
1295 | includes.val) == -1) | | 1365 | includes.val) == -1) |
1296 | fatal("%s: Out of memory\n", ProgramName); | | 1366 | fatal("%s: Out of memory\n", ProgramName); |
1297 | if (!(input = popen(cmd, "r"))) | | 1367 | if (!(input = popen(cmd, "r"))) |
1298 | fatal("%s: cannot run '%s'\n", ProgramName, cmd); | | 1368 | fatal("%s: cannot run '%s'\n", ProgramName, cmd); |
1299 | free(cmd); | | 1369 | free(cmd); |
1300 | #endif | | 1370 | #endif |
1301 | } else { | | 1371 | } |
1302 | #endif | | 1372 | else { |
1303 | if (filename) { | | 1373 | #endif |
1304 | if (!freopen (filename, "r", stdin)) | | 1374 | if (filename) { |
1305 | fatal("%s: can't open file '%s'\n", ProgramName, filename); | | 1375 | if (!freopen(filename, "r", stdin)) |
1306 | } | | 1376 | fatal("%s: can't open file '%s'\n", ProgramName, filename); |
1307 | if (cpp_program) { | | 1377 | } |
| | | 1378 | if (cpp_program) { |
1308 | #ifdef WIN32 | | 1379 | #ifdef WIN32 |
1309 | (void) mktemp(tmpname3); | | 1380 | (void) mktemp(tmpname3); |
1310 | if (asprintf(&cmd, "%s %s %s %s %s > %s", cpp_program, | | 1381 | if (asprintf(&cmd, "%s %s %s %s %s > %s", cpp_program, |
1311 | cpp_addflags, includes.val, defines.val, | | 1382 | cpp_addflags, includes.val, defines.val, |
1312 | filename ? filename : "", tmpname3) == -1) | | 1383 | filename ? filename : "", tmpname3) == -1) |
1313 | fatal("%s: Out of memory\n", ProgramName); | | 1384 | fatal("%s: Out of memory\n", ProgramName); |
1314 | if (system(cmd) < 0) | | 1385 | if (system(cmd) < 0) |
1315 | fatal("%s: cannot run '%s'\n", ProgramName, cmd); | | 1386 | fatal("%s: cannot run '%s'\n", ProgramName, cmd); |
1316 | free(cmd); | | 1387 | free(cmd); |
1317 | if (!(input = fopen(tmpname3, "r"))) | | 1388 | if (!(input = fopen(tmpname3, "r"))) |
1318 | fatal("%s: can't open file '%s'\n", ProgramName, tmpname3); | | 1389 | fatal("%s: can't open file '%s'\n", ProgramName, tmpname3); |
1319 | #else | | 1390 | #else |
1320 | if (asprintf(&cmd, "%s %s %s %s %s", cpp_program, | | 1391 | if (asprintf(&cmd, "%s %s %s %s %s", cpp_program, |
1321 | cpp_addflags, includes.val, defines.val, | | 1392 | cpp_addflags, includes.val, defines.val, |
1322 | filename ? filename : "") == -1) | | 1393 | filename ? filename : "") == -1) |
1323 | fatal("%s: Out of memory\n", ProgramName); | | 1394 | fatal("%s: Out of memory\n", ProgramName); |
1324 | if (!(input = popen(cmd, "r"))) | | 1395 | if (!(input = popen(cmd, "r"))) |
1325 | fatal("%s: cannot run '%s'\n", ProgramName, cmd); | | 1396 | fatal("%s: cannot run '%s'\n", ProgramName, cmd); |
1326 | free(cmd); | | 1397 | free(cmd); |
1327 | #endif | | 1398 | #endif |
1328 | } else { | | 1399 | } |
1329 | input = stdin; | | 1400 | else { |
1330 | } | | 1401 | input = stdin; |
| | | 1402 | } |
1331 | #ifdef PATHETICCPP | | 1403 | #ifdef PATHETICCPP |
1332 | } | | 1404 | } |
1333 | #endif | | 1405 | #endif |
1334 | ReadFile(&buffer, input); | | 1406 | ReadFile(&buffer, input); |
1335 | if (cpp_program) { | | 1407 | if (cpp_program) { |
1336 | #ifdef WIN32 | | 1408 | #ifdef WIN32 |
1337 | fclose(input); | | 1409 | fclose(input); |
1338 | #else | | 1410 | #else |
1339 | pclose(input); | | 1411 | pclose(input); |
1340 | #endif | | 1412 | #endif |
1341 | } | | 1413 | } |
1342 | #ifdef PATHETICCPP | | 1414 | #ifdef PATHETICCPP |
1343 | if (need_real_defines) { | | 1415 | if (need_real_defines) { |
1344 | unlink(tmpname2); | | 1416 | unlink(tmpname2); |
1345 | #ifdef WIN32 | | 1417 | #ifdef WIN32 |
1346 | if (tmpname3[strlen(tmpname3) - 1] != 'X') | | 1418 | if (tmpname3[strlen(tmpname3) - 1] != 'X') |
1347 | unlink(tmpname3); | | 1419 | unlink(tmpname3); |
1348 | #endif | | 1420 | #endif |
1349 | } | | 1421 | } |
1350 | #endif | | 1422 | #endif |
1351 | GetEntries(&newDB, &buffer, 0); | | 1423 | GetEntries(&newDB, &buffer, 0); |
1352 | if (execute) { | | 1424 | if (execute) { |
1353 | FormatEntries(&buffer, &newDB); | | 1425 | FormatEntries(&buffer, &newDB); |
1354 | if (dont_execute) { | | 1426 | if (dont_execute) { |
1355 | if (buffer.used > 0) { | | 1427 | if (buffer.used > 0) { |
1356 | fwrite (buffer.buff, 1, buffer.used, stdout); | | 1428 | fwrite(buffer.buff, 1, buffer.used, stdout); |
1357 | if (buffer.buff[buffer.used - 1] != '\n') putchar ('\n'); | | 1429 | if (buffer.buff[buffer.used - 1] != '\n') |
1358 | } | | 1430 | putchar('\n'); |
1359 | } else if (buffer.used > 1 || !doScreen) | | 1431 | } |
1360 | StoreProperty (dpy, root, res_prop); | | 1432 | } |
1361 | else | | 1433 | else if (buffer.used > 1 || !doScreen) |
1362 | XDeleteProperty (dpy, root, res_prop); | | 1434 | StoreProperty(dpy, root, res_prop); |
1363 | } | | 1435 | else |
| | | 1436 | XDeleteProperty(dpy, root, res_prop); |
| | | 1437 | } |
1364 | } | | 1438 | } |
1365 | if (execute) | | 1439 | if (execute) |
1366 | FreeEntries(&newDB); | | 1440 | FreeEntries(&newDB); |
1367 | if (doScreen && xdefs) | | 1441 | if (doScreen && xdefs) |
1368 | XFree(xdefs); | | 1442 | XFree(xdefs); |
1369 | } | | 1443 | } |
1370 | | | 1444 | |
1371 | static void | | 1445 | static void |
1372 | ShuffleEntries(Entries *db, Entries *dbs, int num) | | 1446 | ShuffleEntries(Entries *db, Entries *dbs, unsigned int num) |
1373 | { | | 1447 | { |
1374 | int *hits; | | 1448 | unsigned int *hits; |
1375 | register int i, j, k; | | 1449 | unsigned int i, j, k; |
1376 | Entries cur, cmp; | | 1450 | Entries cur, cmp; |
1377 | char *curtag, *curvalue; | | 1451 | char *curtag, *curvalue; |
1378 | | | 1452 | |
1379 | hits = malloc(num * sizeof(int)); | | 1453 | hits = mallocarray(num, sizeof(int)); |
| | | 1454 | if (hits == NULL) |
| | | 1455 | fatal("%s: Can't allocate memory in %s\n", ProgramName, __func__); |
1380 | cur = dbs[0]; | | 1456 | cur = dbs[0]; |
1381 | for (i = 0; i < cur.used; i++) { | | 1457 | for (i = 0; i < cur.used; i++) { |
1382 | curtag = cur.entry[i].tag; | | 1458 | curtag = cur.entry[i].tag; |
1383 | curvalue = cur.entry[i].value; | | 1459 | curvalue = cur.entry[i].value; |
1384 | for (j = 1; j < num; j++) { | | 1460 | for (j = 1; j < num; j++) { |
1385 | cmp = dbs[j]; | | 1461 | cmp = dbs[j]; |
1386 | for (k = 0; k < cmp.used; k++) { | | 1462 | for (k = 0; k < cmp.used; k++) { |
1387 | if (cmp.entry[k].usable && | | 1463 | if (cmp.entry[k].usable && |
1388 | !strcmp(curtag, cmp.entry[k].tag) && | | 1464 | !strcmp(curtag, cmp.entry[k].tag) && |
1389 | !strcmp(curvalue, cmp.entry[k].value)) | | 1465 | !strcmp(curvalue, cmp.entry[k].value)) { |
1390 | { | | 1466 | hits[j] = k; |
1391 | hits[j] = k; | | 1467 | break; |
1392 | break; | | 1468 | } |
1393 | } | | 1469 | } |
1394 | } | | 1470 | if (k == cmp.used) |
1395 | if (k == cmp.used) | | 1471 | break; |
1396 | break; | | 1472 | } |
1397 | } | | 1473 | if (j == num) { |
1398 | if (j == num) { | | 1474 | AddEntry(db, &cur.entry[i]); |
1399 | AddEntry(db, &cur.entry[i]); | | 1475 | hits[0] = i; |
1400 | hits[0] = i; | | 1476 | for (j = 0; j < num; j++) |
1401 | for (j = 0; j < num; j++) | | 1477 | dbs[j].entry[hits[j]].usable = False; |
1402 | dbs[j].entry[hits[j]].usable = False; | | 1478 | } |
1403 | } | | | |
1404 | } | | 1479 | } |
1405 | free((char *)hits); | | 1480 | free(hits); |
1406 | } | | 1481 | } |
1407 | | | 1482 | |
1408 | static void | | 1483 | static void |
1409 | ReProcess(int scrno, Bool doScreen) | | 1484 | ReProcess(int scrno, Bool doScreen) |
1410 | { | | 1485 | { |
1411 | Window root; | | 1486 | Window root; |
1412 | Atom res_prop; | | 1487 | Atom res_prop; |
1413 | | | 1488 | |
1414 | FormatEntries(&buffer, &newDB); | | 1489 | FormatEntries(&buffer, &newDB); |
1415 | if (doScreen) { | | 1490 | if (doScreen) { |
1416 | root = RootWindow(dpy, scrno); | | 1491 | root = RootWindow(dpy, scrno); |
1417 | res_prop = XInternAtom(dpy, SCREEN_RESOURCES, False); | | 1492 | res_prop = XInternAtom(dpy, SCREEN_RESOURCES, False); |
1418 | } else { | | 1493 | } |
1419 | root = RootWindow(dpy, 0); | | 1494 | else { |
1420 | res_prop = XA_RESOURCE_MANAGER; | | 1495 | root = RootWindow(dpy, 0); |
| | | 1496 | res_prop = XA_RESOURCE_MANAGER; |
1421 | } | | 1497 | } |
1422 | if (dont_execute) { | | 1498 | if (dont_execute) { |
1423 | if (buffer.used > 0) { | | 1499 | if (buffer.used > 0) { |
1424 | fwrite (buffer.buff, 1, buffer.used, stdout); | | 1500 | fwrite(buffer.buff, 1, buffer.used, stdout); |
1425 | if (buffer.buff[buffer.used - 1] != '\n') putchar ('\n'); | | 1501 | if (buffer.buff[buffer.used - 1] != '\n') |
1426 | } | | 1502 | putchar('\n'); |
1427 | } else { | | 1503 | } |
1428 | if (buffer.used > 1 || !doScreen) | | 1504 | } |
1429 | StoreProperty (dpy, root, res_prop); | | 1505 | else { |
1430 | else | | 1506 | if (buffer.used > 1 || !doScreen) |
1431 | XDeleteProperty (dpy, root, res_prop); | | 1507 | StoreProperty(dpy, root, res_prop); |
| | | 1508 | else |
| | | 1509 | XDeleteProperty(dpy, root, res_prop); |
1432 | } | | 1510 | } |
1433 | FreeEntries(&newDB); | | 1511 | FreeEntries(&newDB); |
1434 | } | | 1512 | } |
1435 | | | 1513 | |
1436 | static void | | 1514 | static void |
1437 | fatal(const char *msg, ...) | | 1515 | fatal(const char *msg, ...) |
1438 | { | | 1516 | { |
1439 | va_list args; | | 1517 | va_list args; |
1440 | | | 1518 | |
1441 | if (errno != 0) | | 1519 | if (errno != 0) |
1442 | perror(ProgramName); | | 1520 | perror(ProgramName); |
1443 | va_start(args, msg); | | 1521 | va_start(args, msg); |
1444 | vfprintf(stderr, msg, args); | | 1522 | vfprintf(stderr, msg, args); |
1445 | va_end(args); | | 1523 | va_end(args); |
1446 | exit(1); | | 1524 | exit(1); |
1447 | } | | 1525 | } |