shell.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. char str_line[10];
  30. int cpos = 0, done = FALSE;
  31. int c, hasregion = FALSE, cmdsize = 0, escaped_size = 0;
  32. int start_col = strlen(prompt);
  33. int i = 0, k = 0, isLineNumber = 0, line, l = 0, onscrap = 0;
  34. int newlines = 0, oline = curbp->b_line;
  35. struct tb_event ev;
  36. char cmdtext[STRBUF_M];
  37. cmdtext[0] = '\0';
  38. point_t point;
  39. char_t *oscrap = NULL, *escaped_region = NULL;
  40. if(io) {
  41. sprintf(prompt, "%s", openp);
  42. if(opcmdtext[0] != '\0') {
  43. strcat(prompt, " (default ");
  44. strcat(prompt, opcmdtext);
  45. strcat(prompt, ")");
  46. }
  47. } else {
  48. sprintf(prompt, "%s", insertp);
  49. if(ipcmdtext[0] != '\0') {
  50. strcat(prompt, " (default ");
  51. strcat(prompt, ipcmdtext);
  52. strcat(prompt, ")");
  53. }
  54. }
  55. strcat(prompt, ": ");
  56. start_col = strlen(prompt);
  57. display_prompt_and_response(prompt, cmdtext);
  58. cpos = strlen(cmdtext);
  59. for (;;) {
  60. tb_present();
  61. if(tb_poll_event(&ev) != TB_OK) return;
  62. if(msgline_editor(ev, prompt, cmdtext, &cpos)) {
  63. continue;
  64. }
  65. if(!ev.mod)
  66. c = ev.ch;
  67. else
  68. c = ev.key;
  69. /* ignore control keys other than return, C-g, backspace, CR, C-s, C-R, ESC */
  70. if (c < 32 &&
  71. c != TB_KEY_CTRL_G &&
  72. c != TB_KEY_BACKSPACE &&
  73. c != TB_KEY_BACKSPACE2 &&
  74. c != TB_KEY_ENTER &&
  75. c != TB_KEY_ESC)
  76. continue;
  77. switch(c) {
  78. case TB_KEY_ENTER: /* return */
  79. done = TRUE;
  80. break;
  81. case TB_KEY_ESC: /* esc */
  82. case TB_KEY_CTRL_G: /* ctrl-g */
  83. tb_set_cursor(0, MSGLINE);
  84. clrtoeol("", MSGLINE);
  85. return;
  86. case TB_KEY_BACKSPACE2: /* del, erase */
  87. case TB_KEY_BACKSPACE: /* backspace */
  88. if (cpos == 0)
  89. continue;
  90. cmdtext[--cpos] = '\0';
  91. tb_set_cursor(start_col + cpos, MSGLINE);
  92. display_prompt_and_response(prompt, cmdtext);
  93. break;
  94. default:
  95. if (cpos < STRBUF_M - 1) {
  96. tb_set_cursor(start_col + cpos, MSGLINE);
  97. cmdtext[cpos++] = c;
  98. cmdtext[cpos] = '\0';
  99. addch(c);
  100. }
  101. break;
  102. }
  103. if(done)
  104. break;
  105. }
  106. if(cmdtext[0] == '\0') {
  107. if(io && opcmdtext[0] != '\0')
  108. strncpy(cmdtext, opcmdtext, STRBUF_M);
  109. else if(!io && ipcmdtext[0] != '\0')
  110. strncpy(cmdtext, ipcmdtext, STRBUF_M);
  111. else {
  112. clrtoeol("", MSGLINE);
  113. return;
  114. }
  115. }
  116. if(io)
  117. strncpy(opcmdtext, cmdtext, STRBUF_M);
  118. else
  119. strncpy(ipcmdtext, cmdtext, STRBUF_M);
  120. if (curbp->b_mark != NOMARK && curbp->b_point != curbp->b_mark) {
  121. oscrap = (char_t*) malloc(nscrap);
  122. onscrap = nscrap;
  123. (void) memcpy(oscrap, scrap, nscrap * sizeof (char_t));
  124. copy_cut(TRUE, TRUE, FALSE);
  125. hasregion = TRUE;
  126. }
  127. strcpy(temp, editor_dir);
  128. tb_shutdown();
  129. if(hasregion) {
  130. /* Find all dollar signs and increase the size by one for each sign. */
  131. for(int i = 0; scrap[i] != '\0'; i++) {
  132. if(scrap[i] == '$' || scrap[i] == '`' || scrap[i] == '"')
  133. escaped_size += 2;
  134. else
  135. escaped_size++;
  136. }
  137. escaped_region = malloc(sizeof(char_t *)*escaped_size+1);
  138. /* Escape all $ with \$, ` with \`, and " with \". This prevents
  139. the echo command from trying to do a variable substitution,
  140. command execution, and removal of double quotes.
  141. */
  142. for(int i = 0, k = 0; scrap[i] != '\0'; i++, k++) {
  143. if(scrap[i] == '$' || scrap[i] == '`' || scrap[i] == '"') {
  144. escaped_region[k] = '\\';
  145. k++;
  146. escaped_region[k] = scrap[i];
  147. } else {
  148. escaped_region[k] = scrap[i];
  149. }
  150. }
  151. escaped_region[escaped_size] = '\0';
  152. cmdsize = 6*4*escaped_size*strlen(cmdtext);
  153. command = malloc(sizeof(char *)*cmdsize);
  154. strcpy(command, "echo \"");
  155. strcat(command, (char *)escaped_region);
  156. strcat(command, "\" | ");
  157. strcat(command, cmdtext);
  158. } else {
  159. cmdsize = strlen(cmdtext);
  160. command = malloc(sizeof(char *)*cmdsize);
  161. strcpy(command, cmdtext);
  162. }
  163. // Setup our pipe for reading and execute our command.
  164. pf = popen(command,"r");
  165. if(pf == NULL){
  166. msg("Could not open pipe for output.");
  167. return;
  168. }
  169. data = malloc(sizeof(char *) * 512 * 3);
  170. fgets(data, sizeof(char *) * 512 * 3, pf);
  171. tb_init();
  172. LINES = tb_height();
  173. COLS = tb_width();
  174. MSGLINE = LINES-1;
  175. tb_set_input_mode(TB_INPUT_ALT);
  176. /* Mark the log for update */
  177. redraw();
  178. /* check if canceled command */
  179. if(data[0] == -1 || data[0] == 0) {
  180. if(io == 0) {
  181. /* put the original contents back in the buffer and reset scrap */
  182. paste_internal(FALSE);
  183. free(scrap);
  184. nscrap = onscrap;
  185. scrap = (char_t*) malloc(nscrap);
  186. (void) memcpy(scrap, oscrap, nscrap * sizeof (char_t));
  187. }
  188. } else {
  189. switch(io) {
  190. case 0: {
  191. for(int z = 0; data[z] != '\0'; z++) {
  192. input[0] = (char_t)data[z];
  193. input[1] = '\0';
  194. undoset(INSERT, z != 0);
  195. insert();
  196. if(input[0] == '\n')
  197. newlines++;
  198. }
  199. while(fgets(data, sizeof(char *)*512*3, pf) != NULL && data[0] != '\0') {
  200. for(int z = 0; data[z] != '\0'; z++) {
  201. input[0] = (char_t)data[z];
  202. input[1] = '\0';
  203. undoset(INSERT, TRUE);
  204. insert();
  205. if(input[0] == '\n')
  206. newlines++;
  207. }
  208. }
  209. if (curbp->b_point >= curbp->b_epage)
  210. curbp->b_reframe = 1;
  211. /* This is ran only for region shell commands, the newlines is required
  212. to keep the line count correct.
  213. */
  214. if(nscrap > 0) {
  215. curbp->b_line += newlines;
  216. currentcommand = KBD_DELETE_CHAR;
  217. backsp();
  218. }
  219. if(oscrap != NULL) {
  220. free(oscrap);
  221. oscrap = NULL;
  222. }
  223. break;
  224. }
  225. case 1: {
  226. data[strlen(data)-1] = '\0';
  227. /* Find the file name and find the line number */
  228. while(data[i] != '\0') {
  229. if(isLineNumber) {
  230. str_line[k] = data[i];
  231. temp[strlen(editor_dir)+i] = data[i];
  232. k++;
  233. } else if(data[i] != ':') {
  234. temp[strlen(editor_dir)+i] = data[i];
  235. } else {
  236. isLineNumber = TRUE;
  237. l = i;
  238. }
  239. i++;
  240. }
  241. if(isLineNumber) {
  242. if((line = atoi(str_line)) == 0) {
  243. i = 0;
  244. while(data[i] != '\0') {
  245. temp[strlen(editor_dir)+i] = data[i];
  246. i++;
  247. }
  248. isLineNumber = FALSE;
  249. l = i;
  250. }
  251. } else {
  252. l = i;
  253. }
  254. temp[strlen(editor_dir)+l] = '\0';
  255. bp = find_buffer(temp, TRUE);
  256. disassociate_b(curwp);
  257. curbp = bp;
  258. associate_b2w(curbp, curwp);
  259. if (!growgap(curbp, CHUNK))
  260. fatal("%s: Failed to allocate required memory.\n");
  261. movegap(curbp, 0);
  262. /* load the file if not already loaded */
  263. if (bp != NULL && bp->b_fname[0] == '\0') {
  264. if (!load_file(temp)) {
  265. msg("New file %s", temp);
  266. }
  267. strncpy(curbp->b_fname, temp, PATH_MAX);
  268. curbp->b_fname[PATH_MAX] = '\0'; /* truncate if required */
  269. if(isLineNumber) {
  270. point = line_to_point(line);
  271. if (point != -1) {
  272. curbp->b_point = point;
  273. if (curbp->b_epage < pos(curbp, curbp->b_ebuf))
  274. curbp->b_reframe = 1;
  275. msg("Line %d", line);
  276. } else {
  277. msg("Line %d, not found", line);
  278. }
  279. update_display();
  280. }
  281. }
  282. break;
  283. }
  284. }
  285. }
  286. /* Some commands, such as ones that copy from region, will mess up
  287. curbp->b_line due to the cut that is preformed. We want to reset
  288. that mistake.
  289. */
  290. if(curbp->b_line != oline && newlines == 0 && io == 0)
  291. curbp->b_line = oline;
  292. if(command != NULL) {
  293. free(command);
  294. command = NULL;
  295. }
  296. if(data != NULL) {
  297. free(data);
  298. data = NULL;
  299. }
  300. if (pclose(pf) != 0) {
  301. msg("Error: Failed to close command stream: %s", strerror(errno));
  302. }
  303. }