Files
mercury/samples/cat.m
Andrew Bromage 586c5e16c6 Implement io__read_line_as_string/{3,4}.
Estimated hours taken: 10

library/io.m:
library/io.nu.nl:
	Implement io__read_line_as_string/{3,4}.

	Also sneaked in here are some trivial whitespace fixes in some
	of the pragma c_code which did not comply with our coding
	standards (to do with type casting).

samples/cat.m:
samples/sort.m:
	Use io__read_line_as_string.

tests/general/Mmakefile:
tests/general/read_line_as_string.exp:
tests/general/read_line_as_string.m:
	Test case for io__read_line_as_string.

NEWS:
	Mention the above change.
1998-09-09 06:05:30 +00:00

86 lines
2.1 KiB
Mathematica

%-----------------------------------------------------------------------------%
:- module cat.
% Simple implementation of the standard unix `cat' filter:
% copy input files (or stdin, if no input files) to stdout.
% This source file is hereby placed in the public domain. -fjh (the author).
%-----------------------------------------------------------------------------%
:- interface.
:- import_module io.
:- pred main(io__state::di, io__state::uo) is det.
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module string, list, char.
main -->
io__command_line_arguments(Args),
( { Args = [] } ->
cat
;
cat_file_list(Args)
).
:- pred cat_file_list(list(string)::in, io__state::di, io__state::uo) is det.
cat_file_list([]) --> [].
cat_file_list([File | Files]) -->
cat_file(File),
cat_file_list(Files).
:- pred cat_file(string::in, io__state::di, io__state::uo) is det.
cat_file(File) -->
io__open_input(File, Result),
( { Result = ok(Stream) },
cat_stream(Stream)
; { Result = error(Error) },
io__progname("cat", Progname),
{ io__error_message(Error, Message) },
io__write_strings([
Progname, ": ",
"error opening file `", File, "' for input:\n\t",
Message, "\n"
])
).
:- pred cat_stream(io__input_stream::in, io__state::di, io__state::uo) is det.
cat_stream(Stream) -->
io__set_input_stream(Stream, _OldStream),
cat.
:- pred cat(io__state::di, io__state::uo) is det.
cat -->
io__read_line_as_string(Result),
cat_2(Result).
:- pred cat_2(io__result(string)::in, io__state::di, io__state::uo) is det.
cat_2(Result) -->
( { Result = ok(Line) },
io__write_string(Line),
io__read_line_as_string(NextResult),
cat_2(NextResult)
; { Result = eof }
; { Result = error(Error) },
{ io__error_message(Error, Message) },
io__input_stream_name(StreamName),
io__progname("cat", ProgName),
io__write_strings([
ProgName, ": ",
"error reading input file `", StreamName, "': \n\t",
Message, "\n"
])
).
%-----------------------------------------------------------------------------%