summaryrefslogtreecommitdiffstats
path: root/src/man-recode.c
blob: e198d1755263a459ef9030ff135fca0a9a67c043 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * man-recode.c: convert manual pages to another encoding
 *
 * Copyright (C) 2019 Colin Watson.
 *
 * This file is part of man-db.
 *
 * man-db 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.
 *
 * man-db 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 man-db; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "argp.h"
#include "dirname.h"
#include "error.h"
#include "gl_array_list.h"
#include "gl_xlist.h"
#include "progname.h"
#include "tempname.h"
#include "xalloc.h"
#include "xvasprintf.h"

#include "gettext.h"
#define _(String) gettext (String)
#define N_(String) gettext_noop (String)

#include "manconfig.h"

#include "pipeline.h"

#include "cleanup.h"
#include "compression.h"
#include "debug.h"
#include "encodings.h"
#include "fatal.h"
#include "glcontainers.h"
#include "sandbox.h"
#include "util.h"

#include "decompress.h"
#include "manconv.h"
#include "manconv_client.h"

int quiet = 0;
man_sandbox *sandbox;

static char *to_code;
static gl_list_t filenames;
static const char *suffix;
static bool in_place;

struct try_file_at_args {
	int dir_fd;
	int flags;
};

static int
try_file_at (char *tmpl, void *flags)
{
	struct try_file_at_args *args = flags;
	return openat (args->dir_fd, tmpl,
		       (args->flags & ~O_ACCMODE) | O_RDWR | O_CREAT | O_EXCL,
		       S_IRUSR | S_IWUSR);
}

static int
mkstempat (int dir_fd, char *xtemplate)
{
	struct try_file_at_args args;

	args.dir_fd = dir_fd;
	args.flags = 0;
	return try_tempname (xtemplate, 0, &args, try_file_at);
}

enum opts {
	OPT_SUFFIX = 256,
	OPT_IN_PLACE = 257,
	OPT_MAX
};

const char *argp_program_version = "man-recode " PACKAGE_VERSION;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
error_t argp_err_exit_status = FAIL;

static const char args_doc[] =
	N_("-t CODE {--suffix SUFFIX | --in-place} FILENAME...");

static struct argp_option options[] = {
	OPT ("to-code", 't', N_("CODE"), N_("encoding for output")),
	OPT ("suffix", OPT_SUFFIX, N_("SUFFIX"),
	     N_("suffix to append to output file name")),
	OPT ("in-place", OPT_IN_PLACE, 0,
	     N_("overwrite input files in place")),
	OPT ("debug", 'd', 0, N_("emit debugging messages")),
	OPT ("quiet", 'q', 0, N_("produce fewer warnings")),
	OPT_HELP_COMPAT,
	{ 0 }
};

static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
	switch (key) {
		case 't':
			to_code = xstrdup (arg);
			return 0;
		case OPT_SUFFIX:
			suffix = arg;
			return 0;
		case OPT_IN_PLACE:
			in_place = true;
			return 0;
		case 'd':
			debug_level = true;
			return 0;
		case 'q':
			quiet = 1;
			return 0;
		case 'h':
			argp_state_help (state, state->out_stream,
					 ARGP_HELP_STD_HELP);
			break;
		case ARGP_KEY_ARG:
			gl_list_add_last (filenames, xstrdup (arg));
			return 0;
		case ARGP_KEY_NO_ARGS:
			argp_usage (state);
			break;
		case ARGP_KEY_SUCCESS:
			if (!to_code)
				argp_error (state,
					    _("must specify an output "
					      "encoding"));
			if (!suffix && !in_place)
				argp_error (state,
					    _("must use either --suffix or "
					      "--in-place"));
			if (suffix && in_place)
				argp_error (state,
					    _("--suffix and --in-place are "
					      "mutually exclusive"));
			return 0;
	}
	return ARGP_ERR_UNKNOWN;
}

static struct argp argp = { options, parse_opt, args_doc };

