Root/
Source at commit 6ca5c22ea48babba3cf578f80a19fbb140e17c15 created 12 years 6 months ago. By Werner Almesberger, fped.c: cleaned up batch mode selection; batch modes are now mutually exclusive | |
---|---|
1 | /* |
2 | * fped.c - Footprint editor, main function |
3 | * |
4 | * Written 2009-2011 by Werner Almesberger |
5 | * Copyright 2009-2011 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 | " -k write KiCad output, then exit\n" |
73 | " -p write Postscript output, then exit\n" |
74 | " -P [-s scale] [-1 package]\n" |
75 | " write Postscript output (full page), then exit\n" |
76 | " -1 name output only the specified package\n" |
77 | " -s scale scale factor for -P (default: auto-scale)\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 | " cpp_option -Idir, -Dname[=value], or -Uname\n" |
82 | , name); |
83 | exit(1); |
84 | } |
85 | |
86 | |
87 | int main(int argc, char **argv) |
88 | { |
89 | enum { |
90 | batch_none = 0, |
91 | batch_kicad, |
92 | batch_ps, |
93 | batch_ps_fullpage, |
94 | batch_test |
95 | } batch = batch_none; |
96 | char *name = *argv; |
97 | char **fake_argv; |
98 | char *args[2]; |
99 | int fake_argc; |
100 | char opt[] = "-?"; |
101 | char *end; |
102 | int error; |
103 | int test_mode = 0; |
104 | const char *one = NULL; |
105 | int c; |
106 | |
107 | while ((c = getopt(argc, argv, "1:kps:D:I:PTU:")) != EOF) |
108 | switch (c) { |
109 | case '1': |
110 | one = optarg; |
111 | break; |
112 | case 'k': |
113 | if (batch) |
114 | usage(*argv); |
115 | batch = batch_kicad; |
116 | break; |
117 | case 'p': |
118 | if (batch) |
119 | usage(*argv); |
120 | batch = batch_ps; |
121 | break; |
122 | case 'P': |
123 | if (batch) |
124 | usage(*argv); |
125 | batch = batch_ps_fullpage; |
126 | break; |
127 | case 's': |
128 | if (batch != batch_ps_fullpage) |
129 | usage(*argv); |
130 | postscript_params.zoom = strtod(optarg, &end); |
131 | if (*end) |
132 | usage(*argv); |
133 | break; |
134 | case 'T': |
135 | batch = batch_test; |
136 | test_mode++; |
137 | break; |
138 | case 'D': |
139 | case 'U': |
140 | case 'I': |
141 | opt[1] = c; |
142 | add_cpp_arg(opt); |
143 | add_cpp_arg(optarg); |
144 | break; |
145 | default: |
146 | usage(name); |
147 | } |
148 | |
149 | if (one && batch != batch_ps && batch != batch_ps_fullpage) |
150 | usage(name); |
151 | |
152 | if (!batch) { |
153 | args[0] = name; |
154 | args[1] = NULL; |
155 | fake_argc = 1; |
156 | fake_argv = args; |
157 | error = gui_init(&fake_argc, &fake_argv); |
158 | if (error) |
159 | return error; |
160 | } |
161 | |
162 | switch (argc-optind) { |
163 | case 0: |
164 | scan_empty(); |
165 | (void) yyparse(); |
166 | break; |
167 | case 1: |
168 | load_file(argv[optind]); |
169 | save_file_name = argv[optind]; |
170 | break; |
171 | case 2: |
172 | load_file(argv[optind]); |
173 | save_file_name = argv[optind+1]; |
174 | if (!strcmp(save_file_name, "-")) |
175 | save_file_name = NULL; |
176 | break; |
177 | default: |
178 | usage(name); |
179 | } |
180 | |
181 | if (!pkg_name) |
182 | pkg_name = stralloc("_"); |
183 | |
184 | reporter = report_to_stderr; |
185 | if (!instantiate()) |
186 | return 1; |
187 | |
188 | switch (batch) { |
189 | case batch_none: |
190 | error = gui_main(); |
191 | if (error) |
192 | return error; |
193 | break; |
194 | case batch_kicad: |
195 | write_kicad(); |
196 | break; |
197 | case batch_ps: |
198 | write_ps(one); |
199 | break; |
200 | case batch_ps_fullpage: |
201 | write_ps_fullpage(one); |
202 | break; |
203 | case batch_test: |
204 | dump(stdout, NULL); |
205 | break; |
206 | default: |
207 | abort(); |
208 | } |
209 | |
210 | purge(); |
211 | inst_revert(); |
212 | obj_cleanup(); |
213 | unique_cleanup(); |
214 | |
215 | return 0; |
216 | } |
217 |
Branches:
master