-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.c
169 lines (137 loc) · 4.2 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* main.c, femto, Hugh Barney, Public Domain, 2017
* Derived from: Anthony's Editor January 93, (Public Domain 1991, 1993 by Anthony Howe)
*/
#include <stdarg.h>
#include <stdio.h>
#include "header.h"
void load_config(); /* Load configuration/init file */
void gui(); /* The GUI loop used in interactive mode */
#define CPP_XSTR(s) CPP_STR(s)
#define CPP_STR(s) #s
int main(int argc, char **argv)
{
char *envv, *flib;
batch_mode = ((envv=getenv("FEMTO_BATCH")) != NULL && strcmp(envv, "0"));
debug_mode = ((envv=getenv("FEMTO_DEBUG")) != NULL && strcmp(envv, "0"));
if ((flib=getenv("FEMTOLIB")) == NULL)
flib = CPP_XSTR(E_SCRIPTDIR);
/* buffers */
setlocale(LC_ALL, "") ; /* required for 3,4 byte UTF8 chars */
curbp = find_buffer(str_scratch, TRUE);
strncpy(curbp->b_bname, str_scratch, STRBUF_S);
beginning_of_buffer();
/* windows */
wheadp = curwp = new_window();
associate_b2w(curbp, curwp);
/* Lisp */
setup_keys();
if (init_lisp(argc, argv, flib))
fatal("fLisp initialization failed");
/* Init file */
char *init_file, cmd[TEMPBUF];
if ((init_file = getenv("FEMTORC")) == NULL)
init_file = CPP_XSTR(E_INITFILE);
snprintf(cmd, TEMPBUF, "(load \"%s\")", init_file);
(void)call_lisp(cmd);
/* GUI */
if (!batch_mode) gui();
debug("main(): shutdown\n");
// Note: exit frees all memory, do we need this here?
if (scrap != NULL) free(scrap);
return 0;
}
void gui()
{
debug("gui(): init\n");
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 */
/* windows */
one_window(curwp);
debug("gui(): loop\n");
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();
}
debug("gui(): shutdown\n");
move(LINES-1, 0);
refresh();
noraw();
endwin();
}
void fatal(char *msg)
{
if (!batch_mode) {
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;
if (batch_mode) {
puts(msgline);
fflush(stdout);
}
}
void debug(char *format, ...)
{
char buffer[256];
va_list args;
if (!debug_mode) return;
va_start (args, format);
static FILE *debug_fp = NULL;
if (debug_fp == NULL)
debug_fp = fopen("debug.out","w");
vsnprintf (buffer, sizeof(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);
}
/*
* Local Variables:
* c-file-style: "k&r"
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/