p.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #define DEF 22 /* lines in chunk: 3*DEF == 66, #lines per nroff page */
  13. Biobuf *cons;
  14. Biobuf bout;
  15. int pglen = DEF;
  16. void printfile(int);
  17. void
  18. main(int argc, char *argv[])
  19. {
  20. int n;
  21. int f;
  22. if((cons = Bopen("/dev/cons", OREAD)) == 0) {
  23. fprint(2, "p: can't open /dev/cons\n");
  24. exits("missing /dev/cons");
  25. }
  26. Binit(&bout, 1, OWRITE);
  27. n = 0;
  28. while(argc > 1) {
  29. --argc; argv++;
  30. if(*argv[0] == '-'){
  31. pglen = atoi(&argv[0][1]);
  32. if(pglen <= 0)
  33. pglen = DEF;
  34. } else {
  35. n++;
  36. f = open(argv[0], OREAD);
  37. if(f < 0){
  38. fprint(2, "p: can't open %s - %r\n", argv[0]);
  39. continue;
  40. }
  41. printfile(f);
  42. close(f);
  43. }
  44. }
  45. if(n == 0)
  46. printfile(0);
  47. exits(0);
  48. }
  49. void
  50. printfile(int f)
  51. {
  52. int i, j, n;
  53. char *s, *cmd;
  54. Biobuf *b;
  55. b = malloc(sizeof(Biobuf));
  56. Binit(b, f, OREAD);
  57. for(;;){
  58. for(i=1; i <= pglen; i++) {
  59. s = Brdline(b, '\n');
  60. if(s == 0){
  61. n = Blinelen(b);
  62. if(n > 0) /* line too int32_t for Brdline */
  63. for(j=0; j<n; j++)
  64. Bputc(&bout, Bgetc(b));
  65. else{ /* true EOF */
  66. free(b);
  67. return;
  68. }
  69. }else{
  70. Bwrite(&bout, s, Blinelen(b)-1);
  71. if(i < pglen)
  72. Bwrite(&bout, "\n", 1);
  73. }
  74. }
  75. Bflush(&bout);
  76. getcmd:
  77. cmd = Brdline(cons, '\n');
  78. if(cmd == 0 || *cmd == 'q')
  79. exits(0);
  80. cmd[Blinelen(cons)-1] = 0;
  81. if(*cmd == '!'){
  82. if(fork() == 0){
  83. dup(Bfildes(cons), 0);
  84. execl("/bin/rc", "rc", "-c", cmd+1, nil);
  85. }
  86. waitpid();
  87. goto getcmd;
  88. }
  89. }
  90. }