home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / ARGLIB.ZIP / ARGLIB.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1985-12-28  |  4.3 KB  |  114 lines

  1.  
  2.   (* ---------------------- begin argument module -------------------------- *)
  3.  
  4.   (* ArgLib.pas  16 March 1985.              TURBO  PASCAL  version          *)
  5.  
  6.   (* Get arguments from CP/M or MS-DOS Command Line, using UNIX conventions. *)
  7.   (* Tested on Turbo Pascal ver 1 and 2; must compile to disk (not memory).  *)
  8.   (* Allows writing portable and/or UNIX-compatable programs on your micro.  *)
  9.   (* All compiler-dependencies are marked with the word "Turbo"; mods for    *)
  10.   (*   changing among CP/M-80, CP/M-86, and MS-DOS are marked in "argv".     *)
  11.  
  12.   (* CAUTION:  CP/M and MS-DOS programs must finish ALL argcount and argv    *)
  13.   (*  calls before doing ANY file operations.  (Not required under UNIX.)    *)
  14.  
  15.   (* Example:  User calls program "cp" with two files:  CP FILEA FILEB
  16.    *
  17.    *  argcount  -->   2
  18.    *  argv(1, ) --> 'FILEA           '
  19.    *  argv(2, ) --> 'FILEB           '
  20.    *)
  21.   (* EXPORT:  ArgStrType, argcount, argv, resetOK   *)
  22.  
  23.   (* author:  Willett Kempton *)
  24.  
  25. const
  26.   MaxArgStrLen = 16; (* maximum characters per argument *)
  27. type
  28.   ArgStrType = packed array[1..MaxArgStrLen]of char; (* file name argument *)
  29.  
  30.  
  31. procedure argv(    argn : integer;     (* requesting Nth argument *)
  32.                var name : ArgStrType); (* returning its char image *)
  33. (* ARGument Value.   Valid argn for files are 1..argcount.  *)
  34. (* Note: argv(0, ) incorrectly returns blank; UNIX returns program name *)
  35. const
  36.   MaxCmdLineLength = 122; (* bug: CP/M-80 Turbo v1 & v2 chops to 31 chars *)
  37. type
  38.   CmdLineType = packed array [0..MaxCmdLineLength] of char;
  39.   charset = set of char;
  40. var
  41.   i, nextch, Start, ArgLen, CmdLen : integer;
  42.   SYSdelim : charset; (* system file separators *)
  43.   CmdLine : CmdLineType
  44.       { CP/M-86 Turbo }    { absolute DSeg : $80 ; }
  45.       { MS-DOS Turbo  }    { absolute CSeg : $80 ; }
  46.       { CP/M-80 Turbo }      absolute        $80 ;
  47.  
  48. procedure skip (delims:charset);
  49. (* skip past either delimiters or arguments.  *)
  50. begin
  51.   while (CmdLine[nextch] in delims) and (nextch <= CmdLen) do
  52.     nextch := nextch + 1
  53. end (* skip *);
  54.  
  55. begin (* argv *)
  56.   CmdLen := ord(CmdLine[0]);  (* length of command line  *)
  57.   {if CmdLen > 31 then                                 }  { CP/M-80 Turbo }
  58.   {  begin writeln('Command line too long.'); Halt end;}  { CP/M-80 Turbo }
  59.   SYSdelim :=
  60. { CP/M   }   [' ',',',';','[',']','=','<','>' ,'/','_' ];
  61. { MS-DOS } { [' ',',',';','[',']','=','<','>'  ]; } (* more?? *)
  62.   nextch := 1;
  63.   Start := 1;
  64.   for i := 1 to argn do
  65.     begin
  66.       skip(SYSdelim)    (* skip leading delimiters *);
  67.       Start := nextch   (* overwriting all but last value *);
  68.       skip([chr(0)..chr(127)]-SYSdelim) (* skip argument *);
  69.       { Turbo bug prohibits [chr(0)..chr(255)]; thus 8-bit chars disallowed }
  70.     end;
  71.   ArgLen := nextch-Start;
  72.   (* now use Start and ArgLen to set the string *)
  73.   if ArgLen > MaxArgStrLen then ArgLen := MaxArgStrLen;
  74.   for i:= 1 to ArgLen do name[i] := CmdLine[Start-1+i];
  75.   for i:= (ArgLen+1) to MaxArgStrLen do name[i] := ' '; (* blank fill *)
  76. end (* argv *);
  77.  
  78.  
  79.   function argcount : integer;
  80.     (* ARGument COUNT:  Number of arguments on command line  *)
  81.     (* This is slow, so call it just once and set an integer variable. *)
  82.   var
  83.     c: integer;
  84.     ArgStr: ArgStrType;
  85.    begin
  86.      c := 0;
  87.      repeat
  88.        c := c+1;
  89.        argv(c, ArgStr);
  90.      until (ArgStr[1] = ' ');
  91.      argcount := c - 1;
  92.    end (* argcount *);
  93.  
  94.   function argc : integer;
  95.     (* Count of all arguments, including program name.  Preferred call is
  96.       "argcount"; this is retained for compatability with earlier version. *)
  97.   begin
  98.     argc := argcount + 1;
  99.   end;
  100.  
  101.  
  102.   function resetOK (var f: text; name: ArgStrType) : boolean;
  103.     (* Associate name with file variable.  Return true if file exists and
  104.       is nonempty. *)
  105.    begin
  106.      assign(f,name);   { Turbo, non-standard }
  107.     {$I-  Turbo: turn off I/O checks, or reset and eof() can cause crash. }
  108.      reset(f);
  109.      resetOK := (IOresult = 0);   { Turbo, non-standard }
  110.     {$I+  Turbo: restore I/O checks }
  111.    end (* resetOK *);
  112.  
  113.   (* ------------------------ end argument module ------------------------- *)
  114.