Root/fped.c

1/*
2 * fped.c - Footprint editor, main function
3 *
4 * Written 2009-2012, 2015 by Werner Almesberger
5 * Copyright 2009-2012, 2015 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 [-m] write Postscript output, then exit\n"
91" -P [-K] [-m] [-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" -m do not show measurements\n"
99" -s scale scale factor for -P (default: auto-scale)\n"
100" -s [width]x[heigth]\n"
101" auto-scale to fit within specified box. Dimensions in mm.\n"
102" cpp_option -Idir, -Dname[=value], or -Uname\n"
103    , name);
104    exit(1);
105}
106
107
108static int parse_scaling(const char *arg)
109{
110    const char *x;
111    char *end;
112
113    x = strchr(arg, 'x');
114    if (!x) {
115        postscript_params.zoom = strtod(arg, &end);
116        return !*end;
117    }
118    if (x != arg) {
119        postscript_params.max_width = mm_to_units(strtod(arg, &end));
120        if (*end != 'x')
121            return 0;
122    }
123    if (x[1]) {
124        postscript_params.max_height = mm_to_units(strtod(x+1, &end));
125        if (*end)
126            return 0;
127    }
128    return 1;
129}
130
131
132int main(int argc, char **argv)
133{
134    enum {
135        batch_none = 0,
136        batch_kicad,
137        batch_ps,
138        batch_ps_fullpage,
139        batch_gnuplot,
140        batch_test
141    } batch = batch_none;
142    char *name = *argv;
143    char **fake_argv;
144    char *args[2];
145    int fake_argc;
146    char opt[] = "-?";
147    int error;
148    int test_mode = 0;
149    const char *one = NULL;
150    int c;
151
152    while ((c = getopt(argc, argv, "1:gkmps:D:I:KPTU:")) != EOF)
153        switch (c) {
154        case '1':
155            one = optarg;
156            break;
157        case 'g':
158            if (batch)
159                usage(*argv);
160            batch = batch_gnuplot;
161            break;
162        case 'k':
163            if (batch)
164                usage(*argv);
165            batch = batch_kicad;
166            break;
167        case 'm':
168            postscript_params.show_meas = 0;
169            break;
170        case 'p':
171            if (batch)
172                usage(*argv);
173            batch = batch_ps;
174            break;
175        case 'P':
176            if (batch)
177                usage(*argv);
178            batch = batch_ps_fullpage;
179            break;
180        case 'K':
181            postscript_params.show_key = 1;
182            break;
183        case 's':
184            if (batch != batch_ps_fullpage)
185                usage(*argv);
186            if (!parse_scaling(optarg))
187                usage(*argv);
188            break;
189        case 'T':
190            batch = batch_test;
191            test_mode++;
192            break;
193        case 'D':
194        case 'U':
195        case 'I':
196            opt[1] = c;
197            add_cpp_arg(opt);
198            add_cpp_arg(optarg);
199            break;
200        default:
201            usage(name);
202        }
203
204    if (one && batch != batch_ps && batch != batch_ps_fullpage &&
205        batch != batch_gnuplot)
206        usage(name);
207    if (postscript_params.show_key && batch != batch_ps_fullpage)
208        usage(name);
209
210    if (!batch) {
211        args[0] = name;
212        args[1] = NULL;
213        fake_argc = 1;
214        fake_argv = args;
215        error = gui_init(&fake_argc, &fake_argv);
216        if (error)
217            return error;
218    }
219
220    switch (argc-optind) {
221    case 0:
222        scan_empty();
223        (void) yyparse();
224        break;
225    case 1:
226        load_file(argv[optind]);
227        save_file_name = argv[optind];
228        break;
229    case 2:
230        load_file(argv[optind]);
231        save_file_name = argv[optind+1];
232        if (!strcmp(save_file_name, "-"))
233            save_file_name = NULL;
234        break;
235    default:
236        usage(name);
237    }
238
239    if (!pkg_name)
240        pkg_name = stralloc("_");
241
242    reporter = report_to_stderr;
243    if (!instantiate())
244        return 1;
245
246    switch (batch) {
247    case batch_none:
248        error = gui_main();
249        if (error)
250            return error;
251        break;
252    case batch_kicad:
253        write_kicad();
254        break;
255    case batch_ps:
256        write_ps(one);
257        break;
258    case batch_ps_fullpage:
259        write_ps_fullpage(one);
260        break;
261    case batch_gnuplot:
262        write_gnuplot(one);
263        break;
264    case batch_test:
265        if (test_mode > 1)
266            dump(stdout, NULL);
267        break;
268    default:
269        abort();
270    }
271
272    purge();
273    inst_revert();
274    obj_cleanup();
275    unique_cleanup();
276
277    return 0;
278}
279

Archive Download this file

Branches:
master



interactive