summaryrefslogtreecommitdiff
path: root/main/eetest.c
blob: a80d22232f7686ecbed8ae16c682b6ba59b32740 (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
/*
 * main/eetest.c - Subsystem tests (for development)
 *
 * 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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "misc/util.h"
#include "misc/diag.h"
#include "file/file.h"
#include "kicad/sexpr.h"
#include "gui/fmt-pango.h"
#include "file/git-hist.h"
#include "version.h"
#include "main/common.h"


static void sexpr(void)
{
	struct sexpr_ctx *parser = sexpr_new();
	struct expr *expr;
	char *buf = NULL;
	size_t n = 0;

	while (getline(&buf, &n, stdin) > 0)
		sexpr_parse(parser, buf);
	if (sexpr_finish(parser, &expr))
		dump_expr(expr);
	else
		exit(1);
}


static void usage(const char *name)
{
	fprintf(stderr,
"usage: %s [-v ...] -C [rev:]file\n"
"       %s [-v ...] -H path_into_repo\n"
"       %s -S\n"
"       %s -V\n"
"       %s gdb ...\n"
"\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"
"  -S    parse S-expressions from stdin and dump to stdout\n"
"  -V    print revision (version) number and exit\n"
"  gdb   run eeshow under gdb\n"
    , name, name, name, name, name);
	exit(1);
}


int main(int argc, char **argv)
{
	const char *cat = NULL;
	const char *history = NULL;
	const char *fmt = NULL;
	int c;

	run_under_gdb(argc, argv);

	while ((c = getopt(argc, argv, "hvC:F:H:SV")) != EOF)
		switch (c) {
		case 'v':
			verbose++;
			break;
		case 'C':
			cat = optarg;
			break;
		case 'F':
			fmt = optarg;
			break;
		case 'H':
			history = optarg;
			break;
		case 'S':
			sexpr();
			return 0;
		case 'V':
			fprintf(stderr, "%s %s\n", version, build_date);
			return 1;
		case 'h':
		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) {
		dump_hist(vcs_git_history(history, 0));
		return 0;
	}

	if (fmt) {
		char *buf;

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

	usage(*argv);
}