Root/
Source at commit ceaa519ccb1b6d400ff57c692a13f183f5f6af61 created 13 years 3 months ago. By Xiangfu Liu, update to r5997 | |
---|---|
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 [-k] [-p|-P [-s scale] [-1 package]] [-T [-T]] [cpp_option ...]\n" |
71 | " %*s [in_file [out_file]]\n\n" |
72 | " -1 name output only the specified package\n" |
73 | " -k write KiCad output, then exit\n" |
74 | " -p write Postscript output, then exit\n" |
75 | " -P write Postscript output (full page), then exit\n" |
76 | " -s scale scale factor for -P (default: auto-scale)\n" |
77 | " -T test mode. Load file, then exit\n" |
78 | " -T -T test mode. Load file, dump to stdout, then exit\n" |
79 | " cpp_option -Idir, -Dname[=value], or -Uname\n" |
80 | , name, (int) strlen(name), ""); |
81 | exit(1); |
82 | } |
83 | |
84 | |
85 | int main(int argc, char **argv) |
86 | { |
87 | char *name = *argv; |
88 | char **fake_argv; |
89 | char *args[2]; |
90 | int fake_argc; |
91 | char opt[] = "-?"; |
92 | char *end; |
93 | int error; |
94 | int batch = 0; |
95 | int test_mode = 0; |
96 | const char *one = NULL; |
97 | int batch_write_kicad = 0; |
98 | int batch_write_ps = 0, batch_write_ps_fullpage = 0; |
99 | int c; |
100 | |
101 | while ((c = getopt(argc, argv, "1:kps:D:I:PTU:")) != EOF) |
102 | switch (c) { |
103 | case '1': |
104 | one = optarg; |
105 | break; |
106 | case 'k': |
107 | batch_write_kicad = 1; |
108 | break; |
109 | case 'p': |
110 | batch_write_ps = 1; |
111 | break; |
112 | case 'P': |
113 | batch_write_ps_fullpage = 1; |
114 | break; |
115 | case 's': |
116 | if (!batch_write_ps_fullpage) |
117 | usage(*argv); |
118 | postscript_params.zoom = strtod(optarg, &end); |
119 | if (*end) |
120 | usage(*argv); |
121 | break; |
122 | case 'T': |
123 | batch = 1; |
124 | test_mode++; |
125 | break; |
126 | case 'D': |
127 | case 'U': |
128 | case 'I': |
129 | opt[1] = c; |
130 | add_cpp_arg(opt); |
131 | add_cpp_arg(optarg); |
132 | break; |
133 | default: |
134 | usage(name); |
135 | } |
136 | |
137 | if (batch_write_ps && batch_write_ps_fullpage) |
138 | usage(name); |
139 | |
140 | if (one && !(batch_write_ps || batch_write_ps_fullpage)) |
141 | usage(name); |
142 | |
143 | if (batch_write_kicad || batch_write_ps || batch_write_ps_fullpage) |
144 | batch = 1; |
145 | |
146 | if (!batch) { |
147 | args[0] = name; |
148 | args[1] = NULL; |
149 | fake_argc = 1; |
150 | fake_argv = args; |
151 | error = gui_init(&fake_argc, &fake_argv); |
152 | if (error) |
153 | return error; |
154 | } |
155 | |
156 | switch (argc-optind) { |
157 | case 0: |
158 | scan_empty(); |
159 | (void) yyparse(); |
160 | break; |
161 | case 1: |
162 | load_file(argv[optind]); |
163 | save_file_name = argv[optind]; |
164 | break; |
165 | case 2: |
166 | load_file(argv[optind]); |
167 | save_file_name = argv[optind+1]; |
168 | if (!strcmp(save_file_name, "-")) |
169 | save_file_name = NULL; |
170 | break; |
171 | default: |
172 | usage(name); |
173 | } |
174 | |
175 | if (!pkg_name) |
176 | pkg_name = stralloc("_"); |
177 | |
178 | reporter = report_to_stderr; |
179 | if (!instantiate()) |
180 | return 1; |
181 | |
182 | if (batch_write_kicad) |
183 | write_kicad(); |
184 | if (batch_write_ps) |
185 | write_ps(one); |
186 | if (batch_write_ps_fullpage) |
187 | write_ps_fullpage(one); |
188 | if (!batch) { |
189 | error = gui_main(); |
190 | if (error) |
191 | return error; |
192 | } |
193 | if (test_mode > 1) |
194 | dump(stdout, NULL); |
195 | |
196 | purge(); |
197 | inst_revert(); |
198 | obj_cleanup(); |
199 | unique_cleanup(); |
200 | |
201 | return 0; |
202 | } |
203 |
Branches:
master