summaryrefslogtreecommitdiff
path: root/base/gendev.c
blob: f36cfe265c3a4e5d271cf220ecb1af1b94956904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* Generate .dev configuration files */
#include "stdpre.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>		/* for calloc */
#include <string.h>

/*
 * This program generates .dev configuration files.
 *
 * Usage:
 *      gendev [-Z] [-n [<name_prefix> | -]] [-C [<dir> | -]]
 *        (-d <devfile> | -m <modfile> | -a <modfile>)
 *        (-<category> | <item>)*
 *
 * The name_prefix is for device[2], font, init, and iodev resources.
 * ****** DOESN'T HANDLE -replace YET ******
 * ****** DOESN'T MERGE device AND device2 ******
 */

#define DEFAULT_NAME_PREFIX "gs_"
#define INITIAL_CATEGORY "obj"

typedef struct config_s {
    /* Set once */
    FILE *out;
    bool debug;
    const char *name_prefix;
    const char *file_prefix;
    ulong file_id;		/* for uniq_last detection */
    /* Updated dynamically */
#define ITEM_ID_BITS 5
    uint item_id;
    bool any_scan_items;
    bool in_res_scan;
    bool in_category;		/* in_category implies in_res_scan */
    const char *current_category;
} config;
static const config init_config =
{
    0,				/* out */
    0,				/* debug */
    DEFAULT_NAME_PREFIX,	/* name_prefix */
    "",				/* file_prefix */
    0,				/* file_id */
    0,				/* item_id */
    0,				/* any_scan_items */
    0,				/* in_res_scan */
    0,				/* in_category */
    ""				/* current_category */
};

static const char *indent_INCLUDED = "\t\t\t\t";
static const char *indent_RES_SCAN = "   ";
static const char *indent_category = "\t";
static const char *indent_SEEN = "\t    ";
static const char *indent_include = "   ";
static const char *indent_scan_item = "\t";
static const char *indent_item = "";

/* Forward definitions */
void add_entry(config *, const char *, const char *, bool);

main(int argc, char *argv[])
{
    config conf;
    int i, j;
    bool dev, append;
    const char *category = INITIAL_CATEGORY;
    const char *fnarg;
    FILE *out;
    long pos;

    conf = init_config;
    /* Process command line arguments. */
    for (i = 1; i < argc; i++) {
        const char *arg = argv[i];

        if (*arg != '-') {
            fprintf(stderr, "-d|m|a must precede non-switches.\n", arg);
            exit(1);
        }
        switch (arg[1]) {
            case 'C':		/* change directory, by analogy with make */
                conf.file_prefix =
                    (argv[i + 1][0] == '-' ? "" : argv[i + 1]);
                ++i;
                continue;
            case 'n':
                conf.name_prefix =
                    (argv[i + 1][0] == '-' ? "" : argv[i + 1]);
                ++i;
                continue;
            case 'a':
                dev = false, append = true;
                break;
            case 'd':
                dev = true, append = false;
                break;
            case 'm':
                dev = false, append = false;
                break;
            case 'Z':
                conf.debug = true;
                continue;
            default:
                fprintf(stderr, "Unknown switch %s.\n", argv[i]);
                exit(1);
        }
        break;
    }
    if (i == argc - 1) {
        fprintf(stderr, "No output file name given, last argument is %s.\n",
                argv[i]);
        exit(1);
    }
    /* Must be the output file. */
    fnarg = argv[++i];
    {
        char fname[100];

        strcpy(fname, fnarg);
        strcat(fname, ".dev");
        out = fopen(fname, (append ? "a" : "w"));
        if (out == 0) {
            fprintf(stderr, "Can't open %s for output.\n", fname);
            exit(1);
        }
        if (!append)
            fprintf(out,
                    "/*\n * File %s created automatically by gendev.\n */\n",
                    fname);
    }
    conf.out = out;
    pos = ftell(out);
    /* We need a separate _INCLUDED flag for each batch of definitions. */
    fprintf(out, "\n#%sifndef %s_%ld_INCLUDED\n",
            indent_INCLUDED, fnarg, pos);
    fprintf(out, "#%s  define %s_%ld_INCLUDED\n",
            indent_INCLUDED, fnarg, pos);
    /* Create a "unique" hash for the output file. */
    for (j = 0; fnarg[j] != 0; ++j)
        conf.file_id = conf.file_id * 41 + fnarg[j];
    conf.item_id <<= ITEM_ID_BITS;
    /* Add the real entries. */
    if (dev)
        add_entry(&conf, "dev", fnarg, false);
    for (j = i + 1; j < argc; ++j) {
        const char *arg = argv[j];

        if (arg[0] == '-')
            category = arg + 1;
        else
            add_entry(&conf, category, arg, false);
    }
    if (conf.in_category)
        fprintf(out, "#%sendif /* -%s */\n",
                indent_category, conf.current_category);
    /* Add the scanning entries, if any. */
    if (conf.any_scan_items) {
        if (conf.in_res_scan)
            fprintf(out, "#%selse /* RES_SCAN */\n", indent_RES_SCAN);
        else
            fprintf(out, "#%sifdef RES_SCAN\n", indent_RES_SCAN);
        conf.in_res_scan = true;
        category = INITIAL_CATEGORY;
        conf.item_id = 0;
        for (j = i + 1; j < argc; ++j) {
            const char *arg = argv[j];

            if (arg[0] == '-')
                category = arg + 1;
            else
                add_entry(&conf, category, arg, true);
        }
    }
    if (conf.in_res_scan)
        fprintf(out, "#%sendif /* RES_SCAN */\n", indent_RES_SCAN);
    fprintf(out, "#%sendif /* !%s_%ld_INCLUDED */\n",
            indent_INCLUDED, fnarg, pos);
    fclose(out);
    exit(0);
}

