-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
349 lines (285 loc) · 6.66 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Mainline
*/
#include "def.h"
#include "macro.h"
#include <locale.h>
INT thisflag = 0; /* Flags, this command */
INT lastflag = 0; /* Flags, last command */
INT curgoal = 0; /* Goal column */
BUFFER *curbp; /* Current buffer */
WINDOW *curwp; /* Current window */
BUFFER *bheadp = NULL; /* BUFFER listhead */
WINDOW *wheadp = NULL; /* WINDOW listhead */
char pat[NPAT] = ""; /* Pattern */
#ifndef NO_DPROMPT
extern char prompt[], *promptp; /* delayed prompting */
#endif
INT inhibit_startup = 0; /* Inhibit startup files */
extern char version[];
static void edinit(void);
INT gotoline(INT f, INT n);
void iresetmark(void);
static int
usage(char *progname)
{
fprintf(stderr, "Usage:\n %s [-q] [-p pattern] [-L loadfile] [-l loadfile]"
" [-e commands] [+line] [files...]\n", progname);
fprintf(stderr, " %s -v: print program version\n", progname);
return 1;
}
int
main(int argc, char *argv[])
{
char *cp;
BUFFER *bp;
INT c, n;
char *linearg = NULL;
INT lineno;
char *loadfile = NULL, *startexpr = NULL, *pre_loadfile = NULL;
#ifdef PIPEIN
FILE *pipein = NULL;
if (!isatty(fileno(stdin))) {
/*
* Create a copy of stdin for future use
*/
pipein = fdopen(dup(fileno(stdin)),"r");
/*
* Reopen the console device /dev/tty as stdin
*/
stdin = freopen("/dev/tty","r",stdin);
}
#else
if (!isatty(0)) {
fprintf(stderr, "mg: standard input is not a tty\n");
exit(1);
}
#endif
setlocale(LC_ALL, "");
while ((c = getopt(argc, argv, "vqp:L:l:e:")) != -1) {
switch (c) {
case 'v':
fprintf(stdout,"%s\n",version);
exit(0);
break;
case 'q':
inhibit_startup = 1;
break;
case 'p':
stringcopy(pat, optarg, NPAT);
break;
case 'L':
pre_loadfile = optarg;
break;
case 'l':
loadfile = optarg;
break;
case 'e':
startexpr = optarg;
break;
default:
return usage(argv[0]);
}
}
vtinit(); /* Virtual terminal. */
dirinit(); /* Get current directory */
edinit(); /* Buffers, windows. */
/*
* Doing update() before reading files causes the error
* messages from the file I/O show up on the screen. (and also
* an extra display of the mode line if there are files
* specified on the command line.)
*/
update();
ttykeymapinit(); /* Symbols, bindings. */
if (!inhibit_startup && (cp = startupfile(NULL)) != NULL) {
load(cp);
}
/* "-L" argument */
if (pre_loadfile) load(pre_loadfile);
if ((bp = bfind("*scratch*", TRUE)) != NULL) {
resetbuffer(bp); /* Buffer "*scratch*" will need reset of modes */
}
/* Goto line if two arguments */
if (argc - optind == 2) {
if (argv[optind][0] == '+') {
linearg = argv[optind];
optind++;
} else if (argv[argc - 1][0] == '+') {
linearg = argv[argc - 1];
argc--;
}
}
for (n = argc - 1; n >= optind; n--) { /* Backwards, for prettier list */
if ((cp = adjustname(argv[n])) == NULL) continue;
#ifndef DIRED
if (isdirectory(cp)) continue;
#endif
if (existsandnotreadable(cp)) continue;
if ((bp = findbuffer(cp)) == NULL) break;
curbp = bp;
showbuffer(curbp, curwp);
if (readin(cp) != TRUE) break;
}
#ifdef PIPEIN
if (NULL != pipein) {
if ((bp = findbuffer("*stdin*")) != NULL) {
curbp = bp;
showbuffer(curbp, curwp);
freadin(pipein);
curbp->b_flag |= BFREADONLY;
}
}
#endif
if (linearg) {
if (linearg[1] == 0) gotoline(FFARG, 0);
else if (getINT(linearg + 1, &lineno, 1)) gotoline(FFARG, lineno);
}
if (loadfile) load(loadfile);
if (startexpr) excline_copy_list(startexpr);
for(;;) {
#ifndef NO_DPROMPT
*(promptp = prompt) = '\0';
#endif
if (epresf == KPROMPT) eerase();
else if (epresf == TRUE) epresf = KPROMPT;
ucs_adjust();
update();
lastflag = thisflag;
thisflag = 0;
switch(doin()) {
case TRUE: break;
case ABORT:
while (type_ahead()) ttgetc();
if (!(thisflag & CFNOQUIT)) ewprintf("Quit");
/* fallthrough */
case FALSE:
default:
ttbeep();
#ifndef NO_MACRO
macrodef = FALSE;
#endif
}
}
}
/*
* Initialize default buffer and window.
*/
static void
edinit()
{
BUFFER *bp;
WINDOW *wp;
bheadp = NULL;
bp = bfind("*scratch*", TRUE); /* Text buffer. */
wp = (WINDOW *)malloc(sizeof(WINDOW)); /* Initial window. */
if (bp==NULL || wp==NULL) panic("edinit", 0);
curbp = bp; /* Current ones. */
wheadp = wp;
curwp = wp;
wp->w_wndp = NULL; /* Initialize window. */
wp->w_bufp = bp;
bp->b_nwnd = 1; /* Displayed. */
wp->w_linep = wp->w_dotp = bp->b_linep;
wp->w_doto = 0;
wp->w_markp = NULL;
wp->w_marko = 0;
wp->w_toprow = 0;
wp->w_ntrows = nrow-2; /* 2 = mode, echo. */
wp->w_force = 0;
wp->w_flag = WFMODE|WFHARD; /* Full. */
wp->w_dotpos = 0;
variable_completion_init();
}
/*
* Mg3a: Warn for new behavior of "save-buffers-kill-emacs" with an
* argument. Return TRUE if OK to save and exit.
*/
static INT
quit_warning()
{
BUFFER *bp;
INT s;
bp = emptyhelpbuffer();
if (!bp) return FALSE;
if ((s = addline(bp, "Mg now behaves like GNU Emacs when \"save-buffers-kill-emacs\" is called")) != TRUE)
return s;
if ((s = addline(bp, "with an argument: all modified buffers will be saved before exiting.")) != TRUE)
return s;
if ((s = addline(bp, "")) != TRUE)
return s;
if ((s = addline(bp, "To turn off this warning set bit 1 in the variable \"emacs-compat\".")) != TRUE)
return s;
if ((s = addline(bp, "")) != TRUE)
return s;
if ((s = addline(bp, "To exit easily without saving, assign \"kill-emacs\" to a key.")) != TRUE)
return s;
if ((s = popbuftop(bp)) != TRUE) return s;
update();
return eyesno("Save all modified buffers and exit");
}
/*
* Quit command.
*/
INT
quit(INT f, INT n)
{
INT s, saveall = 0;
if (inmacro) {
if (anycb(1) != FALSE) {
return emessage(FALSE, "Some buffers could not be saved; not exiting");
}
vttidy();
exit(GOOD);
}
if (f & FFRAND) {
saveall = 1;
} else if (f & FFARG) {
if (!(emacs_compat & 1) && (s = quit_warning()) != TRUE) return s;
saveall = 1;
}
if ((s = anycb(saveall)) == ABORT) return ABORT;
if (s == FALSE
|| eyesno("Modified buffers exist, really exit") == TRUE) {
vttidy();
exit(GOOD);
}
return TRUE;
}
/*
* Mg3a: Quick exit
*/
INT
hardquit(INT f, INT n)
{
vttidy();
exit(GOOD);
return TRUE;
}
/*
* Mg3a: Save all files and exit (not in Emacs)
*/
INT
save_and_exit(INT f, INT n)
{
return quit(FFRAND, 1);
}
/*
* paaguti: in Emacs, Ctrl-G clears the mark!
*/
INT keyboard_quit(INT f, INT n)
{
ewprintf("Clearing...");
iresetmark();
return TRUE;
}
/*
* User abort. Should be called by any input routine that sees a C-g
* to abort whatever C-g is aborting these days. Currently does
* nothing.
*/
INT
ctrlg(INT f, INT n)
{
return ABORT;
}