2 * Copyright (c) 2013-2014 Devin Teske <dteske@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
33 #define _BSD_SOURCE /* to get dprintf() prototype in stdio.h below */
50 static uint8_t debug = FALSE;
53 static struct dpv_file_node *file_list = NULL;
54 static unsigned int nfiles = 0;
57 static uint8_t line_mode = FALSE;
58 static uint8_t no_overrun = FALSE;
59 static char *buf = NULL;
61 static int output_type = DPV_OUTPUT_NONE;
63 static char rpath[PATH_MAX];
65 /* Extra display information */
66 static uint8_t multiple = FALSE; /* `-m' */
67 static char *pgm; /* set to argv[0] by main() */
69 /* Function prototypes */
70 static void sig_int(int sig);
71 static void usage(void);
72 int main(int argc, char *argv[]);
73 static int operate_common(struct dpv_file_node *file, int out);
74 static int operate_on_bytes(struct dpv_file_node *file, int out);
75 static int operate_on_lines(struct dpv_file_node *file, int out);
78 operate_common(struct dpv_file_node *file, int out)
82 /* Open the file if necessary */
85 /* Resolve the file path and attempt to open it */
86 if (realpath(file->path, rpath) == 0 ||
87 (fd = open(rpath, O_RDONLY)) < 0) {
88 warn("%s", file->path);
89 file->status = DPV_STATUS_FAILED;
93 /* Assume stdin, but if that's a TTY instead use the
94 * highest numbered file descriptor (obtained by
95 * generating new fd and then decrementing).
97 * NB: /dev/stdin should always be open(2)'able
101 fd = open("/dev/stdin", O_RDONLY);
105 /* This answer might be wrong, if dpv(3) has (by
106 * request) opened an output file or pipe. If we
107 * told dpv(3) to open a file, subtract one from
108 * previous answer. If instead we told dpv(3) to
109 * prepare a pipe output, subtract two.
111 switch(output_type) {
112 case DPV_OUTPUT_FILE:
115 case DPV_OUTPUT_SHELL:
122 /* Allocate buffer if necessary */
124 /* Use output block size as buffer size if available */
126 if (fstat(out, &sb) != 0) {
128 file->status = DPV_STATUS_FAILED;
131 if (S_ISREG(sb.st_mode)) {
132 if (sysconf(_SC_PHYS_PAGES) >
134 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
136 bsize = BUFSIZE_SMALL;
138 bsize = MAX(sb.st_blksize,
139 (blksize_t)sysconf(_SC_PAGESIZE));
141 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
143 /* Attempt to allocate */
144 if ((buf = malloc(bsize+1)) == NULL) {
146 err(EXIT_FAILURE, "Out of memory?!");
154 operate_on_bytes(struct dpv_file_node *file, int out)
159 if (operate_common(file, out) < 0)
162 /* [Re-]Fill the buffer */
163 if ((r = read(fd, buf, bsize)) <= 0) {
164 if (fd != STDIN_FILENO)
167 file->status = DPV_STATUS_DONE;
171 /* [Re-]Dump the buffer */
173 if ((w = write(out, buf, r)) < 0) {
175 err(EXIT_FAILURE, "output");
180 dpv_overall_read += r;
183 /* Calculate percentage of completion (if possible) */
184 if (file->length >= 0) {
185 progress = (file->read * 100 / (file->length > 0 ?
188 /* If no_overrun, do not return 100% until read >= length */
189 if (no_overrun && progress == 100 && file->read < file->length)
198 operate_on_lines(struct dpv_file_node *file, int out)
204 if (operate_common(file, out) < 0)
207 /* [Re-]Fill the buffer */
208 if ((r = read(fd, buf, bsize)) <= 0) {
209 if (fd != STDIN_FILENO)
212 file->status = DPV_STATUS_DONE;
217 /* [Re-]Dump the buffer */
219 if ((w = write(out, buf, r)) < 0) {
221 err(EXIT_FAILURE, "output");
226 /* Process the buffer for number of lines */
227 for (p = buf; p != NULL && *p != '\0';)
228 if ((p = strchr(p, '\n')) != NULL)
229 dpv_overall_read++, p++, file->read++;
231 /* Calculate percentage of completion (if possible) */
232 if (file->length >= 0) {
233 progress = (file->read * 100 / file->length);
235 /* If no_overrun, do not return 100% until read >= length */
236 if (no_overrun && progress == 100 && file->read < file->length)
245 * Takes a list of names that are to correspond to input streams coming from
246 * stdin or fifos and produces necessary config to drive dpv(3) `--gauge'
247 * widget. If the `-d' flag is used, output is instead send to terminal
248 * standard output (and the output can then be saved to a file, piped into
249 * custom [X]dialog(1) invocation, or whatever.
252 main(int argc, char *argv[])
257 size_t config_size = sizeof(struct dpv_config);
258 size_t file_node_size = sizeof(struct dpv_file_node);
259 struct dpv_config *config;
260 struct dpv_file_node *curfile;
261 struct sigaction act;
263 pgm = argv[0]; /* store a copy of invocation name */
265 /* Allocate config structure */
266 if ((config = malloc(config_size)) == NULL)
267 errx(EXIT_FAILURE, "Out of memory?!");
268 memset((void *)(config), '\0', config_size);
271 * Process command-line options
273 while ((ch = getopt(argc, argv,
274 "a:b:dDhi:I:lL:mn:No:p:P:t:TU:wx:X")) != -1) {
276 case 'a': /* additional message text to append */
277 if (config->aprompt == NULL) {
278 config->aprompt = malloc(DPV_APROMPT_MAX);
279 if (config->aprompt == NULL)
280 errx(EXIT_FAILURE, "Out of memory?!");
282 snprintf(config->aprompt, DPV_APROMPT_MAX, "%s",
285 case 'b': /* [X]dialog(1) backtitle */
286 if (config->backtitle != NULL)
287 free((char *)config->backtitle);
288 config->backtitle = malloc(strlen(optarg) + 1);
289 if (config->backtitle == NULL)
290 errx(EXIT_FAILURE, "Out of memory?!");
291 *(config->backtitle) = '\0';
292 strcat(config->backtitle, optarg);
294 case 'd': /* debugging */
296 config->debug = debug;
298 case 'D': /* use dialog(1) instead of libdialog */
299 config->display_type = DPV_DISPLAY_DIALOG;
301 case 'h': /* help/usage */
303 break; /* NOTREACHED */
304 case 'i': /* status line format string for single-file */
305 config->status_solo = optarg;
307 case 'I': /* status line format string for many-files */
308 config->status_many = optarg;
310 case 'l': /* Line mode */
313 case 'L': /* custom label size */
315 (int)strtol(optarg, (char **)NULL, 10);
316 if (config->label_size == 0 && errno == EINVAL)
318 "`-L' argument must be numeric");
319 else if (config->label_size < -1)
320 config->label_size = -1;
322 case 'm': /* enable multiple file arguments */
325 case 'o': /* `-o path' for sending data-read to file */
326 output_type = DPV_OUTPUT_FILE;
327 config->output_type = DPV_OUTPUT_FILE;
328 config->output = optarg;
330 case 'n': /* custom number of files per `page' */
331 config->display_limit =
332 (int)strtol(optarg, (char **)NULL, 10);
333 if (config->display_limit == 0 && errno == EINVAL)
335 "`-n' argument must be numeric");
336 else if (config->display_limit < 0)
337 config->display_limit = -1;
339 case 'N': /* No overrun (truncate reads of known-length) */
341 config->options |= DPV_NO_OVERRUN;
343 case 'p': /* additional message text to use as prefix */
344 if (config->pprompt == NULL) {
345 config->pprompt = malloc(DPV_PPROMPT_MAX + 2);
346 if (config->pprompt == NULL)
347 errx(EXIT_FAILURE, "Out of memory?!");
348 /* +2 is for implicit "\n" appended later */
350 snprintf(config->pprompt, DPV_PPROMPT_MAX, "%s",
353 case 'P': /* custom size for mini-progressbar */
355 (int)strtol(optarg, (char **)NULL, 10);
356 if (config->pbar_size == 0 && errno == EINVAL)
358 "`-P' argument must be numeric");
359 else if (config->pbar_size < -1)
360 config->pbar_size = -1;
362 case 't': /* [X]dialog(1) title */
363 if (config->title != NULL)
365 config->title = malloc(strlen(optarg) + 1);
366 if (config->title == NULL)
367 errx(EXIT_FAILURE, "Out of memory?!");
368 *(config->title) = '\0';
369 strcat(config->title, optarg);
371 case 'T': /* test mode (don't read data, fake it) */
372 config->options |= DPV_TEST_MODE;
374 case 'U': /* updates per second */
375 config->status_updates_per_second =
376 (int)strtol(optarg, (char **)NULL, 10);
377 if (config->status_updates_per_second == 0 &&
380 "`-U' argument must be numeric");
382 case 'w': /* `-p' and `-a' widths bump [X]dialog(1) width */
383 config->options |= DPV_WIDE_MODE;
385 case 'x': /* `-x cmd' for sending data-read to sh(1) code */
386 output_type = DPV_OUTPUT_SHELL;
387 config->output_type = DPV_OUTPUT_SHELL;
388 config->output = optarg;
390 case 'X': /* X11 support through x11/xdialog */
391 config->display_type = DPV_DISPLAY_XDIALOG;
393 case '?': /* unknown argument (based on optstring) */
395 default: /* unhandled argument (based on switch) */
403 /* Process remaining arguments as list of names to display */
404 for (curfile = file_list; n < argc; n++) {
407 /* Allocate a new struct for the file argument */
408 if (curfile == NULL) {
409 if ((curfile = malloc(file_node_size)) == NULL)
410 errx(EXIT_FAILURE, "Out of memory?!");
411 memset((void *)(curfile), '\0', file_node_size);
414 if ((curfile->next = malloc(file_node_size)) == NULL)
415 errx(EXIT_FAILURE, "Out of memory?!");
416 memset((void *)(curfile->next), '\0', file_node_size);
417 curfile = curfile->next;
419 curfile->name = argv[n];
421 /* Read possible `lines:' prefix from label syntax */
422 if (sscanf(curfile->name, "%lli:%c", &(curfile->length),
424 curfile->name = strchr(curfile->name, ':') + 1;
426 curfile->length = -1;
428 /* Read path argument if enabled */
431 errx(EXIT_FAILURE, "Missing path argument "
432 "for label number %i", nfiles);
433 curfile->path = argv[n];
438 /* Display usage and exit if not given at least one name */
440 warnx("no labels provided");
446 * Set cleanup routine for Ctrl-C action
448 if (config->display_type == DPV_DISPLAY_LIBDIALOG) {
449 act.sa_handler = sig_int;
450 sigaction(SIGINT, &act, 0);
453 /* Set status formats and action */
455 config->status_solo = LINE_STATUS_SOLO;
456 config->status_many = LINE_STATUS_SOLO;
457 config->action = operate_on_lines;
459 config->status_solo = BYTE_STATUS_SOLO;
460 config->status_many = BYTE_STATUS_SOLO;
461 config->action = operate_on_bytes;
465 * Hand off to dpv(3)...
467 if (dpv(config, file_list) != 0 && debug)
468 warnx("dpv(3) returned error!?");
477 * Interrupt handler to indicate we received a Ctrl-C interrupt.
480 sig_int(int sig __unused)
482 dpv_interrupt = TRUE;
486 * Print short usage statement to stderr and exit with error status.
492 if (debug) /* No need for usage */
495 fprintf(stderr, "Usage: %s [options] bytes:label\n", pgm);
496 fprintf(stderr, " %s [options] -m bytes1:label1 path1 "
497 "[bytes2:label2 path2 ...]\n", pgm);
498 fprintf(stderr, "OPTIONS:\n");
499 #define OPTFMT "\t%-14s %s\n"
500 fprintf(stderr, OPTFMT, "-a text",
501 "Append text. Displayed below file progress indicators.");
502 fprintf(stderr, OPTFMT, "-b backtitle",
503 "String to be displayed on the backdrop, at top-left.");
504 fprintf(stderr, OPTFMT, "-d",
505 "Debug. Write to standard output instead of dialog.");
506 fprintf(stderr, OPTFMT, "-D",
507 "Use dialog(1) instead of dialog(3) [default].");
508 fprintf(stderr, OPTFMT, "-h",
509 "Produce this output on standard error and exit.");
510 fprintf(stderr, OPTFMT, "-i format",
511 "Customize status line format. See fdpv(1) for details.");
512 fprintf(stderr, OPTFMT, "-I format",
513 "Customize status line format. See fdpv(1) for details.");
514 fprintf(stderr, OPTFMT, "-L size",
515 "Label size. Must be a number greater than 0, or -1.");
516 fprintf(stderr, OPTFMT, "-m",
517 "Enable processing of multiple file argiments.");
518 fprintf(stderr, OPTFMT, "-n num",
519 "Display at-most num files per screen. Default is -1.");
520 fprintf(stderr, OPTFMT, "-N",
521 "No overrun. Stop reading input at stated length, if any.");
522 fprintf(stderr, OPTFMT, "-o file",
523 "Output data to file. First %s replaced with label text.");
524 fprintf(stderr, OPTFMT, "-p text",
525 "Prefix text. Displayed above file progress indicators.");
526 fprintf(stderr, OPTFMT, "-P size",
527 "Mini-progressbar size. Must be a number greater than 3.");
528 fprintf(stderr, OPTFMT, "-t title",
529 "Title string to be displayed at top of dialog(1) box.");
530 fprintf(stderr, OPTFMT, "-T",
531 "Test mode. Don't actually read any data, but fake it.");
532 fprintf(stderr, OPTFMT, "-U num",
533 "Update status line num times per-second. Default is 2.");
534 fprintf(stderr, OPTFMT, "-w",
535 "Wide. Width of `-p' and `-a' text bump dialog(1) width.");
536 fprintf(stderr, OPTFMT, "-x cmd",
537 "Send data to executed cmd. First %s replaced with label.");
538 fprintf(stderr, OPTFMT, "-X",
539 "X11. Use Xdialog(1) instead of dialog(1).");