-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
333 lines (278 loc) · 8.58 KB
/
init.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
Copyright (c) 1993-Present, Jason W. Bacon, Acadix Software Systems
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. Redistributions
in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <twintk.h>
#include <xtend/proc.h>
//#include <xtend/file.h>
#include "edit.h"
#include "protos.h"
void
load_keymap (term_t *terminal)
{
char *term_type;
/* Pop-down menu esc sequences */
taddseq(terminal, "\033f", FILE_TWC_MENU);
taddseq(terminal, "\033e", EDIT_TWC_MENU);
taddseq(terminal, "\033s", SEARCH_TWC_MENU);
taddseq(terminal, "\033b", BUILD_TWC_MENU);
taddseq(terminal, "\033o", OPTIONS_TWC_MENU);
taddseq(terminal, "\033c", CUSTOM_TWC_MENU);
taddseq(terminal, "\033h", HELP_TWC_MENU);
taddseq(terminal, "\033k", RESUME_SEARCH);
/* Pop down menus with ALT keys */
taddseq(terminal, "\346", FILE_TWC_MENU);
taddseq(terminal, "\345", EDIT_TWC_MENU);
taddseq(terminal, "\363", SEARCH_TWC_MENU);
taddseq(terminal, "\342", BUILD_TWC_MENU);
taddseq(terminal, "\357", OPTIONS_TWC_MENU);
taddseq(terminal, "\343", CUSTOM_TWC_MENU);
taddseq(terminal, "\350", HELP_TWC_MENU);
/* Various Esc followed by other keys */
taddseq(terminal, "\033a", PAGE_TOP);
/*taddseq(terminal, "\033g", GOTO_LINE);*/
taddseq(terminal, "\033i", COMPILE_PROG);
taddseq(terminal, "\033l", LAST_LINE);
taddseq(terminal, "\033m", MARK_AREA);
taddseq(terminal, "\033n", NEXT_ERROR);
taddseq(terminal, "\033r", RUN_PROG);
taddseq(terminal, "\033w", UPLOAD_PROG);
taddseq(terminal, "\033p", PASTE_AREA);
taddseq(terminal, "\033t", FIRST_LINE);
taddseq(terminal, "\033u", UNDO);
taddseq(terminal, "\033y", SYNTAX_CHECK);
taddseq(terminal, "\033x", BUILD_PROG);
taddseq(terminal, "\033\033", SAVE_FILE);
taddseq(terminal, "\033/", QUICK_FIND);
taddseq(terminal, "\033\r", INVOKE_MACRO);
taddseq(terminal, "\033?", KEY_HELP);
taddseq(terminal, "\033j", JOIN_LINES);
taddseq(terminal, "\0331", MARK_COL1);
term_type = getenv("TERM");
if (term_type == NULL)
{
fprintf(stderr, "Terminal not defined in environment.\n");
exit(1);
}
}
/*
* Process command line arguments
*/
int
do_args (int argc, char *argv[], file_t files[], opt_t *options)
{
extern int errno;
int aw = NO_FILES, arg, a;
char startup_dir[APE_PATH_MAX + 1],
*buttons[2] = OK_BUTTON;
if (argc > 1)
{
if ( getcwd(startup_dir, APE_PATH_MAX) == NULL )
{
popup_mesg(strerror(errno),buttons,options);
}
for (arg = MIN(argc - 1, options->max_files); arg > 0; --arg)
{
if (*argv[arg] != '-')
{
chdir(startup_dir); /* Open file may do a chdir() */
if ((a = open_file(files, argv[arg], options, OPEN_FLAG_NORMAL)) != CANT_OPEN)
aw = a;
}
}
if (aw == NO_FILES)
aw = open_file(files, "untitled", options, OPEN_FLAG_NORMAL);
}
else
aw = open_file(files, "untitled", options, OPEN_FLAG_NORMAL);
return aw;
}
/*
* Check for a makefile and offer to set up the project
*/
int
check_makefile (proj_t *project, char *makefile, file_t *file, opt_t *options)
{
struct stat st;
/* Need msg2 static sized array so message can be modified */
/* Constant initializers to pointers are in read-only segment */
char msg[100],
*buttons[3] = YES_NO_BUTTONS;
int retcode = 0;
snprintf(msg, 99, "Use \"%s?\"", makefile);
if ( stat(makefile,&st) == 0 )
{
msg[5] = *makefile;
if ( tolower(popup_mesg(msg,buttons,options)) == 'y' )
{
init_makefile(project,makefile,file,options);
retcode = 1;
}
}
else
retcode = -1; /* File does not exist */
TW_RESTORE_WIN(file->window);
return retcode;
}
/*
* See if APE is running on the proper host
*/
void
check_hostname (void)
{
static char legal_host[APE_PATH_MAX + 1] = "Change this";
char this_host[APE_PATH_MAX + 1];
gethostname(this_host, APE_PATH_MAX);
if (strcmp(this_host, legal_host) != 0)
{
fprintf(stderr, "Warning: APE is not legally installed on %s.\n",
this_host);
fputs("Please review the license agreement.\n", stderr);
fputs("Press return to continue...\n", stderr);
getchar();
}
}
void
check_args (char *argv[])
{
char **p;
for (p=argv; *p != NULL; ++p)
{
if ( **p == '-' )
{
switch(*(*p+1))
{
case 'h':
default:
xt_va_usage("%s [-h[elp]] [filespec]\n",*argv);
break;
}
}
}
}
int
init_xterm (void)
{
char *display_name, *window_str, home[APE_PATH_MAX+1],
xdefaults[APE_PATH_MAX+13];
struct stat st;
/* Set up .Xdefaults if it doesn't exist */
xt_get_home_dir(home,APE_PATH_MAX);
snprintf(xdefaults,APE_PATH_MAX + 12,"%s/.Xdefaults",home);
if ( stat(xdefaults,&st) == -1 )
if ( xt_spawnlp(P_WAIT,P_NOECHO,NULL,NULL,NULL,"xapedefaults",NULL) != 0 )
{
fprintf(stderr,
"Installation problem: Unable to run \"xapedefaults\".\nPress return to continue...\n");
fflush(stderr);
getchar();
}
/* See if we're in the X11 env */
if ( (display_name=getenv("DISPLAY")) == NULL )
return -1;
/* Get Window ID from env set by xterm */
if ( (window_str = getenv("WINDOWID")) == NULL )
return -1;
else
return strtol(window_str,NULL,10);
}
void
register_signal_handlers (void)
{
/* Request notification when background jobs end */
#ifdef SCO_SV
sigset(SIGALRM, SIG_IGN);
/*sigset(SIGCHLD, (sig_t)notify);*/
sigset(SIGWINCH, (sig_t)win_resize);
sigset(SIGINT, SIG_IGN); /* Protect editor when running child */
sigset(SIGTERM, (sig_t)kamakaze);
signal(SIGQUIT, SIG_IGN);
#else
signal(SIGALRM, SIG_IGN);
/*signal(SIGCHLD, (sig_t)notify);*/
signal(SIGWINCH, (sig_t)win_resize);
signal(SIGINT, SIG_IGN); /* Protect editor when running child */
signal(SIGTERM, (sig_t)kamakaze);
signal(SIGQUIT, SIG_IGN);
#endif
}
void
setup_terminal (void)
{
extern term_t *Terminal;
if ((Terminal = init_term(stdin, stdout, stderr,
NULL, MOUSE_ON)) == NULL)
{
fprintf(stderr, "APE: Cannot use \"%s\" terminal.\n",
Terminal->term_type);
exit(1);
}
if ((TLINES(Terminal) < 24) || (TCOLS(Terminal) < 80))
{
fprintf(stderr, "APE: Sorry, terminal must have at least 24 lines and 80 columns.\n");
exit(1);
}
/* Set terminal screen attributes */
if ( Terminal->ena_acs != NULL )
raw_print(Terminal,Terminal->ena_acs);
tset_modes(Terminal, NORMAL_MODE);
tset_foreground(Terminal, WHITE);
tset_background(Terminal, BLACK);
tclear_screen(Terminal);
TCURSOR_NORMAL(Terminal);
#if 0
/* Initialize xterm mouse if possible */
if ( (Terminal->windowid = init_xterm()) != -1 )
{
define_cursor(XC_left_ptr);
}
#endif
TFLUSH_OUT(Terminal);
}
#if 0
void
define_cursor (int cursor_font)
{
char font[10];
snprintf(font,9,"%d",cursor_font);
/* Change mouse cursor appearance */
xt_spawnlp(P_WAIT,P_NOECHO,NULL,NULL,NULL,"defcursor",font,NULL);
}
#endif
void
init_compiler_lines (file_t files[], opt_t *options)
{
size_t c, f;
for (f=0; f<options->max_files; ++f)
if ( files[f].window != NULL )
for (c=0; c<files[f].total_lines; ++c)
files[f].line[c].compiler_line = c+1;
}