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