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: be5a215b9ac0a6faf455419e35b8f204ea07468a
Parent: 5bea1cb22037e539c0bedcc83d8d242817f8f145
Author: Randy Palamar
Date:   Mon, 27 May 2024 08:09:08 -0600

unity build

Diffstat:
DMakefile | 31-------------------------------
MREADME.md | 9+++++----
Abuild.sh | 8++++++++
Dconfig.mk | 7-------
Mjdict.c | 11+++++------
Mutil.c | 20+++++++++++++-------
Dutil.h | 15---------------
Myomidict.c | 35++++++++++++++++++++++++++---------
Dyomidict.h | 27---------------------------
9 files changed, 57 insertions(+), 106 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,31 +0,0 @@ -# See LICENSE for license details. -include config.mk - -OBJ = jdict.o yomidict.o util.o - -default: jdict - -config.h: - cp config.def.h $@ - -.c.o: - $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $< - -$(OBJ): config.h config.mk - -jdict: $(OBJ) - $(CC) -o $@ $(OBJ) $(LDFLAGS) - -install: default jdict.1 - mkdir -p $(PREFIX)/bin - cp jdict $(PREFIX)/bin - chmod 755 $(PREFIX)/bin/jdict - mkdir -p $(MANPREFIX)/man1 - cp jdict.1 $(MANPREFIX)/man1/jdict.1 - chmod 644 $(MANPREFIX)/man1/jdict.1 - -uninstall: - rm $(PREFIX)/bin/jdict - -clean: - rm *.o jdict diff --git a/README.md b/README.md @@ -24,11 +24,12 @@ zip file created by `yomichan-import` needs to be extracted and stored in the prefix specified in `config.h`. The folder name should be also specified in `config.h`. -After modifying `config.h`, `config.mk` can also be modified to suit -your system and then the following can be used to install (using root -as needed): +To build: + ./build.sh - make clean install +To install: + cp ./jdict ~/bin/ + cp ./jdict.1 ~/.local/share/man/man1/ [Yomichan]: https://github.com/FooSoft/yomichan [yomichan-import]: https://github.com/FooSoft/yomichan-import/ diff --git a/build.sh b/build.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -x + +cflags="-march=native -O3 -std=c99 -Wall -Wextra -pedantic" +cflags="$cflags -D_BSD_SOURCE" +ldflags="-s -static" + +cc $cflags $ldflags jdict.c -o jdict diff --git a/config.mk b/config.mk @@ -1,7 +0,0 @@ -# See LICENSE for license details. -PREFIX = /usr/local -MANPREFIX = $(PREFIX)/share/man - -CPPFLAGS = -D_BSD_SOURCE -CFLAGS = -O3 -std=c99 -Wall -Wextra -pedantic -LDFLAGS = -s -static diff --git a/jdict.c b/jdict.c @@ -11,8 +11,8 @@ #include <unistd.h> #include "arg.h" -#include "util.h" -#include "yomidict.h" +#include "util.c" +#include "yomidict.c" #define YOMI_TOKS_PER_ENT 10 @@ -184,7 +184,7 @@ parse_term_bank(struct ht *ht, const char *tbank) size_t flen; char *data; YomiTok *toks = NULL; - YomiScanner *s = NULL; + YomiScanner s = {0}; DictEnt *e, *n; if ((fd = open(tbank, O_RDONLY)) < 0) @@ -200,8 +200,8 @@ parse_term_bank(struct ht *ht, const char *tbank) ntoks = (1 << HT_EXP) * YOMI_TOKS_PER_ENT + 1; toks = xreallocarray(toks, ntoks, sizeof(YomiTok)); - s = yomi_scanner_new(data, flen); - while ((r = yomi_scan(s, toks, ntoks)) < 0) { + yomi_scanner_init(&s, data, flen); + while ((r = yomi_scan(&s, toks, ntoks)) < 0) { switch (r) { case YOMI_ERROR_NOMEM: goto cleanup; @@ -241,7 +241,6 @@ parse_term_bank(struct ht *ht, const char *tbank) cleanup: munmap(data, flen); free(toks); - free(s); } static int diff --git a/util.c b/util.c @@ -6,9 +6,15 @@ #include <stdlib.h> #include <string.h> -#include "util.h" +#define LEN(a) (sizeof(a) / sizeof(*a)) -void +typedef struct { + char *s; + ptrdiff_t len; +} s8; +#define s8(s) (s8){s, LEN(s) - 1} + +static void __attribute__((noreturn)) die(const char *fmt, ...) { va_list ap; @@ -20,7 +26,7 @@ die(const char *fmt, ...) exit(1); } -int +static int s8cmp(s8 a, s8 b) { if (a.len == 0 || a.len != b.len) @@ -32,7 +38,7 @@ s8cmp(s8 a, s8 b) * trim whitespace from start and end of str * returns a new s8 (same memory) */ -s8 +static s8 s8trim(s8 str) { char *p = &str.s[str.len-1]; @@ -44,7 +50,7 @@ s8trim(s8 str) } /* replace escaped control chars with their actual char */ -s8 +static s8 unescape(s8 str) { char *t = str.s; @@ -66,7 +72,7 @@ unescape(s8 str) return str; } -void * +static void * xreallocarray(void *o, size_t n, size_t s) { void *new; @@ -77,7 +83,7 @@ xreallocarray(void *o, size_t n, size_t s) return new; } -s8 +static s8 s8dup(void *src, ptrdiff_t len) { s8 str = {0, len}; diff --git a/util.h b/util.h @@ -1,15 +0,0 @@ -/* See LICENSE for license details. */ -#define LEN(a) (sizeof(a) / sizeof(*a)) - -typedef struct { - char *s; - ptrdiff_t len; -} s8; -#define s8(s) (s8){s, LEN(s) - 1} - -int s8cmp(s8, s8); -s8 s8dup(void *, ptrdiff_t); -s8 s8trim(s8); -s8 unescape(s8); -void *xreallocarray(void *, size_t, size_t); -void die(const char *, ...); diff --git a/yomidict.c b/yomidict.c @@ -8,31 +8,48 @@ #include <stddef.h> #include <stdlib.h> -#include "util.h" -#include "yomidict.h" - #define ul unsigned long #define ISDIGIT(c) ((c) >= '0' && (c) <= '9') -struct YomiScanner { +typedef enum { + YOMI_UNDEF = 0, + YOMI_ENTRY = 1, + YOMI_ARRAY = 2, + YOMI_STR = 4, + YOMI_NUM = 8 +} YomiType; + +typedef struct { + unsigned long start; + unsigned long end; + unsigned long len; + int parent; /* parent tok number */ + YomiType type; +} YomiTok; + +typedef struct { const char *data; ul len; ul pos; /* offset in yomi bank */ ul toknext; int parent; /* parent tok of current element */ +} YomiScanner; + +enum { + YOMI_ERROR_NOMEM = -1, + YOMI_ERROR_INVAL = -2, + YOMI_ERROR_MALFO = -3 }; -YomiScanner * -yomi_scanner_new(const char *data, unsigned long datalen) +static void +yomi_scanner_init(YomiScanner *s, const char *data, ul datalen) { - YomiScanner *s = xreallocarray(NULL, sizeof(YomiScanner), 1); s->data = data; s->len = datalen; s->pos = 0; s->toknext = 0; s->parent = -1; - return s; } static YomiTok * @@ -109,7 +126,7 @@ number(YomiScanner *s, YomiTok *t) return YOMI_ERROR_MALFO; } -int +static int yomi_scan(YomiScanner *s, YomiTok *toks, ul ntoks) { YomiTok *tok; diff --git a/yomidict.h b/yomidict.h @@ -1,27 +0,0 @@ -/* See LICENSE for license details. */ -typedef enum { - YOMI_UNDEF = 0, - YOMI_ENTRY = 1, - YOMI_ARRAY = 2, - YOMI_STR = 4, - YOMI_NUM = 8 -} YomiType; - -typedef struct { - unsigned long start; - unsigned long end; - unsigned long len; - int parent; /* parent tok number */ - YomiType type; -} YomiTok; - -typedef struct YomiScanner YomiScanner; - -enum { - YOMI_ERROR_NOMEM = -1, - YOMI_ERROR_INVAL = -2, - YOMI_ERROR_MALFO = -3 -}; - -YomiScanner *yomi_scanner_new(const char *, unsigned long); -int yomi_scan(YomiScanner *, YomiTok *, unsigned long);