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