main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 record_input = FALSE;
  27. int record_buffer_index = 0;
  28. int run_buffer_index = 0;
  29. struct tb_event record_buffer[256];
  30. int execute_kbd_macro = FALSE;
  31. int undo_index = -1;
  32. char unicode_buf[7];
  33. char character[1];
  34. int lastcommand = KBD_DEFAULT;
  35. int currentcommand = KBD_DEFAULT;
  36. int window_mode = WINDOW_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. buffer_t *lastbp = NULL; /* last active buffer */
  44. int LINES;
  45. int COLS;
  46. int MSGLINE;
  47. static void graceful_exit()
  48. {
  49. tb_shutdown();
  50. exit(1);
  51. }
  52. static void cont()
  53. {
  54. tb_init();
  55. LINES = tb_height();
  56. COLS = tb_width();
  57. MSGLINE = LINES-1;
  58. tb_set_input_mode(TB_INPUT_ALT);
  59. tb_clear();
  60. redraw();
  61. update_display();
  62. }
  63. static void setup_signal_handlers()
  64. {
  65. struct sigaction action;
  66. memset(&action, 0, sizeof(struct sigaction));
  67. action.sa_handler = graceful_exit;
  68. sigaction(SIGTERM, &action, NULL);
  69. sigaction(SIGINT, &action, NULL);
  70. sigaction(SIGQUIT, &action, NULL);
  71. sigaction(SIGHUP, &action, NULL);
  72. action.sa_handler = cont;
  73. sigaction(SIGCONT, &action, NULL);
  74. signal(SIGPIPE, SIG_IGN);
  75. }
  76. int main(int argc, char **argv)
  77. {
  78. int ret, u, buffers = 0;
  79. int line = 0, current = 0, lastln = 0;
  80. point_t p;
  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);
  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. curbp->b_line = 1;
  121. if(argv[v+1]) {
  122. if(argv[v+1][0] == '-' || argv[v+1][0] == '+') {
  123. if(argv[v+1][0] == '+') {
  124. argv[v+1]++;
  125. line = atoi(argv[v+1]);
  126. } else {
  127. argv[v+1]++;
  128. get_line_stats(&current, &lastln, curbp);
  129. line = lastln - atoi(argv[v+1]);
  130. if(line < 0)
  131. line = 0;
  132. }
  133. p = line_to_point(line);
  134. if (p != -1) {
  135. curbp->b_point = p;
  136. if (curbp->b_epage < pos(curbp, curbp->b_ebuf)) curbp->b_reframe = 1;
  137. curbp->b_line = line;
  138. msg("Line %d", line);
  139. } else {
  140. msg("Line %d, not found", line);
  141. }
  142. v++;
  143. }
  144. }
  145. }
  146. } else {
  147. curbp = find_buffer("*scratch*", TRUE);
  148. strncpy(curbp->b_bname, "*scratch*", STRBUF_S);
  149. curbp->b_path = FALSE;
  150. }
  151. if (!growgap(curbp, CHUNK))
  152. fatal("%s: Failed to allocate required memory.\n");
  153. movegap(curbp, 0);
  154. key_map = keymap;
  155. submatch = 0;
  156. wheadp = curwp = new_window();
  157. associate_b2w(curbp, curwp);
  158. one_window(curwp);
  159. if(buffers > 1) {
  160. chop_window();
  161. next_buffer();
  162. }
  163. while (!done) {
  164. update_display();
  165. curbp->b_opoint = curbp->b_point;
  166. input = get_key(key_map, &key_return);
  167. if (key_return != NULL) {
  168. /* TODO: a better way to figure out editing commands */
  169. if((key_return->key_desc[2] == 'd' || key_return->key_desc[4] == '%' ||
  170. key_return->key_desc[2] == 'i' || key_return->key_desc[2] == 'k' ||
  171. key_return->key_desc[2] == '/' || key_return->key_desc[2] == 'm' ||
  172. key_return->key_desc[2] == 't' || key_return->key_desc[2] == 'y' ||
  173. key_return->key_desc[4] == 'd' || key_return->key_desc[4] == 'i' ||
  174. key_return->key_desc[4] == 't' || key_return->key_desc[4] == 'x' ||
  175. key_return->key_desc[4] == 'k' || key_return->key_desc[4] == '/' ||
  176. key_return->key_desc[4] == 'l' || key_return->key_desc[4] == 'c' ||
  177. key_return->key_desc[4] == 'u' || key_return->key_desc[4] == 'z' ||
  178. key_return->key_desc[4] == 'Z' || (key_return->key_desc[4] == 'b'
  179. && key_return->key_desc[5] == 'k') || key_return->key_desc[2] == 'h') &&
  180. is_file_modified(curbp->b_fname)) {
  181. if(!file_was_modified_prompt()) {
  182. continue;
  183. }
  184. }
  185. submatch = 0;
  186. if(execute_kbd_macro) {
  187. (key_return->func)();
  188. } else {
  189. u = numeric_argument > 0 ? numeric_argument : power(4, universal_argument);
  190. if(numeric_argument > 0 &&
  191. key_return->universal_argument_action != UA_PREVENT)
  192. key_return->universal_argument_action = UA_REPEAT;
  193. switch(key_return->universal_argument_action) {
  194. case UA_REPEAT:
  195. for(; u > 0; u--)
  196. (key_return->func)();
  197. universal_argument = 0;
  198. numeric_argument = 0;
  199. /* For gotochar */
  200. character[0] = '\0';
  201. break;
  202. default:
  203. (key_return->func)();
  204. break;
  205. }
  206. }
  207. if(temp[0] != 0)
  208. memset(temp, 0, TEMPBUF);
  209. } else if(submatch > 0) {
  210. // do nothing
  211. } else {
  212. submatch = 0;
  213. if((unicode_buf[0] != '\0' || *input > 31 || *input == 13 || *input == 9)
  214. && is_file_modified(curbp->b_fname)) {
  215. if(!file_was_modified_prompt()) {
  216. continue;
  217. }
  218. }
  219. /* allow TAB and NEWLINE, otherwise any Control Char is 'Not bound' */
  220. if(unicode_buf[0] != '\0') {
  221. u = numeric_argument > 0 ? numeric_argument : power(4, universal_argument);
  222. char tempbuf[7];
  223. strncpy(tempbuf, unicode_buf, 7);
  224. for(; u > 0; u--) {
  225. unicode_buf[0] = tempbuf[0];
  226. insert_unicode();
  227. }
  228. }
  229. else
  230. msg("Not bound");
  231. }
  232. if(currentcommand != KBD_DELETE_CHAR &&
  233. currentcommand != KBD_CUT &&
  234. currentcommand != KBD_UNDO
  235. ) {
  236. if(curbp->b_opoint > curbp->b_point) {
  237. curbp->b_opoint--;
  238. while(curbp->b_opoint >= curbp->b_point) {
  239. if(*ptr(curbp, curbp->b_opoint) == '\n')
  240. curbp->b_line--;
  241. curbp->b_opoint--;
  242. }
  243. } else if(curbp->b_opoint < curbp->b_point) {
  244. while(curbp->b_opoint < curbp->b_point) {
  245. if(*ptr(curbp, curbp->b_opoint) == '\n')
  246. curbp->b_line++;
  247. curbp->b_opoint++;
  248. }
  249. }
  250. }
  251. if(currentcommand != lastcommand)
  252. lastcommand = currentcommand;
  253. currentcommand = KBD_DEFAULT;
  254. }
  255. if (scrap != NULL) free(scrap);
  256. tb_set_cursor(0, LINES-1);
  257. tb_present();
  258. tb_shutdown();
  259. return 0;
  260. }
  261. void fatal(char *msg)
  262. {
  263. tb_present();
  264. fprintf(stderr, msg, PROG_NAME);
  265. exit(1);
  266. }
  267. void msg(char *msg, ...)
  268. {
  269. va_list args;
  270. va_start(args, msg);
  271. (void)vsprintf(msgline, msg, args);
  272. va_end(args);
  273. msgflag = TRUE;
  274. }