main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* main.c, Ait, BSD 3-Clause, Kevin Bloom, 2023,
  2. Derived from: Atto January 2017
  3. Derived from: Anthony's Editor January 93
  4. */
  5. #include "header.h"
  6. #include "termbox.h"
  7. #include "util.h"
  8. int done;
  9. point_t nscrap;
  10. char_t *scrap;
  11. char_t *input;
  12. int msgflag;
  13. char msgline[TEMPBUF];
  14. char temp[TEMPBUF];
  15. char searchtext[STRBUF_M];
  16. char replace[STRBUF_M];
  17. int found_point = -1;
  18. int search_dir = 1;
  19. int universal_argument = 0;
  20. int numeric_argument = 0;
  21. int negated = FALSE;
  22. int submatch = 0;
  23. uint32_t input_char = 0;
  24. int undoset_flag = FALSE;
  25. char editor_dir[NAME_MAX+1];
  26. int number_of_windows = 0;
  27. window_t *windows[10] = {NULL};
  28. int record_input = FALSE;
  29. int record_buffer_index = 0;
  30. int run_buffer_index = 0;
  31. struct tb_event record_buffer[256];
  32. int execute_kbd_macro = FALSE;
  33. int undo_index = -1;
  34. char unicode_buf[7];
  35. char character[1];
  36. int lastcommand = KBD_DEFAULT;
  37. keymap_t *key_return;
  38. keymap_t *key_map;
  39. buffer_t *curbp; /* current buffer */
  40. buffer_t *bheadp; /* head of list of buffers */
  41. window_t *curwp;
  42. window_t *wheadp;
  43. int LINES;
  44. int COLS;
  45. int MSGLINE;
  46. static void graceful_exit()
  47. {
  48. tb_shutdown();
  49. exit(1);
  50. }
  51. static void cont()
  52. {
  53. tb_init();
  54. LINES = tb_height();
  55. COLS = tb_width();
  56. MSGLINE = LINES-1;
  57. tb_set_input_mode(TB_INPUT_ALT | TB_INPUT_MOUSE);
  58. tb_clear();
  59. update_display(TRUE);
  60. }
  61. static void setup_signal_handlers()
  62. {
  63. struct sigaction action;
  64. memset(&action, 0, sizeof(struct sigaction));
  65. action.sa_handler = graceful_exit;
  66. sigaction(SIGTERM, &action, NULL);
  67. sigaction(SIGINT, &action, NULL);
  68. sigaction(SIGQUIT, &action, NULL);
  69. sigaction(SIGHUP, &action, NULL);
  70. action.sa_handler = cont;
  71. sigaction(SIGCONT, &action, NULL);
  72. signal(SIGPIPE, SIG_IGN);
  73. }
  74. int main(int argc, char **argv)
  75. {
  76. setlocale(LC_ALL, "") ; /* required for 3,4 byte UTF8 chars */
  77. int ret, u, buffers = 0;
  78. int line, current = 0, lastln = 0, nwin = 0;
  79. point_t p, epage, page;
  80. char_t *egap, *gap;
  81. if(1 < argc && argv[1][0] == '-') {
  82. if(argv[1][1] == 'v') {
  83. fprintf(stderr, "%s\n", VERSION);
  84. exit(0);
  85. }
  86. if(argv[1][1] == 'h') {
  87. fprintf(stderr, "%s\n\nUsage: ait [options] [file]... +/-line...\n\n\n-h Print help and exit\n-v Print version and exit\n", VERSION);
  88. exit(0);
  89. }
  90. }
  91. ret = tb_init();
  92. LINES = tb_height();
  93. COLS = tb_width();
  94. MSGLINE = LINES-1;
  95. character[0] = '\0';
  96. if (ret) {
  97. fprintf(stderr, "Failed with error code %d", ret);
  98. exit(1);
  99. }
  100. getcwd(editor_dir, sizeof(editor_dir));
  101. strcat(editor_dir, "/");
  102. tb_set_input_mode(TB_INPUT_ALT | TB_INPUT_MOUSE);
  103. tb_clear();
  104. setup_signal_handlers();
  105. if (1 < argc) {
  106. for(int v = 1; v < argc; v++) {
  107. buffers++;
  108. curbp = find_buffer(argv[v], TRUE);
  109. (void) insert_file(argv[v], FALSE);
  110. /* Save filename regardless of load() success. */
  111. if(argv[v][0] == '/') {
  112. strncpy(curbp->b_fname, argv[v], NAME_MAX);
  113. } else {
  114. strncpy(curbp->b_fname, editor_dir, NAME_MAX);
  115. strcat(curbp->b_fname, argv[v]);
  116. cleanup_path(curbp->b_fname, curbp->b_fname);
  117. }
  118. curbp->b_fname[NAME_MAX] = '\0'; /* force truncation */
  119. curbp->b_path = TRUE;
  120. if(argv[v+1]) {
  121. if(argv[v+1][0] == '-' || argv[v+1][0] == '+') {
  122. if(argv[v+1][0] == '+') {
  123. argv[v+1]++;
  124. line = atoi(argv[v+1]);
  125. } else {
  126. argv[v+1]++;
  127. get_line_stats(&current, &lastln, curbp);
  128. line = lastln - atoi(argv[v+1]);
  129. if(line < 0)
  130. line = 0;
  131. }
  132. p = line_to_point(line);
  133. if (p != -1) {
  134. curbp->b_point = p;
  135. if (curbp->b_epage < pos(curbp, curbp->b_ebuf)) curbp->b_reframe = 1;
  136. msg("Line %d", line);
  137. } else {
  138. msg("Line %d, not found", line);
  139. }
  140. v++;
  141. }
  142. }
  143. }
  144. } else {
  145. curbp = find_buffer("*scratch*", TRUE);
  146. strncpy(curbp->b_bname, "*scratch*", STRBUF_S);
  147. curbp->b_path = FALSE;
  148. }
  149. wheadp = curwp = new_window();
  150. one_window(curwp);
  151. associate_b2w(curbp, curwp);
  152. if(buffers > 1) {
  153. chop_window();
  154. next_buffer();
  155. }
  156. if (!growgap(curbp, CHUNK))
  157. fatal("%s: Failed to allocate required memory.\n");
  158. movegap(curbp, 0);
  159. key_map = keymap;
  160. submatch = 0;
  161. while (!done) {
  162. if(epage <= curbp->b_point || page >= curbp->b_point ||
  163. egap != curbp->b_egap || gap != curbp->b_gap || page != curbp->b_page ||
  164. number_of_windows != nwin)
  165. update_display(TRUE);
  166. else
  167. update_display(FALSE);
  168. input = get_key(key_map, &key_return);
  169. epage = curbp->b_epage;
  170. page = curbp->b_page;
  171. egap = curbp->b_egap;
  172. gap = curbp->b_gap;
  173. nwin = number_of_windows;
  174. if (key_return != NULL) {
  175. submatch = 0;
  176. if(execute_kbd_macro) {
  177. (key_return->func)();
  178. } else {
  179. u = numeric_argument > 0 ? numeric_argument : power(4, universal_argument);
  180. if(numeric_argument > 0 &&
  181. key_return->universal_argument_action != UA_PREVENT)
  182. key_return->universal_argument_action = UA_REPEAT;
  183. switch(key_return->universal_argument_action) {
  184. case UA_REPEAT:
  185. for(; u > 0; u--)
  186. (key_return->func)();
  187. universal_argument = 0;
  188. numeric_argument = 0;
  189. /* For gotochar */
  190. character[0] = '\0';
  191. break;
  192. default:
  193. (key_return->func)();
  194. break;
  195. }
  196. }
  197. if(temp[0] != 0)
  198. memset(temp, 0, TEMPBUF);
  199. } else if(submatch > 0) {
  200. // do nothing
  201. } else {
  202. submatch = 0;
  203. /* allow TAB and NEWLINE, otherwise any Control Char is 'Not bound' */
  204. if(unicode_buf[0] != '\0')
  205. insert_unicode();
  206. else if (*input > 31 || *input == 13 || *input == 9)
  207. insert();
  208. else
  209. msg("Not bound");
  210. }
  211. }
  212. if (scrap != NULL) free(scrap);
  213. tb_set_cursor(0, LINES-1);
  214. tb_present();
  215. tb_shutdown();
  216. return 0;
  217. }
  218. void fatal(char *msg)
  219. {
  220. tb_present();
  221. fprintf(stderr, msg, PROG_NAME);
  222. exit(1);
  223. }
  224. void msg(char *msg, ...)
  225. {
  226. va_list args;
  227. va_start(args, msg);
  228. (void)vsprintf(msgline, msg, args);
  229. va_end(args);
  230. msgflag = TRUE;
  231. }