Annotation of src/usr.bin/mg/main.c, Revision 1.73
1.73 ! florian 1: /* $OpenBSD: main.c,v 1.72 2014/03/22 11:05:37 lum 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"
12: #include "macro.h"
1.1 deraadt 13:
1.24 vincent 14: #include <err.h>
1.70 naddy 15: #include <locale.h>
1.24 vincent 16:
1.5 millert 17: int thisflag; /* flags, this command */
18: int lastflag; /* flags, last command */
19: int curgoal; /* goal column */
1.36 deraadt 20: int startrow; /* row to start */
1.71 lum 21: int doaudiblebell; /* audible bell toggle */
22: int dovisiblebell; /* visible bell toggle */
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.66 lum 29: static void edinit(struct buffer *);
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.66 lum 46: char *cp, *init_fcn_name = NULL;
47: PF init_fcn = NULL;
48: int o, i, nfiles;
49: int nobackups = 0;
50: struct buffer *bp = NULL;
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.70 naddy 68:
69: setlocale(LC_CTYPE, "");
1.1 deraadt 70:
1.12 art 71: maps_init(); /* Keymaps and modes. */
1.11 art 72: funmap_init(); /* Functions. */
1.19 vincent 73:
1.13 art 74: /*
75: * This is where we initialize standalone extensions that should
76: * be loaded dynamically sometime in the future.
77: */
78: {
79: extern void grep_init(void);
1.14 art 80: extern void theo_init(void);
1.58 kjell 81: extern void cmode_init(void);
1.42 kjell 82: extern void dired_init(void);
1.13 art 83:
1.42 kjell 84: dired_init();
1.13 art 85: grep_init();
1.14 art 86: theo_init();
1.58 kjell 87: cmode_init();
1.13 art 88: }
1.5 millert 89:
1.24 vincent 90: if (init_fcn_name &&
91: (init_fcn = name_function(init_fcn_name)) == NULL)
92: errx(1, "Unknown function `%s'", init_fcn_name);
93:
94: vtinit(); /* Virtual terminal. */
95: dirinit(); /* Get current directory. */
1.66 lum 96: edinit(bp); /* Buffers, windows. */
1.24 vincent 97: ttykeymapinit(); /* Symbols, bindings. */
1.71 lum 98: bellinit(); /* Audible and visible bell. */
1.24 vincent 99:
1.4 millert 100: /*
101: * doing update() before reading files causes the error messages from
102: * the file I/O show up on the screen. (and also an extra display of
103: * the mode line if there are files specified on the command line.)
1.1 deraadt 104: */
1.71 lum 105: update(CMODE);
1.5 millert 106:
1.65 lum 107: /* user startup file. */
108: if ((cp = startupfile(NULL)) != NULL)
109: (void)load(cp);
110:
111: /*
112: * Now ensure any default buffer modes from the startup file are
113: * given to any files opened when parsing the startup file.
114: * Note *scratch* will also be updated.
115: */
116: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
117: bp->b_flag = defb_flag;
118: for (i = 0; i <= defb_nmodes; i++) {
119: bp->b_modes[i] = defb_modes[i];
120: }
121: }
1.59 kjell 122:
123: /* Force FFOTHARG=1 so that this mode is enabled, not simply toggled */
124: if (init_fcn)
125: init_fcn(FFOTHARG, 1);
126:
1.35 henning 127: if (nobackups)
128: makebkfile(FFARG, 0);
1.27 vincent 129:
130: for (nfiles = 0, i = 0; i < argc; i++) {
131: if (argv[i][0] == '+' && strlen(argv[i]) >= 2) {
1.43 deraadt 132: long long lval;
1.30 pvalchev 133: const char *errstr;
1.23 deraadt 134:
1.32 vincent 135: lval = strtonum(&argv[i][1], INT_MIN, INT_MAX, &errstr);
1.33 vincent 136: if (argv[i][1] == '\0' || errstr != NULL)
1.23 deraadt 137: goto notnum;
1.30 pvalchev 138: startrow = lval;
1.24 vincent 139: } else {
1.23 deraadt 140: notnum:
1.52 jason 141: cp = adjustname(argv[i], FALSE);
1.24 vincent 142: if (cp != NULL) {
1.38 db 143: if (nfiles == 1)
1.27 vincent 144: splitwind(0, 1);
1.38 db 145:
1.39 beck 146: if ((curbp = findbuffer(cp)) == NULL) {
147: vttidy();
148: errx(1, "Can't find current buffer!");
149: }
1.24 vincent 150: (void)showbuffer(curbp, curwp, 0);
1.44 deraadt 151: if (readin(cp) != TRUE)
1.34 jfb 152: killbuffer(curbp);
1.38 db 153: else {
1.59 kjell 154: /* Ensure enabled, not just toggled */
1.34 jfb 155: if (init_fcn_name)
1.59 kjell 156: init_fcn(FFOTHARG, 1);
1.34 jfb 157: nfiles++;
158: }
1.24 vincent 159: }
1.22 vincent 160: }
1.1 deraadt 161: }
1.29 vincent 162:
163: if (nfiles > 2)
164: listbuffers(0, 1);
1.5 millert 165:
166: /* fake last flags */
167: thisflag = 0;
1.4 millert 168: for (;;) {
1.50 kjell 169: if (epresf == KCLEAR)
1.4 millert 170: eerase();
1.50 kjell 171: if (epresf == TRUE)
172: epresf = KCLEAR;
1.17 deraadt 173: if (winch_flag) {
1.49 otto 174: do_redraw(0, 0, TRUE);
1.17 deraadt 175: winch_flag = 0;
176: }
1.71 lum 177: update(CMODE);
1.4 millert 178: lastflag = thisflag;
179: thisflag = 0;
1.5 millert 180:
1.4 millert 181: switch (doin()) {
182: case TRUE:
183: break;
1.1 deraadt 184: case ABORT:
1.5 millert 185: ewprintf("Quit");
1.48 kjell 186: /* FALLTHRU */
1.1 deraadt 187: case FALSE:
188: default:
1.4 millert 189: macrodef = FALSE;
190: }
1.1 deraadt 191: }
192: }
193:
194: /*
1.66 lum 195: * Initialize default buffer and window. Default buffer is called *scratch*.
1.1 deraadt 196: */
1.7 art 197: static void
1.66 lum 198: edinit(struct buffer *bp)
1.4 millert 199: {
1.45 deraadt 200: struct mgwin *wp;
1.1 deraadt 201:
202: bheadp = NULL;
1.66 lum 203: bp = bfind("*scratch*", TRUE); /* Text buffer. */
1.63 lum 204: if (bp == NULL)
205: panic("edinit");
206:
1.26 vincent 207: wp = new_window(bp);
1.21 vincent 208: if (wp == NULL)
1.66 lum 209: panic("edinit: Out of memory");
1.63 lum 210:
1.66 lum 211: curbp = bp; /* Current buffer. */
1.1 deraadt 212: wheadp = wp;
1.4 millert 213: curwp = wp;
1.5 millert 214: wp->w_wndp = NULL; /* Initialize window. */
1.54 kjell 215: wp->w_linep = wp->w_dotp = bp->b_headp;
1.38 db 216: wp->w_ntrows = nrow - 2; /* 2 = mode, echo. */
1.61 kjell 217: wp->w_rflag = WFMODE | WFFULL; /* Full. */
1.1 deraadt 218: }
219:
220: /*
1.5 millert 221: * Quit command. If an argument, always quit. Otherwise confirm if a buffer
222: * has been changed and not written out. Normally bound to "C-X C-C".
1.1 deraadt 223: */
1.4 millert 224: /* ARGSUSED */
1.5 millert 225: int
1.20 vincent 226: quit(int f, int n)
1.1 deraadt 227: {
1.5 millert 228: int s;
1.1 deraadt 229:
1.4 millert 230: if ((s = anycb(FALSE)) == ABORT)
1.38 db 231: return (ABORT);
1.69 lum 232: if (s == FIOERR || s == UERROR)
1.68 lum 233: return (FALSE);
1.1 deraadt 234: if (s == FALSE
1.41 kjell 235: || eyesno("Modified buffers exist; really exit") == TRUE) {
1.1 deraadt 236: vttidy();
1.62 lum 237: closetags();
1.73 ! florian 238: exit(0);
1.1 deraadt 239: }
1.38 db 240: return (TRUE);
1.1 deraadt 241: }
242:
243: /*
1.10 mickey 244: * User abort. Should be called by any input routine that sees a C-g to abort
1.5 millert 245: * whatever C-g is aborting these days. Currently does nothing.
1.1 deraadt 246: */
1.4 millert 247: /* ARGSUSED */
248: int
1.20 vincent 249: ctrlg(int f, int n)
1.1 deraadt 250: {
1.24 vincent 251: return (ABORT);
1.1 deraadt 252: }