2009-08-03 19:12:47 +03:00
|
|
|
/*
|
|
|
|
* fped.c - Footprint editor, main function
|
|
|
|
*
|
2012-07-13 00:16:54 +03:00
|
|
|
* Written 2009-2012 by Werner Almesberger
|
|
|
|
* Copyright 2009-2012 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-05-03 16:41:24 +03:00
|
|
|
#include <string.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-05-03 16:41:24 +03:00
|
|
|
#include "postscript.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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-21 07:59:46 +03:00
|
|
|
void reload(void)
|
|
|
|
{
|
|
|
|
struct frame *old_frames;
|
|
|
|
|
|
|
|
/* @@@ this needs more work */
|
|
|
|
purge();
|
|
|
|
old_frames = frames;
|
|
|
|
scan_file();
|
|
|
|
load_file(save_file_name);
|
|
|
|
if (!instantiate())
|
|
|
|
frames = old_frames;
|
|
|
|
change_world();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-10 16:51:51 +03:00
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
2009-10-21 02:50:45 +03:00
|
|
|
fprintf(stderr,
|
2011-09-04 23:22:10 +03:00
|
|
|
"usage: %s [batch_mode] [cpp_option ...] [in_file [out_file]]\n\n"
|
|
|
|
"Batch mode options:\n"
|
2011-09-05 02:35:34 +03:00
|
|
|
" -g [-1 package]\n"
|
|
|
|
" write gnuplot output, then exit\n"
|
2009-10-21 02:50:45 +03:00
|
|
|
" -k write KiCad output, then exit\n"
|
|
|
|
" -p write Postscript output, then exit\n"
|
2012-07-19 04:09:47 +03:00
|
|
|
" -P [-K] [-s scale] [-1 package]\n"
|
2011-09-04 23:22:10 +03:00
|
|
|
" write Postscript output (full page), then exit\n"
|
2010-04-27 00:30:21 +03:00
|
|
|
" -T test mode. Load file, then exit\n"
|
2011-09-04 23:22:10 +03:00
|
|
|
" -T -T test mode. Load file, dump to stdout, then exit\n\n"
|
|
|
|
"Common options:\n"
|
2011-09-05 02:35:34 +03:00
|
|
|
" -1 name output only the specified package\n"
|
2012-07-19 04:09:47 +03:00
|
|
|
" -K show the pad type key\n"
|
2011-09-05 02:35:34 +03:00
|
|
|
" -s scale scale factor for -P (default: auto-scale)\n"
|
2012-07-13 00:16:54 +03:00
|
|
|
" -s [width]x[heigth]\n"
|
|
|
|
" auto-scale to fit within specified box. Dimensions in mm.\n"
|
2009-10-21 02:50:45 +03:00
|
|
|
" cpp_option -Idir, -Dname[=value], or -Uname\n"
|
2011-09-04 23:22:10 +03:00
|
|
|
, name);
|
2009-08-10 16:51:51 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-13 00:16:54 +03:00
|
|
|
static int parse_scaling(const char *arg)
|
|
|
|
{
|
|
|
|
const char *x;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
x = strchr(arg, 'x');
|
|
|
|
if (!x) {
|
|
|
|
postscript_params.zoom = strtod(arg, &end);
|
|
|
|
return !*end;
|
|
|
|
}
|
|
|
|
if (x != arg) {
|
|
|
|
postscript_params.max_width = mm_to_units(strtod(arg, &end));
|
|
|
|
if (*end != 'x')
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (x[1]) {
|
|
|
|
postscript_params.max_height = mm_to_units(strtod(x+1, &end));
|
|
|
|
if (*end)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2011-09-04 23:22:10 +03:00
|
|
|
enum {
|
|
|
|
batch_none = 0,
|
|
|
|
batch_kicad,
|
|
|
|
batch_ps,
|
|
|
|
batch_ps_fullpage,
|
2011-09-05 02:35:34 +03:00
|
|
|
batch_gnuplot,
|
2011-09-04 23:22:10 +03:00
|
|
|
batch_test
|
|
|
|
} batch = batch_none;
|
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[] = "-?";
|
2010-04-27 00:30:21 +03:00
|
|
|
int error;
|
2010-04-27 02:11:22 +03:00
|
|
|
int test_mode = 0;
|
2011-03-23 09:14:13 +02:00
|
|
|
const char *one = NULL;
|
2009-08-11 23:17:39 +03:00
|
|
|
int c;
|
2009-08-03 19:12:47 +03:00
|
|
|
|
2012-07-19 04:09:47 +03:00
|
|
|
while ((c = getopt(argc, argv, "1:gkps:D:I:KPTU:")) != EOF)
|
2009-08-11 23:17:39 +03:00
|
|
|
switch (c) {
|
2011-03-23 09:14:13 +02:00
|
|
|
case '1':
|
|
|
|
one = optarg;
|
|
|
|
break;
|
2011-09-05 02:35:34 +03:00
|
|
|
case 'g':
|
|
|
|
if (batch)
|
|
|
|
usage(*argv);
|
|
|
|
batch = batch_gnuplot;
|
|
|
|
break;
|
2009-08-11 23:17:39 +03:00
|
|
|
case 'k':
|
2011-09-04 23:22:10 +03:00
|
|
|
if (batch)
|
|
|
|
usage(*argv);
|
|
|
|
batch = batch_kicad;
|
2009-08-11 23:17:39 +03:00
|
|
|
break;
|
|
|
|
case 'p':
|
2011-09-04 23:22:10 +03:00
|
|
|
if (batch)
|
|
|
|
usage(*argv);
|
|
|
|
batch = batch_ps;
|
2009-08-11 23:17:39 +03:00
|
|
|
break;
|
2010-02-20 00:27:25 +02:00
|
|
|
case 'P':
|
2011-09-04 23:22:10 +03:00
|
|
|
if (batch)
|
|
|
|
usage(*argv);
|
|
|
|
batch = batch_ps_fullpage;
|
2010-02-20 00:27:25 +02:00
|
|
|
break;
|
2012-07-19 04:09:47 +03:00
|
|
|
case 'K':
|
|
|
|
postscript_params.show_key = 1;
|
|
|
|
break;
|
2010-05-03 16:41:24 +03:00
|
|
|
case 's':
|
2011-09-04 23:22:10 +03:00
|
|
|
if (batch != batch_ps_fullpage)
|
2010-05-03 16:41:24 +03:00
|
|
|
usage(*argv);
|
2012-07-13 00:16:54 +03:00
|
|
|
if (!parse_scaling(optarg))
|
2010-05-03 16:41:24 +03:00
|
|
|
usage(*argv);
|
|
|
|
break;
|
2010-04-27 00:30:21 +03:00
|
|
|
case 'T':
|
2011-09-04 23:22:10 +03:00
|
|
|
batch = batch_test;
|
2010-04-27 02:11:22 +03:00
|
|
|
test_mode++;
|
2010-04-27 00:30:21 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-05 02:35:34 +03:00
|
|
|
if (one && batch != batch_ps && batch != batch_ps_fullpage &&
|
|
|
|
batch != batch_gnuplot)
|
2010-02-20 00:27:25 +02:00
|
|
|
usage(name);
|
2012-07-19 04:09:47 +03:00
|
|
|
if (postscript_params.show_key && batch != batch_ps_fullpage)
|
|
|
|
usage(name);
|
2010-02-20 00:27:25 +02:00
|
|
|
|
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
|
|
|
|
2011-09-04 23:22:10 +03:00
|
|
|
switch (batch) {
|
|
|
|
case batch_none:
|
2009-08-21 11:34:17 +03:00
|
|
|
error = gui_main();
|
|
|
|
if (error)
|
|
|
|
return error;
|
2011-09-04 23:22:10 +03:00
|
|
|
break;
|
|
|
|
case batch_kicad:
|
|
|
|
write_kicad();
|
|
|
|
break;
|
|
|
|
case batch_ps:
|
|
|
|
write_ps(one);
|
|
|
|
break;
|
|
|
|
case batch_ps_fullpage:
|
|
|
|
write_ps_fullpage(one);
|
|
|
|
break;
|
2011-09-05 02:35:34 +03:00
|
|
|
case batch_gnuplot:
|
|
|
|
write_gnuplot(one);
|
|
|
|
break;
|
2011-09-04 23:22:10 +03:00
|
|
|
case batch_test:
|
2011-10-25 22:05:22 +03:00
|
|
|
if (test_mode > 1)
|
|
|
|
dump(stdout, NULL);
|
2011-09-04 23:22:10 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2009-08-21 11:34:17 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|