/* Add an entry to the output. */
typedef enum {
    uniq_none = 0,
    uniq_first,
    uniq_last
} uniq_mode;
void
write_item(config * pconf, const char *str, const char *category,
           const char *item, uniq_mode mode)
{
    FILE *out = pconf->out;
    char cati[80];

    if (!pconf->in_res_scan) {
        fprintf(out, "#%sifndef RES_SCAN\n", indent_RES_SCAN);
        pconf->in_res_scan = true;
    }
    if (strcmp(pconf->current_category, category)) {
        const char *paren = strchr(str, '(');

        if (pconf->in_category)
            fprintf(out, "#%sendif /* -%s */\n",
                    indent_category, pconf->current_category);
        fprintf(out, "#%sifdef ", indent_category);
        fwrite(str, sizeof(char), paren - str, out);

        fprintf(out, "\n");
        pconf->current_category = category;
        pconf->in_category = true;
    }
    sprintf(cati, "%s_%s_", category, item);
    switch (mode) {
        case uniq_none:
            fprintf(out, "%s%s\n", indent_item, str);
            break;
        case uniq_first:
            fprintf(out, "#%sifndef %sSEEN\n", indent_SEEN, cati);
            fprintf(out, "#%s  define %sSEEN\n", indent_SEEN, cati);
            write_item(pconf, str, category, item, uniq_none);
            fprintf(out, "#%sendif\n", indent_SEEN, cati);
            break;
        case uniq_last:
            fprintf(out, "#%sif %sSEEN == %lu\n", indent_SEEN, cati,
                    pconf->file_id + pconf->item_id++);
            write_item(pconf, str, category, item, uniq_none);
            fprintf(out, "#%sendif\n", indent_SEEN, cati);
            pconf->any_scan_items = true;
            break;
    }
}
void
write_scan_item(config * pconf, const char *str, const char *category,
                const char *item, uniq_mode mode)
{
    FILE *out = pconf->out;
    char cati[80];

    sprintf(cati, "%s_%s_", category, item);
    switch (mode) {
        case uniq_none:
            break;
        case uniq_first:
            break;
        case uniq_last:
            fprintf(out, "#%sundef %sSEEN\n", indent_scan_item, cati);
            fprintf(out, "#%s  define %sSEEN %lu\n", indent_scan_item, cati,
                    pconf->file_id + pconf->item_id++);
    }
}
void
add_entry(config * pconf, const char *category, const char *item, bool scan)
{
    char str[80];
    uniq_mode mode = uniq_first;

    if (pconf->debug && !scan)
        printf("Adding %s %s;\n", category, item);
    str[0] = 0;
    switch (category[0]) {	/* just an accelerator */
#define is_cat(str) !strcmp(category, str)
        case 'd':
            if (is_cat("dev"))
                sprintf(str, "device_(%s%s_device)\n",
                        pconf->name_prefix, item);
            else if (is_cat("dev2"))
                sprintf(str, "device2_(%s%s_device)\n",
                        pconf->name_prefix, item);
            break;
        case 'e':
            if (is_cat("emulator"))
                sprintf(str, "emulator_(\"%s\",%d)",
                        item, strlen(item));
            break;
        case 'f':
            if (is_cat("font"))
                sprintf(str, "font_(\"0.font_%s\",%sf_%s,zf_%s)",
                        item, pconf->name_prefix, item, item);
            break;
        case 'i':
            if (is_cat("include")) {
                int len = strlen(item);

                if (scan)
                    return;
                if (strcmp(pconf->current_category, category)) {
                    if (pconf->in_category) {
                        fprintf(pconf->out, "#%sendif /* -%s */\n",
                                indent_category, pconf->current_category);
                        pconf->in_category = false;
                    }
                    pconf->current_category = category;
                }
                if (pconf->in_res_scan) {
                    fprintf(pconf->out, "#%sendif /* RES_SCAN */\n",
                            indent_RES_SCAN);
                    pconf->in_res_scan = false;
                }
                if (len < 5 || strcmp(item + len - 4, ".dev"))
                    fprintf(pconf->out, "#%sinclude \"%s.dev\"\n",
                            indent_include, item);
                else
                    fprintf(pconf->out, "#%sinclude \"%s\"\n",
                            indent_include, item);
                return;
            } else if (is_cat("init"))
                sprintf(str, "init_(%s%s_init)", pconf->name_prefix, item);
            else if (is_cat("iodev"))
                sprintf(str, "io_device_(%siodev_%s)", pconf->name_prefix, item);
            break;
        case 'l':
            if (is_cat("lib")) {
                sprintf(str, "lib_(%s)", item);
                mode = uniq_last;
            }
            break;
        case 'o':
            if (is_cat("obj"))
                sprintf(str, "obj_(%s%s)", pconf->file_prefix, item);
            else if (is_cat("oper"))
                sprintf(str, "oper_(%s_op_defs)", item);
            break;
        case 'p':
            if (is_cat("ps"))
                sprintf(str, "psfile_(\"%s.ps\",%d)",
                        item, strlen(item) + 3);
            break;
#undef is_cat
        default:
            ;
    }
    if (str[0] == 0) {
        fprintf(stderr, "Unknown category %s.\n", category);
        exit(1);
    }
    if (scan)
        write_scan_item(pconf, str, category, item, mode);
    else
        write_item(pconf, str, category, item, mode);
}