(* 
        This module imports a procedure that interprets
        the command line and builds names for input
        source file and the error file.

        Created: 3/29/87 by Richie Bielak

        Modified:

        Copyright (c) 1987 by Richie Bielak.
        
        This program may be freely copied, but please leave
        my name in.....Thanks....Richie

*)
IMPLEMENTATION MODULE GetFileNames;

FROM CommandLine IMPORT GetCL, CLStrings;
FROM Strings     IMPORT Length, Concat, InitStringModule;

(* +++++++++++++++++++++++++++++++++++++++++++++ *)
(* This procedure splits a file into a name and  *)
(* an extension. If no extension was present a   *)
(* default extension is returned.                *)
PROCEDURE SplitName (   instr : ARRAY OF CHAR;
                     VAR fname : ARRAY OF CHAR;
                     VAR ext   : ARRAY OF CHAR;
                     dext      : ARRAY OF CHAR);
  VAR
    i, j : CARDINAL;
    state : (CopyName, CopyExt, CopyDefExt);
  BEGIN
    i := 0; j := 0; state := CopyName;
    LOOP
      CASE state OF
        CopyName:
          IF i >= Length (instr) THEN
            state := CopyDefExt; fname [i] := 0C;
          ELSIF instr[i] = '.' THEN 
            state := CopyExt; fname [i] := 0C;
          ELSE  
            fname [i] := instr[i];
          END;
          INC (i);
        |
        CopyExt:
          IF i >= Length (instr) THEN
            ext [j] := 0C; EXIT 
          END;
          ext [j] := instr [i]; INC (i); INC (j);
        |
        CopyDefExt:
          IF j >= Length (dext) THEN 
            ext [j] := 0C;  EXIT
          END;
          ext [j] := dext [j]; INC (j);
      END
    END
  END SplitName;

(* +++++++++++++++++++++++++++++++++++++++++++++ *)
(* This procedure makes a full file name out of  *)
(* name and extension.                           *)
PROCEDURE MakeFileName (VAR fullname : ARRAY OF CHAR;
                        name, ext : ARRAY OF CHAR);
  
  BEGIN
    Concat (name, ".", fullname);
    Concat (fullname, ext, fullname);
  END MakeFileName;

(* +++++++++++++++++++++++++++++++++++++++++++++ *)
PROCEDURE ProcessCommandLine (VAR infile, outfile, errfile : ARRAY OF CHAR;
                              VAR succ : BOOLEAN);
  VAR
    argc, i : CARDINAL;
    argv : ARRAY [0..3] OF CLStrings;
    DEF : BOOLEAN;
    fname : CLStrings;
    ext   : ARRAY [0..5] OF CHAR;

  BEGIN
    (* Get the command line arguments *)
    succ := GetCL (argc, argv);
    IF succ AND (argc > 0) THEN
       (* At least an input file provided *)
       SplitName (argv[0], fname, ext, "MOD");
       MakeFileName (infile, fname, ext);
       (* Make the name of the error file *)
       IF CAP(ext[0]) = "D" THEN
         MakeFileName (errfile, fname, "ERD");         
       ELSE
         MakeFileName (errfile, fname, "ERM");
       END;
       (* Now check for an output file *)
       IF argc > 1 THEN
         FOR i := 0 TO Length (argv[1]) DO outfile[i] := argv[1,i] END;
       ELSE
         outfile[0] := 0C;
       END;
    (* No arguments entered *)
    ELSE
       succ := FALSE
    END;
  END ProcessCommandLine;

BEGIN
  InitStringModule
END GetFileNames.
