Re: Using getopt() in a 4gl program.
Posted in 1994
>From: bfar@vrx.vhi.com >Subject: Using getopt() in a 4gl program. >Date: Tue, 27 Sep 94 15:18:17 EDT >X-Informix-List-Id: <list.4781> > >I am interested in processing command line switches and arguments >in a 4gl using a C function and the getopt() function. >I can't get this to work because I don't have access to argc and argv[] >in the C function because they are local to main. > >Is there any way to get access to these in a function (make them global)? No. >Is there a better way to process arguments with switches? Yes. You could try the attached 4GL version of getopt... Yours, Jonathan Leffler (johnl@informix.com) #include <disclaimer.h> : "%W% %E%" #!/bin/sh # shar: Shell Archiver (v1.22) # # This is a shell archive. # Remove everything above this line and run sh on the resulting file # If this archive is complete, you will see this message at the end # "All files extracted" # # Created: Tue Sep 27 18:14:08 1994 by johnl at Informix Software Inc. # Files archived in this archive: # getopt.4gl # instring.4gl # if test -f getopt.4gl; then echo "File getopt.4gl exists"; else echo "x - getopt.4gl" sed 's/^X//' << 'SHAR_EOF' > getopt.4gl && X{ X @(#)getopt.4gl 1.4 90/12/17 X @(#)JLSS Informix Tools: General Library X @(#)Simulate GETOPT(3) X @(#)Author: JL X} 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 optind 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 getopt("abcf:t:x") RETURNING optval, optarg X WHILE optval IS NOT NULL X DISPLAY "optval = ", optval, " optarg = ", optarg CLIPPED X CALL getopt("abcf:t:x") RETURNING optval, optarg X END WHILE X XEND MAIN X} X XFUNCTION 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 sccs CHAR(1), X idx INTEGER, X i INTEGER X X { Initialisation } X IF optind <= 0 THEN X LET optind = 1 X LET argnum = NUM_ARGS() X LET chridx = 1 X LET arglen = 0 X LET no_opt = FALSE X LET sccs = "@(#)getopt.4gl 1.4 90/12/17" 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 optind > argnum THEN X EXIT WHILE X END IF X LET argstr = ARG_VAL(optind) X LET arglen = LENGTH(argstr) X LET optind = optind + 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(optind) X LET optind = optind + 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 {getopt} SHAR_EOF chmod 0444 getopt.4gl || echo "$0: failed to restore getopt.4gl" fi if test -f instring.4gl; then echo "File instring.4gl exists"; else echo "x - instring.4gl" sed 's/^X//' << 'SHAR_EOF' > instring.4gl && X{ X @(#)instring.4gl 1.1 90/07/19 X @(#)JLSS Informix Tools: General Library X @(#)Find index of character in string X @(#)Author: JL X} X XFUNCTION instring(str, c) X X DEFINE X str CHAR(80), X c CHAR(1), X sccs CHAR(1), X l INTEGER, X i INTEGER X X LET l = length(str) X X FOR i = 1 TO l X IF str[i] = c THEN X RETURN i X END IF X END FOR X X RETURN 0 X X LET sccs = "@(#)instring.4gl 1.1 90/07/19" X XEND FUNCTION {instring} SHAR_EOF chmod 0444 instring.4gl || echo "$0: failed to restore instring.4gl" fi echo All files extracted exit 0