shell.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* shell.c, Ait, BSD 3-Clause, Kevin Bloom, 2023 */
  2. #include <stdio.h>
  3. #include "header.h"
  4. #include "termbox.h"
  5. char opcmdtext[STRBUF_M];
  6. char ipcmdtext[STRBUF_M];
  7. /* TODO: make this generic
  8. M-e will display "Shell Command: " in the msgline. You then input the command
  9. you want.
  10. Eventually maybe make it so that there are different types of commands:
  11. - input, inputs something at the point
  12. - open, runs a command and ait will open the output (this currently works)
  13. - region/replace, use the region as the input to the shell cmd and then
  14. replace the region with the output
  15. - new buffer, runs the command and the output is placed in a new buffer
  16. I probably would want some keybinds to certain commands, however.
  17. Also, I'd like to make it so that if you have a region selected, it can be
  18. executed much like how acme does it.
  19. io = Insert = 0, Open = 1
  20. */
  21. void get_popen_data(int io) {
  22. FILE *pf;
  23. char *command = NULL;
  24. char *data = NULL;
  25. buffer_t *bp;
  26. char *insertp = "Shell Command", *openp = "Open Via";
  27. char prompt[STRBUF_M + 12 + strlen(insertp)];
  28. char str_line[10];
  29. int cpos = 0, done = FALSE;
  30. int c, hasregion = FALSE, cmdsize = 0, escaped_size = 0;
  31. int start_col = strlen(prompt);
  32. int i = 0, k = 0, isLineNumber = 0, line, l = 0, onscrap = 0;
  33. struct tb_event ev;
  34. char cmdtext[STRBUF_M];
  35. cmdtext[0] = '\0';
  36. point_t point;
  37. char_t *oscrap = NULL, *escaped_region = NULL;
  38. if(io) {
  39. sprintf(prompt, "%s", openp);
  40. if(opcmdtext[0] != '\0') {
  41. strcat(prompt, " (default ");
  42. strcat(prompt, opcmdtext);
  43. strcat(prompt, ")");
  44. }
  45. } else {
  46. sprintf(prompt, "%s", insertp);
  47. if(ipcmdtext[0] != '\0') {
  48. strcat(prompt, " (default ");
  49. strcat(prompt, ipcmdtext);
  50. strcat(prompt, ")");
  51. }
  52. }
  53. strcat(prompt, ": ");
  54. start_col = strlen(prompt);
  55. display_prompt_and_response(prompt, cmdtext);
  56. cpos = strlen(cmdtext);
  57. for (;;) {
  58. tb_present();
  59. if(tb_poll_event(&ev) != TB_OK) return;
  60. if(!ev.mod)
  61. c = ev.ch;
  62. else
  63. c = ev.key;
  64. /* ignore control keys other than return, C-g, backspace, CR, C-s, C-R, ESC */
  65. if (c < 32 &&
  66. c != TB_KEY_CTRL_G &&
  67. c != TB_KEY_BACKSPACE &&
  68. c != TB_KEY_BACKSPACE2 &&
  69. c != TB_KEY_ENTER &&
  70. c != TB_KEY_ESC)
  71. continue;
  72. switch(c) {
  73. case TB_KEY_ENTER: /* return */
  74. done = TRUE;
  75. break;
  76. case TB_KEY_ESC: /* esc */
  77. case TB_KEY_CTRL_G: /* ctrl-g */
  78. tb_set_cursor(0, MSGLINE);
  79. clrtoeol("", MSGLINE);
  80. return;
  81. case TB_KEY_BACKSPACE2: /* del, erase */
  82. case TB_KEY_BACKSPACE: /* backspace */
  83. if (cpos == 0)
  84. continue;
  85. cmdtext[--cpos] = '\0';
  86. tb_set_cursor(start_col + cpos, MSGLINE);
  87. display_prompt_and_response(prompt, cmdtext);
  88. break;
  89. default:
  90. if (cpos < STRBUF_M - 1) {
  91. tb_set_cursor(start_col + cpos, MSGLINE);
  92. cmdtext[cpos++] = c;
  93. cmdtext[cpos] = '\0';
  94. addch(c);
  95. }
  96. break;
  97. }
  98. if(done)
  99. break;
  100. }
  101. if(cmdtext[0] == '\0') {
  102. if(io && opcmdtext[0] != '\0')
  103. strncpy(cmdtext, opcmdtext, STRBUF_M);
  104. else if(!io && ipcmdtext[0] != '\0')
  105. strncpy(cmdtext, ipcmdtext, STRBUF_M);
  106. else
  107. return;
  108. }
  109. if(io)
  110. strncpy(opcmdtext, cmdtext, STRBUF_M);
  111. else
  112. strncpy(ipcmdtext, cmdtext, STRBUF_M);
  113. if (curbp->b_mark != NOMARK && curbp->b_point != curbp->b_mark) {
  114. oscrap = (char_t*) malloc(nscrap);
  115. onscrap = nscrap;
  116. (void) memcpy(oscrap, scrap, nscrap * sizeof (char_t));
  117. copy_cut(TRUE, TRUE, FALSE);
  118. hasregion = TRUE;
  119. } else {
  120. undoset(5, FALSE);
  121. }
  122. strcpy(temp, editor_dir);
  123. tb_shutdown();
  124. if(hasregion) {
  125. /* Find all dollar signs and increase the size by one for each sign. */
  126. for(int i = 0; scrap[i] != '\0'; i++) {
  127. if(scrap[i] == '$' || scrap[i] == '`')
  128. escaped_size += 2;
  129. else
  130. escaped_size++;
  131. }
  132. escaped_region = malloc(sizeof(char_t *)*escaped_size);
  133. /* Escape all $ with \$. This prevents the echo command from trying to
  134. do a variable substitution.
  135. */
  136. for(int i = 0, k = 0; scrap[i] != '\0'; i++, k++) {
  137. if(scrap[i] == '$' || scrap[i] == '`') {
  138. escaped_region[k] = '\\';
  139. k++;
  140. escaped_region[k] = scrap[i];
  141. } else {
  142. escaped_region[k] = scrap[i];
  143. }
  144. }
  145. cmdsize = 6*4*escaped_size*strlen(cmdtext);
  146. command = malloc(sizeof(char *)*cmdsize);
  147. strcpy(command, "echo \"");
  148. strcat(command, (char *)escaped_region);
  149. strcat(command, "\" | ");
  150. strcat(command, cmdtext);
  151. } else {
  152. cmdsize = strlen(cmdtext);
  153. command = malloc(sizeof(char *)*cmdsize);
  154. strcpy(command, cmdtext);
  155. }
  156. // Setup our pipe for reading and execute our command.
  157. pf = popen(command,"r");
  158. if(pf == NULL){
  159. msg("Could not open pipe for output.");
  160. return;
  161. }
  162. data = malloc(sizeof(char *) * 512 * 3);
  163. fgets(data, sizeof(char *) * 512 * 3, pf);
  164. tb_init();
  165. LINES = tb_height();
  166. COLS = tb_width();
  167. MSGLINE = LINES-1;
  168. tb_set_input_mode(TB_INPUT_ALT);
  169. /* Mark the log for update */
  170. redraw();
  171. /* check if canceled command */
  172. if(data[0] == -1 || data[0] == 0) {
  173. if(io == 0) {
  174. /* put the original contents back in the buffer and reset scrap */
  175. paste_internal(TRUE);
  176. free(scrap);
  177. nscrap = onscrap;
  178. scrap = (char_t*) malloc(nscrap);
  179. (void) memcpy(scrap, oscrap, nscrap * sizeof (char_t));
  180. }
  181. } else {
  182. switch(io) {
  183. case 0: {
  184. /* TODO: don't do this with many insert_str calls
  185. make it into one big string and insert it at once.
  186. */
  187. input = (char_t *)data;
  188. insert_str();
  189. while(fgets(data, sizeof(char *)*512*3, pf) != NULL) {
  190. input = (char_t *)data;
  191. insert_str();
  192. }
  193. if (curbp->b_point >= curbp->b_epage)
  194. curbp->b_reframe = 1;
  195. if(nscrap > 0) {
  196. curbp->b_line++;
  197. lastcommand = KBD_DELETE_CHAR;
  198. backsp();
  199. }
  200. if(oscrap != NULL)
  201. free(oscrap);
  202. break;
  203. }
  204. case 1: {
  205. data[strlen(data)-1] = '\0';
  206. /* Find the file name and find the line number */
  207. while(data[i] != '\0') {
  208. if(isLineNumber) {
  209. str_line[k] = data[i];
  210. k++;
  211. } else if(data[i] != ':') {
  212. temp[strlen(editor_dir)+i] = data[i];
  213. } else {
  214. isLineNumber = TRUE;
  215. l = i;
  216. }
  217. i++;
  218. }
  219. if(!isLineNumber)
  220. l = i;
  221. temp[strlen(editor_dir)+l] = '\0';
  222. bp = find_buffer(temp, TRUE);
  223. disassociate_b(curwp);
  224. curbp = bp;
  225. associate_b2w(curbp, curwp);
  226. /* load the file if not already loaded */
  227. if (bp != NULL && bp->b_fname[0] == '\0') {
  228. if (!load_file(temp)) {
  229. msg("New file %s", temp);
  230. }
  231. strncpy(curbp->b_fname, temp, NAME_MAX);
  232. curbp->b_fname[NAME_MAX] = '\0'; /* truncate if required */
  233. if(isLineNumber) {
  234. line = atoi(str_line);
  235. point = line_to_point(line);
  236. if (point != -1) {
  237. curbp->b_point = point;
  238. if (curbp->b_epage < pos(curbp, curbp->b_ebuf))
  239. curbp->b_reframe = 1;
  240. msg("Line %d", line);
  241. } else {
  242. msg("Line %d, not found", line);
  243. }
  244. update_display(TRUE);
  245. }
  246. }
  247. break;
  248. }
  249. }
  250. }
  251. if(command != NULL)
  252. free(command);
  253. if(data != NULL)
  254. free(data);
  255. if (pclose(pf) != 0)
  256. msg("Error: Failed to close command stream.");
  257. }