summaryrefslogtreecommitdiff
path: root/src/main.c
blob: d34f00e5444053d89352e5d791c8d4a0998271cd (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
/*
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 " -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;

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

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

			output = argv[i + 1];
			i++;
		} 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;
	}

	elist_for (input, inputs, list)
		if (mapcat_load(input->path))
			goto out;

	if (mapcat_save(output))
		goto out;

	rv = 0;
out:
	mapcat_free();

	for (input = inputs; input; input = next) {
		next = elist_next(input, list);
		free(input);
	}

	return rv;
}