RMANGALD@CLARKU.BITNET (08/20/87)
$! Hello, INFO-VAX'ers! $! $! I've collected so much stuff from the net -- for $! which thanks to all the contributors -- that I think it is time $! I contributed something. So, here it is: nothing fancy (hey, $! it's my first contribution!), but still quite useful. $! "getargs()" is a Pascal routine which returns the arguments on $! the command line used to invoke the program; it is modeled after $! the "argc/argv" mechanism of C. You call "getargs()", which $! takes an array of character strings as its only argument. It $! returns the number of arguments as its function value, while the $! actual arguments are stuffed into the array you supplied. $! $! There are no restrictions on the number of arguments or $! the length of each, except those imposed by DCL (i.e., the $! command line cannot exceed 1,024 characters). If your array is $! too small, "getargs()" returns -1. The standard DCL conversions $! apply: everything in double quotes is left alone, while $! everything outside of them is converted to uppercase (I can't $! help it -- DCL converts it to uppercase before I can get at it). $! To include a double quote, repeat it (i.e., ""). $! $! To include the code in your program at link-time, $! extract (just "@" this message after extracting it $! "/noheader") it and compile it to produce an .OBJ file. Then, $! in your program, declare it as an external function: $! $! function getargs( $! var arg: array [l1..u1] of varying[u2] of char $! ): integer; $! extern; $! $! Compile your program; at link-time, include the .OBJ file on the $! "link..." command line. $! $! To include it at compile-time, extract the code and $! remove the statements "module mod$getargs;" and "end.", from the $! beginning and end. Then, in your program, insert: $! $! %include 'getargs' (* or whatever you named the file *) $! $! To call the routine, declare your array: $! $! arg: array[<min>..<max>] of $! varying[<maxarglen>] of char; $! $! and the variable to hold the number of arguments: $! $! numargs: integer; $! $! and call "getargs()": $! $! numargs := getargs(arg); $! $! I hope you find this little routine useful! $! $! -------------------------------- $! $ crea getargs.pas $ deck (* GETARGS.PAS * * by * * Rahul Mangaldas (rmangaldas@clarku.bitnet) * Box 1311, Clark University * Worcester, Massachusetts 01610-1477 * * This software is provide "as is", without warranty of any kind, * either expressed or implied, including, but not limited to, the * implied warranties of merchantability and fitness for a * particular purpose. *) module mod$getargs; type word = 0..65535; [external] function lib$get_foreign( %descr cmdlin: varying[u1] of char; %descr prompt: varying[u2] of char := %immed 0; %ref cmdlen: [word] word; %ref force: integer := %immed 0 ): integer; extern; [global] function getargs( var arg: array[l1..u1: integer] of varying[u2] of char ): integer; var done: boolean; cmdlen: [word] word; i, j, k: integer; cmdlin: varying[1024] of char; begin lib$get_foreign(cmdlin,, cmdlen,); if (cmdlen = 0) then begin j := 0; k := l1; end (* if *) else begin i := 1; j := 1; k := j + l1 - 1; arg[k] := ''; while (i <= cmdlen) and (k <= u1) do begin if (cmdlin[i] = '"') then begin i := i + 1; done := i > cmdlen; while (not done) do begin while (i <= cmdlen) and (not done) do begin if (cmdlin[i] <> '"') then begin arg[k] := arg[k] + cmdlin[i]; i := i + 1; end (* if *) else begin i := i + 1; done := true; end; (* else *) end; (* while *) done := true; if (i <= cmdlen) then if (cmdlin[i] = '"') then begin arg[k] := arg[k] + '"'; done := false; i := i + 1; end (* if *) else if (cmdlin[(i - 1)] = '"') then arg[k] := arg[k] + '"'; end; (* while *) end (* if *) else if (cmdlin[i] = ' ') or (cmdlin[i] = chr(9)) then begin i := i + 1; j := j + 1; k := j + l1 - 1; arg[k] := ''; while (cmdlin[i] = ' ') or (cmdlin[i] = chr(9)) do i := i + 1; end (* else if *) else begin arg[k] := arg[k] + cmdlin[i]; i := i + 1; end; (* else *) end; (* while *) end; (* else *) if (k <= u1) then getargs := j else getargs := -1; end; (* getargs() *) end. (* module *) $ eod $ exit -------------------------------- Rahul Mangaldas (rmangaldas@clarku.bitnet) Box 1311, Clark University 950 Main Street Worcester, MA 01610-1477