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