Root/
Source at commit d250ed236c8eb28eeacced1867510eff42fc0f17 created 11 years 8 months ago. By Werner Almesberger, postscript.c: show key for pad types at bottom of fullpage drawings (-P -K) | |
---|---|
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 | |
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 | static void usage(const char *name) |
68 | { |
69 | fprintf(stderr, |
70 | "usage: %s [batch_mode] [cpp_option ...] [in_file [out_file]]\n\n" |
71 | "Batch mode options:\n" |
72 | " -g [-1 package]\n" |
73 | " write gnuplot output, then exit\n" |
74 | " -k write KiCad output, then exit\n" |
75 | " -p write Postscript output, then exit\n" |
76 | " -P [-K] [-s scale] [-1 package]\n" |
77 | " write Postscript output (full page), then exit\n" |
78 | " -T test mode. Load file, then exit\n" |
79 | " -T -T test mode. Load file, dump to stdout, then exit\n\n" |
80 | "Common options:\n" |
81 | " -1 name output only the specified package\n" |
82 | " -K show the pad type key\n" |
83 | " -s scale scale factor for -P (default: auto-scale)\n" |
84 | " -s [width]x[heigth]\n" |
85 | " auto-scale to fit within specified box. Dimensions in mm.\n" |
86 | " cpp_option -Idir, -Dname[=value], or -Uname\n" |
87 | , name); |
88 | exit(1); |
89 | } |
90 | |
91 | |
92 | static int parse_scaling(const char *arg) |
93 | { |
94 | const char *x; |
95 | char *end; |
96 | |
97 | x = strchr(arg, 'x'); |
98 | if (!x) { |
99 | postscript_params.zoom = strtod(arg, &end); |
100 | return !*end; |
101 | } |
102 | if (x != arg) { |
103 | postscript_params.max_width = mm_to_units(strtod(arg, &end)); |
104 | if (*end != 'x') |
105 | return 0; |
106 | } |
107 | if (x[1]) { |
108 | postscript_params.max_height = mm_to_units(strtod(x+1, &end)); |
109 | if (*end) |
110 | return 0; |
111 | } |
112 | return 1; |
113 | } |
114 | |
115 | |
116 | int main(int argc, char **argv) |
117 | { |
118 | enum { |
119 | batch_none = 0, |
120 | batch_kicad, |
121 | batch_ps, |
122 | batch_ps_fullpage, |
123 | batch_gnuplot, |
124 | batch_test |
125 | } batch = batch_none; |
126 | char *name = *argv; |
127 | char **fake_argv; |
128 | char *args[2]; |
129 | int fake_argc; |
130 | char opt[] = "-?"; |
131 | int error; |
132 | int test_mode = 0; |
133 | const char *one = NULL; |
134 | int c; |
135 | |
136 | while ((c = getopt(argc, argv, "1:gkps:D:I:KPTU:")) != EOF) |
137 | switch (c) { |
138 | case '1': |
139 | one = optarg; |
140 | break; |
141 | case 'g': |
142 | if (batch) |
143 | usage(*argv); |
144 | batch = batch_gnuplot; |
145 | break; |
146 | case 'k': |
147 | if (batch) |
148 | usage(*argv); |
149 | batch = batch_kicad; |
150 | break; |
151 | case 'p': |
152 | if (batch) |
153 | usage(*argv); |
154 | batch = batch_ps; |
155 | break; |
156 | case 'P': |
157 | if (batch) |
158 | usage(*argv); |
159 | batch = batch_ps_fullpage; |
160 | break; |
161 | case 'K': |
162 | postscript_params.show_key = 1; |
163 | break; |
164 | case 's': |
165 | if (batch != batch_ps_fullpage) |
166 | usage(*argv); |
167 | if (!parse_scaling(optarg)) |
168 | usage(*argv); |
169 | break; |
170 | case 'T': |
171 | batch = batch_test; |
172 | test_mode++; |
173 | break; |
174 | case 'D': |
175 | case 'U': |
176 | case 'I': |
177 | opt[1] = c; |
178 | add_cpp_arg(opt); |
179 | add_cpp_arg(optarg); |
180 | break; |
181 | default: |
182 | usage(name); |
183 | } |
184 | |
185 | if (one && batch != batch_ps && batch != batch_ps_fullpage && |
186 | batch != batch_gnuplot) |
187 | usage(name); |
188 | if (postscript_params.show_key && batch != batch_ps_fullpage) |
189 | usage(name); |
190 | |
191 | if (!batch) { |
192 | args[0] = name; |
193 | args[1] = NULL; |
194 | fake_argc = 1; |
195 | fake_argv = args; |
196 | error = gui_init(&fake_argc, &fake_argv); |
197 | if (error) |
198 | return error; |
199 | } |
200 | |
201 | switch (argc-optind) { |
202 | case 0: |
203 | scan_empty(); |
204 | (void) yyparse(); |
205 | break; |
206 | case 1: |
207 | load_file(argv[optind]); |
208 | save_file_name = argv[optind]; |
209 | break; |
210 | case 2: |
211 | load_file(argv[optind]); |
212 | save_file_name = argv[optind+1]; |
213 | if (!strcmp(save_file_name, "-")) |
214 | save_file_name = NULL; |
215 | break; |
216 | default: |
217 | usage(name); |
218 | } |
219 | |
220 | if (!pkg_name) |
221 | pkg_name = stralloc("_"); |
222 | |
223 | reporter = report_to_stderr; |
224 | if (!instantiate()) |
225 | return 1; |
226 | |
227 | switch (batch) { |
228 | case batch_none: |
229 | error = gui_main(); |
230 | if (error) |
231 | return error; |
232 | break; |
233 | case batch_kicad: |
234 | write_kicad(); |
235 | break; |
236 | case batch_ps: |
237 | write_ps(one); |
238 | break; |
239 | case batch_ps_fullpage: |
240 | write_ps_fullpage(one); |
241 | break; |
242 | case batch_gnuplot: |
243 | write_gnuplot(one); |
244 | break; |
245 | case batch_test: |
246 | if (test_mode > 1) |
247 | dump(stdout, NULL); |
248 | break; |
249 | default: |
250 | abort(); |
251 | } |
252 | |
253 | purge(); |
254 | inst_revert(); |
255 | obj_cleanup(); |
256 | unique_cleanup(); |
257 | |
258 | return 0; |
259 | } |
260 |
Branches:
master