sbase

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

commit 146cca114e365d89780e7bcd88b8cb83eb561dcf
parent f1259218f2630b519607f67bd73701a6d3f398f1
Author: Connor Lane Smith <cls@lubutu.com>
Date:   Mon, 14 May 2012 21:28:41 +0100

new ARGBEGIN & usage() function
Diffstat:
MMakefile | 4+---
Darg.h | 31-------------------------------
Mbasename.c | 23++++++-----------------
Mconfig.mk | 4++--
Mkill.c | 20++++++++++++++------
Mtest.c | 37++++++++++++++++++-------------------
Mutil.h | 27+++++++++++++++++++++++++++
Dutil/enprintf.c | 15---------------
Mutil/eprintf.c | 37++++++++++++++++++++++++++++++++++++-
Dutil/venprintf.c | 18------------------
Myes.c | 40+++++++++++++---------------------------
11 files changed, 117 insertions(+), 139 deletions(-)

diff --git a/Makefile b/Makefile @@ -9,13 +9,11 @@ LIB = \ util/cp.o \ util/enmasse.o \ util/eprintf.o \ - util/enprintf.o \ util/estrtol.o \ util/fnck.o \ util/putword.o \ util/recurse.o \ - util/rm.o \ - util/venprintf.o + util/rm.o SRC = \ basename.c \ diff --git a/arg.h b/arg.h @@ -1,31 +0,0 @@ -extern char *argv0; - -#define USED(x) ((void)(x)) - -#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ - argv[0] && argv[0][1]\ - && argv[0][0] == '-';\ - argc--, argv++) {\ - char _argc;\ - char **_argv;\ - if (argv[0][1] == '-' && argv[0][2] == '\0') {\ - argv++;\ - argc--;\ - break;\ - }\ - for (argv[0]++, _argv = argv; argv[0][0];\ - argv[0]++) {\ - if (_argv != argv)\ - break;\ - _argc = argv[0][0];\ - switch (_argc) - -#define ARGEND }\ - USED(_argc);\ - }\ - USED(argv);\ - USED(argc); - -#define EARGF(x) ((argv[1] == NULL)? ((x), abort(), (char *)0) :\ - (argc--, argv++, argv[0])) - diff --git a/basename.c b/basename.c @@ -3,19 +3,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#include <stdbool.h> - -#include "arg.h" #include "util.h" -char *argv0; - -void -usage(void) -{ - eprintf("usage: %s name [suffix]\n", basename(argv0)); -} +#define USAGE() usage("name [suffix]") int main(int argc, char *argv[]) @@ -25,20 +15,19 @@ main(int argc, char *argv[]) ARGBEGIN { default: - usage(); + USAGE(); } ARGEND; - if (argc < 1) - usage(); + if(argc < 1) + USAGE(); s = basename(argv[0]); - if (argc == 2 && argv[1]) { + if(argc == 2 && argv[1]) { n = strlen(s) - strlen(argv[1]); - if (!strcmp(&s[n], argv[1])) + if(!strcmp(&s[n], argv[1])) s[n] = '\0'; } puts(s); return EXIT_SUCCESS; } - diff --git a/config.mk b/config.mk @@ -9,8 +9,8 @@ MANPREFIX = $(PREFIX)/share/man #CC = musl-gcc LD = $(CC) CPPFLAGS = -D_POSIX_C_SOURCE=200112L -CFLAGS = -Os -ansi -Wall -pedantic $(CPPFLAGS) -LDFLAGS = +CFLAGS = -g -ansi -Wall -pedantic $(CPPFLAGS) +LDFLAGS = -g #CC = tcc #LD = $(CC) diff --git a/kill.c b/kill.c @@ -8,7 +8,7 @@ #include <sys/wait.h> #include "util.h" -#define LEN(x) (sizeof (x) / sizeof *(x)) +#define USAGE() killusage() struct { const char *name; @@ -21,6 +21,8 @@ struct { #undef SIG }; +static void killusage(void); + int main(int argc, char *argv[]) { @@ -48,13 +50,12 @@ main(int argc, char *argv[]) eprintf("%s: unknown signal\n", optarg); break; default: - exit(EXIT_FAILURE); + USAGE(); } - if(lflag) { - if(optind < argc-1) - eprintf("usage: %s [-s signal] [pid...]\n" - " %s -l [signum]\n", argv[0], argv[0]); + if(optind < argc-1) + USAGE(); + if(lflag) { sig = (optind == argc) ? 0 : estrtol(argv[optind], 0); if(sig > 128) sig = WTERMSIG(sig); @@ -70,3 +71,10 @@ main(int argc, char *argv[]) } return EXIT_SUCCESS; } + +void +killusage(void) +{ + fprintf(stderr, "usage: %s [-s signal] [pid...]\n" + " %s -l [signum]\n", argv0, argv0); +} diff --git a/test.c b/test.c @@ -7,23 +7,23 @@ #include <sys/stat.h> #include "util.h" +#define USAGE() testusage() + static bool unary(const char *, const char *); -static void usage(void); static bool binary(const char *, const char *, const char *); - -static const char *progname; +static void testusage(void); int main(int argc, char *argv[]) { bool ret = false, not = false; - progname = argv[0]; + argv0 = argv[0]; /* [ ... ] alias */ if(!strcmp(argv[0], "[")) { if(strcmp(argv[argc-1], "]") != 0) - usage(); + USAGE(); argc--; } if(argc > 1 && !strcmp(argv[1], "!")) { @@ -42,7 +42,7 @@ main(int argc, char *argv[]) ret = binary(argv[1], argv[2], argv[3]); break; default: - usage(); + USAGE(); } if(not) ret = !ret; @@ -56,7 +56,7 @@ unary(const char *op, const char *arg) int r; if(op[0] != '-' || op[1] == '\0' || op[2] != '\0') - usage(); + USAGE(); switch(op[1]) { case 'b': case 'c': case 'd': case 'f': case 'g': case 'p': case 'S': case 's': case 'u': @@ -99,25 +99,24 @@ unary(const char *op, const char *arg) case 'z': return arg[0] == '\0'; default: - usage(); + USAGE(); } return false; /* should not reach */ } -void -usage(void) -{ - const char *ket = (progname[0] == '[') ? " ]" : ""; - - fprintf(stderr, "usage: %s string%s\n", progname, ket); - fprintf(stderr, " %s [!] [-bcdefghLnprSstuwxz] string%s\n", progname, ket); - fprintf(stderr, " %s [!] string1 comparison-op string2%s\n", progname, ket); - exit(EXIT_FAILURE); -} - bool binary(const char *arg1, const char *op, const char *arg2) { eprintf("not yet implemented\n"); return false; } + +void +testusage(void) +{ + const char *ket = (*argv0 == '[') ? " ]" : ""; + + fprintf(stderr, "usage: %s string%s\n" + " %s [!] [-bcdefghLnprSstuwxz] string%s\n", argv0, ket, argv0, ket); + exit(EXIT_FAILURE); +} diff --git a/util.h b/util.h @@ -5,6 +5,32 @@ #define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MAX(x,y) ((x) > (y) ? (x) : (y)) +#define LEN(x) (sizeof (x) / sizeof *(x)) + +#define ARGBEGIN \ + { \ + if(!argv0) \ + argv0 = argv[0]; \ + for(argc--, argv++; argv[0] && argv[0][0] == '-' && argv[0][1] != '\0'; argc--, argv++) { \ + if(argv[0][1] == '-' && argv[0][2] == '\0') { /* -- signifies end of flags */ \ + argc--; argv++; \ + break; \ + } \ + for(argv[0]++; *argv[0] != '\0'; argv[0]++) \ + switch(*argv[0]) + +#define ARGEND \ + } \ + } + +#define ARGC() (*argv[0]) +#define ARGF() (*argv[0] ? argv[0] \ + : argv[1] ? (argc--, *++argv) : NULL) +#define EARGF(x) (*argv[0] ? argv[0] \ + : argv[1] ? (argc--, *++argv) : ((x), abort(), NULL)) + +extern char *argv0; + char *agetcwd(void); void apathmax(char **, long *); void enmasse(int, char **, int (*)(const char *, const char *)); @@ -14,3 +40,4 @@ long estrtol(const char *, int); void fnck(const char *, const char *, int (*)(const char *, const char *)); void putword(const char *); void recurse(const char *, void (*)(const char *)); +void usage(const char *); diff --git a/util/enprintf.c b/util/enprintf.c @@ -1,15 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <stdarg.h> -#include "../util.h" - -extern void venprintf(int, const char *, va_list); - -void -enprintf(int status, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - venprintf(status, fmt, ap); - va_end(ap); -} diff --git a/util/eprintf.c b/util/eprintf.c @@ -1,9 +1,13 @@ /* See LICENSE file for copyright and license details. */ #include <stdarg.h> +#include <stdio.h> #include <stdlib.h> +#include <string.h> #include "../util.h" -extern void venprintf(int, const char *, va_list); +char *argv0; + +static void venprintf(int, const char *, va_list); void eprintf(const char *fmt, ...) @@ -14,3 +18,34 @@ eprintf(const char *fmt, ...) venprintf(EXIT_FAILURE, fmt, ap); va_end(ap); } + +void +enprintf(int status, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + venprintf(status, fmt, ap); + va_end(ap); +} + +void +usage(const char *s) +{ + fprintf(stderr, "usage: %s %s\n", argv0, s); + exit(EXIT_FAILURE); +} + +void +venprintf(int status, const char *fmt, va_list ap) +{ + /*fprintf(stderr, "%s: ", argv0);*/ + + vfprintf(stderr, fmt, ap); + + if(fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } + exit(status); +} diff --git a/util/venprintf.c b/util/venprintf.c @@ -1,18 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include "../util.h" - -void -venprintf(int status, const char *fmt, va_list ap) -{ - vfprintf(stderr, fmt, ap); - - if(fmt[0] && fmt[strlen(fmt)-1] == ':') { - fputc(' ', stderr); - perror(NULL); - } - exit(status); -} diff --git a/yes.c b/yes.c @@ -1,44 +1,30 @@ /* See LICENSE file for copyright and license details. */ -#include <unistd.h> -#include <libgen.h> #include <stdio.h> #include <stdlib.h> - -#include "arg.h" #include "util.h" -char *argv0; - -void -usage(void) -{ - eprintf("usage: %s [string ...]\n", basename(argv0)); -} +#define USAGE() usage("[string]") int main(int argc, char *argv[]) { - int i; + char *s = "y"; ARGBEGIN { default: - usage(); + USAGE(); } ARGEND; - if (!argc) { + switch(argc) { + case 1: + s = argv[0]; + /* fallthrough */ + case 0: for(;;) - puts("y"); - } - - for (;;) { - for (i = 0; i < argc; i++) { - fputs(argv[i], stdout); - if (argv[i+1] != NULL) - fputs(" ", stdout); - } - fputs("\n", stdout); + puts(s); + break; + default: + USAGE(); } - - return EXIT_SUCCESS; + return EXIT_FAILURE; /* should not reach */ } -