improve different libc compatibility thanks to _anthk feedback - iomenu - interactive terminal-based selection menu
 (HTM) git clone git://bitreich.org/iomenu git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/iomenu
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 0332f4dd490ea3fe6421a8f064142c3747938653
 (DIR) parent 61d1c7b96e038a9af18330f8a2efa1b648441575
 (HTM) Author: Josuah Demangeon <me@josuah.net>
       Date:   Fri, 29 Oct 2021 19:45:58 +0200
       
       improve different libc compatibility thanks to _anthk feedback
       
       Diffstat:
         M Makefile                            |      13 +++++++------
         M iomenu.c                            |      20 ++++++++++----------
         M term.c                              |      17 +++++++++--------
         M term.h                              |       2 +-
       
       4 files changed, 27 insertions(+), 25 deletions(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       @@ -1,7 +1,8 @@
        NAME = iomenu
       -VERSION = 0.1
       +V = 0.1
        
       -CFLAGS = -DVERSION='"${VERSION}"' -I./src  -Wall -Wextra -std=c99 --pedantic -g
       +D = -DVERSION='"$V"' -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE
       +CFLAGS = $D -Wall -Wextra -std=c99 -pedantic -g
        LDFLAGS = -static
        PREFIX = /usr/local
        MANPREFIX = ${PREFIX}/man
       @@ -22,7 +23,7 @@ ${BIN}: ${OBJ} ${BIN:=.o}
                ${CC} ${LDFLAGS} -o $@ $@.o ${OBJ} ${LIB}
        
        clean:
       -        rm -rf *.o ${BIN} ${NAME}-${VERSION} *.gz
       +        rm -rf *.o ${BIN} ${NAME}-$V *.gz
        
        install:
                mkdir -p ${DESTDIR}${PREFIX}/bin
       @@ -31,6 +32,6 @@ install:
                cp -rf ${MAN1} ${DESTDIR}${MANPREFIX}/man1
        
        dist: clean
       -        mkdir -p ${NAME}-${VERSION}
       -        cp -r README Makefile bin ${MAN1} ${SRC} ${NAME}-${VERSION}
       -        tar -cf - ${NAME}-${VERSION} | gzip -c >${NAME}-${VERSION}.tar.gz
       +        mkdir -p ${NAME}-$V
       +        cp -r README Makefile bin ${MAN1} ${SRC} ${NAME}-$V
       +        tar -cf - ${NAME}-$V | gzip -c >${NAME}-$V.tar.gz
 (DIR) diff --git a/iomenu.c b/iomenu.c
       @@ -16,7 +16,7 @@
        #include "utf8.h"
        
        struct {
       -        char input[LINE_MAX];
       +        char input[256];
                size_t cur;
        
                char **lines_buf;
       @@ -26,7 +26,7 @@ struct {
                size_t match_count;
        } ctx;
        
       -int opt_comment;
       +int flag_comment;
        
        /*
         * Keep the line if it match every token (in no particular order,
       @@ -35,7 +35,7 @@ int opt_comment;
        static int
        match_line(char *line, char **tokv)
        {
       -        if (opt_comment && line[0] == '#')
       +        if (flag_comment && line[0] == '#')
                        return 2;
                for (; *tokv != NULL; tokv++)
                        if (strcasestr(line, *tokv) == NULL)
       @@ -86,7 +86,7 @@ do_move(int sign)
        {
                /* integer overflow will do what we need */
                for (size_t i = ctx.cur + sign; i < ctx.match_count; i += sign) {
       -                if (opt_comment == 0 || ctx.match_buf[i][0] != '#') {
       +                if (flag_comment == 0 || ctx.match_buf[i][0] != '#') {
                                ctx.cur = i;
                                break;
                        }
       @@ -114,7 +114,7 @@ do_filter(char **search_buf, size_t search_count)
                for (size_t n = 0; n < search_count; n++)
                        if (match_line(search_buf[n], tokv))
                                ctx.match_buf[ctx.match_count++] = search_buf[n];
       -        if (opt_comment && ctx.match_buf[ctx.cur][0] == '#')
       +        if (flag_comment && ctx.match_buf[ctx.cur][0] == '#')
                        do_move(+1);
        }
        
       @@ -136,7 +136,7 @@ do_move_header(signed int sign)
        {
                do_move(sign);
        
       -        if (opt_comment == 0)
       +        if (flag_comment == 0)
                        return;
                for (ctx.cur += sign;; ctx.cur += sign) {
                        char *cur = ctx.match_buf[ctx.cur];
       @@ -184,7 +184,7 @@ do_add_char(char c)
        static void
        do_print_selection(void)
        {
       -        if (opt_comment) {
       +        if (flag_comment) {
                        char **match = ctx.match_buf + ctx.cur;
        
                        while (--match >= ctx.match_buf) {
       @@ -197,7 +197,7 @@ do_print_selection(void)
                }
                term_raw_off(2);
                if (ctx.match_count == 0
       -          || (opt_comment && ctx.match_buf[ctx.cur][0] == '#'))
       +          || (flag_comment && ctx.match_buf[ctx.cur][0] == '#'))
                        fprintf(stdout, "%s\n", ctx.input);
                else
                        fprintf(stdout, "%s\n", ctx.match_buf[ctx.cur]);
       @@ -277,7 +277,7 @@ key_action(void)
        static void
        print_line(char *line, int highlight)
        {
       -        if (opt_comment && line[0] == '#') {
       +        if (flag_comment && line[0] == '#') {
                        fprintf(stderr, "\n\x1b[1m\r%.*s\x1b[m",
                          term_at_width(line + 1, term.winsize.ws_col, 0), line + 1);
                } else if (highlight) {
       @@ -390,7 +390,7 @@ main(int argc, char *argv[])
                                fprintf(stdout, "%s\n", VERSION);
                                exit(0);
                        case '#':
       -                        opt_comment = 1;
       +                        flag_comment = 1;
                                break;
                        default:
                                usage(arg0);
 (DIR) diff --git a/term.c b/term.c
       @@ -36,19 +36,20 @@ term_at_width(char const *s, int width, int pos)
        int
        term_raw_on(int fd)
        {
       -        struct termios new_termios = {0};
       -        char *seq = "\x1b[s\x1b[?1049h\x1b[H";
       +        static char *seq = "\x1b[s\x1b[?1049h\x1b[H";
       +        struct termios termios;
                ssize_t len = strlen(seq);
        
                if (write(fd, seq, len) < len)
                        return -1;
        
       -        if (tcgetattr(fd, &term.old_termios) < 0)
       +        memset(&termios, 0, sizeof termios);
       +        if (tcgetattr(fd, &term.termios) < 0)
                        return -1;
       -        memcpy(&new_termios, &term.old_termios, sizeof new_termios);
       +        memcpy(&termios, &term.termios, sizeof termios);
        
       -        new_termios.c_lflag &= ~(ICANON | ECHO | IEXTEN | IGNBRK | ISIG);
       -        if (tcsetattr(fd, TCSANOW, &new_termios) == -1)
       +        termios.c_lflag &= ~(ICANON | ECHO | IEXTEN | IGNBRK | ISIG);
       +        if (tcsetattr(fd, TCSANOW, &termios) == -1)
                        return -1;
                return 0;
        }
       @@ -56,10 +57,10 @@ term_raw_on(int fd)
        int
        term_raw_off(int fd)
        {
       -        char *seq = "\x1b[2J\x1b[u\033[?1049l";
       +        static char *seq = "\x1b[2J\x1b[u\033[?1049l";
                ssize_t len = strlen(seq);
        
       -        if (tcsetattr(fd, TCSANOW, &term.old_termios) < 0)
       +        if (tcsetattr(fd, TCSANOW, &term.termios) < 0)
                        return -1;
                if (write(fd, seq, len) < len)
                        return -1;
 (DIR) diff --git a/term.h b/term.h
       @@ -25,7 +25,7 @@ enum term_key {
        
        struct term {
                struct winsize winsize;
       -        struct termios old_termios;
       +        struct termios termios;
        };
        
        extern struct term term;