Previous Table of Contents Next


5.2.11.1. Executing Another Program

Executing another program from your program can be a simple matter, if you keep in mind the properties of DOS. The DOS command interpreter is always found defined in the environment variable COMSPEC. After spawning a command interpreter, which is what happens in Listing 5.42, the /C command parameter must be given before the command line desired. The command interpreter doesn’t have to be spawned for specific programs, but for batch files and any command that uses any DOS features (such as redirection), the command interpreter must be called.

As shown in the listing, the first requirement is the presence of the $M compiler directive, which states the size of the stack, minimum heap, and maximum heap. The maximum heap defaults to all the memory available, leaving none to execute a program. Therefore, the maximum heap value must be defined downward.

Next is the actual call of the command. A call to swapvectors must exist before and after the exec() call. This procedure swaps the contents of the interrupt table, so an executed program cannot disrupt what the current program has done.

getenv() is a function that returns the contents of any environment variable specified as a parameter, if it exists. Here, this function is used with the environment variable COMSPEC, which is the only proper accepted method to call the command interpreter.

exec() takes the name of the program to spawn to (COMMAND.COM or another one) and then a proper parameter list (or command if COMMAND.COM is used, because the command you want to issue is a parameter to COMMAND.COM). The result of a proper call to exec() is execution of the program specified with all text related to the program being displayed.

All functions and procedures related to executing another program make use of the DOS unit.

Listing 5.42. A demonstration of shelling to execute another program.

   {$M 16382,0,4000}
   program fig42; uses dos;
     { Demonstration of executing another program }
     var
       command: string;
     begin
       write(‘Enter your DOS command line here: ’);
       readln(command);
       command := ‘ /C ’ + command; { requirement for COMMAND.COM }
       swapvectors;
       exec(getenv(‘COMSPEC’), command);
       swapvectors;
       {an example of a command line, if it were DOS issued, would be
       c:\dos\command.com /c chkdsk /f }
       writeln;
       writeln(‘End of program execution.’);
     end.

5.2.11.2. Confirming a File’s Existence

The existence of a file on a drive can be determined in one of two ways, as described in Listing 5.43, with the two sample functions, in the order described. One is faster than the other for obvious reasons (presence of disk I/O), but both are in widespread use.

The first method generally involves testing a file attempting to open it using the IOResult variable after turning off I/O checking ({$I-} {$I+}). If it is not equal to 0, then a problem accessing the file is assumed.

The second method is generally faster, as it doesn’t require as much disk I/O. It generally involves using any of the attribute determination functions in the DOS unit (which are described later) and checking the variable DosError for the value 2.

Listing 5.43. Demonstration of two methods to find whether a file exists.

   function slowexist(filename: string):boolean;
     var
       f: text;
     begin
       assign(f, filename);
       {$I-}
       reset(f);
       {$I+}
      if IOResult <> 0 then
        slowexist := false
      else
        begin
          slowexist := true;
          close(f); { required, since IOresult = 0, file is now OPEN }
        end;
   end;

 function fastexist(filename: string): boolean;
   var
     attr: word;
     f: text;
   begin
     assign(f, filename);
     getfattr(f, attr);
     if doserror = 2 then
       fastexist := false
     else
       fastexist := true;
   end;

5.2.11.3. Accepting Command-Line Parameters

The functions paramstr() and paramcount are used in taking parameters from the command line for a program. In DOS batch file writing, valid parameters are %1 through %9, and in the same method, paramstr(number) returns a parameter equivalent to %number. paramcount holds the total number of parameters specified. Each part of the command line that has a space is considered a parameter.

In processing, if command-line parameters are expected, paramcount is called first, and then if paramcount is 0, then generally a call to halt() is issued, but not always. halt() terminates the program with a DOS error equivalent to the number in the parameter. Afterward, all valid paramstr() calls are returned into a string, and proper actions are taken depending on the parameters. An example of a proper way to handle command-line parameters is shown in Listing 5.44.

Listing 5.44. A demonstration of command-line parameters.

   {$M 16382,0,4000}
   program fig44; uses dos;
     { Demonstration of parameter strings. Note the DOS unit is *NOT*
       required for the parameter systems. }
     var
       command: string;
       i: byte;
     begin
       if paramcount <> 0 then
         begin
           command := ‘ /C ’;
           for i := 1 to paramcount do
             command := command + paramstr(i);
           swapvectors;
           exec(getenv(‘COMSPEC’), command);
           swapvectors;
           writeln;
           writeln(‘End of program execution.’);
         end
       else
         begin
           writeln(‘Incorrect # of parameters specified.’);
           halt(10);
         end;
     end.


Previous Table of Contents Next