mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-15 17:33:38 +00:00
samples/Mmakefile:
Add the beer program to list of targets.
samples/*.m:
Convert (C->T;E) to (if C then T else E).
Delete trailing whitespace.
Use predmode syntax instead of separate pred and mode
declarations.
samples/interpreter.m:
Fix up one predicate that had some clauses that used
DCGs and others that used state variables.
Don't use the name 'IO' for something that isn't the I/O state.
samples/diff/*.m:
samples/c_interface/c_calls_mercury/mercury_main.m:
samples/c_interface/short_example.m:
Delete trailing whitespace.
95 lines
2.5 KiB
Mathematica
95 lines
2.5 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% 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).
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module cat.
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
:- pred main(io::di, io::uo) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module char.
|
|
:- import_module list.
|
|
:- import_module string.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
main(!IO) :-
|
|
io.command_line_arguments(Args, !IO),
|
|
(
|
|
Args = [],
|
|
cat(!IO)
|
|
;
|
|
Args = [_ | _],
|
|
cat_file_list(Args, !IO)
|
|
).
|
|
|
|
:- pred cat_file_list(list(string)::in, io::di, io::uo) is det.
|
|
|
|
cat_file_list([], !IO).
|
|
cat_file_list([File | Files], !IO) :-
|
|
cat_file(File, !IO),
|
|
cat_file_list(Files, !IO).
|
|
|
|
:- pred cat_file(string::in, io::di, io::uo) is det.
|
|
|
|
cat_file(File, !IO) :-
|
|
io.open_input(File, Result, !IO),
|
|
(
|
|
Result = ok(Stream),
|
|
cat_stream(Stream, !IO),
|
|
io.close_input(Stream, !IO)
|
|
;
|
|
Result = error(Error),
|
|
io.progname("cat", Progname, !IO),
|
|
io.error_message(Error, Message),
|
|
io.write_strings([
|
|
Progname, ": ",
|
|
"error opening file `", File, "' for input:\n\t",
|
|
Message, "\n"
|
|
], !IO)
|
|
).
|
|
|
|
:- pred cat_stream(io.input_stream::in, io::di, io::uo) is det.
|
|
|
|
cat_stream(Stream, !IO) :-
|
|
io.set_input_stream(Stream, _OldStream, !IO),
|
|
cat(!IO).
|
|
|
|
:- pred cat(io::di, io::uo) is det.
|
|
|
|
cat(!IO) :-
|
|
io.read_line_as_string(Result, !IO),
|
|
(
|
|
Result = ok(Line),
|
|
io.write_string(Line, !IO),
|
|
cat(!IO)
|
|
;
|
|
Result = eof
|
|
;
|
|
Result = error(Error),
|
|
io.error_message(Error, Message),
|
|
io.input_stream_name(StreamName, !IO),
|
|
io.progname("cat", ProgName, !IO),
|
|
io.write_strings([
|
|
ProgName, ": ",
|
|
"error reading input file `", StreamName, "': \n\t",
|
|
Message, "\n"
|
|
], !IO)
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|