home *** CD-ROM | disk | FTP | other *** search
- { (C) Copyright 1986-1992 MetaWare Incorporated; Santa Cruz, CA 95060. }
-
- pragma C_include('implement.pf');
- package Exec;
- pragma Routine_aliasing_convention(Implement.RTE_aliasing);
- with Loopholes:[Address];
-
- -- This package defines both the Pascal and C version of the exec
- -- routines. The Pascal libraries contain all but c_command.
- -- The C libraries contain only the c_xxx routines.
- -- For instructions on how to use these routines, see the end of this file.
- #ifdef CDOS
- type Exec_return = Longint;
- #else
- type Exec_return = Integer;
- #endif
-
- function Exec (const Progname, Commandstring:String):Exec_return; external;
-
- #ifndef CDOS
- function Exec_env
- (const Progname, Commandstring, Environment: String):Exec_return; external;
- #endif
-
- -- Execute "command -C S" where S is an arbitrary string you supply.
- -- We use -C or /C depending upon the setting of the switch character.
- -- We determine where command is by obtaining comspec from the environment.
- -- For MS-DOS:
- -- UNFORTUNATELY MS-DOS's command does NOT propagate the return code
- -- of the command executed, so that the return code is that of command.com,
- -- not of the command executed.
- function Command(const S:String):Exec_return; External;
-
- #ifndef CDOS
- -- C versions. The first parameter is a nul-terminated string.
- -- For C_exec_env, the length of the environment must be passed in.
- function C_exec_env
- (Progname, Commandstring, Environment: Address; Envlen:Cardinal):Exec_return;
- external;
- function C_exec(Progname, Commandstring: Address):Exec_return; External;
- -- "C_command" in C is known as "System" and is in the standard library.
- -- Internally used function ExecX. Prog is a nul-terminated string,
- -- and Envseg:0 points to the environment.
- function ExecX(Prog,Parms:Address; Envseg:Cardinal):Exec_return; External;
- #endif
- end;
-
- #if 0
- How to EXEC subprocesses in MetaWare Pascal.
-
- There are two parts to this documentation: an explanation of how the
- functions work, and an explanation of memory requirements. Be sure to
- read the latter, no matter which function you intend to use.
-
- You can use the library functions Exec, Exec_env, and Command for executing
- subprocesses (to completion), and obtaining their return code. (On DOS this
- is the return code from COMMAND.COM; unfortunately it't only whether or not
- the program loaded.)
-
- Command takes an arbitrary string S and passes it to COMMAND.COM for execution.
- For example:
-
- Command('ls *.p');
-
- is the same as if you had typed
-
- command -C ls *.p
-
- at a DOS prompt. This only works if the COMSPEC environment variable is
- properly set to the path for COMMAND.COM.
-
- Exec takes a program name to execute (you must specify the program
- name completely; no searching of the environment variable PATH is done) and
- a parameter string.
-
- The parameter string S is at most 129 bytes long. The first byte is the
- number of characters following the first byte. For example:
-
- var S:String(10); Rtn:Exec_return;
- begin
- S := chr(5) || '*.asm';
- Rtn := Exec('/bin/ls.exe',S);
- -- Rtn holds whatever ls.exe returned at termination.
- end;
-
- Exec_env is the same as Exec except it also takes an environment string.
- The environment string is of the form
- S* 0 (S* means zero or more S's)
- where each S is of the form
- character-string 0
- and each character-string of the form
- somename=sometext
- "somename" is the name of the environment variable; sometext is its value.
- See the "set" command in your DOS manual, and the description of the
- environment in the DOS technical reference manual.
- For example:
-
- var Env:string(50); Rtn:Exec_return;
- begin
- Env := 'VAR1=a b' || chr(0) || 'VAR2=xyz' || chr(0)
- || 'COMSPEC=c:\command.com' || chr(0) || chr(0);
- Rtn :=
- Exec_env('c:\command.com',
- chr(9) || '/C set' || chr(#0d) || ' ' || chr(0)),
- Env);
- end;
-
- executes command.com with the instruction to print out the environment
- ("set"). The environment printed will contain just VAR1, VAR2, and COMSPEC
- (variables should be in upper case to conform to DOS conventions).
- Also, if you omit COMSPEC from the environment, COMMAND.COM will hang.
- This appears to be a DOS bug.
-
- Note the extra characters chr(#0d) || ' ' || chr(0). Apparently command.com
- has a bug in it in that it expects those characters to be there; it won't
- work without them. We have found this to be the case with some other
- software as well -- in one particular case, a networking program net.exe.
- If you have trouble using Exec or Exec_env directly, try appending those
- three characters to see if the program being exec-ed suffers from the
- same bug.
-
-
- MEMORY REQUIREMENTS FOR EXEC-ing PROGRAMS:
-
- For EXEC to work in small-data memory models (small and medium) nothing
- need be done. However, note that by default 64K of stack and heap
- will be consumed by a small-data-model program, and the rest freed to
- DOS for use in EXECing subprograms. To reduce the 64K to less, you must
- use the CPARMAXALLOC parameter to the Microsoft linker when linking the
- program. CPARMAXALLOC is the number of paragraphs you want to use
- for both heap and stack in your program.
- For example, /CPARMAXALLOC:100 specifies only 100 paragraphs
- (= 1600 bytes) of memory to be made available; the default is FFFF
- (= 1 megabyte), so that the maximum memory is allocated. You can
- also change this value at a late date by modifying locations C and D (hex)
- of the .exe file; therein is stored CPARMAXALLOC.
-
- For EXEC to work in non-small-data memory models (compact, big, large),
- you must provide that some memory is given back to DOS. Again, you
- can use CPARMAXALLOC to reduce the memory requirements. If your program's
- memory requirements don't vary much, this is an acceptable solution.
- However, if your program could effectively use all of memory sometimes,
- then you need a solution that allows use of all of memory but takes that
- memory only as it needs it, leaving the rest for EXECing programs.
- This second solution is obtained by specifying that
- the run-time system should
- (a) allocate a stack of fixed size,
- (b) free all other memory back to DOS, and finally
- (c) allocate any heap by calling DOS to get the memory.
- To do this you will need
- the Microsoft MASM assembler to re-assemble the initializer, and you
- will need to link in an alternative memory allocation routine rather
- than the default one supplied in the library. Do the following to
- the initiliazer source init.asm in the lib/src subdirectory:
-
- a. Set variable STACK_SIZE to whatever size you want the stack.
- (Normally the stack size is dependent only upon available memory
- and the architecture-imposed restriction of 64K max.)
- For example, "STACK_SIZE = 2000" bytes.
- (For more information on STACK_SIZE, see initializer source.)
- b. Set variable USE_DOS_ALLOC (to anything). For example,
- "USE_DOS_ALLOC = 1".
- (For more information on USE_DOS_ALLOC, see initializer source.)
- c. Assemble the initializer from the appropriate model subdirectory
- so that the memory model information is set properly.
- d. Link your program with "dosalloc.obj" included (the Pascal source to
- dosalloc.p is provided, and for the C compiler, a version of
- dosalloc.obj is provided for the compact, big, and large models),
- and with the newly-assembled initializer.
-
- The primary disadvantage of using DOS to allocate memory is that
- if your program erroneously writes out of bounds of allocated memory,
- you will clobber DOS's memory links and it will print out
- "memory allocation error" and HALT THE OS, requiring a reboot, with
- no chance of debugging your program.
-