Root/fped.c

Source at commit bc27b094af74a7c6f8b71ceaa63d1ba80d067cbb created 9 years 4 months ago.
By werner, With a little help from m8cutils and abyss, we now have regression tests for the topological sort. "make test" or "make tests" invokes the regression tests, "make valgrind" runs them under valgrind's watchful eyes.
1/*
2 * fped.c - Footprint editor, main function
3 *
4 * Written 2009, 2010 by Werner Almesberger
5 * Copyright 2009, 2010 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 <errno.h>
18
19#include "cpp.h"
20#include "util.h"
21#include "error.h"
22#include "obj.h"
23#include "inst.h"
24#include "file.h"
25#include "dump.h"
26#include "gui.h"
27#include "delete.h"
28#include "fpd.h"
29#include "fped.h"
30
31
32char *save_file_name = NULL;
33int no_save = 0;
34
35
36static void load_file(const char *name)
37{
38    FILE *file;
39    char line[sizeof(MACHINE_GENERATED)];
40
41    file = fopen(name, "r");
42    if (file) {
43        if (!fgets(line, sizeof(line), file)) {
44            if (ferror(file)) {
45                perror(name);
46                exit(1);
47            }
48            *line = 0;
49        }
50        no_save = strcmp(line, MACHINE_GENERATED);
51        fclose(file);
52        reporter = report_parse_error;
53        run_cpp_on_file(name);
54    } else {
55        if (errno != ENOENT) {
56            perror(name);
57            exit(1);
58        }
59        scan_empty();
60    }
61    (void) yyparse();
62}
63
64
65static void usage(const char *name)
66{
67    fprintf(stderr,
68"usage: %s [-k] [-p|-P] [-T] [cpp_option ...] [in_file [out_file]]\n\n"
69" -k write KiCad output, then exit\n"
70" -p write Postscript output, then exit\n"
71" -P write Postscript output (full page), then exit\n"
72" -T test mode. Load file, then exit\n"
73" cpp_option -Idir, -Dname[=value], or -Uname\n"
74    , name);
75    exit(1);
76}
77
78
79int main(int argc, char **argv)
80{
81    char *name = *argv;
82    char **fake_argv;
83    char *args[2];
84    int fake_argc;
85    char opt[] = "-?";
86    int error;
87    int batch = 0;
88    int batch_write_kicad = 0;
89    int batch_write_ps = 0, batch_write_ps_fullpage = 0;
90    int c;
91
92    while ((c = getopt(argc, argv, "kpD:I:PTU:")) != EOF)
93        switch (c) {
94        case 'k':
95            batch_write_kicad = 1;
96            break;
97        case 'p':
98            batch_write_ps = 1;
99            break;
100        case 'P':
101            batch_write_ps_fullpage = 1;
102            break;
103        case 'T':
104            batch = 1;
105            break;
106        case 'D':
107        case 'U':
108        case 'I':
109            opt[1] = c;
110            add_cpp_arg(opt);
111            add_cpp_arg(optarg);
112            break;
113        default:
114            usage(name);
115        }
116
117    if (batch_write_ps && batch_write_ps_fullpage)
118        usage(name);
119
120    if (batch_write_kicad || batch_write_ps || batch_write_ps_fullpage)
121        batch = 1;
122
123    if (!batch) {
124        args[0] = name;
125        args[1] = NULL;
126        fake_argc = 1;
127        fake_argv = args;
128        error = gui_init(&fake_argc, &fake_argv);
129        if (error)
130            return error;
131    }
132
133    switch (argc-optind) {
134    case 0:
135        scan_empty();
136        (void) yyparse();
137        break;
138    case 1:
139        load_file(argv[optind]);
140        save_file_name = argv[optind];
141        break;
142    case 2:
143        load_file(argv[optind]);
144        save_file_name = argv[optind+1];
145        if (!strcmp(save_file_name, "-"))
146            save_file_name = NULL;
147        break;
148    default:
149        usage(name);
150    }
151
152    if (!pkg_name)
153        pkg_name = stralloc("_");
154
155    reporter = report_to_stderr;
156    if (!instantiate())
157        return 1;
158
159    if (batch_write_kicad)
160        write_kicad();
161    if (batch_write_ps)
162        write_ps();
163    if (batch_write_ps_fullpage)
164        write_ps_fullpage();
165    if (!batch) {
166        error = gui_main();
167        if (error)
168            return error;
169    }
170
171    purge();
172    inst_revert();
173    obj_cleanup();
174    unique_cleanup();
175
176    return 0;
177}
178

Archive Download this file

Branches:
master



interactive