shell.c 13 KB

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