iomenu

interactive terminal dmenu-style menu
Log | Files | Refs | README | LICENSE

commit 9e523e73c8f395ee7270ed2826efa3f7ffb5908d
parent c4011c678d9f4aaa75e1bbb92a8a9e5b6180043b
Author: Josuah Demangeon <me@josuah.net>
Date:   Tue, 14 Jul 2020 17:55:23 +0200

import code common to multiple projects

Diffstat:
MMakefile | 59++++++++++++++++++++++++++++++++++++-----------------------
AREADME | 2++
Darg.h | 27---------------------------
RREADME -> doc/index.md | 0
Riomenu.1 -> doc/iomenu.1 | 0
Miomenu.c | 152+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Asrc/compat.h | 11+++++++++++
Asrc/compat/strcasestr.c | 25+++++++++++++++++++++++++
Asrc/compat/strsep.c | 23+++++++++++++++++++++++
Asrc/compat/wcwidth.c | 309+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/log.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/log.h | 15+++++++++++++++
Asrc/mem.c | 132+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/mem.h | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/str.c | 119+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/str.h | 26++++++++++++++++++++++++++
Asrc/utf8.c | 214+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/utf8.h | 13+++++++++++++
Asrc/util.h | 12++++++++++++
Dstrcasestr.c | 25-------------------------
Dstrsep.c | 21---------------------
Rtest.c -> t/test.c | 0
Dutf8.c | 424-------------------------------------------------------------------------------
Dutf8.h | 11-----------
Dutil.h | 31-------------------------------
25 files changed, 1128 insertions(+), 635 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,32 +1,45 @@ -include config.mk +NAME = iomenu +VERSION = 0.1 + +SRC = src/utf8.c src/str.c src/log.c src/mem.c src/compat/strcasestr.c \ + src/compat/strsep.c src/compat/wcwidth.c + +HDR = src/mem.h src/compat.h src/util.h src/str.h src/log.h src/utf8.h + +BIN = iomenu -SRC = iomenu.c strcasestr.c strsep.c utf8.c OBJ = ${SRC:.c=.o} -all: iomenu +LIB = -.c.o: - ${CC} -c -o $@ ${CFLAGS} $< +W = -Wall -Wextra -std=c99 --pedantic +I = -I./src +L = +D = -D_POSIX_C_SOURCE=200811L -DVERSION='"${VERSION}"' +CFLAGS = $I $D $W -g +LDFLAGS = $L -static +PREFIX = /usr/local +MANPREFIX = ${PREFIX}/man -iomenu: ${OBJ} - ${CC} -o $@ ${LFLAGS} ${OBJ} +all: ${BIN} -iomenu.o: iomenu.c util.h -strcasestr.o: strcasestr.c util.h -strsep.o: strsep.c util.h -test.o: test.c util.h -utf8.o: utf8.c utf8.h +.c.o: + ${CC} -c ${CFLAGS} -o $@ $< -.PHONY: test -test: test.c - ${CC} -o $@ ${LFLAGS} test.c utf8.c - ./$@ +${OBJ}: ${HDR} +${BIN}: ${OBJ} ${BIN:=.o} + ${CC} ${LDFLAGS} -o $@ $@.o ${OBJ} ${LIB} clean: - rm -f *.o *.core iomenu test - -install: iomenu - mkdir -p ${MANPREFIX}/man1 - cp *.1 ${MANPREFIX}/man1 - mkdir -p ${PREFIX}/bin - cp iomenu bin/* ${PREFIX}/bin + rm -rf *.o */*.o ${BIN} ${NAME}-${VERSION} *.gz + +install: + mkdir -p ${DESTDIR}${PREFIX}/bin + cp -rf ${BIN} ${DESTDIR}${PREFIX}/bin + mkdir -p ${DESTDIR}${MANPREFIX}/man1 + cp -rf doc/*.1 ${DESTDIR}${MANPREFIX}/man1 + +dist: clean + mkdir -p ${NAME}-${VERSION} + cp -r README Makefile doc ${SRC} ${NAME}-${VERSION} + tar -cf - ${NAME}-${VERSION} | gzip -c >${NAME}-${VERSION}.tar.gz diff --git a/README b/README @@ -0,0 +1 @@ +doc/index.md+ \ No newline at end of file diff --git a/arg.h b/arg.h @@ -1,27 +0,0 @@ -#ifndef ARG_H -#define ARG_H - -extern char *argv0; - -#define ARGBEGIN \ - for (argv0 = *argv, argv++, argc--; \ - argv[0] != NULL && argv[0][0] == '-' && argv[0][1] != '\0'; \ - argc--, argv++) { \ - char **_argv, *_a; \ - if (argv[0][1] == '-' && argv[0][2] == '\0') { \ - argv++, argc--; \ - break; \ - } \ - for (_argv = argv, _a = *argv + 1; *_a != '\0'; _a++) { \ - switch (*_a) - -#define ARGEND \ - if (_argv != argv) \ - break; \ - } \ - } - -#define EARGF(x) \ - ((argv[1] == NULL) ? ((x), (char *)0) : (argc--, argv++, argv[0])) - -#endif diff --git a/README b/doc/index.md diff --git a/iomenu.1 b/doc/iomenu.1 diff --git a/iomenu.c b/iomenu.c @@ -1,6 +1,7 @@ #include <sys/ioctl.h> #include <ctype.h> +#include <errno.h> #include <fcntl.h> #include <limits.h> #include <signal.h> @@ -12,29 +13,30 @@ #include <unistd.h> #include "utf8.h" +#include "compat.h" #include "util.h" -#include "arg.h" +#include "log.h" #ifndef SIGWINCH #define SIGWINCH 28 #endif -static struct termios termios; -struct winsize ws; -static int linec = 0, matchc = 0, cur = 0; -static char **linev = NULL, **matchv = NULL; -static char input[LINE_MAX]; -static int hsflag = 0; -char *argv0; +static struct termios termios; +struct winsize ws; +static int linec = 0, matchc = 0, cur = 0; +static char **linev = NULL, **matchv = NULL; +static char input[LINE_MAX]; +static char *flag['z']; +char *argv0; /* -** Keep the line if it match every token (in no particular order, -** and allowed to be overlapping). -*/ + * Keep the line if it match every token (in no particular order, + * and allowed to be overlapping). + */ static int match_line(char *line, char **tokv) { - if (hsflag && line[0] == '#') + if (flag['#'] && line[0] == '#') return 2; for (; *tokv != NULL; tokv++) if (strcasestr(line, *tokv) == NULL) @@ -43,38 +45,39 @@ match_line(char *line, char **tokv) } /* -** Free the structures, reset the terminal state and exit with an -** error message. -*/ + * Free the structures, reset the terminal state and exit with an + * error message. + */ static void -die(const char *s) +iomenu_die(const char *s) { + int e = errno; + if (tcsetattr(STDERR_FILENO, TCSANOW, &termios) == -1) - perror("tcsetattr while dying"); - close(STDERR_FILENO); - perror(s); - exit(EXIT_FAILURE); + warn("tcsetattr while dying"); + errno = e; + die("%s", s); } /* -** Read one key from stdin and die if it failed to prevent to read -** in an endless loop. This caused the load average to go over 10 -** at work. :S -*/ + * Read one key from stdin and die if it failed to prevent to read + * in an endless loop. This caused the load average to go over 10 + * at work. :S + */ int getkey(void) { int c; if ((c = fgetc(stdin)) == EOF) - die("getting a key"); + iomenu_die("getting a key"); return c; } /* -** Split a buffer into an array of lines, without allocating memory for every -** line, but using the input buffer and replacing '\n' by '\0'. -*/ + * Split a buffer into an array of lines, without allocating memory for every + * line, but using the input buffer and replacing '\n' by '\0'. + */ static void split_lines(char *buf) { @@ -84,9 +87,9 @@ split_lines(char *buf) for (b = buf; (b = strchr(b, '\n')) != NULL && b[1] != '\0'; b++) linec++; if ((lv = linev = calloc(linec + 1, sizeof(char **))) == NULL) - die("calloc"); + iomenu_die("calloc"); if ((mv = matchv = calloc(linec + 1, sizeof(char **))) == NULL) - die("calloc"); + iomenu_die("calloc"); *mv = *lv = b = buf; while ((b = strchr(b, '\n')) != NULL) { *b = '\0'; @@ -95,9 +98,9 @@ split_lines(char *buf) } /* -** Read stdin in a single malloc-ed buffer, realloc-ed to twice its size every -** time the previous buffer is filled. -*/ + * Read stdin in a single malloc-ed buffer, realloc-ed to twice its size every + * time the previous buffer is filled. + */ static void read_stdin(void) { @@ -107,13 +110,13 @@ read_stdin(void) size = BUFSIZ; off = 0; if ((buf = malloc(size)) == NULL) - die("malloc"); + iomenu_die("malloc"); while ((len = read(STDIN_FILENO, buf + off, size - off)) > 0) { off += len; if (off == size) { size *= 2; if ((buf = realloc(buf, size + 1)) == NULL) - die("realloc"); + iomenu_die("realloc"); } } buf[off] = '\0'; @@ -126,7 +129,7 @@ move(int sign) int i; for (i = cur + sign; 0 <= i && i < matchc; i += sign) { - if (hsflag == 0 || matchv[i][0] != '#') { + if (flag['#'] == 0 || matchv[i][0] != '#') { cur = i; break; } @@ -144,10 +147,10 @@ tokenize(char **tokv, char *str) } /* -** First split input into token, then match every token independently against -** every line. The matching lines fills matchv. Matches are searched inside -** of `searchv' of size `searchc' -*/ + * First split input into token, then match every token independently against + * every line. The matching lines fills matchv. Matches are searched inside + * of `searchv' of size `searchc' + */ static void filter(int searchc, char **searchv) { @@ -163,7 +166,7 @@ filter(int searchc, char **searchv) for (n = 0; n < searchc; n++) if (match_line(searchv[n], tokv)) matchv[matchc++] = searchv[n]; - if (hsflag && matchv[cur][0] == '#') + if (flag['#'] && matchv[cur][0] == '#') move(+1); } @@ -184,7 +187,7 @@ static void move_header(signed int sign) { move(sign); - if (hsflag == 0) + if (flag['#'] == 0) return; for (cur += sign; 0 <= cur; cur += sign) { if (cur >= matchc) { @@ -229,7 +232,7 @@ print_selection(void) { char **match; - if (hsflag) { + if (flag['#']) { match = matchv + cur; while (--match >= matchv) { if ((*match)[0] == '#') { @@ -239,7 +242,7 @@ print_selection(void) } putchar('\t'); } - if (matchc == 0 || (hsflag && matchv[cur][0] == '#')) + if (matchc == 0 || (flag['#'] && matchv[cur][0] == '#')) puts(input); else puts(matchv[cur]); @@ -266,46 +269,46 @@ top: remove_word(); break; case 127: - case CTL('H'): /* backspace */ + case CTL('H'): /* backspace */ input[strlen(input) - 1] = '\0'; filter(linec, linev); break; - case CSI('A'): /* up */ + case CSI('A'): /* up */ case CTL('P'): move(-1); break; case ALT('p'): move_header(-1); break; - case CSI('B'): /* down */ + case CSI('B'): /* down */ case CTL('N'): move(+1); break; case ALT('n'): move_header(+1); break; - case CSI('5'): /* page up */ + case CSI('5'): /* page up */ if (getkey() != '~') break; /* FALLTHROUGH */ case ALT('v'): move_page(-1); break; - case CSI('6'): /* page down */ + case CSI('6'): /* page down */ if (getkey() != '~') break; /* FALLTHROUGH */ case CTL('V'): move_page(+1); break; - case CTL('I'): /* tab */ + case CTL('I'): /* tab */ if (linec > 0) { strncpy(input, matchv[cur], sizeof(input)); input[sizeof(input) - 1] = '\0'; } filter(matchc, matchv); break; - case CTL('J'): /* enter */ + case CTL('J'):/* enter */ case CTL('M'): print_selection(); return 0; @@ -325,7 +328,7 @@ top: static void print_line(char *line, int highlight) { - if (hsflag && line[0] == '#') + if (flag['#'] && line[0] == '#') fprintf(stderr, "\n\x1b[1m\r%.*s\x1b[m", utf8_col(line + 1, ws.ws_col, 0), line + 1); else if (highlight) @@ -343,7 +346,7 @@ print_screen(void) int p, i, c, cols, rows; cols = ws.ws_col; - rows = ws.ws_row - 1; /* -1 to keep one line for user input */ + rows = ws.ws_row - 1; /* -1 to keep one line for user input */ p = c = 0; i = cur - cur % rows; m = matchv + i; @@ -368,10 +371,10 @@ term_set(void) fputs("\x1b[s\x1b[?1049h\x1b[H", stderr); if (tcgetattr(STDERR_FILENO, &termios) == -1 || tcgetattr(STDERR_FILENO, &new) == -1) - die("setting terminal"); + iomenu_die("setting terminal"); new.c_lflag &= ~(ICANON | ECHO | IEXTEN | IGNBRK | ISIG); if (tcsetattr(STDERR_FILENO, TCSANOW, &new) == -1) - die("tcsetattr"); + iomenu_die("tcsetattr"); } /* @@ -382,46 +385,49 @@ term_reset(void) { fputs("\x1b[2J\x1b[u\033[?1049l", stderr); if (tcsetattr(STDERR_FILENO, TCSANOW, &termios)) - die("resetting terminal"); + iomenu_die("resetting terminal"); } static void sigwinch(int sig) { if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1) - die("ioctl"); + iomenu_die("ioctl"); print_screen(); signal(sig, sigwinch); } static void -usage(void) +usage(char const *arg0) { - fputs("usage: iomenu [-#]\n", stderr); - exit(EXIT_FAILURE); + fprintf(stderr, "usage: %s [-#]\n", arg0); + exit(1); } /* -** Read stdin in a buffer, filling a table of lines, then re-open stdin to -** /dev/tty for an interactive (raw) session to let the user filter and select -** one line by searching words within stdin. This was inspired from dmenu. -*/ + * Read stdin in a buffer, filling a table of lines, then re-open stdin to + * /dev/tty for an interactive (raw) session to let the user filter and select + * one line by searching words within stdin. This was inspired from dmenu. + */ int main(int argc, char *argv[]) { - ARGBEGIN { - case '#': - hsflag = 1; - break; - default: - usage(); - } ARGEND + arg0 = *argv; + for (int c; (c = getopt(argc, argv, "#")) > 0; flag[c] = optarg) + if (c == '?') + usage(arg0); + argc -= optind; + argv += optind; input[0] = '\0'; + read_stdin(); + filter(linec, linev); - if (freopen("/dev/tty", "r", stdin) == NULL) - die("reopening /dev/tty as stdin"); + + if (!isatty(2)) + iomenu_die("file descriptor 2 (stderr)"); + term_set(); sigwinch(SIGWINCH); diff --git a/src/compat.h b/src/compat.h @@ -0,0 +1,11 @@ +#ifndef COMPAT_H +#define COMPAT_H + +#define wcwidth(c) mk_wcwidth_cjk(c) +#define wcswidth(s, n) mk_wcwidth_cjk(s, n) + +/** src/compat/?*.c **/ +char * strcasestr(const char *str1, const char *str2); +char * strsep(char **str_p, char const *sep); + +#endif diff --git a/src/compat/strcasestr.c b/src/compat/strcasestr.c @@ -0,0 +1,25 @@ +#include <ctype.h> +#include <stddef.h> + +#include "compat.h" + +char * +strcasestr(const char *str1, const char *str2) +{ + const char *s1; + const char *s2; + + for (;;) { + s1 = str1; + s2 = str2; + while (*s1 != '\0' && tolower(*s1) == tolower(*s2)) + s1++, s2++; + if (*s2 == '\0') + return (char *) str1; + if (*s1 == '\0') + return NULL; + str1++; + } + + return NULL; +} diff --git a/src/compat/strsep.c b/src/compat/strsep.c @@ -0,0 +1,23 @@ +#include <string.h> + +#include "compat.h" + +char * +strsep(char **str_p, char const *sep) +{ + char *s, *prev; + + if (*str_p == NULL) + return NULL; + + for (s = prev = *str_p; strchr(sep, *s) == NULL; s++) + continue; + + if (*s == '\0') { + *str_p = NULL; + } else { + *s = '\0'; + *str_p = s + 1; + } + return prev; +} diff --git a/src/compat/wcwidth.c b/src/compat/wcwidth.c @@ -0,0 +1,309 @@ +/* + * This is an implementation of wcwidth() and wcswidth() (defined in + * IEEE Std 1002.1-2001) for Unicode. + * + * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html + * http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html + * + * In fixed-width output devices, Latin characters all occupy a single + * "cell" position of equal width, whereas ideographic CJK characters + * occupy two such cells. Interoperability between terminal-line + * applications and (teletype-style) character terminals using the + * UTF-8 encoding requires agreement on which character should advance + * the cursor by how many cell positions. No established formal + * standards exist at present on which Unicode character shall occupy + * how many cell positions on character terminals. These routines are + * a first attempt of defining such behavior based on simple rules + * applied to data provided by the Unicode Consortium. + * + * For some graphical characters, the Unicode standard explicitly + * defines a character-cell width via the definition of the East Asian + * FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes. + * In all these cases, there is no ambiguity about which width a + * terminal shall use. For characters in the East Asian Ambiguous (A) + * class, the width choice depends purely on a preference of backward + * compatibility with either historic CJK or Western practice. + * Choosing single-width for these characters is easy to justify as + * the appropriate long-term solution, as the CJK practice of + * displaying these characters as double-width comes from historic + * implementation simplicity (8-bit encoded characters were displayed + * single-width and 16-bit ones double-width, even for Greek, + * Cyrillic, etc.) and not any typographic considerations. + * + * Much less clear is the choice of width for the Not East Asian + * (Neutral) class. Existing practice does not dictate a width for any + * of these characters. It would nevertheless make sense + * typographically to allocate two character cells to characters such + * as for instance EM SPACE or VOLUME INTEGRAL, which cannot be + * represented adequately with a single-width glyph. The following + * routines at present merely assign a single-cell width to all + * neutral characters, in the interest of simplicity. This is not + * entirely satisfactory and should be reconsidered before + * establishing a formal standard in this area. At the moment, the + * decision which Not East Asian (Neutral) characters should be + * represented by double-width glyphs cannot yet be answered by + * applying a simple rule from the Unicode database content. Setting + * up a proper standard for the behavior of UTF-8 character terminals + * will require a careful analysis not only of each Unicode character, + * but also of each presentation form, something the author of these + * routines has avoided to do so far. + * + * http://www.unicode.org/unicode/reports/tr11/ + * + * Markus Kuhn -- 2007-05-26 (Unicode 5.0) + * + * Permission to use, copy, modify, and distribute this software + * for any purpose and without fee is hereby granted. The author + * disclaims all warranties with regard to this software. + * + * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c + */ + +#include <wchar.h> + +struct interval { + int first; + int last; +}; + +/* auxiliary function for binary search in interval table */ +static int bisearch(wchar_t ucs, const struct interval *table, int max) { + int min = 0; + int mid; + + if (ucs < table[0].first || ucs > table[max].last) + return 0; + while (max >= min) { + mid = (min + max) / 2; + if (ucs > table[mid].last) + min = mid + 1; + else if (ucs < table[mid].first) + max = mid - 1; + else + return 1; + } + + return 0; +} + + +/* The following two functions define the column width of an ISO 10646 + * character as follows: + * + * - The null character (U+0000) has a column width of 0. + * + * - Other C0/C1 control characters and DEL will lead to a return + * value of -1. + * + * - Non-spacing and enclosing combining characters (general + * category code Mn or Me in the Unicode database) have a + * column width of 0. + * + * - SOFT HYPHEN (U+00AD) has a column width of 1. + * + * - Other format characters (general category code Cf in the Unicode + * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0. + * + * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF) + * have a column width of 0. + * + * - Spacing characters in the East Asian Wide (W) or East Asian + * Full-width (F) category as defined in Unicode Technical + * Report #11 have a column width of 2. + * + * - All remaining characters (including all printable + * ISO 8859-1 and WGL4 characters, Unicode control characters, + * etc.) have a column width of 1. + * + * This implementation assumes that wchar_t characters are encoded + * in ISO 10646. + */ + +int mk_wcwidth(wchar_t ucs) +{ + /* sorted list of non-overlapping intervals of non-spacing characters */ + /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */ + static const struct interval combining[] = { + { 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 }, + { 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 }, + { 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 }, + { 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 }, + { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED }, + { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A }, + { 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 }, + { 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D }, + { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 }, + { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, + { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C }, + { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, + { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, + { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, + { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C }, + { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D }, + { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 }, + { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 }, + { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC }, + { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD }, + { 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, + { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, + { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, + { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, + { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, + { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, + { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, + { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, + { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 }, + { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F }, + { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 }, + { 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD }, + { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD }, + { 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 }, + { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B }, + { 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 }, + { 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 }, + { 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF }, + { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 }, + { 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F }, + { 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B }, + { 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F }, + { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB }, + { 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F }, + { 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 }, + { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD }, + { 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F }, + { 0xE0100, 0xE01EF } + }; + + /* test for 8-bit control characters */ + if (ucs == 0) + return 0; + if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) + return -1; + + /* binary search in table of non-spacing characters */ + if (bisearch(ucs, combining, + sizeof(combining) / sizeof(struct interval) - 1)) + return 0; + + /* if we arrive here, ucs is not a combining or C0/C1 control character */ + + return 1 + + (ucs >= 0x1100 && + (ucs <= 0x115f || /* Hangul Jamo init. consonants */ + ucs == 0x2329 || ucs == 0x232a || + (ucs >= 0x2e80 && ucs <= 0xa4cf && + ucs != 0x303f) || /* CJK ... Yi */ + (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */ + (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */ + (ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */ + (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */ + (ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */ + (ucs >= 0xffe0 && ucs <= 0xffe6) || + (ucs >= 0x20000 && ucs <= 0x2fffd) || + (ucs >= 0x30000 && ucs <= 0x3fffd))); +} + + +int mk_wcswidth(const wchar_t *pwcs, size_t n) +{ + int w, width = 0; + + for (;*pwcs && n-- > 0; pwcs++) + if ((w = mk_wcwidth(*pwcs)) < 0) + return -1; + else + width += w; + + return width; +} + + +/* + * The following functions are the same as mk_wcwidth() and + * mk_wcswidth(), except that spacing characters in the East Asian + * Ambiguous (A) category as defined in Unicode Technical Report #11 + * have a column width of 2. This variant might be useful for users of + * CJK legacy encodings who want to migrate to UCS without changing + * the traditional terminal character-width behaviour. It is not + * otherwise recommended for general use. + */ +int mk_wcwidth_cjk(wchar_t ucs) +{ + /* sorted list of non-overlapping intervals of East Asian Ambiguous + * characters, generated by "uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c" */ + static const struct interval ambiguous[] = { + { 0x00A1, 0x00A1 }, { 0x00A4, 0x00A4 }, { 0x00A7, 0x00A8 }, + { 0x00AA, 0x00AA }, { 0x00AE, 0x00AE }, { 0x00B0, 0x00B4 }, + { 0x00B6, 0x00BA }, { 0x00BC, 0x00BF }, { 0x00C6, 0x00C6 }, + { 0x00D0, 0x00D0 }, { 0x00D7, 0x00D8 }, { 0x00DE, 0x00E1 }, + { 0x00E6, 0x00E6 }, { 0x00E8, 0x00EA }, { 0x00EC, 0x00ED }, + { 0x00F0, 0x00F0 }, { 0x00F2, 0x00F3 }, { 0x00F7, 0x00FA }, + { 0x00FC, 0x00FC }, { 0x00FE, 0x00FE }, { 0x0101, 0x0101 }, + { 0x0111, 0x0111 }, { 0x0113, 0x0113 }, { 0x011B, 0x011B }, + { 0x0126, 0x0127 }, { 0x012B, 0x012B }, { 0x0131, 0x0133 }, + { 0x0138, 0x0138 }, { 0x013F, 0x0142 }, { 0x0144, 0x0144 }, + { 0x0148, 0x014B }, { 0x014D, 0x014D }, { 0x0152, 0x0153 }, + { 0x0166, 0x0167 }, { 0x016B, 0x016B }, { 0x01CE, 0x01CE }, + { 0x01D0, 0x01D0 }, { 0x01D2, 0x01D2 }, { 0x01D4, 0x01D4 }, + { 0x01D6, 0x01D6 }, { 0x01D8, 0x01D8 }, { 0x01DA, 0x01DA }, + { 0x01DC, 0x01DC }, { 0x0251, 0x0251 }, { 0x0261, 0x0261 }, + { 0x02C4, 0x02C4 }, { 0x02C7, 0x02C7 }, { 0x02C9, 0x02CB }, + { 0x02CD, 0x02CD }, { 0x02D0, 0x02D0 }, { 0x02D8, 0x02DB }, + { 0x02DD, 0x02DD }, { 0x02DF, 0x02DF }, { 0x0391, 0x03A1 }, + { 0x03A3, 0x03A9 }, { 0x03B1, 0x03C1 }, { 0x03C3, 0x03C9 }, + { 0x0401, 0x0401 }, { 0x0410, 0x044F }, { 0x0451, 0x0451 }, + { 0x2010, 0x2010 }, { 0x2013, 0x2016 }, { 0x2018, 0x2019 }, + { 0x201C, 0x201D }, { 0x2020, 0x2022 }, { 0x2024, 0x2027 }, + { 0x2030, 0x2030 }, { 0x2032, 0x2033 }, { 0x2035, 0x2035 }, + { 0x203B, 0x203B }, { 0x203E, 0x203E }, { 0x2074, 0x2074 }, + { 0x207F, 0x207F }, { 0x2081, 0x2084 }, { 0x20AC, 0x20AC }, + { 0x2103, 0x2103 }, { 0x2105, 0x2105 }, { 0x2109, 0x2109 }, + { 0x2113, 0x2113 }, { 0x2116, 0x2116 }, { 0x2121, 0x2122 }, + { 0x2126, 0x2126 }, { 0x212B, 0x212B }, { 0x2153, 0x2154 }, + { 0x215B, 0x215E }, { 0x2160, 0x216B }, { 0x2170, 0x2179 }, + { 0x2190, 0x2199 }, { 0x21B8, 0x21B9 }, { 0x21D2, 0x21D2 }, + { 0x21D4, 0x21D4 }, { 0x21E7, 0x21E7 }, { 0x2200, 0x2200 }, + { 0x2202, 0x2203 }, { 0x2207, 0x2208 }, { 0x220B, 0x220B }, + { 0x220F, 0x220F }, { 0x2211, 0x2211 }, { 0x2215, 0x2215 }, + { 0x221A, 0x221A }, { 0x221D, 0x2220 }, { 0x2223, 0x2223 }, + { 0x2225, 0x2225 }, { 0x2227, 0x222C }, { 0x222E, 0x222E }, + { 0x2234, 0x2237 }, { 0x223C, 0x223D }, { 0x2248, 0x2248 }, + { 0x224C, 0x224C }, { 0x2252, 0x2252 }, { 0x2260, 0x2261 }, + { 0x2264, 0x2267 }, { 0x226A, 0x226B }, { 0x226E, 0x226F }, + { 0x2282, 0x2283 }, { 0x2286, 0x2287 }, { 0x2295, 0x2295 }, + { 0x2299, 0x2299 }, { 0x22A5, 0x22A5 }, { 0x22BF, 0x22BF }, + { 0x2312, 0x2312 }, { 0x2460, 0x24E9 }, { 0x24EB, 0x254B }, + { 0x2550, 0x2573 }, { 0x2580, 0x258F }, { 0x2592, 0x2595 }, + { 0x25A0, 0x25A1 }, { 0x25A3, 0x25A9 }, { 0x25B2, 0x25B3 }, + { 0x25B6, 0x25B7 }, { 0x25BC, 0x25BD }, { 0x25C0, 0x25C1 }, + { 0x25C6, 0x25C8 }, { 0x25CB, 0x25CB }, { 0x25CE, 0x25D1 }, + { 0x25E2, 0x25E5 }, { 0x25EF, 0x25EF }, { 0x2605, 0x2606 }, + { 0x2609, 0x2609 }, { 0x260E, 0x260F }, { 0x2614, 0x2615 }, + { 0x261C, 0x261C }, { 0x261E, 0x261E }, { 0x2640, 0x2640 }, + { 0x2642, 0x2642 }, { 0x2660, 0x2661 }, { 0x2663, 0x2665 }, + { 0x2667, 0x266A }, { 0x266C, 0x266D }, { 0x266F, 0x266F }, + { 0x273D, 0x273D }, { 0x2776, 0x277F }, { 0xE000, 0xF8FF }, + { 0xFFFD, 0xFFFD }, { 0xF0000, 0xFFFFD }, { 0x100000, 0x10FFFD } + }; + + /* binary search in table of non-spacing characters */ + if (bisearch(ucs, ambiguous, + sizeof(ambiguous) / sizeof(struct interval) - 1)) + return 2; + + return mk_wcwidth(ucs); +} + + +int mk_wcswidth_cjk(const wchar_t *pwcs, size_t n) +{ + int w, width = 0; + + for (;*pwcs && n-- > 0; pwcs++) + if ((w = mk_wcwidth_cjk(*pwcs)) < 0) + return -1; + else + width += w; + + return width; +} diff --git a/src/log.c b/src/log.c @@ -0,0 +1,54 @@ +#include "log.h" + +#include <assert.h> +#include <string.h> + +/* + * log.c - log to standard error according to the log level + * + * Instead of logging to syslog, delegate logging to a separate + * tool, such as FreeBSD's daemon(8), POSIX's logger(1). + */ + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> + +#define LOG_DEFAULT 3 /* info */ + +char *arg0 = NULL; + +static int log_level = -1; + +void +log_print(int level, char const *flag, char const *fmt, ...) +{ + va_list va; + char *env; + int old_errno = errno; + + va_start(va, fmt); + + if (log_level < 0) { + env = getenv("LOG"); + log_level = (env == NULL) ? 0 : atoi(env); + if (log_level == 0) + log_level = LOG_DEFAULT; + } + + if (log_level < level) + return; + + if (arg0 != NULL) + fprintf(stderr, "%s: ", arg0); + + fprintf(stderr, "%s: ", flag); + vfprintf(stderr, fmt, va); + + if (old_errno != 0) + fprintf(stderr, ": %s", strerror(old_errno)); + + fprintf(stderr, "\n"); + fflush(stderr); + va_end(va); +} diff --git a/src/log.h b/src/log.h @@ -0,0 +1,15 @@ +#ifndef LOG_H +#define LOG_H + +#include <stdarg.h> + +/** src/log.c **/ +char *arg0; +void log_print(int level, char const *flag, char const *fmt, ...); + +#define die(...) (log_print(1, "error", __VA_ARGS__), exit(1)) +#define warn(...) log_print(2, "warn", __VA_ARGS__) +#define info(...) log_print(3, "info", __VA_ARGS__) +#define debug(...) log_print(4, "debug", __VA_ARGS__) + +#endif diff --git a/src/mem.c b/src/mem.c @@ -0,0 +1,132 @@ +#include "mem.h" + +#include <assert.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <errno.h> + +static struct mem_block * +mem_block(void *v) +{ + struct mem_block *block = (void *)((char *)v - sizeof *block); + + assert(memcmp(block->magic, MEM_BLOCK_MAGIC, 8) == 0); + return block; +} + +void * +mem_alloc(struct mem_pool *pool, size_t len) +{ + struct mem_block *block; + + block = calloc(1, sizeof *block + len); + if (block == NULL) + return NULL; + memcpy(block->magic, MEM_BLOCK_MAGIC, 8); + + block->len = len; + block->pool = pool; + block->prev = NULL; + block->next = pool->head; + if (pool->head != NULL) + pool->head->prev = block; + pool->head = block; + + return block->buf; +} + +int +mem_resize(void **pp, size_t len) +{ + struct mem_block *block = mem_block(*pp); + int is_first = (block == block->pool->head); + int is_same; + void *v; + + v = realloc(block, sizeof *block + len); + if (v == NULL) + return -1; + is_same = (block == v); + block = v; + + block->len = len; + + if (is_same) + return 0; + + if (block->prev != NULL) + block->prev->next = v; + if (block->next != NULL) + block->next->prev = v; + if (is_first) + block->pool->head = v; + *pp = block->buf; + + assert(memcmp(block->magic, MEM_BLOCK_MAGIC, 8) == 0); + return 0; +} + +int +mem_grow(void **pp, size_t len) +{ + assert(SIZE_MAX - len >= mem_block(*pp)->len); + + return mem_resize(pp, mem_length(*pp) + len); +} + +int +mem_shrink(void **pp, size_t len) +{ + assert(mem_block(*pp)->len >= len); + + return mem_resize(pp, mem_length(*pp) - len); +} + +size_t +mem_length(void *v) +{ + return mem_block(v)->len; +} + +int +mem_append(void **pp, char const *buf, size_t len) +{ + size_t old_len = mem_length(*pp); + struct mem_block *block; + + if (mem_grow(pp, len) < 0) + return -1; + block = mem_block(*pp); + memcpy((char *)block->buf + old_len, buf, len); + + assert(memcmp(block->magic, MEM_BLOCK_MAGIC, 8) == 0); + return 0; +} + +void +mem_delete(void *v) +{ + struct mem_block *block = mem_block(v);; + + if (block == block->pool->head) + block->pool->head = block->next; + if (block->next != NULL) + block->next->prev = block->prev; + if (block->prev != NULL) + block->prev->next = block->next; + memset(block, 0, sizeof *block); + free(block); +} + +void +mem_free(struct mem_pool *pool) +{ + struct mem_block *block, *next; + + for (block = pool->head; block != NULL; block = next) { + next = block->next; + memset(block, 0, sizeof *block); + free(block); + } +} diff --git a/src/mem.h b/src/mem.h @@ -0,0 +1,58 @@ +#ifndef MEM_H +#define MEM_H + +/* + * Lightweight wrapper over malloc, that permit to define a memory pool of + * multiple buffers, and free them all at once. + * + * *──────────┐ + * │ mem_pool │ + * ├──────────┤ + * │*head │ + * └┬─────────┘ + * v + * NULL< *───────────┐< >*───────────┐< >*───────────┐< >*───────────┐ >NULL + * \ │ mem_block │ \/ │ mem_block │ \/ │ mem_block │ \/ │ mem_block │ / + * \ ├───────────┤ /\ ├───────────┤ /\ ├───────────┤ /\ ├───────────┤ / + * `┤*prev *next├' `┤*prev *next├' `┤*prev *next├' `┤*prev *next├' + * │len │ │len │ │len │ │len │ + * ├─┴─magic───┤ ├─┴─magic───┤ ├─┴─magic───┤ ├─┴─magic───┤ + * │///////////│ │///////////│ │///////////│ │///////////│ + * │///////////│ │///////////│ │///////////│ │///////////│ + * │///////////│ │///////////│ │///////////│ └───────────┘ + * └───────────┘ │///////////│ │///////////│ + * │///////////│ └───────────┘ + * └───────────┘ + * + * This permits the type checker to still work on all operations while + * providing generic memory management functions for all types of data + * structures and keep track of each object's length. + */ + +#include <stddef.h> + +#define MEM_BLOCK_MAGIC "\xcc\x68\x23\xd7\x9b\x7d\x39\xb9" + +struct mem_pool { + struct mem_block *head; +}; + +struct mem_block { + struct mem_pool *pool; + struct mem_block *prev, *next; + size_t len; + char magic[8]; /* at the end to detect buffer underflow */ + char buf[]; +}; + +/** src/mem.c **/ +void * mem_alloc(struct mem_pool *pool, size_t len); +int mem_resize(void **pp, size_t len); +int mem_grow(void **pp, size_t len); +int mem_shrink(void **pp, size_t len); +size_t mem_length(void *v); +int mem_append(void **pp, char const *buf, size_t len); +void mem_delete(void *v); +void mem_free(struct mem_pool *pool); + +#endif diff --git a/src/str.c b/src/str.c @@ -0,0 +1,119 @@ +#include "str.h" + +#include <assert.h> +#include <errno.h> +#include <stdarg.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "mem.h" + +/* + * It is heavy on assert as an inexpensive way to detect memory corruption from + * code around: underflows are checked through the magic header, overflow are + * checked through the '\0' byte. + */ + +size_t +str_length(struct str *str) +{ + return mem_length(str->mem) - sizeof ""; +} + +int +str_init(struct str *str, struct mem_pool *pool) +{ + str->mem = mem_alloc(pool, 1); + if (str->mem == NULL) + return -1; + + assert(str->mem[0] == '\0'); + assert(str_length(str) == 0); + assert(mem_length(str->mem) == 1); + return 0; +} + +void +str_truncate(struct str *str, size_t len) +{ + assert(str->mem[str_length(str)] == '\0'); + + if (mem_length(str->mem) > len) + str->mem[len] = '\0'; + mem_resize((void **)&str->mem, len + 1); + + assert(str->mem[str_length(str)] == '\0'); +} + +int +str_append_mem(struct str *str, char const *buf, size_t len) +{ + void **pp = (void **)&str->mem; + size_t old_sz; + + assert((old_sz = mem_length(str->mem)) > 0); + assert(memchr(buf, '\0', len) == NULL); + assert(str->mem[str_length(str)] == '\0'); + + if (mem_shrink(pp, 1)) /* strip '\0' */ + return -1; + if (mem_append(pp, buf, len) < 0) + return -1; + if (mem_append(pp, "", 1) < 0) + return -1; + + assert(memcmp(str->mem + old_sz-1, buf, len) == 0); + assert(str->mem[old_sz-1 + len] == '\0'); + assert(str->mem[str_length(str)] == '\0'); + return 0; +} + +int +str_append_string(struct str *str, char const *s) +{ + assert(str->mem[str_length(str)] == '\0'); + + return str_append_mem(str, s, strlen(s)); +} + +int +str_append_char(struct str *str, char c) +{ + assert(str->mem[str_length(str)] == '\0'); + + str_c(str)[str_length(str)] = c; + if (mem_append((void **)str->mem, "", 1) < 0) + return -1; + + assert(str->mem[str_length(str)] == '\0'); + return 0; +} + +int +str_append_fmt(struct str *str, char const *fmt, ...) +{ + va_list va; + int n; + + assert(str->mem[str_length(str)] == '\0'); + + va_start(va, fmt); + n = vsnprintf(NULL, 0, fmt, va); + assert(n > 0); + if (mem_grow((void **)&str->mem, n + 1) < 0) + return -1; + vsprintf(str->mem, fmt, va); + va_end(va); + + assert(str->mem[str_length(str)] == '\0'); + return 0; +} + +char * +str_c(struct str *str) +{ + assert(str->mem[str_length(str)] == '\0'); + return str->mem; +} diff --git a/src/str.h b/src/str.h @@ -0,0 +1,26 @@ +#ifndef STR_H +#define STR_H + +#include <stddef.h> + +#include "mem.h" + +/* + * Length kept by struct mem_block. + */ + +struct str { + char *mem; +}; + +/** src/str.c **/ +size_t str_length(struct str *str); +int str_init(struct str *str, struct mem_pool *pool); +void str_truncate(struct str *str, size_t len); +int str_append_mem(struct str *str, char const *buf, size_t len); +int str_append_string(struct str *str, char const *s); +int str_append_char(struct str *str, char c); +int str_append_fmt(struct str *str, char const *fmt, ...); +char * str_c(struct str *str); + +#endif diff --git a/src/utf8.c b/src/utf8.c @@ -0,0 +1,214 @@ +/* + * ASCII all have a leading '0' byte: + * + * 0xxxxxxx + * + * UTF-8 have one leading '1' and as many following '1' as there are + * continuation bytes (with leading '1' and '0'). + * + * 0xxxxxxx + * 110xxxxx 10xxxxxx + * 1110xxxx 10xxxxxx 10xxxxxx + * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx + * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx + * + * There is up to 3 continuation bytes -- up to 4 bytes per runes. + */ + +#include <ctype.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + +#include "utf8.h" + +static int +utflen(char const *str) +{ + int i, len; + unsigned char const *s; + + s = (unsigned char const *)str; + len = (*s < 0x80) ? 1 : /* 0xxxxxxx < *s < 10000000 */ + (*s < 0xc0) ? 0 : /* 10xxxxxx < *s < 11000000 */ + (*s < 0xe0) ? 2 : /* 110xxxxx < *s < 11100000 */ + (*s < 0xf0) ? 3 : /* 1110xxxx < *s < 11110000 */ + (*s < 0xf8) ? 4 : /* 11110xxx < *s < 11111000 */ + (*s < 0xfc) ? 5 : /* 111110xx < *s < 11111100 */ + (*s < 0xfe) ? 6 : /* 1111110x < *s < 11111110 */ + (*s < 0xff) ? 7 : /* 11111110 < *s < 11111111 */ + 0; + + /* check continuation bytes and '\0' */ + for (s++, i = 1; i < len; i++, s++) { + if ((*s & 0xc0) != 0x80) /* 10xxxxxx & 11000000 */ + return 0; + } + + return len; +} + +static long +torune(char const **str, size_t len) +{ + long rune; + int n; + char mask[] = { 0x00, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 }; + + if (len == 0 || len > 6) + return 0; + + /* first byte */ + rune = *(*str)++ & mask[len]; + + /* continuation bytes */ + for (n = len - 1; n > 0; n--, (*str)++) + rune = (rune << 6) | (**str & 0x3f); /* 10xxxxxx */ + + return rune; +} + +/* + * Return the number of bytes required to encode <rune> into UTF-8, or + * 0 if rune is too long. + */ +int +utf8_runelen(long rune) +{ + return (rune <= 0x0000007f) ? 1 : + (rune <= 0x000007ff) ? 2 : + (rune <= 0x0000ffff) ? 3 : + (rune <= 0x001fffff) ? 4 : + (rune <= 0x03ffffff) ? 5 : + (rune <= 0x7fffffff) ? 6 : + 0; +} + +/* + * Return the number of bytes in rune for the next char in <s>, or 0 if + * is misencoded or if it is '\0'. + */ +int +utf8_utflen(char const *str) +{ + long rune; + int len = utflen(str); + + rune = torune(&str, len); + if (len != utf8_runelen(rune)) + return 0; + + return len; +} + +/* + * Return a rune corresponding to the firsts bytes of <str> or -1 if + * the rune is invalid, and set <str> to the beginning of the next rune. + */ +long +utf8_torune(char const **str) +{ + long rune; + int len; + + len = utflen(*str); + rune = torune(str, len); + if (len != utf8_runelen(rune)) + return -1; + + return rune; +} + +/* + * Encode the rune <rune> in UTF-8 in <str>, null-terminated, then return the + * number of bytes written, 0 if <rune> is invalid. + * + * Thanks to Connor Lane Smith for the idea of combining switches and + * binary masks. + */ +int +utf8_tostr(char *str, long rune) +{ + switch (utf8_runelen(rune)) { + case 1: + str[0] = rune; /* 0xxxxxxx */ + str[1] = '\0'; + return 1; + case 2: + str[0] = 0xc0 | (0x1f & (rune >> 6)); /* 110xxxxx */ + str[1] = 0x80 | (0x3f & (rune)); /* 10xxxxxx */ + str[2] = '\0'; + return 2; + case 3: + str[0] = 0xe0 | (0x0f & (rune >> 12)); /* 1110xxxx */ + str[1] = 0x80 | (0x3f & (rune >> 6)); /* 10xxxxxx */ + str[2] = 0x80 | (0x3f & (rune)); /* 10xxxxxx */ + str[3] = '\0'; + return 3; + case 4: + str[0] = 0xf0 | (0x07 & (rune >> 18)); /* 11110xxx */ + str[1] = 0x80 | (0x3f & (rune >> 12)); /* 10xxxxxx */ + str[2] = 0x80 | (0x3f & (rune >> 6)); /* 10xxxxxx */ + str[3] = 0x80 | (0x3f & (rune)); /* 10xxxxxx */ + str[4] = '\0'; + return 4; + case 5: + str[0] = 0xf8 | (0x03 & (rune >> 24)); /* 111110xx */ + str[1] = 0x80 | (0x3f & (rune >> 18)); /* 10xxxxxx */ + str[2] = 0x80 | (0x3f & (rune >> 12)); /* 10xxxxxx */ + str[3] = 0x80 | (0x3f & (rune >> 6)); /* 10xxxxxx */ + str[4] = 0x80 | (0x3f & (rune)); /* 10xxxxxx */ + str[5] = '\0'; + return 5; + case 6: + str[0] = 0xfc | (0x01 & (rune >> 30)); /* 1111110x */ + str[1] = 0x80 | (0x3f & (rune >> 24)); /* 10xxxxxx */ + str[2] = 0x80 | (0x3f & (rune >> 18)); /* 10xxxxxx */ + str[3] = 0x80 | (0x3f & (rune >> 12)); /* 10xxxxxx */ + str[4] = 0x80 | (0x3f & (rune >> 6)); /* 10xxxxxx */ + str[5] = 0x80 | (0x3f & (rune)); /* 10xxxxxx */ + str[6] = '\0'; + return 6; + default: + str[0] = '\0'; + return 0; + } +} + +/* + * Return 1 if the rune is a printable character, and 0 otherwise. + */ +int +utf8_isprint(long rune) +{ + return (0x1f < rune && rune != 0x7f && rune < 0x80) || 0x9f < rune; +} + +/* + * Return a pointer to the next rune in <str> or next byte if the rune + * is invalid. + */ +char const * +utf8_nextrune(char const *str) +{ + int len; + + len = utf8_utflen(str); + return (len == 0) ? str + 1 : str + len; +} + +/* + * Return a pointer to the prev rune in <str> or prev byte if the rune + * is invalid. + */ +char const * +utf8_prevrune(char const *str) +{ + char const *s; + + for (s = str; (*s & 0x80) != 0; s--) + ; + return (utf8_utflen(s) != 0) ? s : str - 1; +} diff --git a/src/utf8.h b/src/utf8.h @@ -0,0 +1,13 @@ +#ifndef UTF8_H +#define UTF8_H + +/** src/utf8.c **/ +int utf8_runelen(long rune); +int utf8_utflen(char const *str); +long utf8_torune(char const **str); +int utf8_tostr(char *str, long rune); +int utf8_isprint(long rune); +char const * utf8_nextrune(char const *str); +char const * utf8_prevrune(char const *str); + +#endif diff --git a/src/util.h b/src/util.h @@ -0,0 +1,12 @@ +#ifndef UTIL_H +#define UTIL_H + +#define ESC 0x1b /* Esc key */ +#define CTL(c) ((c) & ~0x40) /* Ctr + (c) key */ +#define ALT(c) ((c) + 0x80) /* Alt + (c) key */ +#define CSI(c) ((c) + 0x80 + 0x80) /* Escape + '[' + (c) code */ +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define LEN(x) (sizeof(x) / sizeof(*(x))) + +#endif diff --git a/strcasestr.c b/strcasestr.c @@ -1,25 +0,0 @@ -#include <ctype.h> -#include <stddef.h> - -#include "util.h" - -char * -strcasesstr(const char *str1, const char *str2) -{ - const char *s1; - const char *s2; - - for (;;) { - s1 = str1; - s2 = str2; - while (*s1 != '\0' && tolower(*s1) == tolower(*s2)) - s1++, s2++; - if (*s2 == '\0') - return (char *) str1; - if (*s1 == '\0') - return NULL; - str1++; - } - - return NULL; -} diff --git a/strsep.c b/strsep.c @@ -1,21 +0,0 @@ -#include <string.h> - -char * -strsep(char **strp, const char *delim) -{ - char *s, *oldp; - - if (*strp == NULL) - return NULL; - for (s = oldp = *strp; ; s++) { - if (*s == '\0') { - *strp = NULL; - return oldp; - } else if (strchr(delim, *s) != NULL) { - break; - } - } - *s = '\0'; - *strp = s + 1; - return oldp; -} diff --git a/test.c b/t/test.c diff --git a/utf8.c b/utf8.c @@ -1,424 +0,0 @@ -/* -** ASCII all have a leading '0' byte: -** -** 0xxxxxxx -** -** UTF-8 have one leading '1' and as many following '1' as there are -** continuation bytes (with leading '1' and '0'). -** -** 0xxxxxxx -** 110xxxxx 10xxxxxx -** 1110xxxx 10xxxxxx 10xxxxxx -** 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx -** 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx -** 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx -** -** There is up to 3 continuation bytes -- up to 4 bytes per runes. -*/ - -#include <ctype.h> -#include <stddef.h> -#include <stdlib.h> -#include <string.h> -#include <stdio.h> - -#include "utf8.h" - -static int -utflen(char const *str) -{ - int i, len; - unsigned char const *s; - - s = (unsigned char const *)str; - len = (*s < 0x80) ? 1 : /* 0xxxxxxx < *s < 10000000 */ - (*s < 0xc0) ? 0 : /* 10xxxxxx < *s < 11000000 */ - (*s < 0xe0) ? 2 : /* 110xxxxx < *s < 11100000 */ - (*s < 0xf0) ? 3 : /* 1110xxxx < *s < 11110000 */ - (*s < 0xf8) ? 4 : /* 11110xxx < *s < 11111000 */ - (*s < 0xfc) ? 5 : /* 111110xx < *s < 11111100 */ - (*s < 0xfe) ? 6 : /* 1111110x < *s < 11111110 */ - (*s < 0xff) ? 7 : /* 11111110 < *s < 11111111 */ - 0; - - /* check continuation bytes and '\0' */ - for (s++, i = 1; i < len; i++, s++) { - if ((*s & 0xc0) != 0x80) /* 10xxxxxx & 11000000 */ - return 0; - } - - return len; -} - -static long -torune(char const **str, size_t len) -{ - long rune; - int n; - char mask[] = { 0x00, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 }; - - if (len == 0 || len > 6) - return 0; - - /* first byte */ - rune = *(*str)++ & mask[len]; - - /* continuation bytes */ - for (n = len - 1; n > 0; n--, (*str)++) - rune = (rune << 6) | (**str & 0x3f); /* 10xxxxxx */ - - return rune; -} - -/* -** Return the number of bytes required to encode <rune> into UTF-8, or -** 0 if rune is too long. -*/ -int -utf8_runelen(long rune) -{ - return (rune <= 0x0000007f) ? 1 : - (rune <= 0x000007ff) ? 2 : - (rune <= 0x0000ffff) ? 3 : - (rune <= 0x001fffff) ? 4 : - (rune <= 0x03ffffff) ? 5 : - (rune <= 0x7fffffff) ? 6 : - 0; -} - -/* -** Return the number of bytes in rune for the next char in <s>, or 0 if -** is misencoded or if it is '\0'. -*/ -int -utf8_utflen(char const *str) -{ - long rune; - int len; - - len = utflen(str); - rune = torune(&str, len); - if (len != utf8_runelen(rune)) - return 0; - - return len; -} - -/* -** Return a rune corresponding to the firsts bytes of <str> or -1 if -** the rune is invalid, and set <str> to the beginning of the next rune. -*/ -long -utf8_torune(char const **str) -{ - long rune; - int len; - - len = utflen(*str); - rune = torune(str, len); - if (len != utf8_runelen(rune)) - return -1; - - return rune; -} - -/* -** Encode the rune <rune> in UTF-8 in <str>, null-terminated, then return the -** number of bytes written, 0 if <rune> is invalid. -** -** Thanks to Connor Lane Smith for the idea of combining switches and -** binary masks. -*/ -int -utf8_tostr(char *str, long rune) -{ - switch (utf8_runelen(rune)) { - case 1: - str[0] = rune; /* 0xxxxxxx */ - str[1] = '\0'; - return 1; - case 2: - str[0] = 0xc0 | (0x1f & (rune >> 6)); /* 110xxxxx */ - str[1] = 0x80 | (0x3f & (rune)); /* 10xxxxxx */ - str[2] = '\0'; - return 2; - case 3: - str[0] = 0xe0 | (0x0f & (rune >> 12)); /* 1110xxxx */ - str[1] = 0x80 | (0x3f & (rune >> 6)); /* 10xxxxxx */ - str[2] = 0x80 | (0x3f & (rune)); /* 10xxxxxx */ - str[3] = '\0'; - return 3; - case 4: - str[0] = 0xf0 | (0x07 & (rune >> 18)); /* 11110xxx */ - str[1] = 0x80 | (0x3f & (rune >> 12)); /* 10xxxxxx */ - str[2] = 0x80 | (0x3f & (rune >> 6)); /* 10xxxxxx */ - str[3] = 0x80 | (0x3f & (rune)); /* 10xxxxxx */ - str[4] = '\0'; - return 4; - case 5: - str[0] = 0xf8 | (0x03 & (rune >> 24)); /* 111110xx */ - str[1] = 0x80 | (0x3f & (rune >> 18)); /* 10xxxxxx */ - str[2] = 0x80 | (0x3f & (rune >> 12)); /* 10xxxxxx */ - str[3] = 0x80 | (0x3f & (rune >> 6)); /* 10xxxxxx */ - str[4] = 0x80 | (0x3f & (rune)); /* 10xxxxxx */ - str[5] = '\0'; - return 5; - case 6: - str[0] = 0xfc | (0x01 & (rune >> 30)); /* 1111110x */ - str[1] = 0x80 | (0x3f & (rune >> 24)); /* 10xxxxxx */ - str[2] = 0x80 | (0x3f & (rune >> 18)); /* 10xxxxxx */ - str[3] = 0x80 | (0x3f & (rune >> 12)); /* 10xxxxxx */ - str[4] = 0x80 | (0x3f & (rune >> 6)); /* 10xxxxxx */ - str[5] = 0x80 | (0x3f & (rune)); /* 10xxxxxx */ - str[6] = '\0'; - return 6; - default: - str[0] = '\0'; - return 0; - } -} - -/* -** Return 1 if the rune is a printable character, and 0 otherwise. -*/ -int -utf8_isprint(long rune) -{ - return (0x1f < rune && rune != 0x7f && rune < 0x80) || 0x9f < rune; -} - -/* -** Markus Kuhn -- 2007-05-26 (Unicode 5.0) -** -** Permission to use, copy, modify, and distribute this software -** for any purpose and without fee is hereby granted. The author -** disclaims all warranties with regard to this software. -** -** Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c -*/ - -struct interval { - int first; - int last; -}; - -/* -** Auxiliary function for binary search in interval table. -*/ -static int -bisearch(long ucs, struct interval const *table, int max) -{ - int min = 0; - int mid; - - if (ucs < table[0].first || ucs > table[max].last) - return 0; - while (max >= min) { - mid = (min + max) / 2; - if (ucs > table[mid].last) - min = mid + 1; - else if (ucs < table[mid].first) - max = mid - 1; - else - return 1; - } - - return 0; -} - -/* The following two functions define the column width of an ISO 10646 -** character as follows: -** -** - The null character (U+0000) has a column width of 0. -** -** - Other C0/C1 control characters and DEL will lead to a return -** value of -1. -** -** - Non-spacing and enclosing combining characters (general -** category code Mn or Me in the Unicode database) have a -** column width of 0. -** -** - SOFT HYPHEN (U+00AD) has a column width of 1. -** -** - Other format characters (general category code Cf in the Unicode -** database) and ZERO WIDTH SPACE (U+200B) have a column width of 0. -** -** - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF) -** have a column width of 0. -** -** - Spacing characters in the East Asian Wide (W) or East Asian -** Full-width (F) category as defined in Unicode Technical -** Report #11 have a column width of 2. -** -** - All remaining characters (including all printable -** ISO 8859-1 and WGL4 characters, Unicode control characters, -** etc.) have a column width of 1. -** -** This implementation was assuming that long characters are encoded -** in ISO 10646. -*/ -int -utf8_wcwidth(long ucs) -{ - /* sorted list of non-overlapping intervals of non-spacing characters */ - /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */ - static struct interval const combining[] = { - { 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 }, - { 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 }, - { 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 }, - { 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 }, - { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED }, - { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A }, - { 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 }, - { 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D }, - { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 }, - { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, - { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C }, - { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, - { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, - { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, - { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C }, - { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D }, - { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 }, - { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 }, - { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC }, - { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD }, - { 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, - { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, - { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, - { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, - { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, - { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, - { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, - { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, - { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 }, - { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F }, - { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 }, - { 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD }, - { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD }, - { 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 }, - { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B }, - { 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 }, - { 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 }, - { 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF }, - { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 }, - { 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F }, - { 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B }, - { 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F }, - { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB }, - { 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F }, - { 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 }, - { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD }, - { 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F }, - { 0xE0100, 0xE01EF } - }; - - /* test for 8-bit control characters */ - if (ucs == 0) - return 0; - if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) - return -1; - - /* binary search in table of non-spacing characters */ - if (bisearch(ucs, combining, sizeof(combining) / - sizeof(struct interval) - 1)) - return 0; - - /* if we arrive here, ucs is not a combining or C0/C1 control character */ - - return 1 + (ucs >= 0x1100 && - (ucs <= 0x115f || /* Hangul Jamo init. consonants */ - ucs == 0x2329 || ucs == 0x232a || - (ucs >= 0x2e80 && ucs <= 0xa4cf && - ucs != 0x303f) || /* CJK ... Yi */ - (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */ - (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */ - (ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */ - (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */ - (ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */ - (ucs >= 0xffe0 && ucs <= 0xffe6) || - (ucs >= 0x20000 && ucs <= 0x2fffd) || - (ucs >= 0x30000 && ucs <= 0x3fffd))); -} - -/* -** Return the width of <rune> with tabs as displayed from position <off>. -*/ -int -utf8_runewidth(long rune, size_t off) -{ - return (rune == '\t') ? (int)(8 - (off % 8)) : utf8_wcwidth(rune); -} - -/* -** Return a index of the first byte of a character of <s> that would be rendered -** at the <col>-th column in a terminal, or NULL if the whole string fit. In -** order to format tabs properly, the string must start with an offset of <off> -** columns. -*/ -int -utf8_col(char const *str, size_t col, size_t off) -{ - long rune; - char const *pos, *s; - - for (s = str; off <= col;) { - pos = s; - if (*s == '\0') - break; - - rune = utf8_torune(&s); - off += utf8_runewidth(rune, off); - } - - return pos - str; -} - -/* -** Print <rune> to <fp> if it is less wide than <maxwidth> and return -** the number of columns of the rune printed or a negative value if -** nothing was printed: -1 if the rune is invalid, and -2 if no width -** is left. If the rune is tab, the width is calculated usin <off> -** as column offset. -*/ -int -utf8_putrune(long rune, int maxwidth, int off, FILE *fp) -{ - int width; - char str[8]; - - if ((width = utf8_runewidth(rune, off)) > maxwidth) - return -2; - if (utf8_tostr(str, rune) == 0) - return -1; - fputs(str, fp); - - return width; -} - -/* -** Return a pointer to the next rune in <str> or next byte if the rune -** is invalid. -*/ -char const * -utf8_nextrune(char const *str) -{ - int len; - - len = utf8_utflen(str); - return (len == 0) ? str + 1 : str + len; -} - -/* -** Return a pointer to the prev rune in <str> or prev byte if the rune -** is invalid. -*/ -char const * -utf8_prevrune(char const *str) -{ - char const *s; - - for (s = str; (*s & 0x80) != 0; s--) - ; - return (utf8_utflen(s) != 0) ? s : str - 1; -} diff --git a/utf8.h b/utf8.h @@ -1,11 +0,0 @@ -int utf8_runelen (long); -int utf8_utflen (char const *); -long utf8_torune (char const **); -int utf8_tostr (char *, long); -int utf8_isprint (long); -int utf8_wcwidth (long); -int utf8_runewidth (long, size_t); -int utf8_col (char const *, size_t, size_t); -int utf8_putrune (long, int, int, FILE *); -char const *utf8_nextrune (char const *); -char const *utf8_prevrune (char const *); diff --git a/util.h b/util.h @@ -1,31 +0,0 @@ -/* -** EPITECH PROJECT, 2017 -** util -** File description: -** util -*/ - -#ifndef UTIL_H -#define UTIL_H - -#include <stdarg.h> - -#define ESC 0x1b /* Esc key */ -#define CTL(c) ((c) & ~0x40) /* Ctr + (c) key */ -#define ALT(c) ((c) + 0x80) /* Alt + (c) key */ -#define CSI(c) ((c) + 0x80 + 0x80) /* Escape + '[' + (c) code */ -#define MIN(x, y) (((x) < (y)) ? (x) : (y)) -#define MAX(x, y) (((x) > (y)) ? (x) : (y)) -#define LEN(x) (sizeof(x) / sizeof(*(x))) - -/* string */ -char *strcasestr(const char *, const char *); -size_t strlcpy(char *, const char *, size_t); -char *strsep(char **, const char *); - -/* error */ -void err(int, const char *, ...); -void vwarn(const char *, va_list); -void warn(const char *, ...); - -#endif