args.h - vx32 - Local 9vx git repository for patches.
 (HTM) git clone git://r-36.net/vx32
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       args.h (4148B)
       ---
            1 /*
            2 
            3 Simple command-line argument parser from Plan 9's u9fs:
            4 
            5 The authors of this software are Bob Flandrena, Ken Thompson,
            6 Rob Pike, and Russ Cox.
            7 
            8                 Copyright (c) 1992-2002 by Lucent Technologies.
            9 
           10 Permission to use, copy, modify, and distribute this software for any
           11 purpose without fee is hereby granted, provided that this entire notice
           12 is included in all copies of any software which is or includes a copy
           13 or modification of this software and in all copies of the supporting
           14 documentation for such software.
           15 
           16 THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
           17 WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
           18 REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
           19 OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
           20 
           21      SYNOPSIS
           22           #include "args.h"
           23 
           24           ARGBEGIN {
           25           char *ARGF();
           26           char *EARGF(code);
           27           Rune ARGC();
           28           } ARGEND
           29 
           30           extern char *argv0;
           31 
           32      DESCRIPTION
           33           These macros assume the names argc and argv are in scope.
           34           ARGBEGIN and ARGEND surround code for processing program
           35           options.  The code should be the cases of a C switch on option
           36           characters; it is executed once for each option character.
           37           Options end after an argument --, before an argument -, or
           38           before an argument that doesn't begin with -.
           39 
           40           The function macro ARGC returns the current option charac-
           41           ter, as an integer.
           42 
           43           The function macro ARGF returns the current option argument:
           44           a pointer to the rest of the option string if not empty, or
           45           the next argument in argv if any, or 0.  ARGF must be called
           46           just once for each option that takes an argument.  The macro
           47           EARGF is like ARGF but instead of returning zero runs code
           48           and, if that returns, calls abort(). A typical value for
           49           code is usage(), as in EARGF(usage()).
           50 
           51           After ARGBEGIN, argv0 is a copy of argv[0] (conventionally
           52           the name of the program).
           53 
           54           After ARGEND, argv points at a zero-terminated list of the
           55           remaining argc arguments.
           56 
           57      EXAMPLE
           58           This C program can take option b and option f, which
           59           requires an argument.
           60 
           61                void
           62                main(int argc, char *argv[])
           63                {
           64                        char *f;
           65                        print("%s", argv[0]);
           66                        ARGBEGIN {
           67                        case 'b':
           68                                print(" -b");
           69                                break;
           70                        case 'f':
           71                                print(" -f(%s)", (f=ARGF())? f: "no arg");
           72                                break;
           73                        default:
           74                                print(" badflag('%c')", ARGC());
           75                        } ARGEND
           76                        print(" %d args:", argc);
           77                        while(*argv)
           78                                print(" '%s'", *argv++);
           79                        print("\n");
           80                        exits(nil);
           81                }
           82 
           83           Here is the output from running the command 
           84           "prog -bffile1 -r -f file2 arg1 arg2":
           85 
           86                prog -b -f(file1) badflag('r') -f(file2) 2 args: 'arg1' 'arg2'
           87 
           88  */
           89 
           90 extern const char        *argv0;
           91 
           92 #define _ARGSET(x)        (x) = 0
           93 #define _ARGUSED(x)        if (x) { } else
           94 
           95 #define        ARGBEGIN        for((argv?0:(argv=(void*)&argc)),(argv0?0:(argv0=*argv)),\
           96                             argv++,argc--;\
           97                             argv[0] && argv[0][0]=='-' && argv[0][1];\
           98                             argc--, argv++) {\
           99                                 const char *_args;\
          100                                 const char *_argt;\
          101                                 char _argc;\
          102                                 _args = &argv[0][1];\
          103                                 if(_args[0]=='-' && _args[1]==0){\
          104                                         argc--; argv++; break;\
          105                                 }\
          106                                 _argc = 0;\
          107                                 while(*_args && (_argc = *_args++))\
          108                                 switch(_argc)
          109 #define        ARGEND                _ARGSET(_argt);_ARGUSED(_argt);_ARGUSED(_argc);_ARGUSED(_args);}_ARGUSED(argv);_ARGUSED(argc);
          110 #define        ARGF()                (_argt=_args, _args="",\
          111                                 (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
          112 #define        EARGF(x)        (_argt=_args, _args="",\
          113                                 (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
          114 
          115 #define        ARGC()                _argc