MODULE TestCopy; (* Copy a textfile with ISO-Modula-2 I/O-Library reading the in- and out-file specification as parameter from the command line. Example: The OpenVMS command $ run testcopy $_ x.lis y.lis copies file x.lis to file y.lis. When installed as a foreign command, the arguments can be specified in the command line: $ testcopy :== $dev:[dir]testcopy.exe $ testcopy x.lis y.lis Copyright (1993-1996) by Guenter Dotzel http://www.modulaware.com/ GD/1993 *) FROM ProgramArgs IMPORT IsArgPresent, ArgChan; FROM IOResult IMPORT ReadResults, ReadResult; FROM SeqFile IMPORT OpenRead, OpenWrite, OpenResults, Close, ChanId, read, write; FROM TextIO IMPORT ReadRestLine, SkipLine, ReadToken, WriteLn, WriteString; IMPORT STextIO, SWholeIO; TYPE fnam= ARRAY [0..254] OF CHAR; VAR in,out: fnam; ores: OpenResults; input, output: ChanId; line: ARRAY[0..8191] OF CHAR; lines: INTEGER; PROCEDURE usage; BEGIN STextIO.WriteString("TestCopy usage: 'testcopy IN.EXT OUT.EXT'"); STextIO.WriteLn; END usage; BEGIN lines:=0; IF IsArgPresent() THEN ReadToken(ArgChan(), in); IF IsArgPresent() THEN ReadToken(ArgChan(), out); OpenRead(input, in, read, ores); IF ores=opened THEN OpenWrite(output, out, write, ores); IF ores=opened THEN ReadRestLine(input, line); WHILE ReadResult(input) <> endOfInput DO IF ReadResult(input) = allRight THEN WriteString(output, line); WriteLn(output); INC(lines); ELSIF ReadResult(input) = endOfLine THEN WriteLn(output); INC(lines); ELSE STextIO.WriteString("TestCopy error reading infile"); STextIO.WriteLn; HALT; END; SkipLine(input); ReadRestLine(input, line); END; Close(input); ELSE STextIO.WriteString("TestCopy can't create outfile"); STextIO.WriteLn; END; Close(output); STextIO.WriteString("TestCopy: endOfInput reached, lines copied="); SWholeIO.WriteInt(lines,0); STextIO.WriteLn; ELSE STextIO.WriteString("TestCopy can't open infile"); STextIO.WriteLn; END; ELSE usage; END; ELSE usage; END; END TestCopy.