shell.c 8.3 KB

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