shell.c 11 KB

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