shell.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. /* 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() {
  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(0, 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], pcmd[PATH_MAX], cu;
  112. int ii = 0;
  113. for(cu = cmdtext[cpos]; !isspace(cu) && cpos > 0; cpos--, cu = cmdtext[cpos])
  114. ;;
  115. for(int iii = 0; cmdtext[cpos+iii] != '\0'; iii++) {
  116. cu = cmdtext[cpos+iii];
  117. if(!isspace(cu)) {
  118. pcmd[ii] = cu;
  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(start_col, 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(0, 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. char n;
  200. for(int i = 0; cmdtext[i] != '\0'; i++, ncmd++) {
  201. n = cmdtext[i+1];
  202. if(cmdtext[i] == '@' && cmdtext[i-1] != '\\') {
  203. ncmd += strlen(curbp->b_fname);
  204. }
  205. if(cmdtext[i] == '#') {
  206. char *s;
  207. asprintf(&s, "%d", curbp->b_line);
  208. ncmd += strlen(s);
  209. free(s);
  210. s = NULL;
  211. }
  212. if(cmdtext[i] == '$' && !isalpha(n) &&
  213. cmdtext[i-1] != '\\') {
  214. char *s;
  215. asprintf(&s, "%d", curbp->b_col);
  216. ncmd += strlen(s);
  217. free(s);
  218. s = NULL;
  219. }
  220. }
  221. char cmd[ncmd];
  222. for(int i = 0; cmdtext[i] != '\0'; i++, cncmd++) {
  223. n = cmdtext[i+1];
  224. if(cmdtext[i] == '@' && cmdtext[i-1] != '\\') {
  225. cncmd += strlen(curbp->b_fname) - 1;
  226. strcat(cmd, curbp->b_fname);
  227. } else if(cmdtext[i] == '#') {
  228. char *s;
  229. asprintf(&s, "%d", curbp->b_line);
  230. cncmd += strlen(s) - 1;
  231. strcat(cmd, s);
  232. free(s);
  233. s = NULL;
  234. } else if(cmdtext[i] == '$' && !isalpha(n) &&
  235. cmdtext[i-1] != '\\') {
  236. char *s;
  237. asprintf(&s, "%d", curbp->b_col);
  238. cncmd += strlen(s) - 1;
  239. strcat(cmd, s);
  240. free(s);
  241. s = NULL;
  242. } else {
  243. cmd[cncmd] = cmdtext[i];
  244. }
  245. cmd[cncmd+1] = '\0';
  246. }
  247. if (curbp->b_mark != NOMARK && curbp->b_point != curbp->b_mark) {
  248. if(io == 0) {
  249. oscrap = (char_t*) malloc(scrap.len);
  250. onscrap = scrap.len;
  251. (void) memcpy(oscrap, scrap.data, scrap.len * sizeof (char_t));
  252. copy_cut(TRUE, TRUE, FALSE);
  253. }
  254. hasregion = TRUE;
  255. }
  256. strcpy(temp, editor_dir);
  257. tb_shutdown();
  258. if(hasregion && io == 0) {
  259. /* Find all dollar signs and increase the size by one for each sign. */
  260. for(int i = 0; scrap.data[i] != '\0'; i++) {
  261. if(scrap.data[i] == '$' || scrap.data[i] == '`' || scrap.data[i] == '"')
  262. escaped_size += 2;
  263. else
  264. escaped_size++;
  265. }
  266. escaped_region = malloc(sizeof(char_t *)*escaped_size+1);
  267. /* Escape all $ with \$, ` with \`, and " with \". This prevents
  268. the echo command from trying to do a variable substitution,
  269. command execution, and removal of double quotes.
  270. */
  271. for(int i = 0, k = 0; scrap.data[i] != '\0'; i++, k++) {
  272. if(scrap.data[i] == '$' || scrap.data[i] == '`' || scrap.data[i] == '"') {
  273. escaped_region[k] = '\\';
  274. k++;
  275. escaped_region[k] = scrap.data[i];
  276. } else {
  277. escaped_region[k] = scrap.data[i];
  278. }
  279. }
  280. escaped_region[escaped_size] = '\0';
  281. asprintf(&command, "echo \"%s\" | %s", (char *)escaped_region, cmd);
  282. } else {
  283. command = strdup(cmd);
  284. }
  285. // Setup our pipe for reading and execute our command.
  286. pf = popen(command,"r");
  287. memset(cmd, 0, STRBUF_L);
  288. memset(cmdtext, 0, STRBUF_L);
  289. if(pf == NULL){
  290. msg("Could not open pipe for output.");
  291. return;
  292. }
  293. data = malloc(sizeof(char *) * 512 * 3);
  294. fgets(data, sizeof(char *) * 512 * 3, pf);
  295. tb_init();
  296. LINES = tb_height();
  297. COLS = tb_width();
  298. MSGLINE = LINES-1;
  299. tb_set_input_mode(TB_INPUT_ALT);
  300. /* Mark the log for update */
  301. redraw();
  302. /* check if canceled command */
  303. if(data[0] == -1 || data[0] == 0) {
  304. if(io == 0) {
  305. /* put the original contents back in the buffer and reset scrap */
  306. paste_internal(FALSE);
  307. free(scrap.data);
  308. scrap.len = onscrap;
  309. scrap.data = (char_t*) malloc(scrap.len);
  310. (void) memcpy(scrap.data, oscrap, scrap.len * sizeof (char_t));
  311. }
  312. } else {
  313. switch(io) {
  314. case 0: {
  315. for(int z = 0; data[z] != '\0'; z++) {
  316. input[0] = (char_t)data[z];
  317. input[1] = '\0';
  318. undoset(INSERT, z != 0);
  319. insert();
  320. if(input[0] == '\n')
  321. newlines++;
  322. }
  323. memset(data, 0, sizeof(char *) * 512 * 3);
  324. while(fgets(data, sizeof(char *)*512*3, pf) != NULL && data[0] != '\0') {
  325. for(int z = 0; data[z] != '\0'; z++) {
  326. input[0] = (char_t)data[z];
  327. input[1] = '\0';
  328. undoset(INSERT, TRUE);
  329. insert();
  330. if(input[0] == '\n')
  331. newlines++;
  332. }
  333. memset(data, 0, sizeof(char *) * 512 * 3);
  334. }
  335. if (curbp->b_point >= curbp->b_epage)
  336. curbp->b_reframe = 1;
  337. /* This is ran only for region shell commands, the newlines is
  338. required to keep the line count correct.
  339. */
  340. if(scrap.len > 0) {
  341. curbp->b_line += newlines;
  342. currentcommand = KBD_DELETE_WORD;
  343. backsp();
  344. }
  345. if(oscrap != NULL) {
  346. free(oscrap);
  347. oscrap = NULL;
  348. }
  349. break;
  350. }
  351. case 1: {
  352. data[strlen(data)-1] = '\0';
  353. /* Find the file name and find the line number */
  354. if(data[0] == '\0')
  355. goto do_finish;
  356. cbuf = strtok(data, ":");
  357. strcat(temp, cbuf);
  358. cbuf = strtok(NULL, ":");
  359. if(cbuf != NULL && (line = atoi(cbuf)) == 0) {
  360. strcat(temp, ":");
  361. strcat(temp, cbuf);
  362. }
  363. strcat(temp, "\0");
  364. if(line < 1)
  365. free(cbuf);
  366. bp = find_buffer(temp, TRUE, FALSE);
  367. disassociate_b(curwp);
  368. curbp = bp;
  369. associate_b2w(curbp, curwp);
  370. if (!growgap(curbp, CHUNK))
  371. fatal("%s: Failed to allocate required memory.\n");
  372. movegap(curbp, 0);
  373. /* load the file if not already loaded */
  374. if (bp != NULL && bp->b_fname[0] == '\0') {
  375. if (!load_file(temp)) {
  376. msg("New file %s", temp);
  377. }
  378. strncpy(curbp->b_fname, temp, PATH_MAX);
  379. curbp->b_fname[PATH_MAX] = '\0'; /* truncate if required */
  380. if(line > 0) {
  381. point = line_to_point(line);
  382. if (point != -1) {
  383. curbp->b_point = point;
  384. if (curbp->b_epage < pos(curbp, curbp->b_ebuf))
  385. curbp->b_reframe = 1;
  386. msg("Line %d", line);
  387. } else {
  388. msg("Line %d, not found", line);
  389. }
  390. update_display();
  391. }
  392. }
  393. break;
  394. }
  395. }
  396. }
  397. do_finish:
  398. /* Some commands, such as ones that copy from region, will mess up
  399. curbp->b_line due to the cut that is preformed. We want to reset
  400. that mistake.
  401. */
  402. if(curbp->b_line != oline && newlines == 0 && io == 0)
  403. curbp->b_line = oline;
  404. if(command != NULL) {
  405. free(command);
  406. command = NULL;
  407. }
  408. if(data != NULL) {
  409. free(data);
  410. data = NULL;
  411. }
  412. if (pclose(pf) != 0) {
  413. msg("Error: Failed to close command stream: %s", strerror(errno));
  414. }
  415. }