summaryrefslogtreecommitdiff
path: root/main/eeplot.c
blob: 979ece11aead33bdb200b0843995851ba40f9a12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * main/eeplot.c - Plots eeschema schematics
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * 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.
 */


#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>	// for strncasecmp

#include <cairo/cairo.h>

#include "misc/util.h"
#include "misc/diag.h"
#include "gfx/fig.h"
#include "gfx/cro.h"
#include "gfx/gfx.h"
#include "file/file.h"
#include "kicad/ext.h"
#include "kicad/pl.h"
#include "kicad/lib.h"
#include "kicad/sch.h"
#include "kicad/pro.h"
#include "version.h"
#include "main/common.h"
#include "main.h"


static struct gfx_ops const *ops_list[] = {
	&fig_ops,
	&cro_png_ops,
	&cro_pdf_ops,
	&cro_ps_ops,
	&cro_eps_ops,
	&cro_svg_ops,
};


static const struct gfx_ops *find_by_output(int argc, char **argv)
{
	const char *arg, *dot, *colon, *ext;
	const struct gfx_ops *const *ops;
	int i;
	unsigned  n;

	for (i = 1; i != argc; i++)
		if (!strncmp(argv[i], "-o", 2))
			break;
	if (i == argc)
		return NULL;
	arg = argv[i] + 2;
	if (!*arg) {
		arg = argv[i + 1];
		if (!arg)
			fatal("-o requires an argument");
	}

	dot = strrchr(arg, '.');
	colon = strchr(arg, ':');
	if (colon && dot && dot < colon)
		usage(*argv);
	if (colon) {
		ext = arg;
		n = colon - arg;
	} else if (dot) {
		ext = dot + 1;
		n = strlen(ext);
	} else {
		fatal("either extension or \"type:\" prefix required for "
		    "output file");
	}

	for (ops = ops_list; ops != ARRAY_END(ops_list); ops++)
		for (i = 0; i != (*ops)->n_ext; i++)
			if (!strncasecmp((*ops)->ext[i], ext, n) &&
			    strlen((*ops)->ext[i]) == n)
				return *ops;
	fatal("graphics backend for \"%s\" not found\n", ext);
}


void usage(const char *name)
{
	fprintf(stderr,
"usage: %s -o [type:]output_file [-1] [-d date] [-e] [-v ...] [driver_opts]\n"
"       %*skicad_file ...\n"
"       %s -V\n"
"       %s gdb ...\n"
"\n"
"  kicad_file  [rev:]file.ext\n"
"    ext       .pro, .lib, .sch, or .kicad_wks\n"
"    rev       git revision\n"
"\n"
"  -1    show only one sheet - do not recurse into sub-sheets\n"
"  -d date\n"
"        use the specified string instead of date entries in sheets\n"
"  -e    show extra information (e.g., pin types)\n"
"  -o [type:]output_file\n"
"        output file. - for standard output. File type is derived from\n"
"        extension and can be overridden with type: prefix (fig, png, pdf,\n"
"        ps, eps).\n"
"  -v    increase verbosity of diagnostic output\n"
"  -V    print revision (version) number and exit\n"
"  gdb   run eeshow under gdb\n"
"\n"
"FIG files:\n"
"  [-t template.fig] [-D var=value ...]\n"
"\n"
"  -t template.fig  merge this file with generated output\n"
"  -D var=value     substitute \"<var>\" with \"value\" in template\n"
"\n"
"PNG, Postscript, and EPS files:\n"
"  [-s scale]\n"
"\n"
"  -s scale       scale by indicated factor (default: 1.0)\n"
"\n"
"PDF files:\n"
"  [-s scale] [-T]\n"
"\n"
"  see PNG for -s\n"
"  -T  do not add table of contents\n"
    , name, (int) strlen(name) + 1, "", name, name);
	exit(1);
}


#define	OPTIONS	"1d:ehvL:OPV"


int main(int argc, char **argv)
{
	struct lib lib;
	struct sch_ctx sch_ctx;
	struct file pro_file, sch_file;
	bool extra = 0;
	bool one_sheet = 0;
	struct pl_ctx *pl = NULL;
	char c;
	unsigned i;
	struct file_names file_names;
	struct file_names *fn = &file_names;
	char *opts;
	const struct gfx_ops *ops;
	struct gfx *gfx;
	int retval;

	run_under_gdb(argc, argv);

	ops = find_by_output(argc, argv);

	alloc_printf(&opts, "%s%s", OPTIONS, ops ? ops->opts : "");
	while ((c = getopt(argc, argv, opts)) != EOF)
		switch (c) {
		case '1':
			one_sheet = 1;
			break;
		case 'd':
			date_override = optarg;
			break;
		case 'e':
			extra = 1;
			break;
		case 'v':
			verbose++;
			break;
		case 'L':
			suppress_page_layout = 1;
			break;
		case 'O':
			disable_overline = 1;
			break;
		case 'P':
			use_pango = 1;
			break;
		case 'V':
			fprintf(stderr, "%s %s\n", version, build_date);
			return 1;
		case 'h':
		case '?':
			usage(*argv);
		default:
			break;
		}
	free(opts);

	if (!ops)
		fatal("required option \"-o output\" is missing");

	if (argc - optind < 1)
		usage(*argv);

	classify_files(&file_names, argv + optind, argc - optind);
	if (!file_names.pro && !file_names.sch)
		fatal("project or top sheet name required");

	if (file_names.pro) {
		if (!file_open(&pro_file, file_names.pro, NULL))
			return 1;
		fn = pro_parse_file(&pro_file, &file_names);
	}

	gfx = gfx_init(ops);
	if (!gfx_args(gfx, argc, argv, OPTIONS))
		return 1;
	if (!gfx_multi_sheet(gfx))
		one_sheet = 1;

	sch_init(&sch_ctx, !one_sheet);
	if (!file_open(&sch_file, fn->sch, file_names.pro ? &pro_file : NULL))
		return 1;

	lib_init(&lib);
	for (i = 0; i != fn->n_libs; i++)
		if (!lib_parse_search(&lib, fn->libs[i], fn,
		    file_names.pro ? &pro_file : &sch_file))
			return 1;

	if (file_names.pro)
		file_close(&pro_file);

	pl = pl_parse_search(fn->pl, &sch_file);
	if (!pl)
		return 1;

	if (fn != &file_names) {
		free_file_names(fn);
		free(fn);
	}
	free_file_names(&file_names);

	if (!sch_parse(&sch_ctx, &sch_file, &lib, NULL))
		return 1;
	file_close(&sch_file);

	if (one_sheet) {
		sch_render(sch_ctx.sheets, gfx);
		if (pl)
			pl_render(pl, gfx, sch_ctx.sheets, sch_ctx.sheets);
	} else {
		const struct sheet *sheet;

		for (sheet = sch_ctx.sheets; sheet; sheet = sheet->next) {
			gfx_sheet_name(gfx, sheet->title);
			sch_render(sheet, gfx);
			if (pl)
				pl_render(pl, gfx, sch_ctx.sheets, sheet);
			if (sheet->next)
				gfx_new_sheet(gfx);
		}
	}
	retval = gfx_end(gfx, extra ? gfx_pin_type : 0);

	sch_free(&sch_ctx);
	lib_free(&lib);
	if (pl)
		pl_free(pl);

	file_cleanup();
	cairo_debug_reset_static_data();

	return retval;
}