#!/bin/sh # # This is a shell archive. To extract its contents, # execute this file with /bin/sh to create the file(s): # # getopt.4gl # # This shell archive created: Wed Nov 10 09:48:11 CST 1999 # echo "Extracting file getopt.4gl" sed -e 's/^X//' <<\SHAR_EOF > getopt.4gl X-- @(#)$Id: getopt.4gl,v 2.4 1999/03/16 17:26:04 jleffler Exp $ X-- @(#)Purpose: Simulate GETOPT(3) in I4GL/D4GL X-- @(#)Author: Jonathan Leffler X-- @(#)Product: :PRODUCT: X X{ XSimulate GETOPT(3) in I4GL X XOperation: XWhen called with an argument such as "abcf:t:x", the function scans Xthe command line argument list for the flags -a, -b -c or -x, or options -f and X-t. The flags are boolean values; the options have a text string associated Xwith them. This can be attached to the option argument (e.g. -ffilename) or in Xthe next argument (-f filename). X XDifferences from GETOPT(3): X1. Function returns optval (CHAR(1)) containing the flag letter and optarg X (CHAR(80)) containing option string. GETOPT(3) just returns the flag letter X and the global variable optarg points to the option string. X3. The optval returned for no more arguments is NULL. X3. The optval returned for a non-option (filename) argument is "-". X} X XDEFINE X argind INTEGER, { Where we've got through list } X argnum INTEGER, { Corresponds to ARGC } X argstr CHAR(80), { Current argument } X arglen INTEGER, { Length of current argument } X chridx INTEGER, { How far through argstr we have analysed } X no_opt INTEGER { Has `--' argument been read? } X X{ XTest program which also indicates how GETOPT(3) is used. X XMAIN X X DEFINE X optval CHAR(1), X optarg CHAR(80) X X CALL i4gl_getopt("abcf:t:x") RETURNING optval, optarg X WHILE optval IS NOT NULL X DISPLAY "optval = ", optval, " optarg = ", optarg CLIPPED X CALL i4gl_getopt("abcf:t:x") RETURNING optval, optarg X END WHILE X XEND MAIN X} X XFUNCTION i4gl_getopt(optstr) X X DEFINE X optstr CHAR(20), { List of options as for GETOPT(3) } X optarg CHAR(80), { Returned option value } X optval CHAR(1), { Returned option flag } X rcs CHAR(1), X idx INTEGER X X { Initialisation } X IF argind <= 0 THEN X LET argind = 1 X LET argnum = NUM_ARGS() X LET chridx = 1 X LET arglen = 0 X LET no_opt = FALSE X LET rcs = "@(#)$Id: getopt.4gl,v 2.4 1999/03/16 17:26:04 jleffler Exp $" X END IF X X LET optarg = NULL X LET optval = NULL X X WHILE TRUE X X { Step to next argument } X IF chridx > arglen THEN X IF argind > argnum THEN X EXIT WHILE X END IF X LET argstr = ARG_VAL(argind) X LET arglen = LENGTH(argstr) X LET argind = argind + 1 X LET chridx = 2 X END IF X X IF argstr[1] != "-" OR no_opt = TRUE OR arglen = 1 THEN X { Non-option argument } X LET optval = "-" X LET optarg = argstr X LET arglen = 0 X EXIT WHILE X END IF X X IF argstr[2] == "-" THEN X { '--' argument. All following arguments are file name arguments } X LET no_opt = TRUE X LET arglen = 0 X CONTINUE WHILE X END IF X X LET optval = argstr[chridx] X LET chridx = chridx + 1 X LET idx = instring(optstr, optval) X IF idx = 0 THEN X { Unknown option } X LET optval = "?" X EXIT WHILE X END IF X X IF optstr[idx+1] != ":" THEN X { Flag-only option } X EXIT WHILE X END IF X X IF arglen <= chridx THEN X { Need to use next argument for value } X LET optarg = ARG_VAL(argind) X LET argind = argind + 1 X LET arglen = 0 { Use next argument next time round } X EXIT WHILE X END IF X X { Value of option attached to current argument } X LET optarg = argstr[chridx,arglen] X LET arglen = 0 { Use next argument next time round } X EXIT WHILE X X END WHILE X X RETURN optval, optarg X XEND FUNCTION {i4gl_getopt} SHAR_EOF if [ `wc -c < getopt.4gl` -ne 3324 ] then echo "Lengths do not match -- Bad Copy of getopt.4gl" fi echo "Done." exit 0