1 #include "a.h"
2
3 /*
4 * 18. Insertions from the standard input
5 */
6 void
r_rd(int argc,Rune ** argv)7 r_rd(int argc, Rune **argv)
8 {
9 char buf[100];
10 char *s;
11 Rune *p;
12 Fmt fmt;
13 static int didstdin;
14 static Biobuf bstdin;
15
16 /*
17 * print prompt, then read until double newline,
18 * then run the text just read as though it were
19 * a macro body, using the remaining arguments.
20 */
21 if(fd2path(0, buf, sizeof buf) >= 0 && strstr(buf, "/dev/cons")){
22 if(argc > 1)
23 fprint(2, "%S", argv[1]);
24 else
25 fprint(2, "%c", 7/*BEL*/);
26 }
27
28 if(!didstdin){
29 Binit(&bstdin, 0, OREAD);
30 didstdin = 1;
31 }
32 runefmtstrinit(&fmt);
33 while((s = Brdstr(&bstdin, '\n', 0)) != nil){
34 if(s[0] == '\n'){
35 free(s);
36 break;
37 }
38 fmtprint(&fmt, "%s", s);
39 free(s);
40 }
41 p = runefmtstrflush(&fmt);
42 if(p == nil)
43 warn("out of memory in %Crd", dot);
44 ds(L(".rd"), p);
45 argc--;
46 argv++;
47 argv[0] = L(".rd");
48 runmacro('.', argc, argv);
49 ds(L(".rd"), nil);
50 }
51
52 /* terminate exactly as if input had ended */
53 void
r_ex(int argc,Rune ** argv)54 r_ex(int argc, Rune **argv)
55 {
56 USED(argc);
57 USED(argv);
58
59 while(popinput())
60 ;
61 }
62
63 void
t18init(void)64 t18init(void)
65 {
66 addreq(L("rd"), r_rd, -1);
67 addreq(L("ex"), r_ex, 0);
68 }
69