jdict

command line tool for looking up terms in yomidict dictionaries
git clone anongit@rnpnr.xyz:jdict.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 2e0b57f7be573e37b40147ac88e077819ae5566e
Parent: 176aea0b28194d920f532d71caf706885740edec
Author: Randy Palamar
Date:   Thu, 10 Oct 2024 06:55:09 -0600

drop arg.h

this header is not really useful - just write the code

Diffstat:
Darg.h | 50--------------------------------------------------
Mbuild.sh | 2+-
Mjdict.c | 97+++++++++++++++++++++++++++++++++++++------------------------------------------
Mutil.c | 35++++++++++++++++++++++++++---------
4 files changed, 73 insertions(+), 111 deletions(-)

diff --git a/arg.h b/arg.h @@ -1,50 +0,0 @@ -/* - * Copy me if you can. - * by 20h - */ - -#ifndef ARG_H__ -#define ARG_H__ - -extern char *argv0; - -/* use main(int argc, char *argv[]) */ -#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ - argv[0] && argv[0][0] == '-'\ - && argv[0][1];\ - argc--, argv++) {\ - char argc_;\ - char **argv_;\ - int brk_;\ - if (argv[0][1] == '-' && argv[0][2] == '\0') {\ - argv++;\ - argc--;\ - break;\ - }\ - int i_;\ - for (i_ = 1, brk_ = 0, argv_ = argv;\ - argv[0][i_] && !brk_;\ - i_++) {\ - if (argv_ != argv)\ - break;\ - argc_ = argv[0][i_];\ - switch (argc_) - -#define ARGEND }\ - } - -#define ARGC() argc_ - -#define EARGF(x) ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ - ((x), abort(), (char *)0) :\ - (brk_ = 1, (argv[0][i_+1] != '\0')?\ - (&argv[0][i_+1]) :\ - (argc--, argv++, argv[0]))) - -#define ARGF() ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ - (char *)0 :\ - (brk_ = 1, (argv[0][i_+1] != '\0')?\ - (&argv[0][i_+1]) :\ - (argc--, argv++, argv[0]))) - -#endif diff --git a/build.sh b/build.sh @@ -5,4 +5,4 @@ cflags="-march=native -O3 -std=c99 -Wall -Wextra -pedantic" cflags="$cflags -D_BSD_SOURCE" ldflags="-s -static" -cc $cflags $ldflags jdict.c -o jdict +cc $cflags $ldflags jdict.c -o jdict diff --git a/jdict.c b/jdict.c @@ -2,15 +2,12 @@ #include <dirent.h> #include <fcntl.h> #include <limits.h> -#include <stddef.h> -#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/mman.h> #include <unistd.h> -#include "arg.h" #include "util.c" #include "yomidict.c" @@ -22,10 +19,6 @@ /* Number of hash table slots (1 << HT_EXP) */ #define HT_EXP 20 -typedef uint64_t u64; -typedef uint32_t u32; -typedef int32_t i32; - typedef struct { s8 term; s8 *defs; @@ -45,10 +38,8 @@ typedef struct { #include "config.h" -char *argv0; - static void -usage(void) +usage(char *argv0) { die("usage: %s [-d path] [-F FS] [-i] term ...\n", argv0); } @@ -251,14 +242,14 @@ make_dict(Dict *d) d->ht.ents = xreallocarray(NULL, sizeof(DictEnt *), 1 << HT_EXP); - snprintf(path, LEN(path), "%s/%s", prefix, d->rom); + snprintf(path, ARRAY_COUNT(path), "%s/%s", prefix, d->rom); if ((nbanks = count_term_banks(path)) == 0) { fprintf(stderr, "no term banks found: %s\n", path); return 0; } for (size_t i = 1; i <= nbanks; i++) { - snprintf(tbank, LEN(tbank), "%s/term_bank_%zu.json", path, i); + snprintf(tbank, ARRAY_COUNT(tbank), "%s/term_bank_%zu.json", path, i); parse_term_bank(&d->ht, tbank); } @@ -318,8 +309,8 @@ find_and_print_defs(Dict *dict, s8 *terms, size_t nterms) static void repl(Dict *dicts, size_t ndicts) { - char t[BUFLEN]; - s8 buf = {t, BUFLEN}; + u8 t[BUFLEN]; + s8 buf = {.len = ARRAY_COUNT(t), .s = t}; size_t i; make_dicts(dicts, ndicts); @@ -328,10 +319,10 @@ repl(Dict *dicts, size_t ndicts) for (;;) { fputs(repl_prompt, stdout); fflush(stdout); - buf.len = BUFLEN; - if (fgets(buf.s, buf.len, stdin) == NULL) + buf.len = ARRAY_COUNT(t); + if (fgets((char *)buf.s, buf.len, stdin) == NULL) break; - buf.len = strlen(buf.s); + buf.len = strlen((char *)buf.s); for (i = 0; i < ndicts; i++) find_and_print(s8trim(buf), &dicts[i]); } @@ -342,59 +333,63 @@ int main(int argc, char *argv[]) { s8 *terms = NULL; - char *t; Dict *dicts = NULL; - size_t i, ndicts = 0, nterms = 0; + size_t ndicts = 0, nterms = 0; int iflag = 0; - argv0 = argv[0]; - - ARGBEGIN { - case 'd': - t = EARGF(usage()); - for (i = 0; i < LEN(default_dict_map); i++) { - if (strcmp(t, default_dict_map[i].rom) == 0) { - dicts = &default_dict_map[i]; - ndicts++; - break; + char *argv0 = argv[0]; + for (argv++, argc--; argv[0] && argv[0][0] == '-' && argv[0][1]; argc--, argv++) { + /* NOTE: '--' to end parameters */ + if (argv[0][1] == '-' && argv[0][2] == 0) { + argv++; + argc--; + break; + } + switch (argv[0][1]) { + case 'F': + if (!argv[1] || !argv[1][0]) + usage(argv0); + fsep = unescape(cstr_to_s8(argv[1])); + argv++; + break; + case 'd': + if (!argv[1] || !argv[1][0]) + usage(argv0); + for (u32 j = 0; j < ARRAY_COUNT(default_dict_map); j++) { + if (strcmp(argv[1], default_dict_map[j].rom) == 0) { + dicts = &default_dict_map[j]; + ndicts++; + break; + } } + if (dicts == NULL) + die("invalid dictionary name: %s\n", argv[1]); + argv++; + break; + case 'i': iflag = 1; break; + default: usage(argv0); break; } - if (dicts == NULL) - die("invalid dictionary name: %s\n", t); - break; - case 'F': - t = EARGF(usage()); - fsep = unescape((s8){t, strlen(t)}); - break; - case 'i': - iflag = 1; - break; - default: - usage(); - } ARGEND + } if (ndicts == 0) { - dicts = default_dict_map; - ndicts = LEN(default_dict_map); + dicts = default_dict_map; + ndicts = ARRAY_COUNT(default_dict_map); } /* remaining argv elements are terms to search for */ - for (i = 0; argc && *argv; argv++, i++, argc--) { + for (i32 i = 0; argc && *argv; argv++, i++, argc--) { terms = xreallocarray(terms, ++nterms, sizeof(s8)); - terms[i].s = *argv; - terms[i].len = strlen(terms[i].s); + terms[i] = cstr_to_s8(*argv); } if (nterms == 0 && iflag == 0) - usage(); + usage(argv0); if (iflag == 0) - for (i = 0; i < ndicts; i++) + for (size_t i = 0; i < ndicts; i++) find_and_print_defs(&dicts[i], terms, nterms); else repl(dicts, ndicts); - free(terms); - return 0; } diff --git a/util.c b/util.c @@ -6,13 +6,22 @@ #include <stdlib.h> #include <string.h> -#define LEN(a) (sizeof(a) / sizeof(*a)) +#include <stdint.h> +#include <stddef.h> +typedef uint8_t u8; +typedef int64_t i64; +typedef uint64_t u64; +typedef int32_t i32; +typedef uint32_t u32; +typedef ptrdiff_t size; + +#define ARRAY_COUNT(a) (sizeof(a) / sizeof(*a)) typedef struct { - char *s; - ptrdiff_t len; + size len; + u8 *s; } s8; -#define s8(s) (s8){s, LEN(s) - 1} +#define s8(cstr) (s8){.len = ARRAY_COUNT(cstr) - 1, .s = (u8 *)cstr} static void __attribute__((noreturn)) die(const char *fmt, ...) @@ -41,7 +50,7 @@ s8cmp(s8 a, s8 b) static s8 s8trim(s8 str) { - char *p = &str.s[str.len-1]; + u8 *p = str.s + str.len - 1; for (; str.len && isspace(*p); str.len--, p--); for (; str.len && isspace(*str.s); str.len--, str.s++); @@ -53,8 +62,8 @@ s8trim(s8 str) static s8 unescape(s8 str) { - char *t = str.s; - ptrdiff_t rem = str.len; + u8 *t = str.s; + size rem = str.len; int off; while ((t = memchr(t, '\\', rem)) != NULL) { @@ -84,12 +93,20 @@ xreallocarray(void *o, size_t n, size_t s) } static s8 -s8dup(void *src, ptrdiff_t len) +s8dup(void *src, size len) { - s8 str = {0, len}; + s8 str = {.len = len}; if (len < 0) die("s8dup(): negative len\n"); str.s = xreallocarray(NULL, 1, len); memcpy(str.s, src, len); return str; } + +static s8 +cstr_to_s8(char *cstr) +{ + s8 result = {.s = (u8 *)cstr}; + if (cstr) while (*cstr) { result.len++; cstr++; } + return result; +}