Advertisement
Guest User

parser

a guest
Sep 9th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. /* This file contains the code for parser used to parse the input
  2. * given to shell program. You shouldn't need to modify this
  3. * file */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include "parse.h"
  9.  
  10. #define PIPE ('|')
  11. #define BG ('&')
  12. #define RIN ('<')
  13. #define RUT ('>')
  14.  
  15. #define ispipe(c) ((c) == PIPE)
  16. #define isbg(c) ((c) == BG)
  17. #define isrin(c) ((c) == RIN)
  18. #define isrut(c) ((c) == RUT)
  19. #define isspec(c) (ispipe(c) || isbg(c) || isrin(c) || isrut(c))
  20.  
  21. static Pgm cmdbuf[20], *cmds;
  22. static char cbuf[256], *cp;
  23. static char *pbuf[50], **pp;
  24.  
  25. /*
  26. * A quick hack to parse a commandline
  27. */
  28.  
  29. int
  30. parse (char *buf, Command *c)
  31. {
  32. int n;
  33. Pgm *cmd0;
  34.  
  35. char *t = buf;
  36. char *tok;
  37.  
  38. init();
  39. c->rstdin = NULL;
  40. c->rstdout = NULL;
  41. c->rstderr = NULL;
  42. c->bakground = 0; /* false */
  43. c->pgm = NULL;
  44.  
  45. newcmd:
  46. if ((n = acmd(t, &cmd0)) <= 0) {
  47. return -1;
  48. }
  49.  
  50. t += n;
  51.  
  52. cmd0->next = c->pgm;
  53. c->pgm = cmd0;
  54.  
  55. newtoken:
  56. n = nexttoken(t, &tok);
  57. if (n == 0) {
  58. return 1;
  59. }
  60. t += n;
  61.  
  62. switch(*tok) {
  63. case PIPE:
  64. goto newcmd;
  65. break;
  66. case BG:
  67. n = nexttoken(t, &tok);
  68. if (n == 0) {
  69. c->bakground = 1;
  70. return 1;
  71. }
  72. else {
  73. fprintf(stderr, "illegal bakgrounding\n");
  74. return -1;
  75. }
  76. break;
  77. case RIN:
  78. if (c->rstdin != NULL) {
  79. fprintf(stderr, "duplicate redirection of stdin\n");
  80. return -1;
  81. }
  82. if ((n = nexttoken(t, &(c->rstdin))) < 0) {
  83. return -1;
  84. }
  85. if (!isidentifier(c->rstdin)) {
  86. fprintf(stderr, "Illegal filename: \"%s\"\n", c->rstdin);
  87. return -1;
  88. }
  89. t += n;
  90. goto newtoken;
  91. break;
  92. case RUT:
  93. if (c->rstdout != NULL) {
  94. fprintf(stderr, "duplicate redirection of stdout\n");
  95. return -1;
  96. }
  97. if ((n = nexttoken(t, &(c->rstdout))) < 0) {
  98. return -1;
  99. }
  100. if (!isidentifier(c->rstdout)) {
  101. fprintf(stderr, "Illegal filename: \"%s\"\n", c->rstdout);
  102. return -1;
  103. }
  104. t += n;
  105. goto newtoken;
  106. break;
  107. default:
  108. return -1;
  109. }
  110. goto newcmd;
  111. }
  112.  
  113. void init( void )
  114. {
  115. int i;
  116. for (i=0;i<19;i++) {
  117. cmdbuf[i].next = &cmdbuf[i+1];
  118. }
  119. cmdbuf[19].next = NULL;
  120. cmds = cmdbuf;
  121. cp = cbuf;
  122. pp = pbuf;
  123. }
  124.  
  125. int
  126. nexttoken( char *s, char **tok)
  127. {
  128. char *s0 = s;
  129. char c;
  130.  
  131. *tok = cp;
  132. while (isspace(c = *s++) && c);
  133. if (c == '\0') {
  134. return 0;
  135. }
  136. if (isspec(c)) {
  137. *cp++ = c;
  138. *cp++ = '\0';
  139. }
  140. else {
  141. *cp++ = c;
  142. do {
  143. c = *cp++ = *s++;
  144. } while (!isspace(c) && !isspec(c) && (c != '\0'));
  145. --s;
  146. --cp;
  147. *cp++ = '\0';
  148. }
  149. return s - s0;
  150. }
  151.  
  152. int
  153. acmd (char *s, Pgm **cmd)
  154. {
  155. char *tok;
  156. int n, cnt = 0;
  157. Pgm *cmd0 = cmds;
  158. cmds = cmds->next;
  159. cmd0->next = NULL;
  160. cmd0->pgmlist = pp;
  161.  
  162. next:
  163. n = nexttoken(s, &tok);
  164. if (n == 0 || isspec(*tok)) {
  165. *cmd = cmd0;
  166. *pp++ = NULL;
  167. return cnt;
  168. }
  169. else {
  170. *pp++ = tok;
  171. cnt += n;
  172. s += n;
  173. goto next;
  174. }
  175. }
  176.  
  177.  
  178. #define IDCHARS "_-.,/~+"
  179.  
  180. int
  181. isidentifier (char *s)
  182. {
  183. while (*s) {
  184. char *p = strrchr (IDCHARS, *s);
  185. if (! isalnum(*s++) && (p == NULL))
  186. return 0;
  187. }
  188. return 1;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement