forked from rfivet/uemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
456 lines (395 loc) · 10.8 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* main.c -- */
/* µEMACS 4.2
*
* Based on:
*
* uEmacs/PK 4.0
*
* Based on:
*
* MicroEMACS 3.9
* Written by Dave G. Conroy.
* Substantially modified by Daniel M. Lawrence
* Modified by Petri Kutvonen
*
* MicroEMACS 3.9 (c) Copyright 1987 by Daniel M. Lawrence
*
* Original statement of copying policy:
*
* MicroEMACS 3.9 can be copied and distributed freely for any
* non-commercial purposes. MicroEMACS 3.9 can only be incorporated
* into commercial software with the permission of the current author.
*
* No copyright claimed for modifications made by Petri Kutvonen.
*
* This file contains the main driving routine, and some keyboard
* processing code.
*
* REVISION HISTORY:
*
* 1.0 Steve Wilhite, 30-Nov-85
*
* 2.0 George Jones, 12-Dec-85
*
* 3.0 Daniel Lawrence, 29-Dec-85
*
* 3.2-3.6 Daniel Lawrence, Feb...Apr-86
*
* 3.7 Daniel Lawrence, 14-May-86
*
* 3.8 Daniel Lawrence, 18-Jan-87
*
* 3.9 Daniel Lawrence, 16-Jul-87
*
* 3.9e Daniel Lawrence, 16-Nov-87
*
* After that versions 3.X and Daniel Lawrence went their own ways.
* A modified 3.9e/PK was heavily used at the University of Helsinki
* for several years on different UNIX, VMS, and MSDOS platforms.
*
* This modified version is now called eEmacs/PK.
*
* 4.0 Petri Kutvonen, 1-Sep-91
*
* This modified version is now called uEMACS.
*
* 4.1 Renaud Fivet, 1-May-13
*
* Renamed as µEMACS to emphasize UTF-8 support.
*
* 4.2 Renaud Fivet, 2015-02-12
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defines.h" /* OS specific customization */
#if UNIX
# include <signal.h>
# include <unistd.h>
#endif
#include "basic.h"
#include "bind.h"
#include "bindable.h"
#include "buffer.h"
#include "display.h"
#include "eval.h"
#include "execute.h"
#include "file.h"
#include "lock.h"
#include "mlout.h"
#include "random.h"
#include "search.h"
#include "terminal.h"
#include "termio.h"
#include "util.h"
#include "version.h"
#include "window.h"
#if UNIX
static void emergencyexit( int signr) {
quickexit( FALSE, 0) ;
quit( TRUE, 0) ; /* If quickexit fails (to save changes), do a force quit */
}
#endif
static void edinit( char *bname) ;
static void version( void) {
fputs( PROGRAM_NAME_UTF8 " version " VERSION "\n", stdout) ;
}
static void usage( void) {
fputs( "Usage: " PROGRAM_NAME " [OPTION|FILE]..\n\n"
" + start at the end of file\n"
" +<n> start at line <n>\n"
" --help display this help and exit\n"
" --version output version information and exit\n"
" -a|A process error file\n"
" -e|E edit file\n"
" -g|G<n> go to line <n>\n"
" -r|R restrictive use\n"
" -s|S<string> search string\n"
" -v|V view file\n"
" -x|Xcmdfile\n"
" -x|X cmdfile execute command file\n"
" @cmdfile execute startup file\n"
, stdout) ;
}
int main( int argc, char *argv[]) {
buffer_p bp; /* temp buffer pointer */
int firstfile; /* first file flag */
int carg; /* current arg to scan */
int startflag; /* startup executed flag */
buffer_p firstbp = NULL; /* ptr to first buffer in cmd line */
int viewflag; /* are we starting in view mode? */
int gotoflag; /* do we need to goto a line at start? */
int gline = 0; /* if so, what line? */
int searchflag; /* Do we need to search at start? */
int errflag; /* C error processing? */
bname_t bname ; /* buffer name of file to read */
#if PKCODE & BSD
sleep(1); /* Time for window manager. */
#endif
if( argc == 2) {
if( strcmp( argv[ 1], "--help") == 0) {
usage() ;
exit( EXIT_SUCCESS) ;
}
if( strcmp( argv[ 1], "--version") == 0) {
version() ;
exit( EXIT_SUCCESS) ;
}
}
vtinit() ; /* Display */
mloutfmt = mlwrite ;
edinit( "main") ; /* Bindings, buffers, windows */
varinit() ; /* user variables */
viewflag = FALSE; /* view mode defaults off in command line */
gotoflag = FALSE; /* set to off to begin with */
searchflag = FALSE; /* set to off to begin with */
firstfile = TRUE; /* no file to edit yet */
startflag = FALSE; /* startup file not executed yet */
errflag = FALSE; /* not doing C error parsing */
/* Insure screen is initialized before startup and goto/search */
update( FALSE) ;
/* Parse the command line */
for (carg = 1; carg < argc; ++carg) {
/* Process Switches */
#if PKCODE
if (argv[carg][0] == '+') {
gotoflag = TRUE;
gline = atoi(&argv[carg][1]);
} else
#endif
if (argv[carg][0] == '-') {
switch (argv[carg][1]) {
/* Process Startup macroes */
case 'a': /* process error file */
case 'A':
errflag = TRUE;
break;
case 'e': /* -e for Edit file */
case 'E':
viewflag = FALSE;
break;
case 'g': /* -g for initial goto */
case 'G':
gotoflag = TRUE;
gline = atoi(&argv[carg][2]);
break;
case 'r': /* -r restrictive use */
case 'R':
restflag = TRUE;
break;
case 's': /* -s for initial search string */
case 'S':
searchflag = TRUE;
mystrscpy( pat, &argv[ carg][ 2], sizeof pat) ;
break;
case 'v': /* -v for View File */
case 'V':
viewflag = TRUE;
break;
case 'x':
case 'X':
if( argv[ carg][ 2]) { /* -Xfilename */
if( startup( &argv[ carg][ 2]) == TRUE)
startflag = TRUE ; /* don't execute emacs.rc */
} else if( argv[ carg + 1]) { /* -X filename */
if( startup( &argv[ carg + 1][ 0]) == TRUE)
startflag = TRUE ; /* don't execute emacs.rc */
carg += 1 ;
}
break ;
default: /* unknown switch */
/* ignore this for now */
break;
}
} else if (argv[carg][0] == '@') {
/* Process Startup macroes */
if (startup(&argv[carg][1]) == TRUE)
/* don't execute emacs.rc */
startflag = TRUE;
} else {
/* Process an input file */
/* set up a buffer for this file */
makename(bname, argv[carg]);
unqname(bname);
/* set this to inactive */
bp = bfind( bname, TRUE, 0) ;
if( bp == NULL) {
fputs( "Buffer creation failed!\n", stderr) ;
exit( EXIT_FAILURE) ;
}
mystrscpy( bp->b_fname, argv[ carg], sizeof bp->b_fname) ; /* max filename length limited to NFILEN - 1 */
bp->b_active = FALSE;
if (firstfile) {
firstbp = bp;
firstfile = FALSE;
}
/* set the modes appropriatly */
if (viewflag)
bp->b_mode |= MDVIEW;
}
}
#if UNIX
#ifdef SIGHUP
signal(SIGHUP, emergencyexit);
#endif
signal(SIGTERM, emergencyexit);
#endif
/* if we are C error parsing... run it! */
if (errflag) {
if (startup("error.cmd") == TRUE)
startflag = TRUE;
}
/* if invoked with no other startup files,
run the system startup file here */
if( (startflag == FALSE) && (startup( "") != TRUE))
mloutstr( "Default startup failed!") ;
discmd = TRUE; /* P.K. */
/* if there are any files to read, read the first one! */
bp = bfind("main", FALSE, 0);
if( bp == NULL) {
/* "main" buffer has been created during early initialisation */
fputs( "Initialisation failure!\n", stderr) ;
exit( EXIT_FAILURE) ;
}
if (firstfile == FALSE && readfirst_f()) {
swbuffer(firstbp);
zotbuf(bp);
} else {
bp->b_mode |= gmode;
upmode() ;
}
/* Deal with startup gotos and searches */
if( gotoflag && searchflag)
mloutstr( "(Can not search and goto at the same time!)") ;
else if( gotoflag) {
if( gotoline( TRUE, gline) == FALSE)
mloutstr( "(Bogus goto argument)") ;
} else if( searchflag)
if( forwhunt( FALSE, 0))
mloutfmt( "Found on line %d", getcline()) ;
kbd_loop() ;
return EXIT_SUCCESS ; /* never reached */
}
/*
* Initialize all of the buffers and windows. The buffer name is passed down
* as an argument, because the main routine may have been told to read in a
* file by default, and we want the buffer name to be right.
*/
static void edinit( char *bname) {
buffer_p bp;
window_p wp;
if( !init_bindings() /* initialize mapping of function to name and key */
|| NULL == (bp = bfind( bname, TRUE, 0)) /* First buffer */
|| NULL == (blistp = bfind( "*List*", TRUE, BFINVS)) /* Buffer list */
|| NULL == (wp = malloc( sizeof *wp))) { /* First window */
fputs( "First initialisation failed!\n", stderr) ;
exit( EXIT_FAILURE) ;
}
curbp = bp; /* Make this current */
wheadp = wp;
curwp = wp;
wp->w_wndp = NULL; /* Initialize window */
wp->w_bufp = bp;
bp->b_nwnd = 1; /* Displayed. */
wp->w_linep = bp->b_linep;
wp->w_dotp = bp->b_linep;
wp->w_doto = 0;
wp->w_markp = NULL;
wp->w_marko = 0;
wp->w_toprow = 0;
#if COLOR
/* initalize colors to global defaults */
wp->w_fcolor = gfcolor;
wp->w_bcolor = gbcolor;
#endif
wp->w_ntrows = term.t_nrow - 1; /* "-1" for mode line. */
wp->w_force = 0;
wp->w_flag = WFMODE | WFHARD; /* Full. */
}
/***** Compiler specific Library functions ****/
#if RAMSIZE
/* These routines will allow me to track memory usage by placing a layer on
top of the standard system malloc() and free() calls. with this code
defined, the environment variable, $RAM, will report on the number of
bytes allocated via malloc.
with SHOWRAM defined, the number is also posted on the end of the bottom
mode line and is updated whenever it is changed.
*/
#if RAMSHOW
static void dspram( void) ;
#endif
void *allocate( size_t nbytes) {
nbytes += sizeof nbytes ; /* add overhead to track allocation */
size_t *mp = (malloc)( nbytes) ; /* call the function not the macro */
if( mp) {
*mp++ = nbytes ;
envram += nbytes ;
#if RAMSHOW
dspram() ;
#endif
}
return mp ;
}
void release( void *mp) {
if( mp) {
size_t *sp = mp ;
sp-- ;
/* update amount of ram currently malloced */
envram -= *sp ;
(free)( sp) ; /* call the function not the macro */
#if RAMSHOW
dspram() ;
#endif
}
}
#if RAMSHOW
static void dspram( void) { /* display the amount of RAM currently malloced */
char mbuf[ 20] ;
TTmove( term.t_nrow, term.t_ncol - 12) ;
#if COLOR
TTforg( 7) ;
TTbacg(0) ;
#endif
sprintf( mbuf, "[%10u]", envram) ;
char *sp = mbuf ;
while( *sp)
TTputc( *sp++) ;
TTmove( term.t_nrow, 0) ;
movecursor( term.t_nrow, 0) ;
}
#endif
#endif
/* On some primitive operation systems, and when emacs is used as
a subprogram to a larger project, emacs needs to de-alloc its
own used memory
*/
#if CLEAN
/* cexit()
*
* int status; return status of emacs
*/
void cexit( int status) {
/* first clean up the windows */
window_p wp = wheadp ;
while( wp) {
window_p tp = wp->w_wndp ;
free( wp) ;
wp = tp ;
}
wheadp = NULL ;
/* then the buffers */
buffer_p bp ;
while( (bp = bheadp) != NULL) {
bp->b_nwnd = 0 ;
bp->b_flag = 0 ; /* don't say anything about a changed buffer! */
zotbuf( bp) ;
}
/* and the kill buffer */
kdelete() ;
/* and the video buffers */
vtfree() ;
(exit)( status) ; /* call the function, not the macro */
}
#endif
/* end of main.c */