Files
mercury/extras/posix/samples/hello.m
Julien Fischer dcf7945280 Build the POSIX binding using mmc --make.
Building the POSIX binding using mmc --make by default and just have mmake
forward its work to that.  Using mmc --make means things like library grade
exclusions will work properly.

Exclude non-C grades from the grade set in which to install the POSIX binding.

Shift a POSIX binding sample into the samples subdirectory.

extras/posix/Makefile:
extras/posix/Mercury.options:
    Build the POSIX binding using mmc --make.

extras/posix/Mmakefile:
    Redirect mmake to use the normal Makefile.

extras/posix/hello.m:
    Shift this file into the samples subdirectory.

extras/samples/Makefile:
    Set up things to build the samples against a libposix in the
    parent directory.

extras/samples/Mmakefile:
    Delete this file.
2019-03-21 10:58:08 +00:00

63 lines
1.5 KiB
Mathematica

% vim: ft=mercury ts=4 sw=4 et
:- module hello.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module posix.
:- import_module posix.open.
:- import_module posix.write.
:- import_module bitmap.
:- import_module char.
:- import_module int.
:- import_module list.
:- import_module string.
main(!IO) :-
open("/dev/tty", [wronly], Res0, !IO),
(
Res0 = ok(Fd),
Str = "hello world.\n",
length(Str, Len),
write(Fd, Len, string_to_bitmap(Str), Res1, !IO),
(
Res1 = ok(NWritten),
( NWritten \= Len ->
% We didn't write all of it!
io.write_string("failed to write it all\n", !IO)
;
true
)
;
Res1 = error(Err),
io.write(Err, !IO),
io.nl(!IO)
)
;
Res0 = error(Err),
io.write(Err, !IO),
io.nl(!IO)
).
:- func string_to_bitmap(string::in) = (bitmap::bitmap_uo) is det.
string_to_bitmap(String) = Bitmap :-
NumBytes = string.length(String),
Bitmap0 = bitmap.init(NumBytes * bits_per_byte),
string.to_char_list(String, Chars),
char_list_to_bitmap(Chars, 0, Bitmap0, Bitmap).
:- pred char_list_to_bitmap(list(char)::in, int::in,
bitmap::bitmap_di, bitmap::bitmap_uo) is det.
char_list_to_bitmap([], _, !Bitmap).
char_list_to_bitmap([C | Cs], Index, !Bitmap) :-
char.to_int(C, I),
!:Bitmap = !.Bitmap ^ byte(Index) := I,
char_list_to_bitmap(Cs, Index + 1, !Bitmap).