xref: /openbsd-src/usr.bin/mg/main.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: main.c,v 1.14 2001/06/23 04:22:44 art Exp $	*/
2 
3 /*
4  *	Mainline.
5  */
6 
7 #include	"def.h"
8 #include	"kbd.h"
9 #include	"funmap.h"
10 
11 #ifndef NO_MACRO
12 #include	"macro.h"
13 #endif	/* NO_MACRO */
14 
15 int		 thisflag;			/* flags, this command	*/
16 int		 lastflag;			/* flags, last command	*/
17 int		 curgoal;			/* goal column		*/
18 BUFFER		*curbp;				/* current buffer	*/
19 BUFFER		*bheadp;			/* BUFFER listhead	*/
20 MGWIN		*curwp;				/* current window	*/
21 MGWIN		*wheadp;			/* MGWIN listhead	*/
22 char		 pat[NPAT];			/* pattern		*/
23 
24 static void	 edinit		__P((void));
25 
26 int
27 main(argc, argv)
28 	int	argc;
29 	char	**argv;
30 {
31 	char	*cp;
32 
33 #ifdef SYSINIT
34 	SYSINIT;		/* System dependent.		*/
35 #endif	/* SYSINIT */
36 	vtinit();		/* Virtual terminal.		*/
37 #ifndef NO_DIR
38 	dirinit();		/* Get current directory.	*/
39 #endif	/* !NO_DIR */
40 	edinit();		/* Buffers, windows.		*/
41 	maps_init();		/* Keymaps and modes.		*/
42 	funmap_init();		/* Functions.			*/
43 	ttykeymapinit();	/* Symbols, bindings.		*/
44 
45 	/*
46 	 * This is where we initialize standalone extensions that should
47 	 * be loaded dynamically sometime in the future.
48 	 */
49 	{
50 		extern void grep_init(void);
51 		extern void theo_init(void);
52 
53 		grep_init();
54 		theo_init();
55 	}
56 
57 	/*
58 	 * doing update() before reading files causes the error messages from
59 	 * the file I/O show up on the screen.	(and also an extra display of
60 	 * the mode line if there are files specified on the command line.)
61 	 */
62 	update();
63 
64 #ifndef NO_STARTUP
65 	/* user startup file */
66 	if ((cp = startupfile(NULL)) != NULL)
67 		(void)load(cp);
68 #endif	/* !NO_STARTUP */
69 	while (--argc > 0) {
70 		cp = adjustname(*++argv);
71 		curbp = findbuffer(cp);
72 		(void)showbuffer(curbp, curwp, 0);
73 		(void)readin(cp);
74 	}
75 
76 	/* fake last flags */
77 	thisflag = 0;
78 	for (;;) {
79 #ifndef NO_DPROMPT
80 		if (epresf == KPROMPT)
81 			eerase();
82 #endif	/* !NO_DPROMPT */
83 		update();
84 		lastflag = thisflag;
85 		thisflag = 0;
86 
87 		switch (doin()) {
88 		case TRUE:
89 			break;
90 		case ABORT:
91 			ewprintf("Quit");
92 			/* and fall through */
93 		case FALSE:
94 		default:
95 			ttbeep();
96 #ifndef NO_MACRO
97 			macrodef = FALSE;
98 #endif	/* !NO_MACRO */
99 		}
100 	}
101 }
102 
103 /*
104  * Initialize default buffer and window.
105  */
106 static void
107 edinit()
108 {
109 	BUFFER	*bp;
110 	MGWIN	*wp;
111 
112 	bheadp = NULL;
113 	bp = bfind("*scratch*", TRUE);		/* Text buffer.		 */
114 	wp = (MGWIN *)malloc(sizeof(MGWIN));	/* Initial window.	 */
115 	if (bp == NULL || wp == NULL)
116 		panic("edinit");
117 	curbp = bp;				/* Current ones.	 */
118 	wheadp = wp;
119 	curwp = wp;
120 	wp->w_wndp = NULL;			/* Initialize window.	 */
121 	wp->w_bufp = bp;
122 	bp->b_nwnd = 1;				/* Displayed.		 */
123 	wp->w_linep = wp->w_dotp = bp->b_linep;
124 	wp->w_doto = 0;
125 	wp->w_markp = NULL;
126 	wp->w_marko = 0;
127 	wp->w_toprow = 0;
128 	wp->w_ntrows = nrow - 2;		/* 2 = mode, echo.	 */
129 	wp->w_force = 0;
130 	wp->w_flag = WFMODE | WFHARD;		/* Full.		 */
131 }
132 
133 /*
134  * Quit command.  If an argument, always quit.  Otherwise confirm if a buffer
135  * has been changed and not written out.  Normally bound to "C-X C-C".
136  */
137 /* ARGSUSED */
138 int
139 quit(f, n)
140 	int f, n;
141 {
142 	int	 s;
143 
144 	if ((s = anycb(FALSE)) == ABORT)
145 		return ABORT;
146 	if (s == FALSE
147 	    || eyesno("Some modified buffers exist, really exit") == TRUE) {
148 		vttidy();
149 #ifdef SYSCLEANUP
150 		SYSCLEANUP;
151 #endif	/* SYSCLEANUP */
152 		exit(GOOD);
153 	}
154 	return TRUE;
155 }
156 
157 /*
158  * User abort.  Should be called by any input routine that sees a C-g to abort
159  * whatever C-g is aborting these days. Currently does nothing.
160  */
161 /* ARGSUSED */
162 int
163 ctrlg(f, n)
164 	int f, n;
165 {
166 	return ABORT;
167 }
168