88 stop /1 ,
99 argparse_def /1 ,
1010 display_help /1 ,
11+ format /3 ,
1112 start_record_stream /4 ,
1213 push_new_record /3 ,
13- end_record_stream /2 ]).
14+ end_record_stream /2 ,
15+ read_file /2 ]).
1416-export ([init /1 ,
1517 handle_call /3 ,
1618 handle_cast /2 ,
@@ -48,13 +50,29 @@ argparse_def(record_stream) ->
4850 default => plain ,
4951 help => " Format output acccording to <FORMAT>" }
5052 ]
53+ };
54+ argparse_def (file_input ) ->
55+ #{arguments =>
56+ [
57+ #{name => input ,
58+ long => " -input" ,
59+ short => $i ,
60+ type => string ,
61+ nargs => 1 ,
62+ help => " Read input from file <FILE>" }
63+ ]
5164 }.
5265
5366display_help (#{io := {transport , Transport }} = Context ) ->
5467 Transport ! {io_cast , {? FUNCTION_NAME , Context }};
5568display_help (#{io := IO } = Context ) ->
5669 gen_server :cast (IO , {? FUNCTION_NAME , Context }).
5770
71+ format ({transport , Transport }, Format , Args ) ->
72+ Transport ! {io_cast , {? FUNCTION_NAME , Format , Args }};
73+ format (IO , Format , Args ) ->
74+ gen_server :cast (IO , {? FUNCTION_NAME , Format , Args }).
75+
5876start_record_stream ({transport , Transport }, Name , Fields , ArgMap ) ->
5977 Transport ! {io_call , self (), {? FUNCTION_NAME , Name , Fields , ArgMap }},
6078 receive Ret -> Ret end ;
@@ -74,6 +92,14 @@ end_record_stream({transport, Transport}, #{name := Name}) ->
7492end_record_stream (IO , #{name := Name }) ->
7593 gen_server :cast (IO , {? FUNCTION_NAME , Name }).
7694
95+ read_file ({transport , Transport }, ArgMap ) ->
96+ Transport ! {io_call , self (), {? FUNCTION_NAME , ArgMap }},
97+ receive Ret -> Ret end ;
98+ read_file (IO , ArgMap )
99+ when is_pid (IO ) andalso
100+ is_map (ArgMap ) ->
101+ gen_server :call (IO , {? FUNCTION_NAME , ArgMap }).
102+
77103init (#{progname := Progname }) ->
78104 process_flag (trap_exit , true ),
79105 State = #? MODULE {progname = Progname },
@@ -91,6 +117,9 @@ handle_call(
91117 {ok , State2 } = format_record_stream_start (Name , State1 ),
92118
93119 {noreply , State2 };
120+ handle_call ({read_file , ArgMap }, From , State ) ->
121+ {ok , State1 } = do_read_file (ArgMap , From , State ),
122+ {noreply , State1 };
94123handle_call (stop , _From , State ) ->
95124 {stop , normal , ok , State };
96125handle_call (_Request , _From , State ) ->
@@ -106,6 +135,9 @@ handle_cast(
106135 Help = argparse :help (ArgparseDef , Options ),
107136 io :format (" ~s~n " , [Help ]),
108137 {noreply , State };
138+ handle_cast ({format , Format , Args }, State ) ->
139+ io :format (Format , Args ),
140+ {noreply , State };
109141handle_cast ({push_new_record , Name , Record }, State ) ->
110142 {ok , State1 } = format_record (Name , Record , State ),
111143 {noreply , State1 };
@@ -249,3 +281,23 @@ isatty(IoDevice) ->
249281 _ ->
250282 false
251283 end .
284+
285+ do_read_file (#{input := " -" }, From , State ) ->
286+ Ret = read_stdin (<<>>),
287+ gen :reply (From , Ret ),
288+ {ok , State };
289+ do_read_file (#{input := Filename }, From , State ) ->
290+ Ret = file :read_file (Filename ),
291+ gen :reply (From , Ret ),
292+ {ok , State }.
293+
294+ read_stdin (Buf ) ->
295+ case file :read (standard_io , 4096 ) of
296+ {ok , Data } ->
297+ Buf1 = [Buf , Data ],
298+ read_stdin (Buf1 );
299+ eof ->
300+ {ok , Buf };
301+ {error , _ } = Error ->
302+ Error
303+ end .
0 commit comments