shell.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. } else {
  127. undoset(5, FALSE);
  128. }
  129. strcpy(temp, editor_dir);
  130. tb_shutdown();
  131. if(hasregion) {
  132. /* Find all dollar signs and increase the size by one for each sign. */
  133. for(int i = 0; scrap[i] != '\0'; i++) {
  134. if(scrap[i] == '$' || scrap[i] == '`' || scrap[i] == '"')
  135. escaped_size += 2;
  136. else
  137. escaped_size++;
  138. }
  139. escaped_region = malloc(sizeof(char_t *)*escaped_size+1);
  140. /* Escape all $ with \$, ` with \`, and " with \". This prevents
  141. the echo command from trying to do a variable substitution,
  142. command execution, and removal of double quotes.
  143. */
  144. for(int i = 0, k = 0; scrap[i] != '\0'; i++, k++) {
  145. if(scrap[i] == '$' || scrap[i] == '`' || scrap[i] == '"') {
  146. escaped_region[k] = '\\';
  147. k++;
  148. escaped_region[k] = scrap[i];
  149. } else {
  150. escaped_region[k] = scrap[i];
  151. }
  152. }
  153. escaped_region[escaped_size] = '\0';
  154. cmdsize = 6*4*escaped_size*strlen(cmdtext);
  155. command = malloc(sizeof(char *)*cmdsize);
  156. strcpy(command, "echo \"");
  157. strcat(command, (char *)escaped_region);
  158. strcat(command, "\" | ");
  159. strcat(command, cmdtext);
  160. } else {
  161. cmdsize = strlen(cmdtext);
  162. command = malloc(sizeof(char *)*cmdsize);
  163. strcpy(command, cmdtext);
  164. }
  165. // Setup our pipe for reading and execute our command.
  166. pf = popen(command,"r");
  167. if(pf == NULL){
  168. msg("Could not open pipe for output.");
  169. return;
  170. }
  171. data = malloc(sizeof(char *) * 512 * 3);
  172. fgets(data, sizeof(char *) * 512 * 3, pf);
  173. tb_init();
  174. LINES = tb_height();
  175. COLS = tb_width();
  176. MSGLINE = LINES-1;
  177. tb_set_input_mode(TB_INPUT_ALT);
  178. /* Mark the log for update */
  179. redraw();
  180. /* check if canceled command */
  181. if(data[0] == -1 || data[0] == 0) {
  182. if(io == 0) {
  183. /* put the original contents back in the buffer and reset scrap */
  184. paste_internal(FALSE);
  185. free(scrap);
  186. nscrap = onscrap;
  187. scrap = (char_t*) malloc(nscrap);
  188. (void) memcpy(scrap, oscrap, nscrap * sizeof (char_t));
  189. }
  190. } else {
  191. switch(io) {
  192. case 0: {
  193. for(int z = 0; data[z] != '\0'; z++) {
  194. input[0] = (char_t)data[z];
  195. input[1] = '\0';
  196. undoset(INSERT, z != 0);
  197. insert();
  198. if(input[0] == '\n')
  199. newlines++;
  200. }
  201. while(fgets(data, sizeof(char *)*512*3, pf) != NULL && data[0] != '\0') {
  202. for(int z = 0; data[z] != '\0'; z++) {
  203. input[0] = (char_t)data[z];
  204. input[1] = '\0';
  205. undoset(INSERT, TRUE);
  206. insert();
  207. if(input[0] == '\n')
  208. newlines++;
  209. }
  210. }
  211. if (curbp->b_point >= curbp->b_epage)
  212. curbp->b_reframe = 1;
  213. /* This is ran only for region shell commands, the newlines is required
  214. to keep the line count correct.
  215. */
  216. if(nscrap > 0) {
  217. curbp->b_line += newlines;
  218. currentcommand = KBD_DELETE_CHAR;
  219. backsp();
  220. }
  221. if(oscrap != NULL) {
  222. free(oscrap);
  223. oscrap = NULL;
  224. }
  225. break;
  226. }
  227. case 1: {
  228. data[strlen(data)-1] = '\0';
  229. /* Find the file name and find the line number */
  230. while(data[i] != '\0') {
  231. if(isLineNumber) {
  232. str_line[k] = data[i];
  233. temp[strlen(editor_dir)+i] = data[i];
  234. k++;
  235. } else if(data[i] != ':') {
  236. temp[strlen(editor_dir)+i] = data[i];
  237. } else {
  238. isLineNumber = TRUE;
  239. l = i;
  240. }
  241. i++;
  242. }
  243. if(isLineNumber) {
  244. if((line = atoi(str_line)) == 0) {
  245. i = 0;
  246. while(data[i] != '\0') {
  247. temp[strlen(editor_dir)+i] = data[i];
  248. i++;
  249. }
  250. isLineNumber = FALSE;
  251. l = i;
  252. }
  253. } else {
  254. l = i;
  255. }
  256. temp[strlen(editor_dir)+l] = '\0';
  257. bp = find_buffer(temp, TRUE);
  258. disassociate_b(curwp);
  259. curbp = bp;
  260. associate_b2w(curbp, curwp);
  261. if (!growgap(curbp, CHUNK))
  262. fatal("%s: Failed to allocate required memory.\n");
  263. movegap(curbp, 0);
  264. /* load the file if not already loaded */
  265. if (bp != NULL && bp->b_fname[0] == '\0') {
  266. if (!load_file(temp)) {
  267. msg("New file %s", temp);
  268. }
  269. strncpy(curbp->b_fname, temp, PATH_MAX);
  270. curbp->b_fname[PATH_MAX] = '\0'; /* truncate if required */
  271. if(isLineNumber) {
  272. point = line_to_point(line);
  273. if (point != -1) {
  274. curbp->b_point = point;
  275. if (curbp->b_epage < pos(curbp, curbp->b_ebuf))
  276. curbp->b_reframe = 1;
  277. msg("Line %d", line);
  278. } else {
  279. msg("Line %d, not found", line);
  280. }
  281. update_display();
  282. }
  283. }
  284. break;
  285. }
  286. }
  287. }
  288. /* Some commands, such as ones that copy from region, will mess up
  289. curbp->b_line due to the cut that is preformed. We want to reset
  290. that mistake.
  291. */
  292. if(curbp->b_line != oline && newlines == 0 && io == 0)
  293. curbp->b_line = oline;
  294. if(command != NULL) {
  295. free(command);
  296. command = NULL;
  297. }
  298. if(data != NULL) {
  299. free(data);
  300. data = NULL;
  301. }
  302. if (pclose(pf) != 0) {
  303. msg("Error: Failed to close command stream: %s", strerror(errno));
  304. }
  305. }