eel

formatter for ii(1) logs
Log | Files | Refs | README

commit c69a91b357c89c1656524ef829124153fc1ff942
parent dc4645b5fbfed87f8a7b7fa8f6925c81aeba0737
Author: Josuah Demangeon <mail@josuah.net>
Date:   Wed,  6 Jun 2018 00:09:44 +0200

saner behavior on unexpected output

Diffstat:
M.gitignore | 2++
MMakefile | 6++++--
Aarg.h | 27+++++++++++++++++++++++++++
Meel.c | 50++++++++++++++++++++++++++------------------------
4 files changed, 59 insertions(+), 26 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1,3 @@ eel +*.o +*.core diff --git a/Makefile b/Makefile @@ -1,7 +1,9 @@ CFLAGS = -g -Wpedantic -Wall -Wextra -std=c89 -D_POSIX_C_SOURCE=200809L +SRC = eel.c +OBJ = $(SRC:.c=.o) -eel: eel.c - ${CC} ${CFLAGS} eel.c -o eel +eel: $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o eel install: eel mkdir -p $(PREFIX)/bin diff --git a/arg.h b/arg.h @@ -0,0 +1,27 @@ +#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/eel.c b/eel.c @@ -10,37 +10,35 @@ #define COLS_MAX 80 char * -print_row(char *time, char *nick, char *message) +print_row(char *time, char *nick, char *msg) { - char *blank, *m; - int offset; + char *blank, *m; + int offset; blank = NULL; offset = printf("%*s %*s ", TIME_COL, time, NICK_COL, nick); - for (m = message; offset + (m - message) < COLS_MAX; m++) { + for (m = msg; offset + (m - msg) < COLS_MAX; m++) { if (*m == ' ') blank = m; if (*m == '\0') goto tail; } if (blank == NULL) { - for (; *m != ' '; m++) { + for (; *m != ' '; m++) if (*m == '\0') goto tail; - } blank = m; } *blank = '\0'; - puts(message); + puts(msg); return blank + 1; tail: - fputs(message, stdout); - fflush(stdout); + fputs(msg, stdout); return NULL; } void -print_message(time_t time, char *nick, int same, char *message) +print_msg(time_t time, char *nick, int same, char *msg) { char time_str[TIME_COL]; @@ -50,26 +48,28 @@ print_message(time_t time, char *nick, int same, char *message) putchar('\n'); strftime(time_str, TIME_COL, "%H:%M", localtime(&time)); - while ((message = print_row(time_str, nick, message))) { + while ((msg = print_row(time_str, nick, msg))) { time_str[0] = '\0'; nick = ""; } } int -parse_message(char *str, time_t *time, char **nick, char **message) +parse_msg(char *str, time_t *time, char **nick, char **msg) { - for (*time = atoi(str); *str != ' '; str++) - if (!*str) + if ((*time = atoi(str)) == 0) + return -1; + for (; *str != ' '; str++) + if (*str == '\0') return -1; *str++ = '\0'; for (*nick = str; *str != ' '; str++) - if (!*str) + if (*str == '\0') return -1; *str = '\0'; - *message = str + 1; + *msg = str + 1; return 0; } @@ -77,19 +77,21 @@ parse_message(char *str, time_t *time, char **nick, char **message) int main(void) { - time_t time; - char *nick, *message, line[RFC1459_LEN + 1], last[NICK_MAX]; + time_t time; + char *nick, *msg, line[RFC1459_LEN + 1], last[NICK_MAX]; time = 0; - nick = message = NULL; + nick = msg = NULL; memset(last, 0, sizeof(last)); while (fgets(line, sizeof(line), stdin) != NULL) { - if (parse_message(line, &time, &nick, &message) < 0) { - fprintf(stderr, "%s\n", line); - continue; + line[strcspn(line, "\n")] = '\0'; + if (parse_msg(line, &time, &nick, &msg) < 0) { + printf("%s\n", line); + } else { + print_msg(time, nick, strcmp(nick, last) == 0, msg); + strncpy(last, nick, sizeof(last) - 1); } - print_message(time, nick, strcmp(nick, last) == 0, message); - strncpy(last, nick, sizeof(last) - 1); + fflush(stdout); } return 0;