sh.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2013-2018 K. Lange
  5. *
  6. * E-Shell
  7. *
  8. * This is the "experimental shell". It provides
  9. * a somewhat unix-like shell environment, but does
  10. * not include a parser any advanced functionality.
  11. * It simply cuts its input into arguments and executes
  12. * programs.
  13. */
  14. #define _XOPEN_SOURCE
  15. #include <stdio.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <time.h>
  21. #include <dirent.h>
  22. #include <signal.h>
  23. #include <getopt.h>
  24. #include <termios.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <ctype.h>
  28. #include <sys/time.h>
  29. #include <sys/wait.h>
  30. #include <sys/utsname.h>
  31. #include <sys/stat.h>
  32. #include <toaru/list.h>
  33. #include <toaru/kbd.h>
  34. #include <toaru/rline.h>
  35. #define PIPE_TOKEN "\xFF\xFFPIPE\xFF\xFF"
  36. #define STAR_TOKEN "\xFF\xFFSTAR\xFF\xFF"
  37. #define WRITE_TOKEN "\xFF\xFFWRITE\xFF\xFF"
  38. #define APPEND_TOKEN "\xFF\xFF""APPEND\xFF"
  39. /* A shell command is like a C program */
  40. typedef uint32_t(*shell_command_t) (int argc, char ** argv);
  41. /* We have a static array that fits a certain number of them. */
  42. #define SHELL_COMMANDS 512
  43. char * shell_commands[SHELL_COMMANDS]; /* Command names */
  44. shell_command_t shell_pointers[SHELL_COMMANDS]; /* Command functions */
  45. char * shell_descript[SHELL_COMMANDS]; /* Command descriptions */
  46. /* This is the number of actual commands installed */
  47. int shell_commands_len = 0;
  48. int shell_interactive = 1;
  49. int last_ret = 0;
  50. char ** shell_argv = NULL;
  51. int shell_argc = 0;
  52. int pid; /* Process ID of the shell */
  53. void shell_install_command(char * name, shell_command_t func, char * desc) {
  54. if (shell_commands_len == SHELL_COMMANDS) {
  55. fprintf(stderr, "Ran out of space for static shell commands. The maximum number of commands is %d\n", SHELL_COMMANDS);
  56. return;
  57. }
  58. shell_commands[shell_commands_len] = name;
  59. shell_pointers[shell_commands_len] = func;
  60. shell_descript[shell_commands_len] = desc;
  61. shell_commands_len++;
  62. }
  63. shell_command_t shell_find(char * str) {
  64. for (int i = 0; i < shell_commands_len; ++i) {
  65. if (!strcmp(str, shell_commands[i])) {
  66. return shell_pointers[i];
  67. }
  68. }
  69. return NULL;
  70. }
  71. void install_commands();
  72. /* Maximum command length */
  73. #define LINE_LEN 4096
  74. /* Current working directory */
  75. char cwd[1024] = {'/',0};
  76. /* Username */
  77. char username[1024];
  78. /* Hostname for prompt */
  79. char _hostname[256];
  80. /* function to update the cached username */
  81. void getuser() {
  82. char * tmp = getenv("USER");
  83. if (tmp) {
  84. strcpy(username, tmp);
  85. } else {
  86. sprintf(username, "%d", getuid());
  87. }
  88. }
  89. /* function to update the cached hostname */
  90. void gethost() {
  91. struct utsname buf;
  92. uname(&buf);
  93. int len = strlen(buf.nodename);
  94. memcpy(_hostname, buf.nodename, len+1);
  95. }
  96. void print_extended_ps(char * format) {
  97. /* Get the time */
  98. struct tm * timeinfo;
  99. struct timeval now;
  100. gettimeofday(&now, NULL); //time(NULL);
  101. timeinfo = localtime((time_t *)&now.tv_sec);
  102. /* Format the date and time for prompt display */
  103. char date_buffer[80];
  104. strftime(date_buffer, 80, "%m/%d", timeinfo);
  105. char time_buffer[80];
  106. strftime(time_buffer, 80, "%H:%M:%S", timeinfo);
  107. /* Collect the current working directory */
  108. getcwd(cwd, 512);
  109. char _cwd[512];
  110. strcpy(_cwd, cwd);
  111. /* Collect the user's home directory and apply it to cwd */
  112. char * home = getenv("HOME");
  113. if (home && strstr(cwd, home) == cwd) {
  114. char * c = cwd + strlen(home);
  115. if (*c == '/' || *c == 0) {
  116. sprintf(_cwd, "~%s", c);
  117. }
  118. }
  119. char ret[80] = {0};
  120. if (last_ret != 0) {
  121. sprintf(ret, "%d ", last_ret);
  122. }
  123. while (*format) {
  124. if (*format == '\\') {
  125. format++;
  126. switch (*format) {
  127. case '\\':
  128. putchar(*format);
  129. format++;
  130. break;
  131. case '0':
  132. case '1':
  133. case '2':
  134. case '3':
  135. case '4':
  136. case '5':
  137. case '6':
  138. case '7':
  139. {
  140. int i = (*format) - '0';
  141. format++;
  142. if (*format >= '0' && *format <= '7') {
  143. i *= 8;
  144. i += (*format) - '0';
  145. format++;
  146. if (*format >= '0' && *format <= '7') {
  147. i *= 8;
  148. i += (*format) - '0';
  149. format++;
  150. }
  151. }
  152. putchar(i);
  153. }
  154. break;
  155. case 'e':
  156. putchar('\033');
  157. format++;
  158. break;
  159. case 'd':
  160. printf("%s", date_buffer);
  161. format++;
  162. break;
  163. case 't':
  164. printf("%s", time_buffer);
  165. format++;
  166. break;
  167. case 'h':
  168. printf("%s", _hostname);
  169. format++;
  170. break;
  171. case 'u':
  172. printf("%s", username);
  173. format++;
  174. break;
  175. case 'w':
  176. printf("%s", _cwd);
  177. format++;
  178. break;
  179. case '$':
  180. putchar(getuid() == 0 ? '#' : '$');
  181. format++;
  182. break;
  183. case 'U': /* prompt color string */
  184. printf("%s", getuid() == 0 ? "\033[1;38;5;196m" : "\033[1;38;5;47m");
  185. format++;
  186. break;
  187. case 'r':
  188. printf("%s", ret);
  189. format++;
  190. break;
  191. default:
  192. printf("\\%c", *format);
  193. format++;
  194. break;
  195. }
  196. } else {
  197. putchar(*format);
  198. format++;
  199. }
  200. }
  201. }
  202. #define FALLBACK_PS1 "\\u@\\h \\w\\$ "
  203. /* Draw the user prompt */
  204. void draw_prompt(void) {
  205. char * ps1 = getenv("PS1");
  206. print_extended_ps(ps1 ? ps1 : FALLBACK_PS1);
  207. fflush(stdout);
  208. }
  209. volatile int break_while = 0;
  210. uint32_t child = 0;
  211. void sig_pass(int sig) {
  212. /* Interrupt handler */
  213. if (child) {
  214. kill(child, sig);
  215. }
  216. break_while = sig;
  217. }
  218. void redraw_prompt_func(rline_context_t * context) {
  219. draw_prompt();
  220. }
  221. void draw_prompt_c() {
  222. char * ps2 = getenv("PS2");
  223. if (ps2) {
  224. print_extended_ps(ps2);
  225. } else {
  226. printf("> ");
  227. }
  228. fflush(stdout);
  229. }
  230. void redraw_prompt_func_c(rline_context_t * context) {
  231. draw_prompt_c();
  232. }
  233. void tab_complete_func(rline_context_t * c) {
  234. char * dup = malloc(LINE_LEN);
  235. memcpy(dup, c->buffer, LINE_LEN);
  236. char *pch, *save;
  237. char *argv[1024];
  238. int argc = 0;
  239. int cursor = 0;
  240. pch = strtok_r(dup, " ", &save);
  241. if (!pch) {
  242. argv[0] = "";
  243. argc = 0;
  244. }
  245. while (pch != NULL) {
  246. if (pch - dup <= c->offset) cursor = argc;
  247. argv[argc] = pch;
  248. ++argc;
  249. pch = strtok_r(NULL, " ", &save);
  250. }
  251. argv[argc] = NULL;
  252. if (c->offset && c->buffer[c->offset-1] == ' ' && argc) {
  253. cursor++;
  254. }
  255. char * word = argv[cursor];
  256. int word_offset = word ? (c->offset - (argv[cursor] - dup)) : 0;
  257. char * prefix = malloc(word_offset + 1);
  258. if (word) memcpy(prefix, word, word_offset);
  259. prefix[word_offset] = '\0';
  260. /* Complete file path */
  261. list_t * matches = list_create();
  262. char * match = NULL;
  263. int free_matches = 0;
  264. int no_space_if_only = 0;
  265. /* TODO custom auto-complete as a configuration file? */
  266. #define COMPLETE_FILE 1
  267. #define COMPLETE_COMMAND 2
  268. #define COMPLETE_CUSTOM 3
  269. #define COMPLETE_VARIABLE 4
  270. int complete_mode = COMPLETE_FILE;
  271. int command_adj = 0;
  272. int cursor_adj = cursor;
  273. /* sudo should shift commands */
  274. if (cursor_adj > command_adj && (!strcmp(argv[command_adj], "sudo") || !strcmp(argv[command_adj], "gsudo"))) {
  275. cursor_adj -= 1;
  276. command_adj += 1;
  277. }
  278. /* initial tab completion should be commands, unless typing a file path */
  279. if (cursor_adj == 0 && !strchr(prefix,'/')) {
  280. complete_mode = COMPLETE_COMMAND;
  281. }
  282. if (cursor_adj >= 1 && !strcmp(argv[command_adj], "toggle-abs-mouse")) {
  283. complete_mode = COMPLETE_CUSTOM;
  284. }
  285. /* complete variable names */
  286. if (*prefix == '$') {
  287. complete_mode = COMPLETE_VARIABLE;
  288. }
  289. if (complete_mode == COMPLETE_COMMAND) {
  290. /* Complete a command name */
  291. for (int i = 0; i < shell_commands_len; ++i) {
  292. if (strstr(shell_commands[i], prefix) == shell_commands[i]) {
  293. list_insert(matches, shell_commands[i]);
  294. match = shell_commands[i];
  295. }
  296. }
  297. } else if (complete_mode == COMPLETE_FILE) {
  298. /* Complete a file path */
  299. free_matches = 1;
  300. char * tmp = strdup(prefix);
  301. char * last_slash = strrchr(tmp, '/');
  302. DIR * dirp;
  303. char * compare = prefix;
  304. if (last_slash) {
  305. *last_slash = '\0';
  306. word = word + (last_slash - tmp) + 1;
  307. word_offset = word_offset - (last_slash - tmp + 1);
  308. compare = word;
  309. if (last_slash == tmp) {
  310. dirp = opendir("/");
  311. } else {
  312. dirp = opendir(tmp);
  313. }
  314. } else {
  315. dirp = opendir(".");
  316. }
  317. if (!dirp) {
  318. free(tmp);
  319. goto finish_tab;
  320. }
  321. struct dirent * ent = readdir(dirp);
  322. while (ent != NULL) {
  323. if (ent->d_name[0] != '.') {
  324. if (!word || strstr(ent->d_name, compare) == ent->d_name) {
  325. struct stat statbuf;
  326. /* stat it */
  327. if (last_slash) {
  328. char * x = malloc(strlen(tmp) + 1 + strlen(ent->d_name) + 1);
  329. sprintf(x,"%s/%s",tmp,ent->d_name);
  330. lstat(x, &statbuf);
  331. } else {
  332. lstat(ent->d_name, &statbuf);
  333. }
  334. char * s;
  335. if (S_ISDIR(statbuf.st_mode)) {
  336. s = malloc(strlen(ent->d_name) + 2);
  337. sprintf(s,"%s/", ent->d_name);
  338. no_space_if_only = 1;
  339. } else {
  340. s = strdup(ent->d_name);
  341. }
  342. list_insert(matches, s);
  343. match = s;
  344. }
  345. }
  346. ent = readdir(dirp);
  347. }
  348. closedir(dirp);
  349. free(tmp);
  350. } else if (complete_mode == COMPLETE_CUSTOM) {
  351. char ** completions = NULL;
  352. char * toggle_abs_mouse_completions[] = {"relative","absolute",NULL};
  353. if (!strcmp(argv[command_adj],"toggle-abs-mouse")) {
  354. completions = toggle_abs_mouse_completions;
  355. }
  356. while (*completions) {
  357. if (strstr(*completions, prefix) == *completions) {
  358. list_insert(matches, *completions);
  359. match = *completions;
  360. }
  361. completions++;
  362. }
  363. } else if (complete_mode == COMPLETE_VARIABLE) {
  364. char ** envvar = environ;
  365. free_matches = 1;
  366. while (*envvar) {
  367. char * tmp = strdup(*envvar);
  368. char * c = strchr(tmp, '=');
  369. *c = '\0';
  370. if (strstr(tmp, prefix+1) == tmp) {
  371. char * m = malloc(strlen(tmp)+2);
  372. sprintf(m, "$%s", tmp);
  373. list_insert(matches, m);
  374. match = m;
  375. }
  376. free(tmp);
  377. envvar++;
  378. }
  379. }
  380. if (matches->length == 1) {
  381. /* Insert */
  382. rline_insert(c, &match[word_offset]);
  383. if (word && word_offset == (int)strlen(word) && !no_space_if_only) {
  384. rline_insert(c, " ");
  385. }
  386. rline_redraw(c);
  387. } else if (matches->length > 1) {
  388. if (!c->tabbed) {
  389. /* see if there is a minimum subset we can fill in */
  390. size_t j = word_offset;
  391. do {
  392. char d = match[j];
  393. int diff = 0;
  394. foreach(node, matches) {
  395. char * match = (char *)node->value;
  396. if (match[j] != d || match[j] == '\0') diff = 1;
  397. }
  398. if (diff) break;
  399. j++;
  400. } while (j < (size_t)c->requested);
  401. if (j > (size_t)word_offset) {
  402. char * tmp = strdup(match);
  403. tmp[j] = '\0';
  404. rline_insert(c, &tmp[word_offset]);
  405. rline_redraw(c);
  406. free(tmp);
  407. } else {
  408. c->tabbed = 1;
  409. }
  410. } else {
  411. /* Print matches */
  412. fprintf(stderr,"\n");
  413. size_t j = 0;
  414. foreach(node, matches) {
  415. char * match = (char *)node->value;
  416. fprintf(stderr, "%s", match);
  417. ++j;
  418. if (j < matches->length) {
  419. fprintf(stderr, ", ");
  420. }
  421. }
  422. fprintf(stderr,"\n");
  423. c->callbacks->redraw_prompt(c);
  424. fprintf(stderr, "\033[s");
  425. rline_redraw(c);
  426. }
  427. }
  428. finish_tab:
  429. if (free_matches) list_destroy(matches);
  430. list_free(matches);
  431. free(prefix);
  432. free(dup);
  433. }
  434. void add_argument(list_t * argv, char * buf) {
  435. char * c = malloc(strlen(buf) + 1);
  436. memcpy(c, buf, strlen(buf) + 1);
  437. list_insert(argv, c);
  438. }
  439. int read_entry(char * buffer) {
  440. rline_callbacks_t callbacks = {
  441. tab_complete_func, redraw_prompt_func, NULL,
  442. NULL, NULL, NULL, NULL, NULL
  443. };
  444. int buffer_size = rline((char *)buffer, LINE_LEN, &callbacks);
  445. return buffer_size;
  446. }
  447. int read_entry_continued(char * buffer) {
  448. rline_callbacks_t callbacks = {
  449. tab_complete_func, redraw_prompt_func_c, NULL,
  450. NULL, NULL, NULL, NULL, NULL
  451. };
  452. int buffer_size = rline((char *)buffer, LINE_LEN, &callbacks);
  453. return buffer_size;
  454. }
  455. int variable_char(uint8_t c) {
  456. if (c >= 'A' && c <= 'Z') return 1;
  457. if (c >= 'a' && c <= 'z') return 1;
  458. if (c >= '0' && c <= '9') return 1;
  459. if (c == '_') return 1;
  460. if (c == '?') return 1;
  461. return 0;
  462. }
  463. void run_cmd(char ** args) {
  464. int i = execvp(*args, args);
  465. shell_command_t func = shell_find(*args);
  466. if (func) {
  467. int argc = 0;
  468. while (args[argc]) {
  469. argc++;
  470. }
  471. i = func(argc, args);
  472. } else {
  473. if (i != 0) {
  474. fprintf(stderr, "%s: Command not found\n", *args);
  475. i = 127;
  476. }
  477. }
  478. exit(i);
  479. }
  480. int is_number(const char * c) {
  481. while (*c) {
  482. if (!isdigit(*c)) return 0;
  483. c++;
  484. }
  485. return 1;
  486. }
  487. int shell_exec(char * buffer, size_t size, FILE * file, char ** out_buffer) {
  488. *out_buffer = NULL;
  489. /* Read previous history entries */
  490. if (buffer[0] == '!') {
  491. int x = atoi((char *)((uintptr_t)buffer + 1));
  492. if (x > 0 && x <= rline_history_count) {
  493. buffer = rline_history_get(x - 1);
  494. } else {
  495. fprintf(stderr, "esh: !%d: event not found\n", x);
  496. return 0;
  497. }
  498. }
  499. char * argv[1024];
  500. int tokenid = 0;
  501. char quoted = 0;
  502. char backtick = 0;
  503. char buffer_[512] = {0};
  504. int collected = 0;
  505. list_t * args = list_create();
  506. int have_star = 0;
  507. while (1) {
  508. char * p = buffer;
  509. while (*p) {
  510. switch (*p) {
  511. case '$':
  512. if (quoted == '\'') {
  513. goto _just_add;
  514. } else {
  515. if (backtick) {
  516. goto _just_add;
  517. }
  518. p++;
  519. char var[100];
  520. int coll = 0;
  521. if (*p == '{') {
  522. p++;
  523. while (*p != '}' && *p != '\0' && (coll < 100)) {
  524. var[coll] = *p;
  525. coll++;
  526. var[coll] = '\0';
  527. p++;
  528. }
  529. if (*p == '}') {
  530. p++;
  531. }
  532. } else {
  533. while (*p != '\0' && variable_char(*p) && (coll < 100)) {
  534. var[coll] = *p;
  535. coll++;
  536. var[coll] = '\0';
  537. if (coll == 0 && (isdigit(*p) || *p == '?')) {
  538. p++;
  539. break; /* Don't let these keep going */
  540. }
  541. p++;
  542. }
  543. }
  544. /* Special cases */
  545. char *c = NULL;
  546. char tmp[128];
  547. if (!strcmp(var, "?")) {
  548. sprintf(tmp,"%d",last_ret);
  549. c = tmp;
  550. } else if (is_number(var)) {
  551. int a = atoi(var);
  552. if (a >= 0 && a < shell_argc) {
  553. c = shell_argv[a];
  554. }
  555. } else {
  556. c = getenv(var);
  557. }
  558. if (c) {
  559. backtick = 0;
  560. for (int i = 0; i < (int)strlen(c); ++i) {
  561. if (c[i] == ' ' && !quoted) {
  562. /* If we are not quoted and we reach a space, it signals a new argument */
  563. if (collected) {
  564. buffer_[collected] = '\0';
  565. add_argument(args, buffer_);
  566. buffer_[0] = '\0';
  567. have_star = 0;
  568. collected = 0;
  569. }
  570. } else {
  571. buffer_[collected] = c[i];
  572. collected++;
  573. }
  574. }
  575. buffer_[collected] = '\0';
  576. }
  577. continue;
  578. }
  579. case '\"':
  580. if (quoted == '\"') {
  581. if (backtick) {
  582. goto _just_add;
  583. }
  584. quoted = 0;
  585. goto _next;
  586. } else if (!quoted) {
  587. if (backtick) {
  588. goto _just_add;
  589. }
  590. quoted = *p;
  591. goto _next;
  592. }
  593. goto _just_add;
  594. case '\'':
  595. if (quoted == '\'') {
  596. if (backtick) {
  597. goto _just_add;
  598. }
  599. quoted = 0;
  600. goto _next;
  601. } else if (!quoted) {
  602. if (backtick) {
  603. goto _just_add;
  604. }
  605. quoted = *p;
  606. goto _next;
  607. }
  608. goto _just_add;
  609. case '*':
  610. if (quoted) {
  611. goto _just_add;
  612. }
  613. if (backtick) {
  614. goto _just_add;
  615. }
  616. if (have_star) {
  617. goto _just_add; /* TODO multiple globs */
  618. }
  619. have_star = 1;
  620. collected += sprintf(&buffer_[collected], STAR_TOKEN);
  621. goto _next;
  622. case '\\':
  623. if (quoted == '\'') {
  624. goto _just_add;
  625. }
  626. if (backtick) {
  627. goto _just_add;
  628. }
  629. backtick = 1;
  630. goto _next;
  631. case ' ':
  632. if (backtick) {
  633. goto _just_add;
  634. }
  635. if (!quoted) {
  636. goto _new_arg;
  637. }
  638. goto _just_add;
  639. case '\n':
  640. if (!quoted) {
  641. goto _done;
  642. }
  643. goto _just_add;
  644. case '|':
  645. if (!quoted && !backtick) {
  646. if (collected) {
  647. add_argument(args, buffer_);
  648. }
  649. collected = sprintf(buffer_, "%s", PIPE_TOKEN);
  650. goto _new_arg;
  651. }
  652. goto _just_add;
  653. case '>':
  654. if (!quoted && !backtick) {
  655. if (collected) {
  656. add_argument(args, buffer_);
  657. }
  658. collected = sprintf(buffer_, "%s", WRITE_TOKEN);
  659. goto _new_arg;
  660. }
  661. goto _just_add;
  662. case ';':
  663. if (!quoted && !backtick) {
  664. *out_buffer = ++p;
  665. goto _done;
  666. }
  667. case '#':
  668. if (!quoted && !backtick) {
  669. goto _done; /* Support comments; must not be part of an existing arg */
  670. }
  671. goto _just_add;
  672. default:
  673. if (backtick) {
  674. buffer_[collected] = '\\';
  675. collected++;
  676. buffer_[collected] = '\0';
  677. }
  678. _just_add:
  679. backtick = 0;
  680. buffer_[collected] = *p;
  681. collected++;
  682. buffer_[collected] = '\0';
  683. goto _next;
  684. }
  685. _new_arg:
  686. backtick = 0;
  687. if (collected) {
  688. add_argument(args, buffer_);
  689. buffer_[0] = '\0';
  690. have_star = 0;
  691. collected = 0;
  692. }
  693. _next:
  694. p++;
  695. }
  696. _done:
  697. if (quoted || backtick) {
  698. backtick = 0;
  699. if (shell_interactive == 1) {
  700. draw_prompt_c();
  701. read_entry_continued(buffer);
  702. rline_history_append_line(buffer);
  703. continue;
  704. } else if (shell_interactive == 2) {
  705. fgets(buffer, size, file);
  706. continue;
  707. } else {
  708. fprintf(stderr, "Syntax error: Unterminated quoted string.\n");
  709. return 127;
  710. }
  711. }
  712. if (collected) {
  713. add_argument(args, buffer_);
  714. break;
  715. }
  716. break;
  717. }
  718. int cmdi = 0;
  719. char ** arg_starts[100] = { &argv[0], NULL };
  720. char * output_files[100] = { NULL };
  721. int file_args[100] = {0};
  722. int argcs[100] = {0};
  723. int next_is_file = 0;
  724. int i = 0;
  725. foreach(node, args) {
  726. char * c = node->value;
  727. if (next_is_file) {
  728. if (next_is_file == 1 && !strcmp(c, WRITE_TOKEN)) {
  729. next_is_file = 2;
  730. file_args[cmdi] = O_WRONLY | O_CREAT | O_APPEND;
  731. continue;
  732. }
  733. output_files[cmdi] = c;
  734. continue;
  735. }
  736. if (!strcmp(c, WRITE_TOKEN)) {
  737. next_is_file = 1;
  738. file_args[cmdi] = O_WRONLY | O_CREAT | O_TRUNC;
  739. continue;
  740. }
  741. if (!strcmp(c, PIPE_TOKEN)) {
  742. if (arg_starts[cmdi] == &argv[i]) {
  743. fprintf(stderr, "Syntax error: Unexpected pipe token\n");
  744. return 2;
  745. }
  746. argv[i] = 0;
  747. i++;
  748. cmdi++;
  749. arg_starts[cmdi] = &argv[i];
  750. continue;
  751. }
  752. char * glob = strstr(c, STAR_TOKEN);
  753. if (glob) {
  754. /* Globbing */
  755. glob[0] = '\0';
  756. glob[1] = '\0';
  757. char * before = c;
  758. char * after = &glob[8];
  759. int has_before = !!strlen(before);
  760. int has_after = !!strlen(after);
  761. if (!has_before || !strchr(before,'/')) {
  762. /* read current directory, add all */
  763. DIR * dirp = opendir(".");
  764. int before_i = i;
  765. struct dirent * ent = readdir(dirp);
  766. while (ent != NULL) {
  767. if (ent->d_name[0] != '.') {
  768. char * s = malloc(sizeof(char) * (strlen(ent->d_name) + 1));
  769. memcpy(s, ent->d_name, strlen(ent->d_name) + 1);
  770. char * t = s;
  771. if (has_before) {
  772. if (strstr(s,before) != s) {
  773. goto _nope;
  774. }
  775. t = &s[strlen(before)];
  776. }
  777. if (has_after) {
  778. if (strlen(t) >= strlen(after)) {
  779. if (!strcmp(after,&t[strlen(t)-strlen(after)])) {
  780. argv[i] = s;
  781. i++;
  782. argcs[cmdi]++;
  783. }
  784. }
  785. } else {
  786. argv[i] = s;
  787. i++;
  788. argcs[cmdi]++;
  789. }
  790. }
  791. _nope:
  792. ent = readdir(dirp);
  793. }
  794. closedir(dirp);
  795. if (before_i == i) {
  796. /* no matches */
  797. glob[0] = '*';
  798. memmove(&glob[1], after, strlen(after)+1);
  799. argv[i] = c;
  800. i++;
  801. argcs[cmdi]++;
  802. } else {
  803. free(c);
  804. }
  805. } else {
  806. /* directory globs not supported */
  807. glob[0] = '*';
  808. argv[i] = c;
  809. i++;
  810. argcs[cmdi]++;
  811. }
  812. } else {
  813. argv[i] = c;
  814. i++;
  815. argcs[cmdi]++;
  816. }
  817. }
  818. argv[i] = NULL;
  819. if (i == 0) {
  820. return -1;
  821. }
  822. list_free(args);
  823. if (!*arg_starts[cmdi]) {
  824. fprintf(stderr, "Syntax error: Unexpected end of input\n");
  825. return 2;
  826. }
  827. char * cmd = *arg_starts[0];
  828. tokenid = i;
  829. unsigned int child_pid;
  830. int nowait = (!strcmp(argv[tokenid-1],"&"));
  831. if (nowait) {
  832. argv[tokenid-1] = NULL;
  833. }
  834. if (cmdi > 0) {
  835. int last_output[2];
  836. pipe(last_output);
  837. child_pid = fork();
  838. if (!child_pid) {
  839. dup2(last_output[1], STDOUT_FILENO);
  840. close(last_output[0]);
  841. run_cmd(arg_starts[0]);
  842. }
  843. for (int j = 1; j < cmdi; ++j) {
  844. int tmp_out[2];
  845. pipe(tmp_out);
  846. if (!fork()) {
  847. dup2(tmp_out[1], STDOUT_FILENO);
  848. dup2(last_output[0], STDIN_FILENO);
  849. close(tmp_out[0]);
  850. close(last_output[1]);
  851. run_cmd(arg_starts[j]);
  852. }
  853. close(last_output[0]);
  854. close(last_output[1]);
  855. last_output[0] = tmp_out[0];
  856. last_output[1] = tmp_out[1];
  857. }
  858. if (!fork()) {
  859. if (output_files[cmdi]) {
  860. dup2(open(output_files[cmdi], file_args[cmdi], 0666), STDOUT_FILENO);
  861. }
  862. dup2(last_output[0], STDIN_FILENO);
  863. close(last_output[1]);
  864. run_cmd(arg_starts[cmdi]);
  865. }
  866. close(last_output[0]);
  867. close(last_output[1]);
  868. /* Now execute the last piece and wait on all of them */
  869. } else {
  870. shell_command_t func = shell_find(*arg_starts[0]);
  871. if (func) {
  872. return func(argcs[0], arg_starts[0]);
  873. } else {
  874. child_pid = fork();
  875. if (!child_pid) {
  876. if (output_files[cmdi]) {
  877. dup2(open(output_files[cmdi], file_args[cmdi], 0666), STDOUT_FILENO);
  878. }
  879. run_cmd(arg_starts[0]);
  880. }
  881. }
  882. }
  883. tcsetpgrp(STDIN_FILENO, child_pid);
  884. int ret_code = 0;
  885. if (!nowait) {
  886. child = child_pid;
  887. int pid;
  888. do {
  889. pid = waitpid(-1, &ret_code, 0);
  890. } while (pid != -1 || (pid == -1 && errno != ECHILD));
  891. child = 0;
  892. }
  893. tcsetpgrp(STDIN_FILENO, getpid());
  894. free(cmd);
  895. return ret_code;
  896. }
  897. void add_path_contents(char * path) {
  898. DIR * dirp = opendir(path);
  899. if (!dirp) return; /* Failed to load directly */
  900. struct dirent * ent = readdir(dirp);
  901. while (ent != NULL) {
  902. if (ent->d_name[0] != '.') {
  903. char * s = malloc(sizeof(char) * (strlen(ent->d_name) + 1));
  904. memcpy(s, ent->d_name, strlen(ent->d_name) + 1);
  905. shell_install_command(s, NULL, NULL);
  906. }
  907. ent = readdir(dirp);
  908. }
  909. closedir(dirp);
  910. }
  911. struct command {
  912. char * string;
  913. void * func;
  914. char * desc;
  915. };
  916. static int comp_shell_commands(const void *p1, const void *p2) {
  917. return strcmp(((struct command *)p1)->string, ((struct command *)p2)->string);
  918. }
  919. void sort_commands() {
  920. struct command commands[SHELL_COMMANDS];
  921. for (int i = 0; i < shell_commands_len; ++i) {
  922. commands[i].string = shell_commands[i];
  923. commands[i].func = shell_pointers[i];
  924. commands[i].desc = shell_descript[i];
  925. }
  926. qsort(&commands, shell_commands_len, sizeof(struct command), comp_shell_commands);
  927. for (int i = 0; i < shell_commands_len; ++i) {
  928. shell_commands[i] = commands[i].string;
  929. shell_pointers[i] = commands[i].func;
  930. shell_descript[i] = commands[i].desc;
  931. }
  932. }
  933. void show_version(void) {
  934. printf("esh 1.3.0\n");
  935. }
  936. void show_usage(int argc, char * argv[]) {
  937. printf(
  938. "Esh: The Experimental Shell\n"
  939. "\n"
  940. "usage: %s [-lha] [path]\n"
  941. "\n"
  942. " -c \033[4mcmd\033[0m \033[3mparse and execute cmd\033[0m\n"
  943. //-c cmd \033[...
  944. " -v \033[3mshow version information\033[0m\n"
  945. " -? \033[3mshow this help text\033[0m\n"
  946. "\n", argv[0]);
  947. }
  948. void add_path(void) {
  949. char * envvar = getenv("PATH");
  950. if (!envvar) {
  951. add_path_contents("/bin");
  952. return;
  953. }
  954. char * tmp = strdup(envvar);
  955. do {
  956. char * end = strstr(tmp,":");
  957. if (end) {
  958. *end = '\0';
  959. end++;
  960. }
  961. add_path_contents(tmp);
  962. tmp = end;
  963. } while (tmp);
  964. free(tmp);
  965. }
  966. int run_script(FILE * f) {
  967. while (!feof(f)) {
  968. char buf[LINE_LEN] = {0};
  969. fgets(buf, LINE_LEN, f);
  970. int ret;
  971. char * out = NULL;
  972. char * b = buf;
  973. do {
  974. ret = shell_exec(b, LINE_LEN, f, &out);
  975. b = out;
  976. } while (b);
  977. if (ret >= 0) last_ret = ret;
  978. }
  979. fclose(f);
  980. return last_ret;
  981. }
  982. void source_eshrc(void) {
  983. char * home = getenv("HOME");
  984. if (!home) return;
  985. char tmp[512];
  986. sprintf(tmp, "%s/.eshrc", home);
  987. FILE * f = fopen(tmp, "r");
  988. if (!f) return;
  989. run_script(f);
  990. }
  991. int main(int argc, char ** argv) {
  992. pid = getpid();
  993. signal(SIGINT, sig_pass);
  994. signal(SIGWINCH, sig_pass);
  995. getuser();
  996. gethost();
  997. install_commands();
  998. /* Parse $PATH to add contents */
  999. add_path();
  1000. sort_commands();
  1001. if (argc > 1) {
  1002. int c;
  1003. while ((c = getopt(argc, argv, "c:v?")) != -1) {
  1004. switch (c) {
  1005. case 'c':
  1006. shell_interactive = 0;
  1007. {
  1008. char * out = NULL;
  1009. do {
  1010. last_ret = shell_exec(optarg, strlen(optarg), NULL, &out);
  1011. optarg = out;
  1012. } while (optarg);
  1013. }
  1014. return (last_ret == -1) ? 0 : last_ret;
  1015. case 'v':
  1016. show_version();
  1017. return 0;
  1018. case '?':
  1019. show_usage(argc, argv);
  1020. return 0;
  1021. }
  1022. }
  1023. }
  1024. if (optind < argc) {
  1025. shell_interactive = 2;
  1026. FILE * f = fopen(argv[optind],"r");
  1027. if (!f) {
  1028. fprintf(stderr, "%s: %s: %s\n", argv[0], argv[optind], strerror(errno));
  1029. return 1;
  1030. }
  1031. shell_argc = argc - 1;
  1032. shell_argv = &argv[1];
  1033. return run_script(f);
  1034. }
  1035. shell_interactive = 1;
  1036. source_eshrc();
  1037. while (1) {
  1038. draw_prompt();
  1039. char buffer[LINE_LEN] = {0};
  1040. read_entry(buffer);
  1041. char * history = malloc(strlen(buffer) + 1);
  1042. memcpy(history, buffer, strlen(buffer) + 1);
  1043. if (buffer[0] != ' ' && buffer[0] != '\n' && buffer[0] != '!') {
  1044. rline_history_insert(history);
  1045. } else {
  1046. free(history);
  1047. }
  1048. int ret;
  1049. char * out = NULL;
  1050. char * b = buffer;
  1051. do {
  1052. ret = shell_exec(b, LINE_LEN, stdin, &out);
  1053. b = out;
  1054. } while (b);
  1055. if (ret >= 0) last_ret = ret;
  1056. rline_scroll = 0;
  1057. }
  1058. return 0;
  1059. }
  1060. /*
  1061. * cd [path]
  1062. */
  1063. uint32_t shell_cmd_cd(int argc, char * argv[]) {
  1064. if (argc > 1) {
  1065. if (chdir(argv[1])) {
  1066. goto cd_error;
  1067. } /* else success */
  1068. } else /* argc < 2 */ {
  1069. char * home = getenv("HOME");
  1070. if (home) {
  1071. if (chdir(home)) {
  1072. goto cd_error;
  1073. }
  1074. } else {
  1075. char home_path[512];
  1076. sprintf(home_path, "/home/%s", username);
  1077. if (chdir(home_path)) {
  1078. goto cd_error;
  1079. }
  1080. }
  1081. }
  1082. return 0;
  1083. cd_error:
  1084. fprintf(stderr, "%s: could not cd '%s': no such file or directory\n", argv[0], argv[1]);
  1085. return 1;
  1086. }
  1087. /*
  1088. * history
  1089. */
  1090. uint32_t shell_cmd_history(int argc, char * argv[]) {
  1091. for (int i = 0; i < rline_history_count; ++i) {
  1092. printf("%d\t%s\n", i + 1, rline_history_get(i));
  1093. }
  1094. return 0;
  1095. }
  1096. uint32_t shell_cmd_export(int argc, char * argv[]) {
  1097. if (argc > 1) {
  1098. putenv(argv[1]);
  1099. }
  1100. return 0;
  1101. }
  1102. uint32_t shell_cmd_exit(int argc, char * argv[]) {
  1103. if (argc > 1) {
  1104. exit(atoi(argv[1]));
  1105. } else {
  1106. exit(0);
  1107. }
  1108. return -1;
  1109. }
  1110. uint32_t shell_cmd_help(int argc, char * argv[]) {
  1111. show_version();
  1112. printf("\nThis shell is not POSIX-compliant, please be careful.\n\n");
  1113. printf("Built-in commands:\n");
  1114. /* First, determine max width of command names */
  1115. unsigned int max_len = 0;
  1116. for (int i = 0; i < shell_commands_len; ++i) {
  1117. if (!shell_descript[i]) continue;
  1118. if (strlen(shell_commands[i]) > max_len) {
  1119. max_len = strlen(shell_commands[i]);
  1120. }
  1121. }
  1122. /* Then print the commands their help text */
  1123. for (int i = 0; i < shell_commands_len; ++i) {
  1124. if (!shell_descript[i]) continue;
  1125. printf(" %-*s - %s\n", max_len + 1, shell_commands[i], shell_descript[i]);
  1126. }
  1127. return 0;
  1128. }
  1129. uint32_t shell_cmd_if(int argc, char * argv[]) {
  1130. char ** if_args = &argv[1];
  1131. char ** then_args = NULL;
  1132. char ** else_args = NULL;
  1133. for (int i = 2; i < argc; ++i) {
  1134. if (!strcmp(argv[i],"then")) {
  1135. argv[i] = NULL;
  1136. then_args = &argv[i+1];
  1137. } else if (!strcmp(argv[i],"else")) {
  1138. argv[i] = NULL;
  1139. else_args = &argv[i+1];
  1140. }
  1141. }
  1142. if (!then_args) {
  1143. fprintf(stderr, "%s: syntax error: expected 'then' clause\n", argv[0]);
  1144. return 1;
  1145. }
  1146. if (else_args && else_args < then_args) {
  1147. fprintf(stderr, "%s: syntax error: 'else' clause before 'then' clase\n", argv[0]);
  1148. return 1;
  1149. }
  1150. pid_t child_pid = fork();
  1151. if (!child_pid) {
  1152. run_cmd(if_args);
  1153. }
  1154. tcsetpgrp(STDIN_FILENO, child_pid);
  1155. child = child_pid;
  1156. int pid, ret_code = 0;
  1157. do {
  1158. pid = waitpid(-1, &ret_code, 0);
  1159. } while (pid != -1 || (pid == -1 && errno != ECHILD));
  1160. child = 0;
  1161. if (ret_code == 0) {
  1162. shell_command_t func = shell_find(*then_args);
  1163. if (func) {
  1164. int argc = 0;
  1165. while (then_args[argc]) {
  1166. argc++;
  1167. }
  1168. return func(argc, then_args);
  1169. } else {
  1170. child_pid = fork();
  1171. if (!child_pid) {
  1172. run_cmd(then_args);
  1173. }
  1174. tcsetpgrp(STDIN_FILENO, child_pid);
  1175. child = child_pid;
  1176. do {
  1177. pid = waitpid(-1, &ret_code, 0);
  1178. } while (pid != -1 || (pid == -1 && errno != ECHILD));
  1179. child = 0;
  1180. tcsetpgrp(STDIN_FILENO, getpid());
  1181. return ret_code;
  1182. }
  1183. } else if (else_args) {
  1184. shell_command_t func = shell_find(*else_args);
  1185. if (func) {
  1186. int argc = 0;
  1187. while (else_args[argc]) {
  1188. argc++;
  1189. }
  1190. return func(argc, else_args);
  1191. } else {
  1192. child_pid = fork();
  1193. if (!child_pid) {
  1194. run_cmd(else_args);
  1195. }
  1196. tcsetpgrp(STDIN_FILENO, child_pid);
  1197. child = child_pid;
  1198. do {
  1199. pid = waitpid(-1, &ret_code, 0);
  1200. } while (pid != -1 || (pid == -1 && errno != ECHILD));
  1201. child = 0;
  1202. return ret_code;
  1203. }
  1204. }
  1205. tcsetpgrp(STDIN_FILENO, getpid());
  1206. return 0;
  1207. }
  1208. uint32_t shell_cmd_while(int argc, char * argv[]) {
  1209. char ** while_args = &argv[1];
  1210. char ** do_args = NULL;
  1211. for (int i = 2; i < argc; ++i) {
  1212. if (!strcmp(argv[i],"do")) {
  1213. argv[i] = NULL;
  1214. do_args = &argv[i+1];
  1215. }
  1216. }
  1217. if (!do_args) {
  1218. fprintf(stderr, "%s: syntax error: expected 'do' clause\n", argv[0]);
  1219. return 1;
  1220. }
  1221. break_while = 0;
  1222. tcsetpgrp(STDIN_FILENO, getpid());
  1223. do {
  1224. pid_t child_pid = fork();
  1225. if (!child_pid) {
  1226. run_cmd(while_args);
  1227. }
  1228. child = child_pid;
  1229. int pid, ret_code = 0;
  1230. do {
  1231. pid = waitpid(-1, &ret_code, 0);
  1232. } while (pid != -1 || (pid == -1 && errno != ECHILD));
  1233. child = 0;
  1234. if (ret_code == 0) {
  1235. child_pid = fork();
  1236. if (!child_pid) {
  1237. run_cmd(do_args);
  1238. }
  1239. child = child_pid;
  1240. do {
  1241. pid = waitpid(-1, &ret_code, 0);
  1242. } while (pid != -1 || (pid == -1 && errno != ECHILD));
  1243. child = 0;
  1244. } else {
  1245. return ret_code;
  1246. }
  1247. } while (!break_while);
  1248. return 127;
  1249. }
  1250. uint32_t shell_cmd_export_cmd(int argc, char * argv[]) {
  1251. if (argc < 3) {
  1252. fprintf(stderr, "%s: syntax error: not enough arguments\n", argv[0]);
  1253. return 1;
  1254. }
  1255. int pipe_fds[2];
  1256. pipe(pipe_fds);
  1257. pid_t child_pid = fork();
  1258. if (!child_pid) {
  1259. dup2(pipe_fds[1], STDOUT_FILENO);
  1260. close(pipe_fds[0]);
  1261. run_cmd(&argv[2]);
  1262. }
  1263. close(pipe_fds[1]);
  1264. tcsetpgrp(STDIN_FILENO, child_pid);
  1265. char buf[1024];
  1266. size_t accum = 0;
  1267. do {
  1268. int r = read(pipe_fds[0], buf+accum, 1023-accum);
  1269. if (r == 0) break;
  1270. if (r < 0) {
  1271. return -r;
  1272. }
  1273. accum += r;
  1274. } while (accum < 1023);
  1275. tcsetpgrp(STDIN_FILENO, getpid());
  1276. buf[accum] = '\0';
  1277. if (accum && buf[accum-1] == '\n') {
  1278. buf[accum-1] = '\0';
  1279. }
  1280. setenv(argv[1], buf, 1);
  1281. return 0;
  1282. }
  1283. uint32_t shell_cmd_empty(int argc, char * argv[]) {
  1284. for (int i = 1; i < argc; i++) {
  1285. if (argv[i] && *argv[i]) return 1;
  1286. }
  1287. return 0;
  1288. }
  1289. uint32_t shell_cmd_equals(int argc, char * argv[]) {
  1290. if (argc < 3) return 1;
  1291. return strcmp(argv[1], argv[2]);
  1292. }
  1293. uint32_t shell_cmd_return(int argc, char * argv[]) {
  1294. if (argc < 2) return 0;
  1295. return atoi(argv[1]);
  1296. }
  1297. uint32_t shell_cmd_source(int argc, char * argv[]) {
  1298. if (argc < 2) return 0;
  1299. FILE * f = fopen(argv[1], "r");
  1300. if (!f) {
  1301. fprintf(stderr, "%s: %s: %s", argv[0], argv[1], strerror(errno));
  1302. }
  1303. return run_script(f);
  1304. }
  1305. uint32_t shell_cmd_exec(int argc, char * argv[]) {
  1306. if (argc < 2) return 1;
  1307. return execvp(argv[1], &argv[1]);
  1308. }
  1309. uint32_t shell_cmd_not(int argc, char * argv[]) {
  1310. if (argc < 2) {
  1311. fprintf(stderr, "%s: expected command argument\n", argv[0]);
  1312. return 1;
  1313. }
  1314. int ret_code = 0;
  1315. pid_t child_pid = fork();
  1316. if (!child_pid) {
  1317. run_cmd(&argv[1]);
  1318. }
  1319. tcsetpgrp(STDIN_FILENO, child_pid);
  1320. child = child_pid;
  1321. do {
  1322. pid = waitpid(-1, &ret_code, 0);
  1323. } while (pid != -1 || (pid == -1 && errno != ECHILD));
  1324. child = 0;
  1325. tcsetpgrp(STDIN_FILENO, getpid());
  1326. return !ret_code;
  1327. }
  1328. void install_commands() {
  1329. shell_install_command("cd", shell_cmd_cd, "change directory");
  1330. shell_install_command("exit", shell_cmd_exit, "exit the shell");
  1331. shell_install_command("export", shell_cmd_export, "set environment variables: export VAR=value");
  1332. shell_install_command("help", shell_cmd_help, "display this help text");
  1333. shell_install_command("history", shell_cmd_history, "list command history");
  1334. shell_install_command("if", shell_cmd_if, "if ... then ... [else ...]");
  1335. shell_install_command("while", shell_cmd_while, "while ... do ...");
  1336. shell_install_command("empty?", shell_cmd_empty, "empty? args...");
  1337. shell_install_command("equals?", shell_cmd_equals, "equals? arg1 arg2");
  1338. shell_install_command("return", shell_cmd_return, "return status code");
  1339. shell_install_command("export-cmd", shell_cmd_export_cmd, "set variable to result of command: export-cmd VAR command...");
  1340. shell_install_command("source", shell_cmd_source, "run a shell script in the context of this shell");
  1341. shell_install_command("exec", shell_cmd_exec, "replace shell (or subshell) with command");
  1342. shell_install_command("not", shell_cmd_not, "invert status of command");
  1343. }