shell.c 11 KB

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