static void recode (const char *filename)
{
	decompress *decomp;
	pipeline *convert, *decomp_p;
	struct compression *comp;
	int dir_fd = -1;
	char *dirname, *basename, *stem, *outfilename;
	char *page_encoding;
	int status;

	decomp = decompress_open (filename, 0);
	if (!decomp)
		error (FAIL, 0, _("can't open %s"), filename);

	dirname = dir_name (filename);
	basename = base_name (filename);
	comp = comp_info (basename, true);
	if (comp)
		stem = comp->stem;	/* steal memory */
	else
		stem = xstrdup (basename);

	convert = pipeline_new ();
	if (suffix) {
		outfilename = xasprintf ("%s/%s%s", dirname, stem, suffix);
		pipeline_want_outfile (convert, outfilename);
	} else {
		int dir_fd_open_flags;
		char *template_path;
		int outfd;

		dir_fd_open_flags = O_SEARCH | O_DIRECTORY;
#ifdef O_PATH
		dir_fd_open_flags |= O_PATH;
#endif
		dir_fd = open (dirname, dir_fd_open_flags);
		if (dir_fd < 0)
			fatal (errno, _("can't open %s"), dirname);

		outfilename = xasprintf ("%s.XXXXXX", stem);
		/* For error messages. */
		template_path = xasprintf ("%s/%s", dirname, outfilename);
		outfd = mkstempat (dir_fd, outfilename);
		if (outfd == -1)
			fatal (errno,
			       _("can't open temporary file %s"),
			       template_path);
		free (template_path);
		pipeline_want_out (convert, outfd);
	}

	decompress_start (decomp);
	page_encoding = check_preprocessor_encoding (decomp, NULL, NULL);
	if (!page_encoding) {
		char *lang = lang_dir (filename);
		page_encoding = get_page_encoding (lang);
		free (lang);
	}
	debug ("guessed input encoding %s for %s\n", page_encoding, filename);
	add_manconv (convert, page_encoding, to_code);

	if (!pipeline_get_ncommands (convert))
		pipeline_command (convert, pipecmd_new_passthrough ());

	decomp_p = decompress_get_pipeline (decomp);
	pipeline_connect (decomp_p, convert, (void *) 0);
	pipeline_pump (decomp_p, convert, (void *) 0);
	pipeline_wait (decomp_p);
	status = pipeline_wait (convert);
	if (status != 0)
		error (CHILD_FAIL, 0, _("command exited with status %d: %s"),
		       status, pipeline_tostring (convert));

	if (in_place) {
		assert (dir_fd != -1);
		if (renameat (dir_fd, outfilename, dir_fd, stem) == -1) {
			char *outfilepath = xasprintf
				("%s/%s", dirname, outfilename);
			unlink (outfilename);
			fatal (errno, _("can't rename %s to %s"),
			       outfilepath, filename);
		}
		debug ("stem: %s, basename: %s\n", stem, basename);
		if (!STREQ (stem, basename)) {
			if (unlinkat (dir_fd, basename, 0) == -1)
				fatal (errno, _("can't remove %s"), filename);
		}
	}

	free (page_encoding);
	free (outfilename);
	free (stem);
	free (basename);
	free (dirname);
	if (dir_fd)
		close (dir_fd);
	pipeline_free (convert);
	decompress_free (decomp);
}

int main (int argc, char *argv[])
{
	const char *filename;

	set_program_name (argv[0]);

	init_debug ();
	pipeline_install_post_fork (pop_all_cleanups);
	sandbox = sandbox_init ();
	init_locale ();
	filenames = new_string_list (GL_ARRAY_LIST, true);

	if (argp_parse (&argp, argc, argv, 0, 0, 0))
		exit (FAIL);

	GL_LIST_FOREACH (filenames, filename)
		recode (filename);

	free (to_code);

	gl_list_free (filenames);
	sandbox_free (sandbox);

	return 0;
}