diff --git a/text-utils/colcrt.c b/text-utils/colcrt.c index 6f796656d..d9806515e 100644 --- a/text-utils/colcrt.c +++ b/text-utils/colcrt.c @@ -42,13 +42,16 @@ #include #include /* for close() */ #include +#include #include "nls.h" #include "widechar.h" +#include "c.h" int plus(wchar_t c, wchar_t d); void move(int l, int m); void pflush(int ol); +static void __attribute__ ((__noreturn__)) usage(FILE * out); /* * colcrt - replaces col for crts with new nroff esp. when using tbl. @@ -72,45 +75,66 @@ int outcol; char suppresul; char printall; -char *progname; void colcrt(FILE *f); -int -main(int argc, char **argv) { +int main(int argc, char **argv) { FILE *f; + int i, opt; + enum { NO_UL_OPTION = CHAR_MAX + 1 }; + + static const struct option longopts[] = { + { "no-underlining", no_argument, 0, NO_UL_OPTION }, + { "half-lines", no_argument, 0, '2' }, + { "version", no_argument, 0, 'V' }, + { "help", no_argument, 0, 'h' }, + { NULL, 0, 0, 0} + }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - argc--; - progname = *argv++; - while (argc > 0 && argv[0][0] == '-') { - switch (argv[0][1]) { - case 0: + /* Take care of lonely hyphen option. */ + for (i = 0; i < argc; i++) + if (argv[i][0] == '-' && argv[i][1] == '\0') { + suppresul = 1; + argc--; + memmove(argv + i, argv + i + 1, + sizeof(char *) * (argc - i)); + i--; + } + + while ((opt = getopt_long(argc, argv, "2Vh", longopts, NULL)) != -1) + switch (opt) { + case NO_UL_OPTION: suppresul = 1; break; case '2': printall = 1; break; + case 'V': + printf(_("%s from %s\n"), + program_invocation_short_name, + PACKAGE_STRING); + return EXIT_SUCCESS; + case 'h': + usage(stdout); default: - printf(_("usage: %s [ - ] [ -2 ] [ file ... ]\n"), progname); - fflush(stdout); - exit(1); + usage(stderr); } - argc--; - argv++; - } - f = stdin; + argc -= optind; + argv += optind; + do { if (argc > 0) { if (!(f = fopen(argv[0], "r"))) { fflush(stdout); - perror(argv[0]); - exit (1); + err(EXIT_FAILURE, "%s", argv[0]); } argc--; argv++; + } else { + f = stdin; } colcrt(f); if (f != stdin) @@ -118,12 +142,11 @@ main(int argc, char **argv) { } while (argc > 0); fflush(stdout); if (ferror(stdout) || fclose(stdout)) - return 1; - return 0; + return EXIT_FAILURE; + return EXIT_SUCCESS; } -void -colcrt(FILE *f) { +void colcrt(FILE *f) { wint_t c; wchar_t *cp, *dp; int i, w; @@ -190,19 +213,19 @@ colcrt(FILE *f) { } if (*cp == 0) { /* trick! */ - for (i=0; i= dp && *cp == 0; cp--) *cp = ' '; } else { if (plus(c, *cp) || plus(*cp, c)) *cp = '+'; else if (*cp == ' ' || *cp == 0) { - for (i=1; i