complete.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* complete.c, Ait Emacs, Kevin Bloom, BSD 3-Clause, 2023-2025 */
  2. #include <sys/stat.h>
  3. #include "header.h"
  4. #include "termbox.h"
  5. /* basic filename completion, based on code in uemacs/PK */
  6. int getfilename(char *prompt, char *buf, int nbuf)
  7. {
  8. static char temp_file[15] = TEMPFILE;
  9. int cpos = 0; /* current character position in string */
  10. int k = 0, c, fd, didtry, iswild = 0, start_col = strlen(prompt);
  11. int isdir = TRUE;
  12. struct tb_event ev;
  13. char sys_command[255];
  14. FILE *fp = NULL;
  15. struct stat s;
  16. // buf[0] ='\0';
  17. cpos = strlen(buf);
  18. display_prompt_and_response(prompt, buf);
  19. for (;;) {
  20. didtry = (k == 0x09); /* Was last command tab-completion? */
  21. tb_present();
  22. if(tb_poll_event(&ev) != TB_OK) return 0;
  23. if(msgline_editor(ev, prompt, buf, nbuf, &cpos)) {
  24. k = 0;
  25. continue;
  26. }
  27. if(!ev.mod)
  28. k = ev.ch;
  29. else
  30. k = ev.key;
  31. if (k < 32 &&
  32. k != TB_KEY_CTRL_G &&
  33. k != TB_KEY_BACKSPACE &&
  34. k != TB_KEY_BACKSPACE2 &&
  35. k != TB_KEY_ENTER &&
  36. k != TB_KEY_CTRL_R &&
  37. k != TB_KEY_CTRL_S &&
  38. k != TB_KEY_ESC &&
  39. k != TB_KEY_CTRL_I &&
  40. k != TB_KEY_TAB)
  41. continue;
  42. switch(k) {
  43. case TB_KEY_CTRL_J: /* cr, lf */
  44. case TB_KEY_ENTER: {
  45. if(isdir) {
  46. didtry = 0;
  47. break;
  48. }
  49. /* backup files end with ~, therefore, don't tab if the ~ is at
  50. the end
  51. */
  52. char *idx = strchr(buf, '~');
  53. if (cpos > 0 && (idx != NULL && idx != &buf[strlen(buf)-1]))
  54. goto do_tab;
  55. }
  56. case TB_KEY_CTRL_G: /* ctrl-g, abort */
  57. if (fp != NULL) fclose(fp);
  58. tb_set_cursor(0, MSGLINE);
  59. clrtoeol(0, MSGLINE);
  60. return (k != 0x07 && cpos > 0);
  61. case TB_KEY_CTRL_U: /* C-u kill */
  62. cpos = 0;
  63. buf[0] = '\0';
  64. break;
  65. do_tab:
  66. case TB_KEY_TAB: /* TAB, complete file name */
  67. /* scan backwards for a wild card and set */
  68. iswild=0;
  69. while (cpos > 0) {
  70. cpos--;
  71. if (buf[cpos] == '*' || buf[cpos] == '?')
  72. iswild = 1;
  73. }
  74. /* first time retrieval */
  75. if (! didtry) {
  76. if (fp != NULL) fclose(fp);
  77. strncpy(temp_file, TEMPFILE, 14);
  78. if (-1 == (fd = mkstemp(temp_file)))
  79. fatal("%s: Failed to create temp file\n");
  80. strcpy(sys_command, "printf \"%s\\n\" ");
  81. strcat(sys_command, buf);
  82. if (!iswild) strcat(sys_command, "*");
  83. strcat(sys_command, " >");
  84. strcat(sys_command, temp_file);
  85. strcat(sys_command, " 2>&1");
  86. (void) ! system(sys_command); /* stop compiler unused result warning */
  87. fp = fdopen(fd, "r");
  88. unlink(temp_file);
  89. }
  90. /* copy next filename into buf */
  91. while ((c = getc(fp)) != EOF && c != '\n')
  92. if (cpos < nbuf - 1 && c != '*')
  93. buf[cpos++] = c;
  94. buf[cpos] = '\0';
  95. if(stat(buf, &s) == 0 && s.st_mode & S_IFDIR) {
  96. buf[cpos++] = '/';
  97. isdir = TRUE;
  98. } else {
  99. isdir = FALSE;
  100. }
  101. buf[cpos] = '\0';
  102. for(int i = cpos+1; buf[i] != '\0'; i++)
  103. buf[i] = 0;
  104. if (c != '\n' || c == -1) rewind(fp);
  105. if(c == -1) goto do_tab;
  106. didtry = 1;
  107. tb_set_cursor(start_col, MSGLINE);
  108. clrtoeol(start_col, MSGLINE);
  109. addstr(buf);
  110. break;
  111. default:
  112. if (cpos < nbuf - 1) {
  113. for(int i = strlen(buf); i > cpos; i--) {
  114. buf[i] = buf[i - 1];
  115. }
  116. buf[cpos] = k;
  117. buf[strlen(buf)] = '\0';
  118. tb_set_cursor(start_col, MSGLINE);
  119. addstr(buf);
  120. cpos++;
  121. tb_set_cursor(start_col + cpos, MSGLINE);
  122. isdir = k == '/';
  123. }
  124. break;
  125. }
  126. }
  127. }
  128. buffer_t * getbuffername(char *prompt, char *buf, int nbuf, int *ret)
  129. {
  130. int cpos = 0; /* current character position in string */
  131. int k = 0, c, didtry = 0;
  132. int start_col = strlen(prompt);
  133. int total_buffers = count_buffers();
  134. int filtered_buffers = total_buffers;
  135. struct tb_event ev;
  136. buffer_t *bp = NULL, *bbp = NULL;
  137. char tabbuffer[PATH_MAX];
  138. buf[0] ='\0';
  139. cpos = strlen(buf);
  140. display_prompt_and_response(prompt, buf);
  141. for (;;) {
  142. /* Since the number of valid buffer name options changes based on
  143. what is input, we have to recalculate the valid total number of
  144. buffers before we continue.
  145. */
  146. if(tabbuffer[0] == '\0')
  147. filtered_buffers = total_buffers;
  148. else {
  149. filtered_buffers = 0;
  150. for (bbp = bheadp; bbp != NULL; bbp = bbp->b_next) {
  151. if(strncmp(tabbuffer, bbp->b_bname, strlen(tabbuffer)) == 0)
  152. filtered_buffers++;
  153. }
  154. }
  155. didtry = (k == 0x09) ? didtry + 1 : 0; /* Was last command tab-completion? */
  156. if(k == 0x09 && didtry > filtered_buffers) {
  157. didtry = 1;
  158. }
  159. tb_present();
  160. if(tb_poll_event(&ev) != TB_OK) return 0;
  161. if(msgline_editor(ev, prompt, buf, nbuf, &cpos)) {
  162. k = 0;
  163. continue;
  164. }
  165. if(!ev.mod)
  166. k = ev.ch;
  167. else
  168. k = ev.key;
  169. switch(k) {
  170. case TB_KEY_CTRL_C: {
  171. char *list, *command;
  172. int ots = FALSE;
  173. FILE *file;
  174. struct stat sb;
  175. if(switch_command == NULL) {
  176. break;
  177. }
  178. list = malloc(total_buffers*sizeof(char *)*(STRBUF_L+1));
  179. memset(list, 0, total_buffers*sizeof(char *)*(STRBUF_L+1));
  180. bp = bheadp;
  181. strcpy(list, bp->b_bname);
  182. strcat(list, " ");
  183. bp = bp->b_next;
  184. for (; bp != NULL; bp = bp->b_next) {
  185. strcat(list, bp->b_bname);
  186. strcat(list, " ");
  187. }
  188. if(switch_command[0] == '!') {
  189. ots = TRUE;
  190. switch_command[0] = ' ';
  191. }
  192. asprintf(&command, "printf \"%%s\\n\" %s | %s%s", list, switch_command, ots ? "" : " > /tmp/ait-temp.txt");
  193. tb_shutdown();
  194. system(command);
  195. if (stat("/tmp/ait-temp.txt", &sb) < 0) {
  196. msg("Failed to find temp file.");
  197. return 0;
  198. }
  199. if (MAX_SIZE_T < sb.st_size) {
  200. msg("Temp file is too big to load.");
  201. return 0;
  202. }
  203. if ((file = fopen("/tmp/ait-temp.txt", "r")) == NULL) {
  204. msg("Failed to open temp file.");
  205. return 0;
  206. }
  207. ngtemp = sb.st_size * sizeof (char_t);
  208. if(gtemp != NULL) {
  209. free(gtemp);
  210. gtemp = NULL;
  211. }
  212. if(ngtemp > 0) {
  213. gtemp = calloc(ngtemp + 1, sizeof(char));
  214. fread(gtemp, sizeof(char), ngtemp, file);
  215. gtemp[ngtemp-1] = '\0'; // there is usually a trailing newline
  216. }
  217. fclose(file);
  218. remove("/tmp/ait-temp.txt");
  219. tb_init();
  220. LINES = tb_height();
  221. COLS = tb_width();
  222. MSGLINE = LINES-1;
  223. tb_set_input_mode(TB_INPUT_ALT);
  224. redraw();
  225. if(gtemp[0] == -1 || gtemp[0] == 0) {
  226. free(list);
  227. free(command);
  228. free(gtemp);
  229. ngtemp = 0;
  230. break;
  231. }
  232. for (bp = bheadp; bp != NULL; bp = bp->b_next) {
  233. /* gtemp has a trailing \n character that we don't want to compare */
  234. if(strncmp(gtemp, bp->b_bname, ngtemp - 1) == 0) {
  235. free(list);
  236. free(command);
  237. free(gtemp);
  238. ngtemp = 0;
  239. *ret = 1;
  240. return bp;
  241. }
  242. }
  243. display_prompt_and_response(prompt, buf);
  244. break;
  245. }
  246. case TB_KEY_CTRL_J: /* cr, lf */
  247. case TB_KEY_ENTER:
  248. *ret = 1;
  249. return bp;
  250. case TB_KEY_CTRL_G: /* ctrl-g, abort */
  251. tb_set_cursor(0, MSGLINE);
  252. clrtoeol(0, MSGLINE);
  253. return NULL;
  254. case TB_KEY_BACKSPACE2: /* del, erase */
  255. case TB_KEY_BACKSPACE: /* backspace */
  256. if (cpos == 0) continue;
  257. buf[--cpos] = '\0';
  258. tb_set_cursor(start_col + cpos, MSGLINE);
  259. addch(' ');
  260. tb_set_cursor(start_col + cpos, MSGLINE);
  261. break;
  262. /* case TB_KEY_CTRL_U:
  263. cpos = 0;
  264. buf[0] = '\0';
  265. break;
  266. */
  267. case TB_KEY_TAB: /* TAB, complete file name */
  268. if(didtry == 0) {
  269. strncpy(tabbuffer, buf, PATH_MAX);
  270. didtry++;
  271. }
  272. for(c = 1, bp = bheadp; bp != NULL; bp = bp->b_next) {
  273. if(tabbuffer[0] == '\0') {
  274. if (didtry == c) {
  275. strncpy(buf, bp->b_bname, PATH_MAX);
  276. break;
  277. }
  278. c++;
  279. continue;
  280. }
  281. if(strncmp(tabbuffer, bp->b_bname, strlen(tabbuffer)) == 0) {
  282. if(didtry == c) {
  283. strncpy(buf, bp->b_bname, PATH_MAX);
  284. break;
  285. }
  286. c++;
  287. }
  288. }
  289. cpos = 0;
  290. cpos += strlen(buf);
  291. buf[cpos] = '\0';
  292. for(int i = cpos+1; buf[i] != '\0'; i++)
  293. buf[i] = 0;
  294. tb_set_cursor(start_col, MSGLINE);
  295. clrtoeol(start_col, MSGLINE);
  296. addstr(buf);
  297. break;
  298. default:
  299. if (cpos < nbuf - 1) {
  300. for(int i = strlen(buf); i > cpos; i--) {
  301. buf[i] = buf[i - 1];
  302. }
  303. buf[cpos] = k;
  304. tb_set_cursor(start_col, MSGLINE);
  305. addstr(buf);
  306. cpos++;
  307. tb_set_cursor(start_col + cpos, MSGLINE);
  308. }
  309. break;
  310. }
  311. }
  312. }