From 52cf627598c2f124c18665172417bdb36d6fdb1d Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 4 Sep 2024 17:01:46 +0200 Subject: Remove --latex in favour of --table-style --- src/main.c | 55 ++++++++++++++++++++++++++++++++++++++++++------------- src/parser.y | 3 +-- src/pinocchio.h | 3 +-- 3 files changed, 44 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 1480d0b..e3ab50f 100644 --- a/src/main.c +++ b/src/main.c @@ -21,6 +21,13 @@ # define __has_builtin(x) (0) #endif +typedef enum { + TS_UNSET = 0, + TS_ASCII, + TS_LATEX, + TS_UTF8, +} tbl_style_t; + typedef enum { BS_ALPHA, BS_DIGITS, @@ -28,12 +35,14 @@ typedef enum { } bool_style_t; static int rv; -static bool interactive, utf8; +static bool interactive; static bool_style_t bflag = BS_DIGITS; +static tbl_style_t tflag = TS_UNSET; -bool lflag; const char *current_file; +static void astprocess_cli(ast_t); +static void astprocess_latex(ast_t); static bool eqnsolve(eqn_t *, uint64_t, uint64_t); static int eqnprint(eqn_t *); static void eqnfree(eqn_t *); @@ -56,17 +65,16 @@ main(int argc, char **argv) { argv[0] = basename(argv[0]); setlocale(LC_ALL, ""); - utf8 = strcmp(nl_langinfo(CODESET), "UTF-8") == 0; interactive = isatty(STDIN_FILENO); int opt; static struct option longopts[] = { - {"bool-style", required_argument, 0, 'b'}, - {"latex", no_argument, 0, 'l'}, + {"bool-style", required_argument, 0, 'b'}, + {"table-style", required_argument, 0, 't'}, {0}, }; - while ((opt = getopt_long(argc, argv, "b:l", longopts, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "b:t:", longopts, NULL)) != -1) { switch (opt) { case 'b': if (strcmp(optarg, "alpha") == 0) @@ -80,17 +88,31 @@ main(int argc, char **argv) goto usage; } break; - case 'l': - lflag = true; + case 't': + if (strcmp(optarg, "ascii") == 0) + tflag = TS_ASCII; + else if (strcmp(optarg, "latex") == 0) + tflag = TS_LATEX; + else if (strcmp(optarg, "utf8") == 0) + tflag = TS_UTF8; + else { + warnx("invalid table style -- '%s'", optarg); + goto usage; + } break; default: usage: - fprintf(stderr, "Usage: %s [-b alpha|digits|symbols] [-l] [file ...]\n", + fprintf(stderr, "Usage: %s [-b alpha|digits|symbols] [-t ascii|latex|utf8] [file ...]\n", argv[0]); exit(EXIT_FAILURE); } } + if (tflag == TS_UNSET) { + tflag = strcmp(nl_langinfo(CODESET), "UTF-8") == 0 + ? TS_UTF8 : TS_ASCII; + } + argc -= optind; argv += optind; @@ -118,6 +140,12 @@ usage: return rv; } +void +astprocess(ast_t a) +{ + (tflag == TS_LATEX ? astprocess_latex : astprocess_cli)(a); +} + void astprocess_cli(ast_t a) { @@ -144,7 +172,7 @@ astprocess_cli(ast_t a) [BS_SYMBOLS] = {"⊥", "⊤"}, }; - const char **tblsyms = utf8 ? tblsyms_utf8 : tblsyms_ascii; + const char **tblsyms = tflag == TS_UTF8 ? tblsyms_utf8 : tblsyms_ascii; for (int i = 0; i < MAXVARS; i++) { if ((a.vars & UINT64_C(1)<type) { case IDENT: diff --git a/src/parser.y b/src/parser.y index d7e8168..c09067b 100644 --- a/src/parser.y +++ b/src/parser.y @@ -11,7 +11,6 @@ static ast_t astmerge(int, ast_t, ast_t); static void *xmalloc(size_t); static void yyerror(const char *); -extern bool lflag; extern const char *current_file; %} @@ -47,7 +46,7 @@ input : %empty | input line { if ($2.eqn != NULL) - (lflag ? astprocess_latex : astprocess_cli)($2); + astprocess($2); } ; diff --git a/src/pinocchio.h b/src/pinocchio.h index 51e4dd5..fb74f3c 100644 --- a/src/pinocchio.h +++ b/src/pinocchio.h @@ -19,8 +19,7 @@ typedef struct { uint64_t vars; } ast_t; -void astprocess_cli(ast_t); -void astprocess_latex(ast_t); +void astprocess(ast_t); void user_error(const char *, ...) #if __GNUC__ __attribute__((format(printf, 1, 2))) -- cgit v1.2.3