Root/fped.c

1/*
2 * fped.c - Footprint editor, main function
3 *
4 * Written 2009-2012 by Werner Almesberger
5 * Copyright 2009-2012 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <unistd.h>
17#include <string.h>
18#include <errno.h>
19
20#include "cpp.h"
21#include "util.h"
22#include "error.h"
23#include "obj.h"
24#include "inst.h"
25#include "file.h"
26#include "postscript.h"
27#include "dump.h"
28#include "gui.h"
29#include "delete.h"
30#include "fpd.h"
31#include "fped.h"
32
33
34char *save_file_name = NULL;
35int no_save = 0;
36
37
38static void load_file(const char *name)
39{
40    FILE *file;
41    char line[sizeof(MACHINE_GENERATED)];
42
43    file = fopen(name, "r");
44    if (file) {
45        if (!fgets(line, sizeof(line), file)) {
46            if (ferror(file)) {
47                perror(name);
48                exit(1);
49            }
50            *line = 0;
51        }
52        no_save = strcmp(line, MACHINE_GENERATED);
53        fclose(file);
54        reporter = report_parse_error;
55        run_cpp_on_file(name);
56    } else {
57        if (errno != ENOENT) {
58            perror(name);
59            exit(1);
60        }
61        scan_empty();
62    }
63    (void) yyparse();
64}
65
66
67void reload(void)
68{
69    struct frame *old_frames;
70
71    /* @@@ this needs more work */
72    purge();
73    old_frames = frames;
74    scan_file();
75    load_file(save_file_name);
76    if (!instantiate())
77        frames = old_frames;
78    change_world();
79}
80
81
82static void usage(const char *name)
83{
84    fprintf(stderr,
85"usage: %s [batch_mode] [cpp_option ...] [in_file [out_file]]\n\n"
86"Batch mode options:\n"
87" -g [-1 package]\n"
88" write gnuplot output, then exit\n"
89" -k write KiCad output, then exit\n"
90" -p write Postscript output, then exit\n"
91" -P [-K] [-s scale] [-1 package]\n"
92" write Postscript output (full page), then exit\n"
93" -T test mode. Load file, then exit\n"
94" -T -T test mode. Load file, dump to stdout, then exit\n\n"
95"Common options:\n"
96" -1 name output only the specified package\n"
97" -K show the pad type key\n"
98" -s scale scale factor for -P (default: auto-scale)\n"
99" -s [width]x[heigth]\n"
100" auto-scale to fit within specified box. Dimensions in mm.\n"
101" cpp_option -Idir, -Dname[=value], or -Uname\n"
102    , name);
103    exit(1);
104}
105
106
107static int parse_scaling(const char *arg)
108{
109    const char *x;
110    char *end;
111
112    x = strchr(arg, 'x');
113    if (!x) {
114        postscript_params.zoom = strtod(arg, &end);
115        return !*end;
116    }
117    if (x != arg) {
118        postscript_params.max_width = mm_to_units(strtod(arg, &end));
119        if (*end != 'x')
120            return 0;
121    }
122    if (x[1]) {
123        postscript_params.max_height = mm_to_units(strtod(x+1, &end));
124        if (*end)
125            return 0;
126    }
127    return 1;
128}
129
130
131int main(int argc, char **argv)
132{
133    enum {
134        batch_none = 0,
135        batch_kicad,
136        batch_ps,
137        batch_ps_fullpage,
138        batch_gnuplot,
139        batch_test
140    } batch = batch_none;
141    char *name = *argv;
142    char **fake_argv;
143    char *args[2];
144    int fake_argc;
145    char opt[] = "-?";
146    int error;
147    int test_mode = 0;
148    const char *one = NULL;
149    int c;
150
151    while ((c = getopt(argc, argv, "1:gkps:D:I:KPTU:")) != EOF)
152        switch (c) {
153        case '1':
154            one = optarg;
155            break;
156        case 'g':
157            if (batch)
158                usage(*argv);
159            batch = batch_gnuplot;
160            break;
161        case 'k':
162            if (batch)
163                usage(*argv);
164            batch = batch_kicad;
165            break;
166        case 'p':
167            if (batch)
168                usage(*argv);
169            batch = batch_ps;
170            break;
171        case 'P':
172            if (batch)
173                usage(*argv);
174            batch = batch_ps_fullpage;
175            break;
176        case 'K':
177            postscript_params.show_key = 1;
178            break;
179        case 's':
180            if (batch != batch_ps_fullpage)
181                usage(*argv);
182            if (!parse_scaling(optarg))
183                usage(*argv);
184            break;
185        case 'T':
186            batch = batch_test;
187            test_mode++;
188            break;
189        case 'D':
190        case 'U':
191        case 'I':
192            opt[1] = c;
193            add_cpp_arg(opt);
194            add_cpp_arg(optarg);
195            break;
196        default:
197            usage(name);
198        }
199
200    if (one && batch != batch_ps && batch != batch_ps_fullpage &&
201        batch != batch_gnuplot)
202        usage(name);
203    if (postscript_params.show_key && batch != batch_ps_fullpage)
204        usage(name);
205
206    if (!batch) {
207        args[0] = name;
208        args[1] = NULL;
209        fake_argc = 1;
210        fake_argv = args;
211        error = gui_init(&fake_argc, &fake_argv);
212        if (error)
213            return error;
214    }
215
216    switch (argc-optind) {
217    case 0:
218        scan_empty();
219        (void) yyparse();
220        break;
221    case 1:
222        load_file(argv[optind]);
223        save_file_name = argv[optind];
224        break;
225    case 2:
226        load_file(argv[optind]);
227        save_file_name = argv[optind+1];
228        if (!strcmp(save_file_name, "-"))
229            save_file_name = NULL;
230        break;
231    default:
232        usage(name);
233    }
234
235    if (!pkg_name)
236        pkg_name = stralloc("_");
237
238    reporter = report_to_stderr;
239    if (!instantiate())
240        return 1;
241
242    switch (batch) {
243    case batch_none:
244        error = gui_main();
245        if (error)
246            return error;
247        break;
248    case batch_kicad:
249        write_kicad();
250        break;
251    case batch_ps:
252        write_ps(one);
253        break;
254    case batch_ps_fullpage:
255        write_ps_fullpage(one);
256        break;
257    case batch_gnuplot:
258        write_gnuplot(one);
259        break;
260    case batch_test:
261        if (test_mode > 1)
262            dump(stdout, NULL);
263        break;
264    default:
265        abort();
266    }
267
268    purge();
269    inst_revert();
270    obj_cleanup();
271    unique_cleanup();
272
273    return 0;
274}
275

Archive Download this file

Branches:
master



interactive