colctr: use long options and clean coding style

This commit introduces help & version options.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2011-05-24 22:56:43 +02:00 committed by Karel Zak
parent 2a324edd32
commit e71daef249
1 changed files with 64 additions and 26 deletions

View File

@ -42,13 +42,16 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> /* for close() */ #include <unistd.h> /* for close() */
#include <string.h> #include <string.h>
#include <getopt.h>
#include "nls.h" #include "nls.h"
#include "widechar.h" #include "widechar.h"
#include "c.h"
int plus(wchar_t c, wchar_t d); int plus(wchar_t c, wchar_t d);
void move(int l, int m); void move(int l, int m);
void pflush(int ol); void pflush(int ol);
static void __attribute__ ((__noreturn__)) usage(FILE * out);
/* /*
* colcrt - replaces col for crts with new nroff esp. when using tbl. * colcrt - replaces col for crts with new nroff esp. when using tbl.
@ -72,45 +75,66 @@ int outcol;
char suppresul; char suppresul;
char printall; char printall;
char *progname;
void colcrt(FILE *f); void colcrt(FILE *f);
int int main(int argc, char **argv) {
main(int argc, char **argv) {
FILE *f; 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, ""); setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR); bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE); textdomain(PACKAGE);
/* Take care of lonely hyphen option. */
for (i = 0; i < argc; i++)
if (argv[i][0] == '-' && argv[i][1] == '\0') {
suppresul = 1;
argc--; argc--;
progname = *argv++; memmove(argv + i, argv + i + 1,
while (argc > 0 && argv[0][0] == '-') { sizeof(char *) * (argc - i));
switch (argv[0][1]) { i--;
case 0: }
while ((opt = getopt_long(argc, argv, "2Vh", longopts, NULL)) != -1)
switch (opt) {
case NO_UL_OPTION:
suppresul = 1; suppresul = 1;
break; break;
case '2': case '2':
printall = 1; printall = 1;
break; break;
case 'V':
printf(_("%s from %s\n"),
program_invocation_short_name,
PACKAGE_STRING);
return EXIT_SUCCESS;
case 'h':
usage(stdout);
default: default:
printf(_("usage: %s [ - ] [ -2 ] [ file ... ]\n"), progname); usage(stderr);
fflush(stdout);
exit(1);
} }
argc--; argc -= optind;
argv++; argv += optind;
}
f = stdin;
do { do {
if (argc > 0) { if (argc > 0) {
if (!(f = fopen(argv[0], "r"))) { if (!(f = fopen(argv[0], "r"))) {
fflush(stdout); fflush(stdout);
perror(argv[0]); err(EXIT_FAILURE, "%s", argv[0]);
exit (1);
} }
argc--; argc--;
argv++; argv++;
} else {
f = stdin;
} }
colcrt(f); colcrt(f);
if (f != stdin) if (f != stdin)
@ -118,12 +142,11 @@ main(int argc, char **argv) {
} while (argc > 0); } while (argc > 0);
fflush(stdout); fflush(stdout);
if (ferror(stdout) || fclose(stdout)) if (ferror(stdout) || fclose(stdout))
return 1; return EXIT_FAILURE;
return 0; return EXIT_SUCCESS;
} }
void void colcrt(FILE *f) {
colcrt(FILE *f) {
wint_t c; wint_t c;
wchar_t *cp, *dp; wchar_t *cp, *dp;
int i, w; int i, w;
@ -190,19 +213,19 @@ colcrt(FILE *f) {
} }
if (*cp == 0) { if (*cp == 0) {
/* trick! */ /* trick! */
for (i=0; i<w; i++) for (i = 0; i < w; i++)
cp[i] = c; cp[i] = c;
dp = cp - (outcol-w); dp = cp - (outcol - w);
for (cp--; cp >= dp && *cp == 0; cp--) for (cp--; cp >= dp && *cp == 0; cp--)
*cp = ' '; *cp = ' ';
} else { } else {
if (plus(c, *cp) || plus(*cp, c)) if (plus(c, *cp) || plus(*cp, c))
*cp = '+'; *cp = '+';
else if (*cp == ' ' || *cp == 0) { else if (*cp == ' ' || *cp == 0) {
for (i=1; i<w; i++) for (i = 1; i < w; i++)
if (cp[i] != ' ' && cp[i] != 0) if (cp[i] != ' ' && cp[i] != 0)
continue; continue;
for (i=0; i<w; i++) for (i = 0; i < w; i++)
cp[i] = c; cp[i] = c;
} }
} }
@ -253,7 +276,7 @@ void pflush(int ol)
putwchar('\n'); putwchar('\n');
} }
memmove(page, page[ol], (267 - ol) * 132 * sizeof(wchar_t)); memmove(page, page[ol], (267 - ol) * 132 * sizeof(wchar_t));
memset(page[267- ol], '\0', ol * 132 * sizeof(wchar_t)); memset(page[267 - ol], '\0', ol * 132 * sizeof(wchar_t));
outline -= ol; outline -= ol;
outcol = 0; outcol = 0;
first = 1; first = 1;
@ -284,3 +307,18 @@ void move(int l, int m)
page[l][0] = 0; page[l][0] = 0;
} }
} }
static void __attribute__ ((__noreturn__)) usage(FILE * out)
{
fprintf(out,
_("\nUsage:\n"
" %s [options] [file ...]\n"), program_invocation_short_name);
fprintf(out,
_(" -, --no-underlining suppress all underlining\n"
" -2, --half-lines print all half-lines\n"
" -V, --version output version information and exit\n"
" -h, --help display this help and exit\n\n"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}