switch back to arg.h - ploot - simple plotting tools HTML git clone git://bitreich.org/ploot git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ploot DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit dd70db86b2270b2a539f863664d2af0c5c7f1040 DIR parent 5bf25770fadc2327ef217388561e203217b1870e HTML Author: Josuah Demangeon <mail@josuah.net> Date: Sat, 17 Mar 2018 16:33:30 +0100 switch back to arg.h Diffstat: M Makefile | 8 +++++--- A arg.h | 27 +++++++++++++++++++++++++++ M ploot.c | 54 +++++++++++++++---------------- 3 files changed, 58 insertions(+), 31 deletions(-) --- DIR diff --git a/Makefile b/Makefile @@ -1,15 +1,17 @@ CFLAGS = -Wall -Wextra -Werror -std=c89 -pedantic -D_POSIX_C_SOURCE=200809L +.PHONY: all all:ploot -ploot: ploot.o config.h +ploot.o: config.h arg.h +ploot: ploot.o ${CC} -static -o ploot ploot.o +.PHONY: install install: ploot mkdir -p ${PREFIX}/bin cp ploot ${PREFIX}/bin/ploot +.PHONY: clean clean: rm -f *.o ploot - -.PHONY: all clean DIR diff --git a/arg.h b/arg.h @@ -0,0 +1,27 @@ +#ifndef ARG_H +#define ARG_H + +extern char *argv0; + +#define ARGBEGIN(argc, argv) \ + 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 DIR diff --git a/ploot.c b/ploot.c @@ -6,6 +6,7 @@ #include <unistd.h> #include <time.h> +#include "arg.h" #include "config.h" #define ABS(x) ((x) < 0 ? -(x) : (x)) @@ -13,6 +14,8 @@ #define MAX(x, y) ((x) > (y) ? (x) : (y)) #define LEN(buf) (sizeof(buf) / sizeof(*(buf))) +char *argv0; + /* ** Add `val' at the current position `pos' of the `ring' buffer and set pos to ** the next postion. @@ -36,9 +39,9 @@ do { \ #define MAX_VAL 80 #define MARGIN 7 -int flag_h = 20; -char *flag_t = NULL; -time_t flag_o = 0; +int hflag = 20; +char *tflag = NULL; +time_t oflag = 0; /* ** Set `str' to a human-readable form of `num' with always a width of 7 (+ 1 @@ -121,13 +124,13 @@ haxis(double *beg, double *end, time_t time) for (tp = beg; tp < end; tp++) putchar((*tp < 0) ? ('x') : ('-')); putchar('\n'); - if (flag_o > 0) { + if (oflag > 0) { printf("%*c", MARGIN - 1, ' '); strftime(dbeg, sizeof(dbeg), "%Y/%m/%d", localtime(&time)); for (tp = beg; tp < end; tp += 7) { strftime(buf, sizeof(buf), " %H:%M", localtime(&time)); fputs(buf, stdout); - time += flag_o * 7; + time += oflag * 7; } strftime(dend, sizeof(dend), "%Y/%m/%d", localtime(&time)); printf("\n %-*s %s\n", (int)(beg - end) + 4, dbeg, dend); @@ -271,36 +274,31 @@ main(int argc, char **argv) { time_t tbuf[MAX_VAL], *tend, start; double vbuf[MAX_VAL], *vend; - int c; - - while ((c = getopt(argc, argv, "h:t:o:")) != -1) { - switch (c) { - case -1: - break; - case 'h': - if ((flag_h = atoi(optarg)) <= 0) - usage(); - break; - case 't': - flag_t = optarg; - break; - case 'o': - flag_o = atol(optarg); - break; - default: - usage(); - } - } - if (flag_o == 0) { + ARGBEGIN(argc, argv) { + case 'h': + if ((hflag = atoi(EARGF(usage()))) <= 0) + usage(); + break; + case 't': + tflag = EARGF(usage()); + break; + case 'o': + oflag = atol(EARGF(usage())); + break; + default: + usage(); + } ARGEND + + if (oflag == 0) { vend = read_simple(vbuf); start = 0; } else { tend = read_time_series(vbuf, tbuf); - vend = skip_gaps(tbuf, tend, vbuf, flag_o); + vend = skip_gaps(tbuf, tend, vbuf, oflag); start = *tbuf; } - plot(vbuf, vend, flag_h, flag_t, start); + plot(vbuf, vend, hflag, tflag, start); return 0; }