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