shell.c 7.3 KB

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