forked from hughbarney/femto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
153 lines (126 loc) · 3.54 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* main.c, femto, Hugh Barney, Public Domain, 2017
* Derived from: Anthony's Editor January 93, (Public Domain 1991, 1993 by Anthony Howe)
*/
#include "header.h"
int main(int argc, char **argv)
{
setup_keys();
(void)init_lisp();
setlocale(LC_ALL, "") ; /* required for 3,4 byte UTF8 chars */
if (initscr() == NULL) fatal(f_initscr);
raw();
noecho();
idlok(stdscr, TRUE);
start_color();
init_pair(ID_DEFAULT, COLOR_CYAN, COLOR_BLACK); /* alpha */
init_pair(ID_SYMBOL, COLOR_WHITE, COLOR_BLACK); /* non alpha, non digit */
init_pair(ID_MODELINE, COLOR_BLACK, COLOR_WHITE); /* modeline */
init_pair(ID_DIGITS, COLOR_YELLOW, COLOR_BLACK); /* digits */
init_pair(ID_BLOCK_COMMENT, COLOR_GREEN, COLOR_BLACK); /* block comments */
init_pair(ID_LINE_COMMENT, COLOR_GREEN, COLOR_BLACK); /* line comments */
init_pair(ID_SINGLE_STRING, COLOR_YELLOW, COLOR_BLACK); /* single quoted strings */
init_pair(ID_DOUBLE_STRING, COLOR_YELLOW, COLOR_BLACK); /* double quoted strings */
init_pair(ID_BRACE, COLOR_BLACK, COLOR_CYAN); /* brace highlight */
if (1 < argc) {
char bname[NBUFN];
char fname[NAME_MAX + 1];
/* Save filename irregardless of load() success. */
safe_strncpy(fname, argv[1], NAME_MAX);
make_buffer_name(bname, fname);
curbp = find_buffer(bname, TRUE);
(void)insert_file(fname, FALSE);
strcpy(curbp->b_fname, fname);
} else {
curbp = find_buffer(str_scratch, TRUE);
strncpy(curbp->b_bname, str_scratch, STRBUF_S);
}
wheadp = curwp = new_window();
one_window(curwp);
associate_b2w(curbp, curwp);
beginning_of_buffer();
load_config();
while (!done) {
update_display();
input = get_key(khead, &key_return);
if (key_return != NULL) {
(key_return->k_func)();
} else {
/*
* if first char of input is a control char then
* key is not bound, except TAB and NEWLINE
*/
if (*input > 31 || *input == 0x0A || *input == 0x09)
insert();
else {
flushinp(); /* discard without writing in buffer */
msg(str_not_bound);
}
}
/* debug_stats("main loop:"); */
match_parens();
}
if (scrap != NULL) free(scrap);
move(LINES-1, 0);
refresh();
noraw();
endwin();
return 0;
}
void fatal(char *msg)
{
if (curscr != NULL) {
noraw();
endwin();
}
printf("\n%s %s:\n%s\n", E_NAME, E_VERSION, msg);
exit(1);
}
void msg(char *m, ...)
{
va_list args;
va_start(args, m);
(void) vsprintf(msgline, m, args);
va_end(args);
msgflag = TRUE;
}
void load_config()
{
char fname[300];
char *output;
int fd;
reset_output_stream();
(void)snprintf(fname, 300, "%s/%s", getenv("HOME"), E_INITFILE);
if ((fd = open(fname, O_RDONLY)) == -1)
fatal("failed to open " E_INITFILE " in HOME directory");
reset_output_stream();
output = load_file(fd);
assert(output != NULL);
close(fd);
/* all exceptions start with the word error: */
if (NULL != strstr(output, "error:"))
fatal(output);
reset_output_stream();
}
void suspend_process()
{
/* send a signal to ourself */
raise(SIGTSTP);
}
void debug(char *format, ...)
{
char buffer[256]; /* warning this is limited size, we should use vnsprintf */
va_list args;
va_start (args, format);
static FILE *debug_fp = NULL;
if (debug_fp == NULL) {
debug_fp = fopen("debug.out","w");
}
vsprintf (buffer, format, args);
va_end(args);
fprintf(debug_fp,"%s", buffer);
fflush(debug_fp);
}
void debug_stats(char *s) {
debug("%s bsz=%d p=%d m=%d gap=%d egap=%d\n", s, curbp->b_ebuf - curbp->b_buf, curbp->b_point, curbp->b_mark, curbp->b_gap - curbp->b_buf, curbp->b_egap - curbp->b_buf);
}