[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/main.c, Revision 1.41

1.41    ! kjell       1: /*     $OpenBSD: main.c,v 1.40 2005/06/14 18:14:40 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.5       millert    23: BUFFER         *curbp;                         /* current buffer       */
1.38      db         24: BUFFER         *bheadp;                        /* BUFFER list head     */
1.5       millert    25: MGWIN          *curwp;                         /* current window       */
1.8       art        26: 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;
                     36:        int      o, i, nfiles, status;
                     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.13      art        66:
                     67:                grep_init();
1.14      art        68:                theo_init();
1.24      vincent    69:                mail_init();
1.13      art        70:        }
1.5       millert    71:
1.24      vincent    72:        if (init_fcn_name &&
                     73:            (init_fcn = name_function(init_fcn_name)) == NULL)
                     74:                errx(1, "Unknown function `%s'", init_fcn_name);
                     75:
                     76:        vtinit();               /* Virtual terminal.            */
                     77: #ifndef NO_DIR
                     78:        dirinit();              /* Get current directory.       */
                     79: #endif /* !NO_DIR */
                     80:        edinit(init_fcn);       /* Buffers, windows.            */
                     81:        ttykeymapinit();        /* Symbols, bindings.           */
                     82:
1.4       millert    83:        /*
                     84:         * doing update() before reading files causes the error messages from
                     85:         * the file I/O show up on the screen.  (and also an extra display of
                     86:         * the mode line if there are files specified on the command line.)
1.1       deraadt    87:         */
                     88:        update();
1.5       millert    89:
                     90: #ifndef NO_STARTUP
                     91:        /* user startup file */
1.8       art        92:        if ((cp = startupfile(NULL)) != NULL)
1.7       art        93:                (void)load(cp);
1.5       millert    94: #endif /* !NO_STARTUP */
1.35      henning    95:
                     96:        if (nobackups)
                     97:                makebkfile(FFARG, 0);
1.27      vincent    98:
                     99:        for (nfiles = 0, i = 0; i < argc; i++) {
                    100:                if (argv[i][0] == '+' && strlen(argv[i]) >= 2) {
1.30      pvalchev  101:                        int lval;
                    102:                        const char *errstr;
1.23      deraadt   103:
1.32      vincent   104:                        lval = strtonum(&argv[i][1], INT_MIN, INT_MAX, &errstr);
1.33      vincent   105:                        if (argv[i][1] == '\0' || errstr != NULL)
1.23      deraadt   106:                                goto notnum;
1.30      pvalchev  107:                        startrow = lval;
1.24      vincent   108:                } else {
1.23      deraadt   109: notnum:
1.27      vincent   110:                        cp = adjustname(argv[i]);
1.24      vincent   111:                        if (cp != NULL) {
1.38      db        112:                                if (nfiles == 1)
1.27      vincent   113:                                        splitwind(0, 1);
1.38      db        114:
1.39      beck      115:                                if ((curbp = findbuffer(cp)) == NULL) {
                    116:                                        vttidy();
                    117:                                        errx(1, "Can't find current buffer!");
                    118:                                }
1.24      vincent   119:                                (void)showbuffer(curbp, curwp, 0);
1.38      db        120:                                if ((status = readin(cp)) != TRUE)
1.34      jfb       121:                                        killbuffer(curbp);
1.38      db        122:                                else {
1.34      jfb       123:                                        if (init_fcn_name)
                    124:                                                init_fcn(0, 1);
                    125:                                        nfiles++;
                    126:                                }
1.24      vincent   127:                        }
1.22      vincent   128:                }
1.1       deraadt   129:        }
1.29      vincent   130:
                    131:        if (nfiles > 2)
                    132:                listbuffers(0, 1);
1.5       millert   133:
                    134:        /* fake last flags */
                    135:        thisflag = 0;
