[hackers] [sbase] new ARGBEGIN & usage() function || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Mon, 14 May 2012 22:30:42 +0200 (CEST)

changeset: 117:e34b04007eb3
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Mon May 14 21:28:41 2012 +0100
files: Makefile arg.h basename.c config.mk kill.c test.c util.h util/enprintf.c util/eprintf.c util/venprintf.c yes.c
description:
new ARGBEGIN & usage() function


diff -r 1a132443ba6c -r e34b04007eb3 Makefile
--- a/Makefile Mon May 14 13:33:07 2012 +0100
+++ b/Makefile Mon May 14 21:28:41 2012 +0100
_AT_@ -9,13 +9,11 @@
         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 -r 1a132443ba6c -r e34b04007eb3 arg.h
--- a/arg.h Mon May 14 13:33:07 2012 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
_AT_@ -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 -r 1a132443ba6c -r e34b04007eb3 basename.c
--- a/basename.c Mon May 14 13:33:07 2012 +0100
+++ b/basename.c Mon May 14 21:28:41 2012 +0100
_AT_@ -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[])
_AT_@ -25,20 +15,19 @@
 
         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 -r 1a132443ba6c -r e34b04007eb3 config.mk
--- a/config.mk Mon May 14 13:33:07 2012 +0100
+++ b/config.mk Mon May 14 21:28:41 2012 +0100
_AT_@ -9,8 +9,8 @@
 #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 -r 1a132443ba6c -r e34b04007eb3 kill.c
--- a/kill.c Mon May 14 13:33:07 2012 +0100
+++ b/kill.c Mon May 14 21:28:41 2012 +0100
_AT_@ -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;
_AT_@ -21,6 +21,8 @@
 #undef SIG
 };
 
+static void killusage(void);
+
 int
 main(int argc, char *argv[])
 {
_AT_@ -48,13 +50,12 @@
                                 eprintf("%s: unknown signal\n", optarg);
                         break;
                 default:
- exit(EXIT_FAILURE);
+ USAGE();
                 }
+ if(optind < argc-1)
+ USAGE();
+
         if(lflag) {
- if(optind < argc-1)
- eprintf("usage: %s [-s signal] [pid...]\n"
- " %s -l [signum]\n", argv[0], argv[0]);
-
                 sig = (optind == argc) ? 0 : estrtol(argv[optind], 0);
                 if(sig > 128)
                         sig = WTERMSIG(sig);
_AT_@ -70,3 +71,10 @@
         }
         return EXIT_SUCCESS;
 }
+
+void
+killusage(void)
+{
+ fprintf(stderr, "usage: %s [-s signal] [pid...]\n"
+ " %s -l [signum]\n", argv0, argv0);
+}
diff -r 1a132443ba6c -r e34b04007eb3 test.c
--- a/test.c Mon May 14 13:33:07 2012 +0100
+++ b/test.c Mon May 14 21:28:41 2012 +0100
_AT_@ -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], "!")) {
_AT_@ -42,7 +42,7 @@
                 ret = binary(argv[1], argv[2], argv[3]);
                 break;
         default:
- usage();
+ USAGE();
         }
         if(not)
                 ret = !ret;
_AT_@ -56,7 +56,7 @@
         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':
_AT_@ -99,25 +99,24 @@
         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 -r 1a132443ba6c -r e34b04007eb3 util.h
--- a/util.h Mon May 14 13:33:07 2012 +0100
+++ b/util.h Mon May 14 21:28:41 2012 +0100
_AT_@ -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 *));
_AT_@ -14,3 +40,4 @@
 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 -r 1a132443ba6c -r e34b04007eb3 util/enprintf.c
--- a/util/enprintf.c Mon May 14 13:33:07 2012 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
_AT_@ -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 -r 1a132443ba6c -r e34b04007eb3 util/eprintf.c
--- a/util/eprintf.c Mon May 14 13:33:07 2012 +0100
+++ b/util/eprintf.c Mon May 14 21:28:41 2012 +0100
_AT_@ -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, ...)
_AT_@ -14,3 +18,34 @@
         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 -r 1a132443ba6c -r e34b04007eb3 util/venprintf.c
--- a/util/venprintf.c Mon May 14 13:33:07 2012 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
_AT_@ -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 -r 1a132443ba6c -r e34b04007eb3 yes.c
--- a/yes.c Mon May 14 13:33:07 2012 +0100
+++ b/yes.c Mon May 14 21:28:41 2012 +0100
_AT_@ -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");
+ puts(s);
+ break;
+ default:
+ USAGE();
         }
-
- for (;;) {
- for (i = 0; i < argc; i++) {
- fputs(argv[i], stdout);
- if (argv[i+1] != NULL)
- fputs(" ", stdout);
- }
- fputs("\n", stdout);
- }
-
- return EXIT_SUCCESS;
+ return EXIT_FAILURE; /* should not reach */
 }
-
Received on Mon May 14 2012 - 22:30:42 CEST

This archive was generated by hypermail 2.3.0 : Mon May 14 2012 - 22:36:06 CEST