Annotation of src/usr.bin/mg/main.c, Revision 1.62
1.62 ! lum 1: /* $OpenBSD: main.c,v 1.61 2009/06/04 02:23:37 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;
1.62 ! lum 33: extern void closetags(void);
1.53 kjell 34:
35: static __dead void
36: usage()
37: {
1.57 sobrado 38: fprintf(stderr, "usage: %s [-n] [-f mode] [+number] [file ...]\n",
1.53 kjell 39: __progname);
40: exit(1);
41: }
1.1 deraadt 42:
1.2 deraadt 43: int
1.19 vincent 44: main(int argc, char **argv)
1.1 deraadt 45: {
1.24 vincent 46: char *cp, *init_fcn_name = NULL;
1.38 db 47: PF init_fcn = NULL;
1.44 deraadt 48: int o, i, nfiles;
1.38 db 49: int nobackups = 0;
1.59 kjell 50: struct buffer *bp;
1.24 vincent 51:
1.56 cloder 52: while ((o = getopt(argc, argv, "nf:")) != -1)
1.24 vincent 53: switch (o) {
1.35 henning 54: case 'n':
55: nobackups = 1;
56: break;
1.24 vincent 57: case 'f':
58: if (init_fcn_name != NULL)
59: errx(1, "cannot specify more than one "
60: "initial function");
61: init_fcn_name = optarg;
62: break;
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.58 kjell 79: extern void cmode_init(void);
1.42 kjell 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.58 kjell 85: cmode_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:
1.59 kjell 110: /*
111: * Create scratch buffer now, killing old *init* buffer.
112: * This causes *scratch* to be created and made curbp,
113: * ensuring default modes are inherited from the startup
114: * file correctly
115: */
116:
117: if ((bp = bfind("*init*", FALSE)) != NULL)
118: killbuffer(bp);
119:
120: /* Force FFOTHARG=1 so that this mode is enabled, not simply toggled */
121: if (init_fcn)
122: init_fcn(FFOTHARG, 1);
123:
1.35 henning 124: if (nobackups)
125: makebkfile(FFARG, 0);
1.27 vincent 126:
127: for (nfiles = 0, i = 0; i < argc; i++) {
128: if (argv[i][0] == '+' && strlen(argv[i]) >= 2) {
1.43 deraadt 129: long long lval;
1.30 pvalchev 130: const char *errstr;
1.23 deraadt 131:
1.32 vincent 132: lval = strtonum(&argv[i][1], INT_MIN, INT_MAX, &errstr);
1.33 vincent 133: if (argv[i][1] == '\0' || errstr != NULL)
1.23 deraadt 134: goto notnum;
1.30 pvalchev 135: startrow = lval;
1.24 vincent 136: } else {
1.23 deraadt 137: notnum:
1.52 jason 138: cp = adjustname(argv[i], FALSE);
1.24 vincent 139: if (cp != NULL) {
1.38 db 140: if (nfiles == 1)
1.27 vincent 141: splitwind(0, 1);
1.38 db 142:
1.39 beck 143: if ((curbp = findbuffer(cp)) == NULL) {
144: vttidy();
145: errx(1, "Can't find current buffer!");
146: }
1.24 vincent 147: (void)showbuffer(curbp, curwp, 0);
1.44 deraadt 148: if (readin(cp) != TRUE)
1.34 jfb 149: killbuffer(curbp);
1.38 db 150: else {
1.59 kjell 151: /* Ensure enabled, not just toggled */
1.34 jfb 152: if (init_fcn_name)
1.59 kjell 153: init_fcn(FFOTHARG, 1);
1.34 jfb 154: nfiles++;
155: }
1.24 vincent 156: }
1.22 vincent 157: }
1.1 deraadt 158: }
1.29 vincent 159:
160: if (nfiles > 2)
161: listbuffers(0, 1);
1.5 millert 162:
163: /* fake last flags */
164: thisflag = 0;
1.4 millert 165: for (;;) {
1.50 kjell 166: if (epresf == KCLEAR)
1.4 millert 167: eerase();
1.50 kjell 168: if (epresf == TRUE)
169: epresf = KCLEAR;
1.17 deraadt 170: if (winch_flag) {
1.49 otto 171: do_redraw(0, 0, TRUE);
1.17 deraadt 172: winch_flag = 0;
173: }
1.4 millert 174: update();
175: lastflag = thisflag;
176: thisflag = 0;
1.5 millert 177:
1.4 millert 178: switch (doin()) {
179: case TRUE:
180: break;
1.1 deraadt 181: case ABORT:
1.5 millert 182: ewprintf("Quit");
1.48 kjell 183: /* FALLTHRU */
1.1 deraadt 184: case FALSE:
185: default:
1.4 millert 186: ttbeep();
1.1 deraadt 187: #ifndef NO_MACRO
1.4 millert 188: macrodef = FALSE;
1.5 millert 189: #endif /* !NO_MACRO */
1.4 millert 190: }
1.1 deraadt 191: }
192: }
193:
194: /*
195: * Initialize default buffer and window.
1.59 kjell 196: * Initially, buffer is named *init*. This is changed later
197: * to *scratch* after the startup files are read.
1.1 deraadt 198: */
1.7 art 199: static void
1.24 vincent 200: edinit(PF init_fcn)
1.4 millert 201: {
1.45 deraadt 202: struct buffer *bp;
203: struct mgwin *wp;
1.1 deraadt 204:
205: bheadp = NULL;
1.59 kjell 206: bp = bfind("*init*", TRUE); /* Text buffer. */
1.26 vincent 207: wp = new_window(bp);
1.21 vincent 208: if (wp == NULL)
209: panic("Out of memory");
1.4 millert 210: if (bp == NULL || wp == NULL)
211: panic("edinit");
1.5 millert 212: curbp = bp; /* Current ones. */
1.1 deraadt 213: wheadp = wp;
1.4 millert 214: curwp = wp;
1.5 millert 215: wp->w_wndp = NULL; /* Initialize window. */
1.54 kjell 216: wp->w_linep = wp->w_dotp = bp->b_headp;
1.38 db 217: wp->w_ntrows = nrow - 2; /* 2 = mode, echo. */
1.61 kjell 218: wp->w_rflag = WFMODE | WFFULL; /* Full. */
1.1 deraadt 219: }
220:
221: /*
1.5 millert 222: * Quit command. If an argument, always quit. Otherwise confirm if a buffer
223: * has been changed and not written out. Normally bound to "C-X C-C".
1.1 deraadt 224: */
1.4 millert 225: /* ARGSUSED */
1.5 millert 226: int
1.20 vincent 227: quit(int f, int n)
1.1 deraadt 228: {
1.5 millert 229: int s;
1.1 deraadt 230:
1.4 millert 231: if ((s = anycb(FALSE)) == ABORT)
1.38 db 232: return (ABORT);
1.1 deraadt 233: if (s == FALSE
1.41 kjell 234: || eyesno("Modified buffers exist; really exit") == TRUE) {
1.1 deraadt 235: vttidy();
1.5 millert 236: #ifdef SYSCLEANUP
1.4 millert 237: SYSCLEANUP;
1.5 millert 238: #endif /* SYSCLEANUP */
1.62 ! lum 239: closetags();
1.1 deraadt 240: exit(GOOD);
241: }
1.38 db 242: return (TRUE);
1.1 deraadt 243: }
244:
245: /*
1.10 mickey 246: * User abort. Should be called by any input routine that sees a C-g to abort
1.5 millert 247: * whatever C-g is aborting these days. Currently does nothing.
1.1 deraadt 248: */
1.4 millert 249: /* ARGSUSED */
250: int
1.20 vincent 251: ctrlg(int f, int n)
1.1 deraadt 252: {
1.24 vincent 253: return (ABORT);
1.1 deraadt 254: }