1.4       millert   136:        for (;;) {
1.1       deraadt   137: #ifndef NO_DPROMPT
1.4       millert   138:                if (epresf == KPROMPT)
                    139:                        eerase();
1.5       millert   140: #endif /* !NO_DPROMPT */
1.17      deraadt   141:                if (winch_flag) {
                    142:                        refresh(0, 0);
                    143:                        winch_flag = 0;
                    144:                }
1.4       millert   145:                update();
                    146:                lastflag = thisflag;
                    147:                thisflag = 0;
1.5       millert   148:
1.4       millert   149:                switch (doin()) {
                    150:                case TRUE:
                    151:                        break;
1.1       deraadt   152:                case ABORT:
1.5       millert   153:                        ewprintf("Quit");
                    154:                        /* and fall through */
1.1       deraadt   155:                case FALSE:
                    156:                default:
1.4       millert   157:                        ttbeep();
1.1       deraadt   158: #ifndef NO_MACRO
1.4       millert   159:                        macrodef = FALSE;
1.5       millert   160: #endif /* !NO_MACRO */
1.4       millert   161:                }
1.1       deraadt   162:        }
                    163: }
                    164:
                    165: /*
                    166:  * Initialize default buffer and window.
                    167:  */
1.7       art       168: static void
1.24      vincent   169: edinit(PF init_fcn)
1.4       millert   170: {
1.5       millert   171:        BUFFER  *bp;
                    172:        MGWIN   *wp;
1.1       deraadt   173:
                    174:        bheadp = NULL;
1.5       millert   175:        bp = bfind("*scratch*", TRUE);          /* Text buffer.          */
1.26      vincent   176:        wp = new_window(bp);
1.21      vincent   177:        if (wp == NULL)
                    178:                panic("Out of memory");
1.4       millert   179:        if (bp == NULL || wp == NULL)
                    180:                panic("edinit");
1.5       millert   181:        curbp = bp;                             /* Current ones.         */
1.1       deraadt   182:        wheadp = wp;
1.4       millert   183:        curwp = wp;
1.5       millert   184:        wp->w_wndp = NULL;                      /* Initialize window.    */
1.1       deraadt   185:        wp->w_linep = wp->w_dotp = bp->b_linep;
1.38      db        186:        wp->w_ntrows = nrow - 2;                /* 2 = mode, echo.       */
                    187:        wp->w_flag = WFMODE | WFHARD;           /* Full.                 */
1.24      vincent   188:
                    189:        if (init_fcn)
1.25      vincent   190:                init_fcn(0, 1);
1.1       deraadt   191: }
                    192:
                    193: /*
1.5       millert   194:  * Quit command.  If an argument, always quit.  Otherwise confirm if a buffer
                    195:  * has been changed and not written out.  Normally bound to "C-X C-C".
1.1       deraadt   196:  */
1.4       millert   197: /* ARGSUSED */
1.5       millert   198: int
1.20      vincent   199: quit(int f, int n)
1.1       deraadt   200: {
1.5       millert   201:        int      s;
1.1       deraadt   202:
1.4       millert   203:        if ((s = anycb(FALSE)) == ABORT)
1.38      db        204:                return (ABORT);
1.1       deraadt   205:        if (s == FALSE
1.41    ! kjell     206:            || eyesno("Modified buffers exist; really exit") == TRUE) {
1.1       deraadt   207:                vttidy();
1.5       millert   208: #ifdef SYSCLEANUP
1.4       millert   209:                SYSCLEANUP;
1.5       millert   210: #endif /* SYSCLEANUP */
1.1       deraadt   211:                exit(GOOD);
                    212:        }
1.38      db        213:        return (TRUE);
1.1       deraadt   214: }
                    215:
                    216: /*
1.10      mickey    217:  * User abort.  Should be called by any input routine that sees a C-g to abort
1.5       millert   218:  * whatever C-g is aborting these days. Currently does nothing.
1.1       deraadt   219:  */
1.4       millert   220: /* ARGSUSED */
                    221: int
1.20      vincent   222: ctrlg(int f, int n)
1.1       deraadt   223: {
1.24      vincent   224:        return (ABORT);
1.1       deraadt   225: }