robert@isgtec.UUCP (Robert Osborne) (05/21/91)
In article <remus@paul.alpha.edu>, denv@nywu writes: |> this is just a test. Why is this in alt.sources? It was crossposted to alt.test, so obviously it's more than just the fact that you're a moron. Why post a test why not post some sources? Rob. OB. Source Here is a getopt I wrote (now PD) because Turbo C doesn't have one! (Maybe they'll put this one into their libraries now :-) #!/bin/sh # This is a shell archive (produced by shar 3.49) # To extract the files from this archive, save it to a file, remove # everything above the "!/bin/sh" line above, and type "sh file_name". # # made 05/21/1991 15:01 UTC by robert@isgtec.UUCP # Source directory /tmp_mnt/home/joker1/robert/test/getopt # # existing files will NOT be overwritten unless -c is specified # # This shar contains: # length mode name # ------ ---------- ------------------------------------------ # 1870 -rw-rw-r-- getopt.c # # ============= getopt.c ============== if test -f 'getopt.c' -a X"$1" != X"-c"; then echo 'x - skipping getopt.c (File already exists)' else echo 'x - extracting getopt.c (Text)' sed 's/^X//' << 'SHAR_EOF' > 'getopt.c' && /* ** This getopt behaves pretty much like you would expect. ** It does handle arguments like '-ab-' a little differently ** then normal; I think the -- 'stop option processing' should ** be treated like just another option, so that's what mine does. ** Other getopts seem to ignore the second '-' in '-ab-'. ** ** I hereby place this version of getopt in ** the public domain. Do with this what you will. ** I'm sure there is a nicer and faster version out there ** somewhere but I don't care! ** ** Robert Osborne, May 1991. */ #include <stdio.h> X int optind = 1; int opterr = 1; char *optarg = (char *) 0; X static char *next_arg = (char *) 0; X #define NO_OPT 0 #define OPT_PLAIN 1 #define OPT_ARG 2 X X int getopt(argc, argv, optstring) X int argc; X char *argv[]; X char *optstring; { X int ret; X int which = NO_OPT; X X if( next_arg == (char *)0 ) X { X if( argv[optind] == (char *) 0 || argv[optind][0] != '-' ) X return -1; X next_arg = &argv[optind][1]; X } X if( *next_arg == '\0' || *next_arg == '-' ) X { X optind++; X return -1; X } X X while(*optstring) X if( *next_arg == *optstring++ ) X which = (*optstring == ':') ? OPT_ARG : OPT_PLAIN; X X switch( which ) X { X case NO_OPT: X case OPT_PLAIN: X ret = *next_arg++; X if( *next_arg == '\0' ) X { X optind++; X next_arg = (char *)0; X } X if( which == OPT_PLAIN ) X return ret; X if( opterr ) X fprintf(stderr, "%s: illegal option -- %c\n", argv[0], ret); X return '?'; X case OPT_ARG: X ret = *next_arg++; X optind++; X if( *next_arg != '\0' ) X { X optarg = next_arg; X next_arg = (char *)0; X return ret; X } X else if( argv[optind] != (char *) 0 ) X { X optarg = argv[optind]; X optind++; X next_arg = (char *) 0; X return ret; X } X else X { X next_arg = (char *) 0; X if( opterr ) X fprintf(stderr, "%s: option requires an option -- %c\n", X argv[0], ret); X return '?'; X } X } } SHAR_EOF chmod 0664 getopt.c || echo 'restore of getopt.c failed' Wc_c="`wc -c < 'getopt.c'`" test 1870 -eq "$Wc_c" || echo 'getopt.c: original size 1870, current size' "$Wc_c" fi exit 0
brad@SSD.CSD.HARRIS.COM (Brad Appleton) (05/22/91)
In article <1074@isgtec.UUCP> robert@isgtec.UUCP writes: >In article <remus@paul.alpha.edu>, denv@nywu writes: >|> this is just a test. > >Why is this in alt.sources? It was crossposted to alt.test, so obviously >it's more than just the fact that you're a moron. Why post a test >why not post some sources? > >Rob. > >OB. Source > >Here is a getopt I wrote (now PD) because Turbo C doesn't have one! >(Maybe they'll put this one into their libraries now :-) > First of all - AT&T already put getopt into the Public domain. Second, getopt is a piece of crap. DOWN WITH GETOPT! There is much better stuff available. Take Parseargs for example (which I released). For any interested parties, the following is a brief description of Parseargs. You can get the latest version from me, or you can look in your local comp.sources.misc archives for the sources and latest patches (patch05). ______________________ "And miles to go before I sleep." ______________________ Brad Appleton brad@ssd.csd.harris.com Harris Computer Systems uunet!hcx1!brad Fort Lauderdale, FL USA ~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~ PARSEARGS extracted from Eric Allman's NIFTY UTILITY LIBRARY Created by Eric P. Allman <eric@Berkeley.EDU> Modified by Peter da Silva <peter@Ferranti.COM> Modified and Rewritten by Brad Appleton <brad@SSD.CSD.Harris.COM> Welcome to parseargs! Dont let the initial size of this package scare you. over 75% of it is English text, and more than 50% of the source is comments. Parseargs is a set of functions to parse command-line arguments. Unlike getopt and its variants, parseargs does more than just split up the command-line into some canonical form. Parseargs will actually parse the command-line, assigning the appropriate command-line values to the corresponding variables, and will verify the command-line syntax (and print a usage message if necessary). Furthermore, many features of it's parsing behavior are configurable at run-time. Some of these features include the following: o Prompting the user for missing arguments o Allowing keywords (+count=4) and/or options (-c4) o Checking for default arguments in an environment variable o Ignoring bad syntax instead of terminating o Ignoring upper/lower case on the command-line o Controlling the location of non-positional parameters o Controlling the contents (syntax and verbosity) of usage messages o Having long usage messages piped through a paging program Parseargs also allows for options that take an optional argument, and options that take a (possibly optional) list of one or more arguments. In addition, parseargs may be configured at compile-time to parse command-lines in accordance with the native command-syntax of any of the following operating systems: o Unix o VAX/VMS o OS/2 o MS-DOS o AmigaDOS Parseargs consists of a set of C-functions to parse arguments from the command-line, from files, from strings, from linked-lists, and from string-vectors. Also included is a command-line interface which will parse arguments for shell scripts (sh, csh/tcsh/itcsh, ksh, bash, zsh, and rc), awk-scripts, and perl-scripts. The basic structure used by parseargs is the argument-descriptor (sometimes called "argdesc" for brevity). An array/string of argdescs is declared by the user to describe the command in question. The resulting argdesc-array is passed to all the parseargs functions and is used to hold all information about the command. a sample argdesc-array is shown below. STARTOFARGS, { 'a', ARGVALOPT, argStr, &area, "AREAcode : optional area-code" }, { 'g', ARGLIST, argStr, &groups, "newsGROUPS : groups to test" }, { 'r', ARGOPT, argInt, &count, "REPcount : repetition factor" }, { 's', ARGOPT, argChar, &sepch, "SEPchar : field separator" }, { 'x', ARGOPT, argBool, &xflag, "Xflag : turn on X-mode" }, { ' ', ARGREQ, argStr, &name, "name : name to use" }, { ' ', ARGLIST, argStr, &args, "args : any remaining arguments" }, ENDOFARGS Once the above array/string is declared it is a simple matter to invoke parseargs from C as in the following example: status = parseargs( argdesc_array, argv ); or from a shell script as in the following example: echo "$ARGDESC_STR" | parseargs -s sh -- "$0" "$@" >tmp$$ test $? = 0 && . tmp$$ /bin/rm -f tmp$$ And before you know it, your command-line had been parsed, all variables have been assigned their corresponding values from the command-line, syntax has been verified, and a usage message (if required) has been printed. Under UNIX, the command-line syntax (using single character options) for the above command would be: cmdname [-a [<areacode>]] [-g <newsgroups>...] [-r <repcount>] [-s <sepchar>] [-x] <name> [<args>...] The UNIX command-line syntax using keywords (or long options) would be: cmdname [+area [<areacode>]] [+groups <newsgroups>...] [+rep <repcount>] [+sep <sepchar>] [+x] <name> [<args>...] The VMS command-line syntax would be the following: cmdname [/AREA[=<areacode>]] [/GROUPS=<newsgroups>[,<newsgroups>...] [/REP=<repcount>] [/SEP=<sepchar>] [/X] <name> [<args>[,<args>...]] The MS-DOS and OS/2 command-line syntax would be the following (unless the environment variable $SWITCHAR is '-' in which case UNIX syntax is used): cmdname [/a[=<areacode>]] [/g=<newsgroups>...] [/r=<repcount>] [/s=<sepchar>] [/x] <name> [<args>...] The AmigaDOS command-line syntax would be the following: cmdname [AREA [<areacode>]] [GROUPS <newsgroups>...] [REP <repcount>] [SEP <sepchar>] [X] <name> [<args>...] Please look at the README files and manpages for more detailed information! ______________________ "And miles to go before I sleep." ______________________ Brad Appleton brad@ssd.csd.harris.com Harris Computer Systems uunet!hcx1!brad Fort Lauderdale, FL USA ~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~