new ARGBEGIN & usage() function

This commit is contained in:
Connor Lane Smith 2012-05-14 21:28:41 +01:00
parent f1259218f2
commit 146cca114e
11 changed files with 118 additions and 140 deletions

View File

@ -9,13 +9,11 @@ LIB = \
util/cp.o \ util/cp.o \
util/enmasse.o \ util/enmasse.o \
util/eprintf.o \ util/eprintf.o \
util/enprintf.o \
util/estrtol.o \ util/estrtol.o \
util/fnck.o \ util/fnck.o \
util/putword.o \ util/putword.o \
util/recurse.o \ util/recurse.o \
util/rm.o \ util/rm.o
util/venprintf.o
SRC = \ SRC = \
basename.c \ basename.c \

31
arg.h
View File

@ -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]))

View File

@ -3,19 +3,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include "arg.h"
#include "util.h" #include "util.h"
char *argv0; #define USAGE() usage("name [suffix]")
void
usage(void)
{
eprintf("usage: %s name [suffix]\n", basename(argv0));
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -25,11 +15,11 @@ main(int argc, char *argv[])
ARGBEGIN { ARGBEGIN {
default: default:
usage(); USAGE();
} ARGEND; } ARGEND;
if(argc < 1) if(argc < 1)
usage(); USAGE();
s = basename(argv[0]); s = basename(argv[0]);
if(argc == 2 && argv[1]) { if(argc == 2 && argv[1]) {
@ -41,4 +31,3 @@ main(int argc, char *argv[])
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -9,8 +9,8 @@ MANPREFIX = $(PREFIX)/share/man
#CC = musl-gcc #CC = musl-gcc
LD = $(CC) LD = $(CC)
CPPFLAGS = -D_POSIX_C_SOURCE=200112L CPPFLAGS = -D_POSIX_C_SOURCE=200112L
CFLAGS = -Os -ansi -Wall -pedantic $(CPPFLAGS) CFLAGS = -g -ansi -Wall -pedantic $(CPPFLAGS)
LDFLAGS = LDFLAGS = -g
#CC = tcc #CC = tcc
#LD = $(CC) #LD = $(CC)

18
kill.c
View File

@ -8,7 +8,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#include "util.h" #include "util.h"
#define LEN(x) (sizeof (x) / sizeof *(x)) #define USAGE() killusage()
struct { struct {
const char *name; const char *name;
@ -21,6 +21,8 @@ struct {
#undef SIG #undef SIG
}; };
static void killusage(void);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -48,13 +50,12 @@ main(int argc, char *argv[])
eprintf("%s: unknown signal\n", optarg); eprintf("%s: unknown signal\n", optarg);
break; break;
default: default:
exit(EXIT_FAILURE); USAGE();
} }
if(lflag) {
if(optind < argc-1) if(optind < argc-1)
eprintf("usage: %s [-s signal] [pid...]\n" USAGE();
" %s -l [signum]\n", argv[0], argv[0]);
if(lflag) {
sig = (optind == argc) ? 0 : estrtol(argv[optind], 0); sig = (optind == argc) ? 0 : estrtol(argv[optind], 0);
if(sig > 128) if(sig > 128)
sig = WTERMSIG(sig); sig = WTERMSIG(sig);
@ -70,3 +71,10 @@ main(int argc, char *argv[])
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void
killusage(void)
{
fprintf(stderr, "usage: %s [-s signal] [pid...]\n"
" %s -l [signum]\n", argv0, argv0);
}

39
test.c
View File

@ -7,23 +7,23 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "util.h" #include "util.h"
static bool unary(const char *, const char *); #define USAGE() testusage()
static void usage(void);
static bool binary(const char *, const char *, const char *);
static const char *progname; static bool unary(const char *, const char *);
static bool binary(const char *, const char *, const char *);
static void testusage(void);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool ret = false, not = false; bool ret = false, not = false;
progname = argv[0]; argv0 = argv[0];
/* [ ... ] alias */ /* [ ... ] alias */
if(!strcmp(argv[0], "[")) { if(!strcmp(argv[0], "[")) {
if(strcmp(argv[argc-1], "]") != 0) if(strcmp(argv[argc-1], "]") != 0)
usage(); USAGE();
argc--; argc--;
} }
if(argc > 1 && !strcmp(argv[1], "!")) { if(argc > 1 && !strcmp(argv[1], "!")) {
@ -42,7 +42,7 @@ main(int argc, char *argv[])
ret = binary(argv[1], argv[2], argv[3]); ret = binary(argv[1], argv[2], argv[3]);
break; break;
default: default:
usage(); USAGE();
} }
if(not) if(not)
ret = !ret; ret = !ret;
@ -56,7 +56,7 @@ unary(const char *op, const char *arg)
int r; int r;
if(op[0] != '-' || op[1] == '\0' || op[2] != '\0') if(op[0] != '-' || op[1] == '\0' || op[2] != '\0')
usage(); USAGE();
switch(op[1]) { switch(op[1]) {
case 'b': case 'c': case 'd': case 'f': case 'g': case 'b': case 'c': case 'd': case 'f': case 'g':
case 'p': case 'S': case 's': case 'u': case 'p': case 'S': case 's': case 'u':
@ -99,25 +99,24 @@ unary(const char *op, const char *arg)
case 'z': case 'z':
return arg[0] == '\0'; return arg[0] == '\0';
default: default:
usage(); USAGE();
} }
return false; /* should not reach */ 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 bool
binary(const char *arg1, const char *op, const char *arg2) binary(const char *arg1, const char *op, const char *arg2)
{ {
eprintf("not yet implemented\n"); eprintf("not yet implemented\n");
return false; 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);
}

27
util.h
View File

@ -5,6 +5,32 @@
#define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(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); char *agetcwd(void);
void apathmax(char **, long *); void apathmax(char **, long *);
void enmasse(int, char **, int (*)(const char *, const char *)); 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 fnck(const char *, const char *, int (*)(const char *, const char *));
void putword(const char *); void putword(const char *);
void recurse(const char *, void (*)(const char *)); void recurse(const char *, void (*)(const char *));
void usage(const char *);

View File

@ -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);
}

View File

@ -1,9 +1,13 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "../util.h" #include "../util.h"
extern void venprintf(int, const char *, va_list); char *argv0;
static void venprintf(int, const char *, va_list);
void void
eprintf(const char *fmt, ...) eprintf(const char *fmt, ...)
@ -14,3 +18,34 @@ eprintf(const char *fmt, ...)
venprintf(EXIT_FAILURE, fmt, ap); venprintf(EXIT_FAILURE, fmt, ap);
va_end(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);
}

View File

@ -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);
}

40
yes.c
View File

@ -1,44 +1,30 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <unistd.h>
#include <libgen.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "arg.h"
#include "util.h" #include "util.h"
char *argv0; #define USAGE() usage("[string]")
void
usage(void)
{
eprintf("usage: %s [string ...]\n", basename(argv0));
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i; char *s = "y";
ARGBEGIN { ARGBEGIN {
default: default:
usage(); USAGE();
} ARGEND; } ARGEND;
if (!argc) { switch(argc) {
case 1:
s = argv[0];
/* fallthrough */
case 0:
for(;;) for(;;)
puts("y"); puts(s);
break;
default:
USAGE();
} }
return EXIT_FAILURE; /* should not reach */
for (;;) {
for (i = 0; i < argc; i++) {
fputs(argv[i], stdout);
if (argv[i+1] != NULL)
fputs(" ", stdout);
} }
fputs("\n", stdout);
}
return EXIT_SUCCESS;
}