sh.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #include <stdint.h>
  2. #include <stdarg.h>
  3. #include <sys/wait.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. int printf(const char* fmt, ...)
  10. {
  11. va_list args;
  12. va_start(args, fmt);
  13. char buf[256] = {};
  14. int len = vsnprintf(buf, sizeof(buf), fmt, args);
  15. len = write(STDOUT_FILENO, buf, len);
  16. va_end(args);
  17. return len;
  18. }
  19. // Parsed command representation
  20. #define EXEC 1
  21. #define REDIR 2
  22. #define PIPE 3
  23. #define LIST 4
  24. #define BACK 5
  25. #define MAXARGS 10
  26. struct cmd {
  27. int type;
  28. };
  29. struct execcmd {
  30. int type;
  31. char *argv[MAXARGS];
  32. char *eargv[MAXARGS];
  33. };
  34. struct redircmd {
  35. int type;
  36. struct cmd *cmd;
  37. char *file;
  38. char *efile;
  39. int mode;
  40. int fd;
  41. };
  42. struct pipecmd {
  43. int type;
  44. struct cmd *left;
  45. struct cmd *right;
  46. };
  47. struct listcmd {
  48. int type;
  49. struct cmd *left;
  50. struct cmd *right;
  51. };
  52. struct backcmd {
  53. int type;
  54. struct cmd *cmd;
  55. };
  56. int fork1(void); // Fork but panics on failure.
  57. void panic(char*);
  58. struct cmd *parsecmd(char*);
  59. // Execute cmd. Never returns.
  60. void
  61. runcmd(struct cmd *cmd)
  62. {
  63. int p[2];
  64. int code;
  65. struct backcmd *bcmd;
  66. struct execcmd *ecmd;
  67. struct listcmd *lcmd;
  68. struct pipecmd *pcmd;
  69. struct redircmd *rcmd;
  70. if(cmd == 0)
  71. _exit(-1);
  72. switch(cmd->type){
  73. default:
  74. panic("runcmd");
  75. case EXEC:
  76. ecmd = (struct execcmd*)cmd;
  77. if(ecmd->argv[0] == 0)
  78. _exit(-1);
  79. char* const envp[1] = { NULL };
  80. execve(ecmd->argv[0], ecmd->argv, envp);
  81. printf("exec %s failed\n", ecmd->argv[0]);
  82. break;
  83. case REDIR:
  84. rcmd = (struct redircmd*)cmd;
  85. close(rcmd->fd);
  86. if(open(rcmd->file, rcmd->mode) < 0){
  87. printf("open %s failed\n", rcmd->file);
  88. _exit(-1);
  89. }
  90. runcmd(rcmd->cmd);
  91. break;
  92. case LIST:
  93. lcmd = (struct listcmd*)cmd;
  94. if(fork1() == 0)
  95. runcmd(lcmd->left);
  96. wait(&code);
  97. runcmd(lcmd->right);
  98. break;
  99. case PIPE:
  100. pcmd = (struct pipecmd*)cmd;
  101. if(pipe(p) < 0)
  102. panic("pipe");
  103. if(fork1() == 0){
  104. close(1);
  105. dup(p[1]);
  106. close(p[0]);
  107. close(p[1]);
  108. runcmd(pcmd->left);
  109. }
  110. if(fork1() == 0){
  111. close(0);
  112. dup(p[0]);
  113. close(p[0]);
  114. close(p[1]);
  115. runcmd(pcmd->right);
  116. }
  117. close(p[0]);
  118. close(p[1]);
  119. wait(&code);
  120. wait(&code);
  121. break;
  122. case BACK:
  123. bcmd = (struct backcmd*)cmd;
  124. if(fork1() == 0)
  125. runcmd(bcmd->cmd);
  126. break;
  127. }
  128. _exit(0);
  129. }
  130. int
  131. getcmd(char *buf, int nbuf)
  132. {
  133. printf("$ ");
  134. memset(buf, 0, nbuf);
  135. gets(buf);
  136. if(buf[0] == 0) // EOF
  137. return -1;
  138. return 0;
  139. }
  140. int
  141. main(void)
  142. {
  143. static char buf[100];
  144. int fd = 0;
  145. // Assumes three file descriptors open.
  146. while((fd = open("/dev/console", 0)) >= 0){
  147. if(fd >= 3){
  148. close(fd);
  149. break;
  150. }
  151. }
  152. // Read and run input commands.
  153. while(getcmd(buf, sizeof(buf)) >= 0){
  154. if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ')
  155. {
  156. // Clumsy but will have to do for now.
  157. // Chdir has no effect on the parent if run in the child.
  158. if(chdir(buf+3) < 0)
  159. printf("cannot cd %s\n", buf+3);
  160. continue;
  161. }
  162. pid_t pid = 0;
  163. if((pid = fork1()) == 0) {
  164. setpgid(0, 0);
  165. runcmd(parsecmd(buf));
  166. }
  167. tcsetpgrp(STDOUT_FILENO, pid);
  168. setpgid(pid, 0);
  169. int code;
  170. wait(&code);
  171. tcsetpgrp(STDOUT_FILENO, getpid());
  172. }
  173. _exit(0);
  174. }
  175. void __attribute__((noreturn))
  176. panic(char *s)
  177. {
  178. printf("%s\n", s);
  179. _exit(-1);
  180. }
  181. int
  182. fork1(void)
  183. {
  184. int pid;
  185. pid = fork();
  186. if(pid == -1)
  187. panic("fork");
  188. return pid;
  189. }
  190. //PAGEBREAK!
  191. // Constructors
  192. struct cmd*
  193. execcmd(void)
  194. {
  195. struct execcmd *cmd;
  196. cmd = malloc(sizeof(*cmd));
  197. memset(cmd, 0, sizeof(*cmd));
  198. cmd->type = EXEC;
  199. return (struct cmd*)cmd;
  200. }
  201. struct cmd*
  202. redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd)
  203. {
  204. struct redircmd *cmd;
  205. cmd = malloc(sizeof(*cmd));
  206. memset(cmd, 0, sizeof(*cmd));
  207. cmd->type = REDIR;
  208. cmd->cmd = subcmd;
  209. cmd->file = file;
  210. cmd->efile = efile;
  211. cmd->mode = mode;
  212. cmd->fd = fd;
  213. return (struct cmd*)cmd;
  214. }
  215. struct cmd*
  216. pipecmd(struct cmd *left, struct cmd *right)
  217. {
  218. struct pipecmd *cmd;
  219. cmd = malloc(sizeof(*cmd));
  220. memset(cmd, 0, sizeof(*cmd));
  221. cmd->type = PIPE;
  222. cmd->left = left;
  223. cmd->right = right;
  224. return (struct cmd*)cmd;
  225. }
  226. struct cmd*
  227. listcmd(struct cmd *left, struct cmd *right)
  228. {
  229. struct listcmd *cmd;
  230. cmd = malloc(sizeof(*cmd));
  231. memset(cmd, 0, sizeof(*cmd));
  232. cmd->type = LIST;
  233. cmd->left = left;
  234. cmd->right = right;
  235. return (struct cmd*)cmd;
  236. }
  237. struct cmd*
  238. backcmd(struct cmd *subcmd)
  239. {
  240. struct backcmd *cmd;
  241. cmd = malloc(sizeof(*cmd));
  242. memset(cmd, 0, sizeof(*cmd));
  243. cmd->type = BACK;
  244. cmd->cmd = subcmd;
  245. return (struct cmd*)cmd;
  246. }
  247. //PAGEBREAK!
  248. // Parsing
  249. char whitespace[] = " \t\r\n\v";
  250. char symbols[] = "<|>&;()";
  251. int
  252. gettoken(char **ps, char *es, char **q, char **eq)
  253. {
  254. char *s;
  255. int ret;
  256. s = *ps;
  257. while(s < es && strchr(whitespace, *s))
  258. s++;
  259. if(q)
  260. *q = s;
  261. ret = *s;
  262. switch(*s){
  263. case 0:
  264. break;
  265. case '|':
  266. case '(':
  267. case ')':
  268. case ';':
  269. case '&':
  270. case '<':
  271. s++;
  272. break;
  273. case '>':
  274. s++;
  275. if(*s == '>'){
  276. ret = '+';
  277. s++;
  278. }
  279. break;
  280. default:
  281. ret = 'a';
  282. while(s < es && !strchr(whitespace, *s) && !strchr(symbols, *s))
  283. s++;
  284. break;
  285. }
  286. if(eq)
  287. *eq = s;
  288. while(s < es && strchr(whitespace, *s))
  289. s++;
  290. *ps = s;
  291. return ret;
  292. }
  293. int
  294. peek(char **ps, char *es, char *toks)
  295. {
  296. char *s;
  297. s = *ps;
  298. while(s < es && strchr(whitespace, *s))
  299. s++;
  300. *ps = s;
  301. return *s && strchr(toks, *s);
  302. }
  303. struct cmd *parseline(char**, char*);
  304. struct cmd *parsepipe(char**, char*);
  305. struct cmd *parseexec(char**, char*);
  306. struct cmd *nulterminate(struct cmd*);
  307. struct cmd*
  308. parsecmd(char *s)
  309. {
  310. char *es;
  311. struct cmd *cmd;
  312. es = s + strlen(s);
  313. cmd = parseline(&s, es);
  314. peek(&s, es, "");
  315. if(s != es){
  316. printf("leftovers: %s\n", s);
  317. panic("syntax");
  318. }
  319. nulterminate(cmd);
  320. return cmd;
  321. }
  322. struct cmd*
  323. parseline(char **ps, char *es)
  324. {
  325. struct cmd *cmd;
  326. cmd = parsepipe(ps, es);
  327. while(peek(ps, es, "&")){
  328. gettoken(ps, es, 0, 0);
  329. cmd = backcmd(cmd);
  330. }
  331. if(peek(ps, es, ";")){
  332. gettoken(ps, es, 0, 0);
  333. cmd = listcmd(cmd, parseline(ps, es));
  334. }
  335. return cmd;
  336. }
  337. struct cmd*
  338. parsepipe(char **ps, char *es)
  339. {
  340. struct cmd *cmd;
  341. cmd = parseexec(ps, es);
  342. if(peek(ps, es, "|")){
  343. gettoken(ps, es, 0, 0);
  344. cmd = pipecmd(cmd, parsepipe(ps, es));
  345. }
  346. return cmd;
  347. }
  348. struct cmd*
  349. parseredirs(struct cmd *cmd, char **ps, char *es)
  350. {
  351. int tok;
  352. char *q, *eq;
  353. while(peek(ps, es, "<>")){
  354. tok = gettoken(ps, es, 0, 0);
  355. if(gettoken(ps, es, &q, &eq) != 'a')
  356. panic("missing file for redirection");
  357. switch(tok){
  358. case '<':
  359. cmd = redircmd(cmd, q, eq, 0, 0);
  360. break;
  361. case '>':
  362. cmd = redircmd(cmd, q, eq, 0, 1);
  363. break;
  364. case '+': // >>
  365. cmd = redircmd(cmd, q, eq, 0, 1);
  366. break;
  367. }
  368. }
  369. return cmd;
  370. }
  371. struct cmd*
  372. parseblock(char **ps, char *es)
  373. {
  374. struct cmd *cmd;
  375. if(!peek(ps, es, "("))
  376. panic("parseblock");
  377. gettoken(ps, es, 0, 0);
  378. cmd = parseline(ps, es);
  379. if(!peek(ps, es, ")"))
  380. panic("syntax - missing )");
  381. gettoken(ps, es, 0, 0);
  382. cmd = parseredirs(cmd, ps, es);
  383. return cmd;
  384. }
  385. struct cmd*
  386. parseexec(char **ps, char *es)
  387. {
  388. char *q, *eq;
  389. int tok, argc;
  390. struct execcmd *cmd;
  391. struct cmd *ret;
  392. if(peek(ps, es, "("))
  393. return parseblock(ps, es);
  394. ret = execcmd();
  395. cmd = (struct execcmd*)ret;
  396. argc = 0;
  397. ret = parseredirs(ret, ps, es);
  398. while(!peek(ps, es, "|)&;")){
  399. if((tok=gettoken(ps, es, &q, &eq)) == 0)
  400. break;
  401. if(tok != 'a')
  402. panic("syntax");
  403. cmd->argv[argc] = q;
  404. cmd->eargv[argc] = eq;
  405. argc++;
  406. if(argc >= MAXARGS)
  407. panic("too many args");
  408. ret = parseredirs(ret, ps, es);
  409. }
  410. cmd->argv[argc] = 0;
  411. cmd->eargv[argc] = 0;
  412. return ret;
  413. }
  414. // NUL-terminate all the counted strings.
  415. struct cmd*
  416. nulterminate(struct cmd *cmd)
  417. {
  418. int i;
  419. struct backcmd *bcmd;
  420. struct execcmd *ecmd;
  421. struct listcmd *lcmd;
  422. struct pipecmd *pcmd;
  423. struct redircmd *rcmd;
  424. if(cmd == 0)
  425. return 0;
  426. switch(cmd->type){
  427. case EXEC:
  428. ecmd = (struct execcmd*)cmd;
  429. for(i=0; ecmd->argv[i]; i++)
  430. *ecmd->eargv[i] = 0;
  431. break;
  432. case REDIR:
  433. rcmd = (struct redircmd*)cmd;
  434. nulterminate(rcmd->cmd);
  435. *rcmd->efile = 0;
  436. break;
  437. case PIPE:
  438. pcmd = (struct pipecmd*)cmd;
  439. nulterminate(pcmd->left);
  440. nulterminate(pcmd->right);
  441. break;
  442. case LIST:
  443. lcmd = (struct listcmd*)cmd;
  444. nulterminate(lcmd->left);
  445. nulterminate(lcmd->right);
  446. break;
  447. case BACK:
  448. bcmd = (struct backcmd*)cmd;
  449. nulterminate(bcmd->cmd);
  450. break;
  451. }
  452. return cmd;
  453. }