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