plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

t18.c (1055B)


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