Annotation of src/usr.bin/mg/main.c, Revision 1.51
1.51 ! kjell 1: /* $OpenBSD: main.c,v 1.50 2006/04/03 02:43:22 kjell Exp $ */
1.40 kjell 2:
3: /* This file is in the public domain. */
1.6 niklas 4:
1.1 deraadt 5: /*
1.5 millert 6: * Mainline.
1.1 deraadt 7: */
1.5 millert 8:
1.38 db 9: #include "def.h"
10: #include "kbd.h"
11: #include "funmap.h"
1.5 millert 12:
1.1 deraadt 13: #ifndef NO_MACRO
1.38 db 14: #include "macro.h"
1.5 millert 15: #endif /* NO_MACRO */
1.1 deraadt 16:
1.24 vincent 17: #include <err.h>
18:
1.5 millert 19: int thisflag; /* flags, this command */
20: int lastflag; /* flags, last command */
21: int curgoal; /* goal column */
1.36 deraadt 22: int startrow; /* row to start */
1.45 deraadt 23: struct buffer *curbp; /* current buffer */
24: struct buffer *bheadp; /* BUFFER list head */
25: struct mgwin *curwp; /* current window */
26: struct mgwin *wheadp; /* MGWIN listhead */
1.5 millert 27: char pat[NPAT]; /* pattern */
1.1 deraadt 28:
1.24 vincent 29: static void edinit(PF);
1.1 deraadt 30:
1.2 deraadt 31: int
1.19 vincent 32: main(int argc, char **argv)
1.1 deraadt 33: {
1.24 vincent 34: char *cp, *init_fcn_name = NULL;
1.38 db 35: PF init_fcn = NULL;
1.44 deraadt 36: int o, i, nfiles;
1.38 db 37: int nobackups = 0;
1.24 vincent 38:
1.35 henning 39: while ((o = getopt(argc, argv, "nf:")) != -1)
1.24 vincent 40: switch (o) {
1.35 henning 41: case 'n':
42: nobackups = 1;
43: break;
1.24 vincent 44: case 'f':
45: if (init_fcn_name != NULL)
46: errx(1, "cannot specify more than one "
47: "initial function");
48: init_fcn_name = optarg;
49: break;
50: default:
1.37 jmc 51: errx(1, "usage: mg [options] [file ...]");
1.24 vincent 52: }
53: argc -= optind;
54: argv += optind;
1.1 deraadt 55:
1.12 art 56: maps_init(); /* Keymaps and modes. */
1.11 art 57: funmap_init(); /* Functions. */
1.19 vincent 58:
1.13 art 59: /*
60: * This is where we initialize standalone extensions that should
61: * be loaded dynamically sometime in the future.
62: */
63: {
64: extern void grep_init(void);
1.14 art 65: extern void theo_init(void);
1.42 kjell 66: extern void mail_init(void);
67: extern void dired_init(void);
1.13 art 68:
1.42 kjell 69: dired_init();
1.13 art 70: grep_init();
1.14 art 71: theo_init();
1.24 vincent 72: mail_init();
1.13 art 73: }
1.5 millert 74:
1.24 vincent 75: if (init_fcn_name &&
76: (init_fcn = name_function(init_fcn_name)) == NULL)
77: errx(1, "Unknown function `%s'", init_fcn_name);
78:
79: vtinit(); /* Virtual terminal. */
80: dirinit(); /* Get current directory. */
81: edinit(init_fcn); /* Buffers, windows. */
82: ttykeymapinit(); /* Symbols, bindings. */
83:
1.4 millert 84: /*
85: * doing update() before reading files causes the error messages from
86: * the file I/O show up on the screen. (and also an extra display of
87: * the mode line if there are files specified on the command line.)
1.1 deraadt 88: */
89: update();
1.5 millert 90:
91: #ifndef NO_STARTUP
92: /* user startup file */
1.8 art 93: if ((cp = startupfile(NULL)) != NULL)
1.7 art 94: (void)load(cp);
1.5 millert 95: #endif /* !NO_STARTUP */
1.35 henning 96:
97: if (nobackups)
98: makebkfile(FFARG, 0);
1.27 vincent 99:
100: for (nfiles = 0, i = 0; i < argc; i++) {
101: if (argv[i][0] == '+' && strlen(argv[i]) >= 2) {
1.43 deraadt 102: long long lval;
1.30 pvalchev 103: const char *errstr;
1.23 deraadt 104:
1.32 vincent 105: lval = strtonum(&argv[i][1], INT_MIN, INT_MAX, &errstr);
1.33 vincent 106: if (argv[i][1] == '\0' || errstr != NULL)
1.23 deraadt 107: goto notnum;
1.30 pvalchev 108: startrow = lval;
1.24 vincent 109: } else {
1.23 deraadt 110: notnum:
1.27 vincent 111: cp = adjustname(argv[i]);
1.24 vincent 112: if (cp != NULL) {
1.38 db 113: if (nfiles == 1)
1.27 vincent 114: splitwind(0, 1);
1.38 db 115:
1.39 beck 116: if ((curbp = findbuffer(cp)) == NULL) {
117: vttidy();
118: errx(1, "Can't find current buffer!");
119: }
1.24 vincent 120: (void)showbuffer(curbp, curwp, 0);
1.44 deraadt 121: if (readin(cp) != TRUE)
1.34 jfb 122: killbuffer(curbp);
1.38 db 123: else {
1.34 jfb 124: if (init_fcn_name)
125: init_fcn(0, 1);
126: nfiles++;
127: }
1.24 vincent 128: }
1.22 vincent 129: }
1.1 deraadt 130: }
1.29 vincent 131:
132: if (nfiles > 2)
133: listbuffers(0, 1);
1.5 millert 134:
135: /* fake last flags */
136: thisflag = 0;
1.4 millert 137: for (;;) {
1.50 kjell 138: if (epresf == KCLEAR)
1.4 millert 139: eerase();
1.50 kjell 140: if (epresf == TRUE)
141: epresf = KCLEAR;
1.17 deraadt 142: if (winch_flag) {
1.49 otto 143: do_redraw(0, 0, TRUE);
1.17 deraadt 144: winch_flag = 0;
145: }
1.4 millert 146: update();
147: lastflag = thisflag;
148: thisflag = 0;
1.5 millert 149:
1.4 millert 150: switch (doin()) {
151: case TRUE:
152: break;
1.1 deraadt 153: case ABORT:
1.5 millert 154: ewprintf("Quit");
1.48 kjell 155: /* FALLTHRU */
1.1 deraadt 156: case FALSE:
157: default:
1.4 millert 158: ttbeep();
1.1 deraadt 159: #ifndef NO_MACRO
1.4 millert 160: macrodef = FALSE;
1.5 millert 161: #endif /* !NO_MACRO */
1.4 millert 162: }
1.1 deraadt 163: }
164: }
165:
166: /*
167: * Initialize default buffer and window.
168: */
1.7 art 169: static void
1.24 vincent 170: edinit(PF init_fcn)
1.4 millert 171: {
1.45 deraadt 172: struct buffer *bp;
173: struct mgwin *wp;
1.1 deraadt 174:
175: bheadp = NULL;
1.5 millert 176: bp = bfind("*scratch*", TRUE); /* Text buffer. */
1.26 vincent 177: wp = new_window(bp);
1.21 vincent 178: if (wp == NULL)
179: panic("Out of memory");
1.4 millert 180: if (bp == NULL || wp == NULL)
181: panic("edinit");
1.5 millert 182: curbp = bp; /* Current ones. */
1.1 deraadt 183: wheadp = wp;
1.4 millert 184: curwp = wp;
1.5 millert 185: wp->w_wndp = NULL; /* Initialize window. */
1.1 deraadt 186: wp->w_linep = wp->w_dotp = bp->b_linep;
1.38 db 187: wp->w_ntrows = nrow - 2; /* 2 = mode, echo. */
1.51 ! kjell 188: wp->w_flag = WFMODE | WFFULL; /* Full. */
1.24 vincent 189:
190: if (init_fcn)
1.25 vincent 191: init_fcn(0, 1);
1.1 deraadt 192: }
193:
194: /*
1.5 millert 195: * Quit command. If an argument, always quit. Otherwise confirm if a buffer
196: * has been changed and not written out. Normally bound to "C-X C-C".
1.1 deraadt 197: */
1.4 millert 198: /* ARGSUSED */
1.5 millert 199: int
1.20 vincent 200: quit(int f, int n)
1.1 deraadt 201: {
1.5 millert 202: int s;
1.1 deraadt 203:
1.4 millert 204: if ((s = anycb(FALSE)) == ABORT)
1.38 db 205: return (ABORT);
1.1 deraadt 206: if (s == FALSE
1.41 kjell 207: || eyesno("Modified buffers exist; really exit") == TRUE) {
1.1 deraadt 208: vttidy();
1.5 millert 209: #ifdef SYSCLEANUP
1.4 millert 210: SYSCLEANUP;
1.5 millert 211: #endif /* SYSCLEANUP */
1.1 deraadt 212: exit(GOOD);
213: }
1.38 db 214: return (TRUE);
1.1 deraadt 215: }
216:
217: /*
1.10 mickey 218: * User abort. Should be called by any input routine that sees a C-g to abort
1.5 millert 219: * whatever C-g is aborting these days. Currently does nothing.
1.1 deraadt 220: */
1.4 millert 221: /* ARGSUSED */
222: int
1.20 vincent 223: ctrlg(int f, int n)
1.1 deraadt 224: {
1.24 vincent 225: return (ABORT);
1.1 deraadt 226: }