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