summaryrefslogtreecommitdiff
path: root/main.c
blob: 1ca8032ef274834d4c1ff6624a8427c8f9cf2d1d (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
/*
 * main.c - Convert Eeschema schematics to FIG
 *
 * 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 <gtk/gtk.h>

#include "util.h"
#include "diag.h"
#include "fig.h"
#include "cro.h"
#include "diff.h"
#include "gfx.h"
#include "file/file.h"
#include "kicad/lib.h"
#include "kicad/sch.h"
#include "gui/fmt-pango.h"
#include "file/git-hist.h"
#include "gui/gui.h"
#include "main.h"


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


void usage(const char *name)
{
	fprintf(stderr,
"usage: %s [gtk_flags] [-r] [-N n] [[rev:]file.lib ...] [rev:]file.sch\n"
"       %s [-r] [-v ...] [[rev:]file.lib ...] [rev:]file.sch\n"
"       %*s[-- driver_spec]\n"
"       %s [-v ...] -C [rev:]file\n"
"       %s [-v ...] -H path_into_repo\n"
"\n"
"  rev   git revision\n"
"  -r    recurse into sub-sheets\n"
"  -v    increase verbosity of diagnostic output\n"
"  -C    'cat' the file to standard output\n"
"  -H    show history of repository on standard output\n"
"  -N n  limit history to n revisions (unlimited if omitted or 0)\n"
"\n"
"No driver spec: enter GUI\n"
"\n"
"FIG driver spec:\n"
"  fig [-t template.fig] [var=value ...]\n"
"\n"
"  var=value        substitute \"<var>\" with \"value\" in template\n"
"  -t template.fig  merge this file with generated output\n"
"\n"
"Cairo PNG driver spec:\n"
"  png [-o output.png] [-s scale]\n"
"\n"
"  -o output.png  write PNG to specified file (default; standard output)\n"
"  -s scale       scale by indicated factor (default: 1.0)\n"
"\n"
"Cairo PDF driver spec:\n"
"  pdf [-o output.pdf] [-s scale]\n"
"\n"
"  see PNG\n"
"\n"
"Diff driver spec:\n"
"  diff [-o output.pdf] [-s scale] [file.lib ...] file.sch\n"
"\n"
"  see PNG\n"
    , name, name, (int) strlen(name) + 1, "", name, name);
	exit(1);
}


int main(int argc, char **argv)
{
	struct lib lib;
	struct sch_ctx sch_ctx;
	struct file sch_file;
	bool recurse = 0;
	const char *cat = NULL;
	const char *history = NULL;
	const char *fmt = NULL;
	int limit = 0;
	char c;
	int arg, dashdash;
	bool have_dashdash = 0;
	int gfx_argc;
	char **gfx_argv;
	const struct gfx_ops **ops = ops_list;

	for (dashdash = 1; dashdash != argc; dashdash++)
		if (!strcmp(argv[dashdash], "--")) {
			have_dashdash = 1;
			break;
		}

	if (!have_dashdash)
		gtk_init(&argc, &argv);

	while ((c = getopt(dashdash, argv, "rvC:F:H:N:")) != EOF)
		switch (c) {
		case 'r':
			recurse = 1;
			break;
		case 'v':
			verbose++;
			break;
		case 'C':
			cat = optarg;
			break;
		case 'F':
			fmt = optarg;
			break;
		case 'H':
			history = optarg;
			break;
		case 'N':
			limit = atoi(optarg);
			break;
		default:
			usage(*argv);
		}

	if (cat) {
		struct file file;

		if (argc != optind)
			usage(*argv);
		if (!file_open(&file, cat, NULL))
			return 1;
		if (!file_read(&file, file_cat, NULL))
			return 1;
		file_close(&file);
		return 0;
	}

	if (history) {
		struct hist *h;

		h = vcs_git_hist(history);
		dump_hist(h);
		return 0;
	}

	if (fmt) {
		char *buf;

		buf = fmt_pango(fmt, argv[optind]);
		printf("\"%s\"\n", buf);
		return 0;
	}

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

	if (!have_dashdash) {
		unsigned n = argc - optind;
		char **args;

		args = alloc_type_n(char *, n);
		memcpy(args, argv + optind, sizeof(const char *) * n);
	
		optind = 0; /* reset getopt */
		return gui(n, args, recurse, limit);
	}

	sch_init(&sch_ctx, recurse);
	if (!file_open(&sch_file, argv[dashdash - 1], NULL))
		return 1;

	lib_init(&lib);
	for (arg = optind; arg != dashdash - 1; arg++)
		if (!lib_parse(&lib, argv[arg], &sch_file))
			return 1;

	if (dashdash == argc) {
		gfx_argc = 1;
		gfx_argv = alloc_type_n(char *, 2);
		gfx_argv[0] = (char *) (*ops)->name;
		gfx_argv[1] = NULL;
	} else {
		gfx_argc = argc - dashdash - 1;
		if (!gfx_argc)
			usage(*argv);
		gfx_argv = alloc_type_n(char *, gfx_argc + 1);
		memcpy(gfx_argv, argv + dashdash + 1,
		    sizeof(const char *) * (gfx_argc + 1));

		for (ops = ops_list; ops != ARRAY_END(ops_list); ops++)
			if (!strcmp((*ops)->name, *gfx_argv))
				goto found;
		fatal("graphics backend \"%s\" not found\n", *gfx_argv);
found:
		;
	}

	optind = 0; /* reset getopt */

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

	gfx_init(*ops, gfx_argc, gfx_argv);
	if (recurse) {
		const struct sheet *sheet;

		if (!gfx_multi_sheet())
			fatal("graphics backend only supports single sheet\n");
		for (sheet = sch_ctx.sheets; sheet; sheet = sheet->next) {
			sch_render(sheet);
			if (sheet->next)
				gfx_new_sheet();
		}
	} else {
		sch_render(sch_ctx.sheets);
	}
	gfx_end();

	sch_free(&sch_ctx);
	lib_free(&lib);

	return 0;
}