[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.30

1.30    ! pvalchev    1: /*     $OpenBSD: main.c,v 1.29 2004/02/02 20:21:14 vincent Exp $       */
1.6       niklas      2:
1.1       deraadt     3: /*
1.5       millert     4:  *     Mainline.
1.1       deraadt     5:  */
1.5       millert     6:
1.1       deraadt     7: #include       "def.h"
1.12      art         8: #include       "kbd.h"
1.11      art         9: #include       "funmap.h"
1.5       millert    10:
1.1       deraadt    11: #ifndef NO_MACRO
                     12: #include       "macro.h"
1.5       millert    13: #endif /* NO_MACRO */
1.1       deraadt    14:
1.24      vincent    15: #include <err.h>
                     16:
1.5       millert    17: int             thisflag;                      /* flags, this command  */
                     18: int             lastflag;                      /* flags, last command  */
                     19: int             curgoal;                       /* goal column          */
1.23      deraadt    20: int             startrow;                      /* row to start         */
1.5       millert    21: BUFFER         *curbp;                         /* current buffer       */
1.29      vincent    22: BUFFER         *bheadp;                        /* BUFFER list head */
1.5       millert    23: MGWIN          *curwp;                         /* current window       */
1.8       art        24: MGWIN          *wheadp;                        /* MGWIN listhead       */
1.5       millert    25: char            pat[NPAT];                     /* pattern              */
1.1       deraadt    26:
1.24      vincent    27: static void     edinit(PF);
1.1       deraadt    28:
1.2       deraadt    29: int
1.19      vincent    30: main(int argc, char **argv)
1.1       deraadt    31: {
1.24      vincent    32:        char    *cp, *init_fcn_name = NULL;
                     33:        PF init_fcn = NULL;
1.27      vincent    34:        int o, i, nfiles;
1.24      vincent    35:
                     36:        while ((o = getopt(argc, argv, "f:")) != -1)
                     37:                switch (o) {
                     38:                case 'f':
                     39:                        if (init_fcn_name != NULL)
                     40:                                errx(1, "cannot specify more than one "
                     41:                                    "initial function");
                     42:                        init_fcn_name = optarg;
                     43:                        break;
                     44:                default:
                     45:                        errx(1, "usage: mg [-f <mode>] [files...]");
                     46:                }
                     47:        argc -= optind;
                     48:        argv += optind;
1.1       deraadt    49:
1.12      art        50:        maps_init();            /* Keymaps and modes.           */
1.11      art        51:        funmap_init();          /* Functions.                   */
1.19      vincent    52:
1.13      art        53:        /*
                     54:         * This is where we initialize standalone extensions that should
                     55:         * be loaded dynamically sometime in the future.
                     56:         */
                     57:        {
                     58:                extern void grep_init(void);
1.14      art        59:                extern void theo_init(void);
1.13      art        60:
                     61:                grep_init();
1.14      art        62:                theo_init();
1.24      vincent    63:                mail_init();
1.13      art        64:        }
1.5       millert    65:
1.24      vincent    66:        if (init_fcn_name &&
                     67:            (init_fcn = name_function(init_fcn_name)) == NULL)
                     68:                errx(1, "Unknown function `%s'", init_fcn_name);
                     69:
                     70:        vtinit();               /* Virtual terminal.            */
                     71: #ifndef NO_DIR
                     72:        dirinit();              /* Get current directory.       */
                     73: #endif /* !NO_DIR */
                     74:        edinit(init_fcn);       /* Buffers, windows.            */
                     75:        ttykeymapinit();        /* Symbols, bindings.           */
                     76:
1.4       millert    77:        /*
                     78:         * doing update() before reading files causes the error messages from
                     79:         * the file I/O show up on the screen.  (and also an extra display of
                     80:         * the mode line if there are files specified on the command line.)
1.1       deraadt    81:         */
                     82:        update();
1.5       millert    83:
                     84: #ifndef NO_STARTUP
                     85:        /* user startup file */
1.8       art        86:        if ((cp = startupfile(NULL)) != NULL)
1.7       art        87:                (void)load(cp);
1.5       millert    88: #endif /* !NO_STARTUP */
1.27      vincent    89:
                     90:        for (nfiles = 0, i = 0; i < argc; i++) {
                     91:                if (argv[i][0] == '+' && strlen(argv[i]) >= 2) {
1.30    ! pvalchev   92:                        int lval;
        !            93:                        const char *errstr;
1.23      deraadt    94:
1.30    ! pvalchev   95:                        lval = strtonum(&argv[i][1], INT_MIN, INT_MAX, &errstr);
        !            96:                        if (argv[i][1] == '\0' || *errstr != '\0')
1.23      deraadt    97:                                goto notnum;
1.30    ! pvalchev   98:                        startrow = lval;
1.24      vincent    99:                } else {
1.23      deraadt   100: notnum:
1.27      vincent   101:                        cp = adjustname(argv[i]);
1.24      vincent   102:                        if (cp != NULL) {
1.29      vincent   103:                                if (nfiles == 1) {
1.27      vincent   104:                                        splitwind(0, 1);
1.29      vincent   105:                                }
1.24      vincent   106:                                curbp = findbuffer(cp);
                    107:                                (void)showbuffer(curbp, curwp, 0);
                    108:                                (void)readin(cp);
                    109:                                if (init_fcn_name)
1.25      vincent   110:                                        init_fcn(0, 1);
1.27      vincent   111:                                nfiles++;
1.24      vincent   112:                        }
1.22      vincent   113:                }
1.1       deraadt   114:        }
1.29      vincent   115:
                    116:        if (nfiles > 2)
                    117:                listbuffers(0, 1);
1.5       millert   118:
                    119:        /* fake last flags */
                    120:        thisflag = 0;
1.4       millert   121:        for (;;) {
1.1       deraadt   122: #ifndef NO_DPROMPT
1.4       millert   123:                if (epresf == KPROMPT)
                    124:                        eerase();
1.5       millert   125: #endif /* !NO_DPROMPT */
1.17      deraadt   126:                if (winch_flag) {
                    127:                        refresh(0, 0);
                    128:                        winch_flag = 0;
                    129:                }
1.4       millert   130:                update();
                    131:                lastflag = thisflag;
                    132:                thisflag = 0;
1.5       millert   133:
1.4       millert   134:                switch (doin()) {
                    135:                case TRUE:
                    136:                        break;
1.1       deraadt   137:                case ABORT:
1.5       millert   138:                        ewprintf("Quit");
                    139:                        /* and fall through */
1.1       deraadt   140:                case FALSE:
                    141:                default:
1.4       millert   142:                        ttbeep();
1.1       deraadt   143: #ifndef NO_MACRO
1.4       millert   144:                        macrodef = FALSE;
1.5       millert   145: #endif /* !NO_MACRO */
1.4       millert   146:                }
1.1       deraadt   147:        }
                    148: }
                    149:
                    150: /*
                    151:  * Initialize default buffer and window.
                    152:  */
1.7       art       153: static void
1.24      vincent   154: edinit(PF init_fcn)
1.4       millert   155: {
1.5       millert   156:        BUFFER  *bp;
                    157:        MGWIN   *wp;
1.1       deraadt   158:
                    159:        bheadp = NULL;
1.5       millert   160:        bp = bfind("*scratch*", TRUE);          /* Text buffer.          */
1.26      vincent   161:        wp = new_window(bp);
1.21      vincent   162:        if (wp == NULL)
                    163:                panic("Out of memory");
1.4       millert   164:        if (bp == NULL || wp == NULL)
                    165:                panic("edinit");
1.5       millert   166:        curbp = bp;                             /* Current ones.         */
1.1       deraadt   167:        wheadp = wp;
1.4       millert   168:        curwp = wp;
1.5       millert   169:        wp->w_wndp = NULL;                      /* Initialize window.    */
1.1       deraadt   170:        wp->w_linep = wp->w_dotp = bp->b_linep;
1.28      vincent   171:        wp->w_ntrows = nrow - 2;                /* 2 = mode, echo. */
                    172:        wp->w_flag = WFMODE | WFHARD;           /* Full. */
1.24      vincent   173:
                    174:        if (init_fcn)
1.25      vincent   175:                init_fcn(0, 1);
1.1       deraadt   176: }
                    177:
                    178: /*
1.5       millert   179:  * Quit command.  If an argument, always quit.  Otherwise confirm if a buffer
                    180:  * has been changed and not written out.  Normally bound to "C-X C-C".
1.1       deraadt   181:  */
1.4       millert   182: /* ARGSUSED */
1.5       millert   183: int
1.20      vincent   184: quit(int f, int n)
1.1       deraadt   185: {
1.5       millert   186:        int      s;
1.1       deraadt   187:
1.4       millert   188:        if ((s = anycb(FALSE)) == ABORT)
                    189:                return ABORT;
1.1       deraadt   190:        if (s == FALSE
1.4       millert   191:            || eyesno("Some modified buffers exist, really exit") == TRUE) {
1.1       deraadt   192:                vttidy();
1.5       millert   193: #ifdef SYSCLEANUP
1.4       millert   194:                SYSCLEANUP;
1.5       millert   195: #endif /* SYSCLEANUP */
1.1       deraadt   196:                exit(GOOD);
                    197:        }
                    198:        return TRUE;
                    199: }
                    200:
                    201: /*
1.10      mickey    202:  * User abort.  Should be called by any input routine that sees a C-g to abort
1.5       millert   203:  * whatever C-g is aborting these days. Currently does nothing.
1.1       deraadt   204:  */
1.4       millert   205: /* ARGSUSED */
                    206: int
1.20      vincent   207: ctrlg(int f, int n)
1.1       deraadt   208: {
1.24      vincent   209:        return (ABORT);
1.1       deraadt   210: }