1 ///////////////////////////////////////////////////////////////////////////////
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
17 /// Exit status to use. This can be changed with set_exit_status().
18 static enum exit_status_type exit_status = E_SUCCESS;
20 /// True if --no-warn is specified. When this is true, we don't set
21 /// the exit status to E_WARNING when something worth a warning happens.
22 static bool no_warn = false;
26 set_exit_status(enum exit_status_type new_status)
28 assert(new_status == E_WARNING || new_status == E_ERROR);
30 if (exit_status != E_ERROR)
31 exit_status = new_status;
38 set_exit_no_warn(void)
46 read_name(const args_info *args)
48 // FIXME: Maybe we should have some kind of memory usage limit here
49 // like the tool has for the actual compression and uncompression.
50 // Giving some huge text file with --files0 makes us to read the
52 static char *name = NULL;
53 static size_t size = 256;
55 // Allocate the initial buffer. This is never freed, since after it
56 // is no longer needed, the program exits very soon. It is safe to
57 // use xmalloc() and xrealloc() in this function, because while
58 // executing this function, no files are open for writing, and thus
59 // there's no need to cleanup anything before exiting.
63 // Write position in name
66 // Read one character at a time into name.
68 const int c = fgetc(args->files_file);
70 if (ferror(args->files_file)) {
71 // Take care of EINTR since we have established
72 // the signal handlers already.
76 message_error(_("%s: Error reading filenames: %s"),
77 args->files_name, strerror(errno));
81 if (feof(args->files_file)) {
83 message_error(_("%s: Unexpected end of input "
84 "when reading filenames"),
90 if (c == args->files_delim) {
91 // We allow consecutive newline (--files) or '\0'
92 // characters (--files0), and ignore such empty
97 // A non-empty name was read. Terminate it with '\0'
104 // A null character was found when using --files,
105 // which expects plain text input separated with
107 message_error(_("%s: Null character found when "
108 "reading filenames; maybe you meant "
109 "to use `--files0' instead "
110 "of `--files'?"), args->files_name);
116 // Allocate more memory if needed. There must always be space
117 // at least for one character to allow terminating the string
121 name = xrealloc(name, size);
130 main(int argc, char **argv)
132 // Set up the progname variable.
133 tuklib_progname_init(argv);
135 // Initialize the file I/O. This makes sure that
136 // stdin, stdout, and stderr are something valid.
139 // Set up the locale and message translations.
140 tuklib_gettext_init(PACKAGE, LOCALEDIR);
142 // Initialize handling of error/warning/other messages.
145 // Set hardware-dependent default values. These can be overriden
146 // on the command line, thus this must be done before parse_args().
149 // Parse the command line arguments and get an array of filenames.
150 // This doesn't return if something is wrong with the command line
151 // arguments. If there are no arguments, one filename ("-") is still
152 // returned to indicate stdin.
154 args_parse(&args, argc, argv);
156 // Tell the message handling code how many input files there are if
157 // we know it. This way the progress indicator can show it.
158 if (args.files_name != NULL)
159 message_set_files(0);
161 message_set_files(args.arg_count);
163 // Refuse to write compressed data to standard output if it is
164 // a terminal and --force wasn't used.
165 if (opt_mode == MODE_COMPRESS && !opt_force) {
166 if (opt_stdout || (args.arg_count == 1
167 && strcmp(args.arg_names[0], "-") == 0)) {
168 if (is_tty_stdout()) {
170 tuklib_exit(E_ERROR, E_ERROR, false);
175 if (opt_mode == MODE_LIST) {
176 message_fatal("--list is not implemented yet.");
179 // Hook the signal handlers. We don't need these before we start
180 // the actual action, so this is done after parsing the command
184 // Process the files given on the command line. Note that if no names
185 // were given, parse_args() gave us a fake "-" filename.
186 for (size_t i = 0; i < args.arg_count && !user_abort; ++i) {
187 if (strcmp("-", args.arg_names[i]) == 0) {
188 // Processing from stdin to stdout. Unless --force
189 // was used, check that we aren't writing compressed
190 // data to a terminal or reading it from terminal.
192 if (opt_mode == MODE_COMPRESS) {
195 } else if (is_tty_stdin()) {
200 // It doesn't make sense to compress data from stdin
201 // if we are supposed to read filenames from stdin
202 // too (enabled with --files or --files0).
203 if (args.files_name == stdin_filename) {
204 message_error(_("Cannot read data from "
205 "standard input when "
207 "from standard input"));
211 // Replace the "-" with a special pointer, which is
212 // recognized by coder_run() and other things.
213 // This way error messages get a proper filename
214 // string and the code still knows that it is
215 // handling the special case of stdin.
216 args.arg_names[i] = (char *)stdin_filename;
219 // Do the actual compression or uncompression.
220 coder_run(args.arg_names[i]);
223 // If --files or --files0 was used, process the filenames from the
224 // given file or stdin. Note that here we don't consider "-" to
225 // indicate stdin like we do with the command line arguments.
226 if (args.files_name != NULL) {
227 // read_name() checks for user_abort so we don't need to
228 // check it as loop termination condition.
230 const char *name = read_name(&args);
234 // read_name() doesn't return empty names.
235 assert(name[0] != '\0');
239 if (args.files_name != stdin_filename)
240 (void)fclose(args.files_file);
243 // If we have got a signal, raise it to kill the program instead
244 // of calling tuklib_exit().
247 // Suppress the exit status indicating a warning if --no-warn
249 if (exit_status == E_WARNING && no_warn)
250 exit_status = E_SUCCESS;
252 tuklib_exit(exit_status, E_ERROR,
253 message_verbosity_get() != V_SILENT);