input.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /*
  10. *
  11. * debugger
  12. *
  13. */
  14. #include "defs.h"
  15. #include "fns.h"
  16. Rune line[LINSIZ];
  17. extern int infile;
  18. Rune *lp;
  19. int peekc,lastc = EOR;
  20. int eof;
  21. /* input routines */
  22. eol(int c)
  23. {
  24. return(c==EOR || c==';');
  25. }
  26. int
  27. rdc(void)
  28. {
  29. do {
  30. readchar();
  31. } while (lastc==SPC || lastc==TB);
  32. return(lastc);
  33. }
  34. void
  35. reread(void)
  36. {
  37. peekc = lastc;
  38. }
  39. void
  40. clrinp(void)
  41. {
  42. flush();
  43. lp = 0;
  44. peekc = 0;
  45. }
  46. int
  47. readrune(int fd, Rune *r)
  48. {
  49. char buf[UTFmax+1];
  50. int i;
  51. for(i=0; i<UTFmax && !fullrune(buf, i); i++)
  52. if(read(fd, buf+i, 1) <= 0)
  53. return -1;
  54. buf[i] = 0;
  55. chartorune(r, buf);
  56. return 1;
  57. }
  58. int
  59. readchar(void)
  60. {
  61. Rune *p;
  62. if (eof)
  63. lastc=0;
  64. else if (peekc) {
  65. lastc = peekc;
  66. peekc = 0;
  67. }
  68. else {
  69. if (lp==0) {
  70. for (p = line; p < &line[LINSIZ-1]; p++) {
  71. eof = readrune(infile, p) <= 0;
  72. if (mkfault) {
  73. eof = 0;
  74. error(0);
  75. }
  76. if (eof) {
  77. p--;
  78. break;
  79. }
  80. if (*p == EOR) {
  81. if (p <= line)
  82. break;
  83. if (p[-1] != '\\')
  84. break;
  85. p -= 2;
  86. }
  87. }
  88. p[1] = 0;
  89. lp = line;
  90. }
  91. if ((lastc = *lp) != 0)
  92. lp++;
  93. }
  94. return(lastc);
  95. }
  96. nextchar(void)
  97. {
  98. if (eol(rdc())) {
  99. reread();
  100. return(0);
  101. }
  102. return(lastc);
  103. }
  104. quotchar(void)
  105. {
  106. if (readchar()=='\\')
  107. return(readchar());
  108. else if (lastc=='\'')
  109. return(0);
  110. else
  111. return(lastc);
  112. }
  113. void
  114. getformat(char *deformat)
  115. {
  116. char *fptr;
  117. BOOL quote;
  118. Rune r;
  119. fptr=deformat;
  120. quote=FALSE;
  121. while ((quote ? readchar()!=EOR : !eol(readchar()))){
  122. r = lastc;
  123. fptr += runetochar(fptr, &r);
  124. if (lastc == '"')
  125. quote = ~quote;
  126. }
  127. lp--;
  128. if (fptr!=deformat)
  129. *fptr = '\0';
  130. }
  131. /*
  132. * check if the input line if of the form:
  133. * <filename>:<digits><verb> ...
  134. *
  135. * we handle this case specially because we have to look ahead
  136. * at the token after the colon to decide if it is a file reference
  137. * or a colon-command with a symbol name prefix.
  138. */
  139. int
  140. isfileref(void)
  141. {
  142. Rune *cp;
  143. for (cp = lp-1; *cp && !strchr(CMD_VERBS, *cp); cp++)
  144. if (*cp == '\\' && cp[1]) /* escape next char */
  145. cp++;
  146. if (*cp && cp > lp-1) {
  147. while (*cp == ' ' || *cp == '\t')
  148. cp++;
  149. if (*cp++ == ':') {
  150. while (*cp == ' ' || *cp == '\t')
  151. cp++;
  152. if (isdigit(*cp))
  153. return 1;
  154. }
  155. }
  156. return 0;
  157. }