sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 29ce2560a74273eb7cd97b227249c87256f78710
parent d512d7938ee31fa440f2470e6ed3b4c2f736b212
Author: Christoph Lohmann <20h@r-36.net>
Date:   Mon, 16 Apr 2012 21:46:59 +0200

Fixes and checks for seq. Thanks David Galos!
Diffstat:
MLICENSE | 1+
MMakefile | 1+
Mseq.c | 315++++++++++++++++++++++++++++++++++++++++++-------------------------------------
3 files changed, 169 insertions(+), 148 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -10,6 +10,7 @@ MIT/X Consortium License © 2012 William Haddon <william@haddonthethird.net> © 2012 Kurt H. Maier <khm@intma.in> © 2012 Christoph Lohmann <20h@r-36.net> +© 2012 David Galos <galosd83@students.rowan.edu> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), diff --git a/Makefile b/Makefile @@ -52,6 +52,7 @@ SRC = \ tty.c \ uname.c \ seq.c \ + seq-test.c \ wc.c OBJ = $(SRC:.c=.o) $(LIB) diff --git a/seq.c b/seq.c @@ -5,163 +5,182 @@ #include <unistd.h> #include <stdbool.h> #include <libgen.h> +#include <regex.h> #include "util.h" -char afmt[4096]; +#define MAX(a, b) (((a) > (b))? (a):(b)) -char * -getalignedwidthfmt(char *fmt, char *starts, char *steps, char *ends) +int +validfloat(char *fmt) { - char starttmp[4096], steptmp[4096], endtmp[4096]; - char *startdelim, *stepdelim, *enddelim; - int startl, startr, stepr, endl, endr; - int maxl, maxr; - - /* - * 1.) Get a temp buffer we can work in. - * 2.) Calculate the *r(ight) and *l(eft) size. - */ - memmove(starttmp, starts, strlen(starts)+1); - startdelim = strchr(starttmp, '.'); - if (startdelim != NULL) { - startdelim[0] = '\0'; - startr = strlen(startdelim+1); - } else { - startr = 0; - } - startl = strlen(starttmp); - if (starttmp[0] == '+') - startl--; - - memmove(endtmp, ends, strlen(ends)+1); - enddelim = strchr(endtmp, '.'); - if (enddelim != NULL) { - enddelim[0] = '\0'; - endr = strlen(enddelim+1); - } else { - endr = 0; - } - endl = strlen(endtmp); - if (endtmp[0] == '+') - endl--; - - /* - * We do not care for the left length of the step, because it - * will never be displayed. - */ - memmove(steptmp, steps, strlen(steps)+1); - stepdelim = strchr(steptmp, '.'); - if (stepdelim != NULL) { - stepdelim[0] = '\0'; - stepr = strlen(stepdelim+1); - } else { - stepr = 0; - } - - maxl = (startl > endl)? startl : endl; - maxr = (startl > endr)? startr : endr; - if (stepr > maxr) - maxr = stepr; - - if (maxl <= 1) { - snprintf(afmt, sizeof(afmt), "%%.%df", maxr); - } else if (maxr == 0) { - snprintf(afmt, sizeof(afmt), "%%0%d.f", maxl); - } else { - snprintf(afmt, sizeof(afmt), "%%0%d.%df", maxl+maxr+1, maxr); - } - - return afmt; + char *end; + + fmt += strspn(fmt, " "); + strtod(fmt, &end); + if (fmt == end || end != fmt + strlen(fmt)) + return 0; + + return 1; +} + +int +digitsleft(char *d) +{ + char *exp; + int shift; + + if (d[0] == '-' || d[0] == '+') + d++; + exp = strpbrk(d, "eE"); + shift = exp? atoi(exp+1) : 0; + + return MAX(0, strspn(d, "0123456789")+shift); +} + +int +digitsright(char *d) +{ + char *exp; + int shift, after; + + if (d[0] == '-' || d[0] == '+') + d++; + exp = strpbrk(d, "eE"); + shift = exp ? atoi(exp+1) : 0; + after = (d = strchr(d, '.'))? strspn(d+1, "0123456789") : 0; + + return MAX(0, after-shift); +} + +int +validfmt(char *fmt) +{ + regex_t reg; + int ret; + + regcomp(&reg, "\\([^%]|%%\\)*%[0-9]*\\.[0-9]*[fFgG]" + "\\([^%]|%%\\)*", REG_NOSUB); + ret = regexec(&reg, fmt, 0, NULL, 0); + regfree(&reg); + + return (ret == 0); } int main(int argc, char *argv[]) { - char c, *fmt, *sep, *starts, *steps, *ends; - bool wflag = false; - double start, step, end, out; - - sep = "\n"; - fmt = "%G"; - - starts = "1"; - steps = "1"; - ends = "1"; - - while((c = getopt(argc, argv, "f:s:w")) != -1) { - switch(c) { - case 'f': - fmt = optarg; - break; - case 's': - sep = optarg; - break; - case 'w': - wflag = true; - break; - } - } - - switch(argc-optind) { - case 3: - starts = argv[optind++]; - steps = argv[optind++]; - ends = argv[optind++]; - break; - case 2: - starts = argv[optind++]; - ends = argv[optind++]; - break; - case 1: - ends = argv[optind++]; - break; - default: - eprintf("usage: %s [-w] [-f fmt] [-s separator] [start [step]]" - " end\n", basename(argv[0])); - } - - start = atof(starts); - step = atof(steps); - end = atof(ends); - - if (step == 0) - return EXIT_FAILURE; - - if (start > end) { - if (step > 0) - return EXIT_FAILURE; - } else if (start < end) { - if (step < 0) - return EXIT_FAILURE; - } - - if (wflag) - fmt = getalignedwidthfmt(fmt, starts, steps, ends); - printf("%s\n", fmt); - - for (out = start;;) { - printf(fmt, out); - - out += step; - if (start > end) { - if (out >= end) { - printf("%s", sep); - } else { - break; - } - } else if (start < end) { - if (out <= end) { - printf("%s", sep); - } else { - break; - } - } else { - break; - } - } - printf("\n"); - - return EXIT_SUCCESS; + char c, *fmt, ftmp[4096], *sep, *starts, *steps, *ends; + bool wflag, fflag; + double start, step, end, out; + int left, right; + + sep = "\n"; + fmt = ftmp; + + wflag = false; + fflag = false; + + starts = "1"; + steps = "1"; + ends = "1"; + + while((c = getopt(argc, argv, "f:s:w")) != -1) { + switch(c) { + case 'f': + if (!validfmt(optarg)) + eprintf("invalid format.\n"); + fmt = optarg; + fflag = true; + break; + case 's': + sep = optarg; + break; + case 'w': + wflag = true; + break; + } + } + + if (wflag && fflag) + eprintf("-f and -w cannot be combined.\n"); + + switch(argc-optind) { + case 3: + starts = argv[optind++]; + steps = argv[optind++]; + ends = argv[optind++]; + break; + case 2: + starts = argv[optind++]; + ends = argv[optind++]; + break; + case 1: + ends = argv[optind++]; + break; + default: + eprintf("usage: %s [-w] [-f fmt] [-s separator] " + "[start [step]] end\n", + basename(argv[0])); + } + + if (!validfloat(starts)) + eprintf("start is not a valid float.\n"); + if (!validfloat(steps)) + eprintf("step is not a valid float.\n"); + if (!validfloat(ends)) + eprintf("end is not a valid float.\n"); + + start = atof(starts); + step = atof(steps); + end = atof(ends); + + if (step == 0) + return EXIT_FAILURE; + + if (start > end) { + if (step > 0) + return EXIT_FAILURE; + } else if (start < end) { + if (step < 0) + return EXIT_FAILURE; + } + + right = MAX(digitsright(starts), + MAX(digitsright(ends), + digitsright(steps))); + if (wflag) { + left = MAX(digitsleft(starts), digitsleft(ends)); + + snprintf(ftmp, sizeof(ftmp), "%%0%d.%df", + right+left+(right != 0), + right); + } else if (fmt == ftmp) { + snprintf(ftmp, sizeof(ftmp), "%%.%df", right); + } + + for (out = start;;) { + printf(fmt, out); + + out += step; + if (start > end) { + if (out >= end) { + printf("%s", sep); + } else { + break; + } + } else if (start < end) { + if (out <= end) { + printf("%s", sep); + } else { + break; + } + } else { + break; + } + } + printf("\n"); + + return EXIT_SUCCESS; }