2009-08-03 19:12:47 +03:00
|
|
|
/*
|
|
|
|
* fped.c - Footprint editor, main function
|
|
|
|
*
|
2010-02-20 00:27:25 +02:00
|
|
|
* Written 2009, 2010 by Werner Almesberger
|
|
|
|
* Copyright 2009, 2010 by Werner Almesberger
|
2009-08-03 19:12:47 +03:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-08-10 16:51:51 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2009-08-11 23:17:39 +03:00
|
|
|
#include <unistd.h>
|
2010-04-20 04:11:45 +03:00
|
|
|
#include <errno.h>
|
2009-08-03 19:12:47 +03:00
|
|
|
|
|
|
|
#include "cpp.h"
|
2009-08-07 03:48:06 +03:00
|
|
|
#include "util.h"
|
2009-08-03 19:12:47 +03:00
|
|
|
#include "error.h"
|
|
|
|
#include "obj.h"
|
|
|
|
#include "inst.h"
|
2009-08-11 23:17:39 +03:00
|
|
|
#include "file.h"
|
2010-04-20 04:11:45 +03:00
|
|
|
#include "dump.h"
|
2009-08-03 19:12:47 +03:00
|
|
|
#include "gui.h"
|
2009-08-21 11:34:17 +03:00
|
|
|
#include "delete.h"
|
2009-08-16 07:12:37 +03:00
|
|
|
#include "fpd.h"
|
2010-04-20 04:11:45 +03:00
|
|
|
#include "fped.h"
|
2009-08-03 19:12:47 +03:00
|
|
|
|
|
|
|
|
2009-08-11 23:17:39 +03:00
|
|
|
char *save_file_name = NULL;
|
2010-04-20 04:11:45 +03:00
|
|
|
int no_save = 0;
|
2009-08-10 16:51:51 +03:00
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
|
|
|
|
static void load_file(const char *name)
|
|
|
|
{
|
2010-04-20 04:11:45 +03:00
|
|
|
FILE *file;
|
|
|
|
char line[sizeof(MACHINE_GENERATED)];
|
|
|
|
|
|
|
|
file = fopen(name, "r");
|
|
|
|
if (file) {
|
|
|
|
if (!fgets(line, sizeof(line), file)) {
|
2010-04-20 06:16:09 +03:00
|
|
|
if (ferror(file)) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
*line = 0;
|
2010-04-20 04:11:45 +03:00
|
|
|
}
|
|
|
|
no_save = strcmp(line, MACHINE_GENERATED);
|
|
|
|
fclose(file);
|
2009-08-12 02:26:38 +03:00
|
|
|
reporter = report_parse_error;
|
|
|
|
run_cpp_on_file(name);
|
|
|
|
} else {
|
2010-04-20 04:11:45 +03:00
|
|
|
if (errno != ENOENT) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-08-12 02:26:38 +03:00
|
|
|
scan_empty();
|
|
|
|
}
|
2009-08-03 19:12:47 +03:00
|
|
|
(void) yyparse();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-10 16:51:51 +03:00
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
2009-10-21 02:50:45 +03:00
|
|
|
fprintf(stderr,
|
2010-02-20 00:27:25 +02:00
|
|
|
"usage: %s [-k] [-p|-P] [cpp_option ...] [in_file [out_file]]\n\n"
|
2009-10-21 02:50:45 +03:00
|
|
|
" -k write KiCad output, then exit\n"
|
|
|
|
" -p write Postscript output, then exit\n"
|
2010-02-20 00:27:25 +02:00
|
|
|
" -P write Postscript output (full page), then exit\n"
|
2009-10-21 02:50:45 +03:00
|
|
|
" cpp_option -Idir, -Dname[=value], or -Uname\n"
|
|
|
|
, name);
|
2009-08-10 16:51:51 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2009-08-30 09:04:00 +03:00
|
|
|
char *name = *argv;
|
|
|
|
char **fake_argv;
|
|
|
|
char *args[2];
|
|
|
|
int fake_argc;
|
2009-10-21 02:50:45 +03:00
|
|
|
char opt[] = "-?";
|
2009-08-30 09:04:00 +03:00
|
|
|
int error, batch;
|
2010-02-20 00:27:25 +02:00
|
|
|
int batch_write_kicad = 0;
|
|
|
|
int batch_write_ps = 0, batch_write_ps_fullpage = 0;
|
2009-08-11 23:17:39 +03:00
|
|
|
int c;
|
2009-08-03 19:12:47 +03:00
|
|
|
|
2010-02-20 00:27:25 +02:00
|
|
|
while ((c = getopt(argc, argv, "kpD:I:U:P")) != EOF)
|
2009-08-11 23:17:39 +03:00
|
|
|
switch (c) {
|
|
|
|
case 'k':
|
|
|
|
batch_write_kicad = 1;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
batch_write_ps = 1;
|
|
|
|
break;
|
2010-02-20 00:27:25 +02:00
|
|
|
case 'P':
|
|
|
|
batch_write_ps_fullpage = 1;
|
|
|
|
break;
|
2009-10-21 02:50:45 +03:00
|
|
|
case 'D':
|
|
|
|
case 'U':
|
|
|
|
case 'I':
|
|
|
|
opt[1] = c;
|
|
|
|
add_cpp_arg(opt);
|
|
|
|
add_cpp_arg(optarg);
|
|
|
|
break;
|
2009-08-11 23:17:39 +03:00
|
|
|
default:
|
|
|
|
usage(name);
|
|
|
|
}
|
|
|
|
|
2010-02-20 00:27:25 +02:00
|
|
|
if (batch_write_ps && batch_write_ps_fullpage)
|
|
|
|
usage(name);
|
|
|
|
|
|
|
|
batch = batch_write_kicad || batch_write_ps || batch_write_ps_fullpage;
|
2009-08-30 09:04:00 +03:00
|
|
|
|
|
|
|
if (!batch) {
|
|
|
|
args[0] = name;
|
|
|
|
args[1] = NULL;
|
|
|
|
fake_argc = 1;
|
|
|
|
fake_argv = args;
|
|
|
|
error = gui_init(&fake_argc, &fake_argv);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2009-08-11 23:17:39 +03:00
|
|
|
switch (argc-optind) {
|
|
|
|
case 0:
|
2009-08-03 19:12:47 +03:00
|
|
|
scan_empty();
|
|
|
|
(void) yyparse();
|
2009-08-10 16:51:51 +03:00
|
|
|
break;
|
2009-08-11 23:17:39 +03:00
|
|
|
case 1:
|
|
|
|
load_file(argv[optind]);
|
|
|
|
save_file_name = argv[optind];
|
|
|
|
break;
|
2009-08-10 16:51:51 +03:00
|
|
|
case 2:
|
2009-08-11 23:17:39 +03:00
|
|
|
load_file(argv[optind]);
|
|
|
|
save_file_name = argv[optind+1];
|
|
|
|
if (!strcmp(save_file_name, "-"))
|
|
|
|
save_file_name = NULL;
|
2009-08-10 16:51:51 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(name);
|
2009-08-03 19:12:47 +03:00
|
|
|
}
|
2009-08-07 03:48:06 +03:00
|
|
|
|
2009-08-17 23:42:51 +03:00
|
|
|
if (!pkg_name)
|
|
|
|
pkg_name = stralloc("_");
|
2009-08-07 03:48:06 +03:00
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
reporter = report_to_stderr;
|
|
|
|
if (!instantiate())
|
|
|
|
return 1;
|
2009-08-11 23:17:39 +03:00
|
|
|
|
|
|
|
if (batch_write_kicad)
|
|
|
|
write_kicad();
|
|
|
|
if (batch_write_ps)
|
|
|
|
write_ps();
|
2010-02-20 00:27:25 +02:00
|
|
|
if (batch_write_ps_fullpage)
|
|
|
|
write_ps_fullpage();
|
2009-08-30 09:04:00 +03:00
|
|
|
if (!batch) {
|
2009-08-21 11:34:17 +03:00
|
|
|
error = gui_main();
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
purge();
|
|
|
|
inst_revert();
|
|
|
|
obj_cleanup();
|
|
|
|
unique_cleanup();
|
2009-08-05 13:35:48 +03:00
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
return 0;
|
|
|
|
}
|