summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 4778b5101878f8a281a869beb1c492777ebd3cb1 (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
/*
Copyright (C) 2016  Paweł Redman

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 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include "common.h"
#include <unistd.h>

#define error(fmt, ...) fprintf(stderr, PROGRAM_NAME ": " fmt, ##__VA_ARGS__)

void print_version(void)
{
	puts(PROGRAM_NAME " " PROGRAM_VERSION ", Copyright (C) 2016  Paweł Redman\n"
	     PROGRAM_NAME " comes with ABSOLUTELY NO WARRANTY.\n"
	     "This is free software, and you are welcome to redistribute it\n"
             "under certain conditions. See the file COPYING.");
}

void print_usage(void)
{
	puts(PROGRAM_NAME " " PROGRAM_VERSION "\n"
	     "usage: " PROGRAM_NAME " [-q] -o outfile infile...\n"
	     "    or " PROGRAM_NAME " -v\n"
	     "    or " PROGRAM_NAME " -h");
}

typedef struct {
	char *path;
	elist_header_t list;
} input_file_t;

int main(int argc, char **argv)
{
	int rv = 1, i;
	input_file_t *inputs = NULL, *input, *next;
	char *output = NULL;
	bool read_flags = true, quiet = false;
	map_t map;

	for (i = 1; i < argc; i++) {
		if (read_flags && !strcmp(argv[i], "-v")) {
			print_version();
			rv = 0;
			goto out;
		} else if (read_flags && !strcmp(argv[i], "-h")) {
			print_usage();
			rv = 0;
			goto out;
		} else if (read_flags && !strcmp(argv[i], "-q")) {
			quiet = true;
		} else if (read_flags && !strcmp(argv[i], "-o")) {
			if (i + 1 >= argc) {
			o_needs_an_argument:
				error("-o needs an argument\n");
				goto out;
			}

			if (!strcmp(argv[i + 1], "--")) {
				i++;

				if (i + 1 >= argc)
					goto o_needs_an_argument;
			}

			if (output) {
				error("-o can be specified only once\n");
				goto out;
			}

			output = argv[i + 1];
			i++;
		} else if (read_flags && !strcmp(argv[i], "--")) {
			read_flags = false;
		} else {
			input = malloc(sizeof(input_file_t));
			if (!input) {
				error("out of memory\n");
				goto out;
			}

			input->path = argv[i];
			elist_append(&inputs, input, list);
		}
	}

	if (!inputs) {
		error("no input files specified, try '" PROGRAM_NAME " -h'\n");
		goto out;
	}

	if (!output) {
		error("no output file specified, try '" PROGRAM_NAME " -h'\n");
		goto out;
	}

	map_init(&map);

	elist_for(input, inputs, list) {
		map_t part;

		map_init(&part);

		if (map_read(&part, input->path)) {
			error("error: couldn't read %s\n", input->path);
			goto out;
		}

		// team_* and info_* ents are kept only in the first part
		if (map_postprocess(&part, (input != inputs))) {
			map_free(&map);
			map_free(&part);
			goto out;
		}

		if (!quiet)
			map_print_stats(input->path, &part);

		if (map_merge(&map, &part)) {
			error("error: couldn't merge %s into %s\n",
			      input->path, output);
			map_free(&map);
			map_free(&part);
			goto out;
		}
	}

	if (!quiet)
		map_print_stats(output, &map);

	if (map_write(&map, output)) {
		error("error: couldn't write %s\n", output);
		map_free(&map);
		goto out;
	}

	map_free(&map);
	rv = 0;
out:
	for (input = inputs; input; input = next) {
		next = elist_next(input, list);
		free(input);
	}

	return rv;
}