aboutsummaryrefslogtreecommitdiff
path: root/src/lzma/main.c
blob: 26edc47ecaad3d547a13c7f67e85b074baf18288 (plain) (blame)
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
///////////////////////////////////////////////////////////////////////////////
//
/// \file       main.c
/// \brief      main()
//
//  Copyright (C) 2007 Lasse Collin
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 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
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#include "private.h"
#include "open_stdxxx.h"
#include <ctype.h>

static sig_atomic_t exit_signal = 0;


static void
signal_handler(int sig)
{
	// FIXME Is this thread-safe together with main()?
	exit_signal = sig;

	user_abort = 1;
	return;
}


static void
establish_signal_handlers(void)
{
	struct sigaction sa;
	sa.sa_handler = &signal_handler;
	sigfillset(&sa.sa_mask);
	sa.sa_flags = 0;

	static const int sigs[] = {
		SIGHUP,
		SIGINT,
		SIGPIPE,
		SIGTERM,
		SIGXCPU,
		SIGXFSZ,
	};

	for (size_t i = 0; i < sizeof(sigs) / sizeof(sigs[0]); ++i) {
		if (sigaction(sigs[i], &sa, NULL)) {
			errmsg(V_ERROR, _("Cannot establish signal handlers"));
			my_exit(ERROR);
		}
	}

	/*
	SIGINFO/SIGUSR1 for status reporting?
	*/
}


static bool
is_tty_stdin(void)
{
	const bool ret = isatty(STDIN_FILENO);
	if (ret) {
		// FIXME: Other threads may print between these lines.
		// Maybe that should be fixed. Not a big issue in practice.
		errmsg(V_ERROR, _("Compressed data not read from "
				"a terminal."));
		errmsg(V_ERROR, _("Use `--force' to force decompression."));
		show_try_help();
	}

	return ret;
}


static bool
is_tty_stdout(void)
{
	const bool ret = isatty(STDOUT_FILENO);
	if (ret) {
		errmsg(V_ERROR, _("Compressed data not written to "
				"a terminal."));
		errmsg(V_ERROR, _("Use `--force' to force decompression."));
		show_try_help();
	}

	return ret;
}


static char *
read_name(void)
{
	size_t size = 256;
	size_t pos = 0;
	char *name = malloc(size);
	if (name == NULL) {
		out_of_memory();
		return NULL;
	}

	while (true) {
		const int c = fgetc(opt_files_file);
		if (c == EOF) {
			free(name);

			if (ferror(opt_files_file))
				errmsg(V_ERROR, _("%s: Error reading "
						"filenames: %s"),
						opt_files_name,
						strerror(errno));
			else if (pos != 0)
				errmsg(V_ERROR, _("%s: Unexpected end of "
						"input when reading "
						"filenames"), opt_files_name);

			return NULL;
		}

		if (c == '\0' || c == opt_files_split)
			break;

		name[pos++] = c;

		if (pos == size) {
			size *= 2;
			char *tmp = realloc(name, size);
			if (tmp == NULL) {
				free(name);
				out_of_memory();
				return NULL;
			}

			name = tmp;
		}
	}

	if (name != NULL)
		name[pos] = '\0';

	return name;
}


int
main(int argc, char **argv)
{
	// Make sure that stdin, stdout, and and stderr are connected to
	// a valid file descriptor. Exit immediatelly with exit code ERROR
	// if we cannot make the file descriptors valid. Maybe we should
	// print an error message, but our stderr could be screwed anyway.
	open_stdxxx(ERROR);

	// Set the program invocation name used in various messages.
	argv0 = argv[0];

	setlocale(LC_ALL, "en_US.UTF-8");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	// Set hardware-dependent default values. These can be overriden
	// on the command line, thus this must be done before parse_args().
	hardware_init();

	char **files = parse_args(argc, argv);

	if (opt_mode == MODE_COMPRESS && opt_stdout && is_tty_stdout())
		return ERROR;

	if (opt_mode == MODE_COMPRESS)
		lzma_init_encoder();
	else
		lzma_init_decoder();

	io_init();
	process_init();

	if (opt_mode == MODE_LIST) {
		errmsg(V_ERROR, "--list is not implemented yet.");
		my_exit(ERROR);
	}

	// Hook the signal handlers. We don't need these before we start
	// the actual action, so this is done after parsing the command
	// line arguments.
	establish_signal_handlers();

	while (*files != NULL && !user_abort) {
		if (strcmp("-", *files) == 0) {
			if (!opt_force) {
				if (opt_mode == MODE_COMPRESS) {
					if (is_tty_stdout()) {
						++files;
						continue;
					}
				} else if (is_tty_stdin()) {
					++files;
					continue;
				}
			}

			if (opt_files_name == stdin_filename) {
				errmsg(V_ERROR, _("Cannot read data from "
						"standard input when "
						"reading filenames "
						"from standard input"));
				++files;
				continue;
			}

			*files = (char *)stdin_filename;
		}

		process_file(*files++);
	}

	if (opt_files_name != NULL) {
		while (true) {
			char *name = read_name();
			if (name == NULL)
				break;

			if (name[0] != '\0')
				process_file(name);

			free(name);
		}

		if (opt_files_name != stdin_filename)
			(void)fclose(opt_files_file);
	}

	io_finish();

	if (exit_signal != 0) {
		struct sigaction sa;
		sa.sa_handler = SIG_DFL;
		sigfillset(&sa.sa_mask);
		sa.sa_flags = 0;
		sigaction(exit_signal, &sa, NULL);
		raise(exit_signal);
	}

	my_exit(exit_status);
}