shell.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* shell.c, Ait, BSD 3-Clause, Kevin Bloom, 2023-2024 */
  2. #include <stdio.h>
  3. #include "header.h"
  4. #include "termbox.h"
  5. #include "util.h"
  6. char opcmdtext[STRBUF_M];
  7. char ipcmdtext[STRBUF_M];
  8. /* TODO: make this generic
  9. M-e will display "Shell Command: " in the msgline. You then input the command
  10. you want.
  11. Eventually maybe make it so that there are different types of commands:
  12. - input, inputs something at the point
  13. - open, runs a command and ait will open the output (this currently works)
  14. - region/replace, use the region as the input to the shell cmd and then
  15. replace the region with the output
  16. - new buffer, runs the command and the output is placed in a new buffer
  17. I probably would want some keybinds to certain commands, however.
  18. Also, I'd like to make it so that if you have a region selected, it can be
  19. executed much like how acme does it.
  20. io = Insert = 0, Open = 1
  21. */
  22. void get_popen_data(int io) {
  23. FILE *pf;
  24. char *command = NULL;
  25. char *data = NULL;
  26. buffer_t *bp;
  27. char *insertp = "Shell Command", *openp = "Open Via";
  28. char prompt[STRBUF_M + 12 + strlen(insertp)];
  29. int cpos = 0, done = FALSE;
  30. int c = 0, k = 0, hasregion = FALSE, escaped_size = 0;
  31. int start_col = strlen(prompt), didtry, fd;
  32. int line = 0, onscrap = 0;
  33. int newlines = 0, oline = curbp->b_line;
  34. struct tb_event ev;
  35. char cmdtext[STRBUF_L], *cbuf; /* cbuf is the colon buffer for line number */
  36. memset(cmdtext, 0, STRBUF_L);
  37. point_t point;
  38. char_t *oscrap = NULL, *escaped_region = NULL;
  39. const char* path = getenv("PATH");
  40. FILE *fp = NULL;
  41. char sys_command[CHUNK];
  42. static char temp_file[] = TEMPFILE;
  43. if(is_file_modified(curbp->b_fname) && !file_was_modified_prompt()) {
  44. return;
  45. }
  46. if(io == 1) {
  47. sprintf(prompt, "%s", openp);
  48. if(opcmdtext[0] != '\0') {
  49. strcat(prompt, " (default ");
  50. strcat(prompt, opcmdtext);
  51. strcat(prompt, ")");
  52. }
  53. } else {
  54. sprintf(prompt, "%s", insertp);
  55. if(ipcmdtext[0] != '\0') {
  56. strcat(prompt, " (default ");
  57. strcat(prompt, ipcmdtext);
  58. strcat(prompt, ")");
  59. }
  60. }
  61. strcat(prompt, ": ");
  62. start_col = strlen(prompt);
  63. display_prompt_and_response(prompt, cmdtext);
  64. cpos = strlen(cmdtext);
  65. for (;;) {
  66. didtry = (k == 0x09); /* Was last command tab-completion? */
  67. tb_present();
  68. if(execute_kbd_macro) {
  69. use_kbd_macro(&ev);
  70. } else if(tb_poll_event(&ev) != TB_OK) return;
  71. if(msgline_editor(ev, prompt, cmdtext, STRBUF_L, &cpos)) {
  72. continue;
  73. }
  74. if(!ev.mod)
  75. k = ev.ch;
  76. else
  77. k = ev.key;
  78. if(record_input) {
  79. record_buffer[record_buffer_index] = ev;
  80. record_buffer_index++;
  81. }
  82. /* ignore control keys other than return, C-g, backspace, CR, C-s, C-R, ESC, tab */
  83. if (k < 32 &&
  84. k != TB_KEY_CTRL_G &&
  85. k != TB_KEY_BACKSPACE &&
  86. k != TB_KEY_BACKSPACE2 &&
  87. k != TB_KEY_ENTER &&
  88. k != TB_KEY_ESC &&
  89. k != TB_KEY_TAB)
  90. continue;
  91. switch(k) {
  92. case TB_KEY_ENTER: /* return */
  93. done = TRUE;
  94. break;
  95. case TB_KEY_ESC: /* esc */
  96. case TB_KEY_CTRL_G: /* ctrl-g */
  97. if (fp != NULL) fclose(fp);
  98. tb_set_cursor(0, MSGLINE);
  99. clrtoeol("", MSGLINE);
  100. return;
  101. case TB_KEY_BACKSPACE2: /* del, erase */
  102. case TB_KEY_BACKSPACE: /* backspace */
  103. if (cpos == 0)
  104. continue;
  105. cmdtext[--cpos] = '\0';
  106. tb_set_cursor(start_col + cpos, MSGLINE);
  107. display_prompt_and_response(prompt, cmdtext);
  108. break;
  109. do_tab:
  110. case TB_KEY_TAB: {
  111. char curpath[PATH_MAX];
  112. char pcmd[PATH_MAX];
  113. int ii = 0;
  114. for(; !isspace(cmdtext[cpos]) && cpos > 0; cpos--)
  115. ;;
  116. for(int iii = 0; cmdtext[cpos+iii] != '\0'; iii++){
  117. if(!isspace(cmdtext[cpos+iii])) {
  118. pcmd[ii] = cmdtext[cpos+iii];
  119. ii++;
  120. }
  121. }
  122. if(cpos > 0)
  123. cpos++;
  124. pcmd[ii] = '\0';
  125. if(!didtry) {
  126. if (fp != NULL) fclose(fp);
  127. strcpy(temp_file, TEMPFILE);
  128. if (-1 == (fd = mkstemp(temp_file)))
  129. fatal("%s: Failed to create temp file\n");
  130. strcpy(sys_command, "printf \"%s\\n\" ");
  131. for(int i = 0, ii = 0; path[i] != '\0'; i++, ii++) {
  132. if(path[i] == ':') {
  133. curpath[ii] = '\0';
  134. strcat(sys_command, curpath);
  135. strcat(sys_command, "/");
  136. strcat(sys_command, pcmd);
  137. strcat(sys_command, "* ");
  138. ii = 0;
  139. i++;
  140. } else
  141. curpath[ii] = path[i];
  142. }
  143. strcat(sys_command, "| awk '$0 !~ \"\\\\*\" { split($0, a, \"/\"); print a[length(a)] }' | sort -u >");
  144. strcat(sys_command, temp_file);
  145. // strcat(sys_command, " 2>&1");
  146. (void) ! system(sys_command); /* stop compiler unused result warning */
  147. fp = fdopen(fd, "r");
  148. unlink(temp_file);
  149. }
  150. while ((c = getc(fp)) != EOF && c != '\n') {
  151. if (cpos < PATH_MAX - 1 && c != '*') {
  152. cmdtext[cpos++] = c;
  153. cmdtext[cpos] = '\0';
  154. }
  155. }
  156. cmdtext[cpos] = '\0';
  157. for(int i = cpos+1; cmdtext[i] != '\0'; i++)
  158. cmdtext[i] = 0;
  159. if (c != '\n' || c == -1) rewind(fp);
  160. if(c == -1) goto do_tab;
  161. didtry = 1;
  162. tb_set_cursor(start_col, MSGLINE);
  163. clrtoeol(prompt, MSGLINE);
  164. addstr(cmdtext);
  165. break;
  166. }
  167. default:
  168. if (cpos < STRBUF_M - 1) {
  169. for(int i = strlen(cmdtext); i > cpos; i--) {
  170. cmdtext[i] = cmdtext[i - 1];
  171. }
  172. cmdtext[cpos] = k;
  173. cmdtext[strlen(cmdtext)] = '\0';
  174. tb_set_cursor(start_col, MSGLINE);
  175. addstr(cmdtext);
  176. cpos++;
  177. tb_set_cursor(start_col + cpos, MSGLINE);
  178. }
  179. break;
  180. }
  181. if(done)
  182. break;
  183. }
  184. if(cmdtext[0] == '\0') {
  185. if(io == 1 && opcmdtext[0] != '\0')
  186. strncpy(cmdtext, opcmdtext, STRBUF_M);
  187. else if(io == 0 && ipcmdtext[0] != '\0')
  188. strncpy(cmdtext, ipcmdtext, STRBUF_M);
  189. else {
  190. clrtoeol("", MSGLINE);
  191. return;
  192. }
  193. }
  194. if(io == 1)
  195. strncpy(opcmdtext, cmdtext, STRBUF_M);
  196. else if(io == 0)
  197. strncpy(ipcmdtext, cmdtext, STRBUF_M);
  198. int ncmd = 0, cncmd = 0;
  199. for(int i = 0; cmdtext[i] != '\0'; i++, ncmd++) {
  200. if(cmdtext[i] == '@') {
  201. ncmd += strlen(curbp->b_fname);
  202. }
  203. if(cmdtext[i] == '#') {
  204. char *s;
  205. asprintf(&s, "%d", curbp->b_line);
  206. ncmd += strlen(s);
  207. free(s);
  208. s = NULL;
  209. }
  210. if(cmdtext[i] == '$') {
  211. char *s;
  212. asprintf(&s, "%d", curbp->b_col);
  213. ncmd += strlen(s);
  214. free(s);
  215. s = NULL;
  216. }
  217. }
  218. char cmd[ncmd];
  219. for(int i = 0; cmdtext[i] != '\0'; i++, cncmd++) {
  220. if(cmdtext[i] == '@') {
  221. cncmd += strlen(curbp->b_fname) - 1;
  222. strcat(cmd, curbp->b_fname);
  223. } else if(cmdtext[i] == '#') {
  224. char *s;
  225. asprintf(&s, "%d", curbp->b_line);
  226. cncmd += strlen(s) - 1;
  227. strcat(cmd, s);
  228. free(s);
  229. s = NULL;
  230. } else if(cmdtext[i] == '$') {
  231. char *s;
  232. asprintf(&s, "%d", curbp->b_col);
  233. cncmd += strlen(s) - 1;
  234. strcat(cmd, s);
  235. free(s);
  236. s = NULL;
  237. } else {
  238. cmd[cncmd] = cmdtext[i];
  239. }
  240. cmd[cncmd+1] = '\0';
  241. }
  242. if (curbp->b_mark != NOMARK && curbp->b_point != curbp->b_mark) {
  243. if(io == 0) {
  244. oscrap = (char_t*) malloc(scrap.len);
  245. onscrap = scrap.len;
  246. (void) memcpy(oscrap, scrap.data, scrap.len * sizeof (char_t));
  247. copy_cut(TRUE, TRUE, FALSE);
  248. }
  249. hasregion = TRUE;
  250. }
  251. strcpy(temp, editor_dir);
  252. tb_shutdown();
  253. if(hasregion && io == 0) {
  254. /* Find all dollar signs and increase the size by one for each sign. */
  255. for(int i = 0; scrap.data[i] != '\0'; i++) {
  256. if(scrap.data[i] == '$' || scrap.data[i] == '`' || scrap.data[i] == '"')
  257. escaped_size += 2;
  258. else
  259. escaped_size++;
  260. }
  261. escaped_region = malloc(sizeof(char_t *)*escaped_size+1);
  262. /* Escape all $ with \$, ` with \`, and " with \". This prevents
  263. the echo command from trying to do a variable substitution,
  264. command execution, and removal of double quotes.
  265. */
  266. for(int i = 0, k = 0; scrap.data[i] != '\0'; i++, k++) {
  267. if(scrap.data[i] == '$' || scrap.data[i] == '`' || scrap.data[i] == '"') {
  268. escaped_region[k] = '\\';
  269. k++;
  270. escaped_region[k] = scrap.data[i];
  271. } else {
  272. escaped_region[k] = scrap.data[i];
  273. }
  274. }
  275. escaped_region[escaped_size] = '\0';
  276. asprintf(&command, "echo \"%s\" | %s", (char *)escaped_region, cmd);
  277. } else {
  278. command = strdup(cmd);
  279. }
  280. // command[cmdsize] = '\0';
  281. memset(cmd, 0, STRBUF_L);
  282. memset(cmdtext, 0, STRBUF_L);
  283. // Setup our pipe for reading and execute our command.
  284. pf = popen(command,"r");
  285. if(pf == NULL){
  286. msg("Could not open pipe for output.");
  287. return;
  288. }
  289. data = malloc(sizeof(char *) * 512 * 3);
  290. fgets(data, sizeof(char *) * 512 * 3, pf);
  291. tb_init();
  292. LINES = tb_height();
  293. COLS = tb_width();
  294. MSGLINE = LINES-1;
  295. tb_set_input_mode(TB_INPUT_ALT);
  296. /* Mark the log for update */
  297. redraw();
  298. /* check if canceled command */
  299. if(data[0] == -1 || data[0] == 0) {
  300. if(io == 0) {
  301. /* put the original contents back in the buffer and reset scrap */
  302. paste_internal(FALSE);
  303. free(scrap.data);
  304. scrap.len = onscrap;
  305. scrap.data = (char_t*) malloc(scrap.len);
  306. (void) memcpy(scrap.data, oscrap, scrap.len * sizeof (char_t));
  307. }
  308. } else {
  309. switch(io) {
  310. case 0: {
  311. for(int z = 0; data[z] != '\0'; z++) {
  312. input[0] = (char_t)data[z];
  313. input[1] = '\0';
  314. undoset(INSERT, z != 0);
  315. insert();
  316. if(input[0] == '\n')
  317. newlines++;
  318. }
  319. memset(data, 0, sizeof(char *) * 512 * 3);
  320. while(fgets(data, sizeof(char *)*512*3, pf) != NULL && data[0] != '\0') {
  321. for(int z = 0; data[z] != '\0'; z++) {
  322. input[0] = (char_t)data[z];
  323. input[1] = '\0';
  324. undoset(INSERT, TRUE);
  325. insert();
  326. if(input[0] == '\n')
  327. newlines++;
  328. }
  329. memset(data, 0, sizeof(char *) * 512 * 3);
  330. }
  331. if (curbp->b_point >= curbp->b_epage)
  332. curbp->b_reframe = 1;
  333. /* This is ran only for region shell commands, the newlines is
  334. required to keep the line count correct.
  335. */
  336. if(scrap.len > 0) {
  337. curbp->b_line += newlines;
  338. currentcommand = KBD_DELETE_WORD;
  339. backsp();
  340. }
  341. if(oscrap != NULL) {
  342. free(oscrap);
  343. oscrap = NULL;
  344. }
  345. break;
  346. }
  347. case 1: {
  348. data[strlen(data)-1] = '\0';
  349. /* Find the file name and find the line number */
  350. if(data[0] == '\0')
  351. goto do_finish;
  352. cbuf = strtok(data, ":");
  353. strcat(temp, cbuf);
  354. cbuf = strtok(NULL, ":");
  355. if(cbuf != NULL && (line = atoi(cbuf)) == 0) {
  356. strcat(temp, ":");
  357. strcat(temp, cbuf);
  358. }
  359. strcat(temp, "\0");
  360. if(line < 1)
  361. free(cbuf);
  362. bp = find_buffer(temp, TRUE, FALSE);
  363. disassociate_b(curwp);
  364. curbp = bp;
  365. associate_b2w(curbp, curwp);
  366. if (!growgap(curbp, CHUNK))
  367. fatal("%s: Failed to allocate required memory.\n");
  368. movegap(curbp, 0);
  369. /* load the file if not already loaded */
  370. if (bp != NULL && bp->b_fname[0] == '\0') {
  371. if (!load_file(temp)) {
  372. msg("New file %s", temp);
  373. }
  374. strncpy(curbp->b_fname, temp, PATH_MAX);
  375. curbp->b_fname[PATH_MAX] = '\0'; /* truncate if required */
  376. if(line > 0) {
  377. point = line_to_point(line);
  378. if (point != -1) {
  379. curbp->b_point = point;
  380. if (curbp->b_epage < pos(curbp, curbp->b_ebuf))
  381. curbp->b_reframe = 1;
  382. msg("Line %d", line);
  383. } else {
  384. msg("Line %d, not found", line);
  385. }
  386. update_display();
  387. }
  388. }
  389. break;
  390. }
  391. }
  392. }
  393. do_finish:
  394. /* Some commands, such as ones that copy from region, will mess up
  395. curbp->b_line due to the cut that is preformed. We want to reset
  396. that mistake.
  397. */
  398. if(curbp->b_line != oline && newlines == 0 && io == 0)
  399. curbp->b_line = oline;
  400. if(command != NULL) {
  401. free(command);
  402. command = NULL;
  403. }
  404. if(data != NULL) {
  405. free(data);
  406. data = NULL;
  407. }
  408. if (pclose(pf) != 0) {
  409. msg("Error: Failed to close command stream: %s", strerror(errno));
  410. }
  411. }