ArgLib - A Portable Pascal File Argument Library 11 October 1984 Willett Kempton What ArgLib Does ---------------- Arglib provides a portable means of reading file arguments from the operating system command line into a Pascal program. It also allows assignment of a name to a file variable and detection of empty files. ArgLib became necessary because almost every company's version of Pascal has implemented these functions in a different way. By calling ArgLib, rather than writing system-specific code in each program, software can be more easily moved across compilers, operating systems and computers. ArgLib is currently available for: 1. Turbo Pascal, CP/M-80, CP/M-86, MS-DOS 2. Pascal/MT+, Pascal/MT+86 3. Berkeley UNIX Pascal, pi, pc Specifics on these implementations follow: 1. ArgLib.pas: Turbo pascal. Runs on either 8 or 16 bit Turbo Pascal, under CP/M-86 and MS-DOS. Tested with Turbo CP/M-80 (V 1.0) and CP/M-86 (V 1.0 and V 2.0) on a DEC Rainbow. Requires a minor modification (marked in the code) to switch between 8- and 16-bit systems. Also, a bug in 8-bit Turbo truncates any command line arguments past 31 characters. 2. ArgLibMT.pas: Pascal/MT+86 and Pascal/MT+. Tested with MT+86 V 3.1. Note that in the MT+ version, "argc" must be called prior to "argv". This is the typical calling sequence anyway, but is REQUIRED under MT+ systems. 3. ArgLibUX.pas: Berkeley Pascal for BSD 4.x UNIX. Tested on version 3.0, using both the pi interpreter and the pc compiler on a VAX 11/750 running BSD 4.1. Procedures avaliable -------------------- argc: Argument count. Function returning an integer count of how many arguments were given on the command line. argv(count,name): Argument Value. Procedure setting the variable "name" to argument number "count" from the command line. resetOK(file,name): Assign, reset, and check. Assign the file variable "file" to read from the external file or device named "name". If the external file is nonexistant or empty, return false. Otherwise return true. Calling specifics are as follows, and are also documented in the source code. For example, suppose that the user calls program "cp" with two files: A>CP FILEA FILEB argc --> returns the value 3 argv(1,name) --> sets "name" to 'FILEA ' argv(2,name) --> sets "name" to 'FILEB ' resetOK(f,name) --> resets f for reading from "name"; if nonempty, return TRUE Note that the last file is argc-1, not argc. In the example, argv(3,name) would be undefined. Also note that under UNIX, argv(0,name) would set "name" to 'CP', but it the micro version it sets "name" to blank. This numbering system is not the clearest possible; it is used to be consistent with UNIX numbering. Most pascal systems will require ArgLib to be included after the last variable of the main program and before the first procedure. After the include, the following symbols are available. Symbols available to calling program const MaxArgStrLen = 16; (* max chars per argument; VARIES WITH OS! *) type ArgStrType = packed array [1..MaxArgStrLen] of char; procedure argv(argn : integer; var name : ArgStrType); function argc : integer; function resetOK (var f: text; name: ArgStrType) : boolean; Design tradeoffs were as follows. Execution speed was sacrificed for compact code, since these routines will typically only be called once. Standard Pascal (ISO standard) was used whenever possible (no 'string', etc.). (Berkeley UNIX Pascal provides no string type.) Testing ArgLib -------------- A test program is provided with ArgLib. It is called ArgTest.pas and it includes ArgLib into its source. The following is a session using argtest on CP/M: F>dir F: SMALL : EMPTY : ARGTEST.CMD F>type empty F>type small this is a small file F>type nothing NO FILE F>argtest empty small nothing argc=4 file arguments=3 0= empty 1= EMPTY empty 2= SMALL exists 3= NOTHING empty F> Note that both the empty and the nonexistant files are detected. Hints for Portable Code ----------------------- Some of the programs you write will only be used a few times, or by their nature, will only be used on one system. These need not be portable. Other programs will retain their value over a period of years. During those years, you are likely to use different computers, and almost certain to use several different compilers. To preserve your software-writing investment, you want to be able to transfer your programs without having to re-write them for each system. Programs which allow easy transfer are considered "portable". For those programs which you want to be portable, do not use the compiler manufacturer's manual to guide your writing. Use a manual on "Standard Pascal", which is a subset of most commercial Pascals. The Standard is defined by ISO, the International Standards Organization, and by ANSI, the American member of ISO. The defining ISO document (ISO dp7185, December 1980), is precise but difficult to read. A readable and easy-to-understand description of Standard Pascal can be found in the book: Standard Pascal, User Reference Manual, by Doug Cooper, 1983, W. W. Norton & Company. Cooper's book is preferable to the earlier Jensen & Wirth textbook (1974), which is less readable and partially inconsistent with the final ISO standard. Sometimes, system dependent features will be necessary, for example in getting file names from the operating system command line. When they are unavoidable, put them in a few routines which can be called in a standard way. Then to change all your software for a new system, you need only change a few routines. ArgLib is an example use of this strategy.