shell.c 13 KB

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