t18.c 1.1 KB

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