t18.c 1.5 KB

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