sh.c 8.8 KB

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