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

1.28    ! vincent     1: /*     $OpenBSD: main.c,v 1.27 2003/12/04 01:52:01 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       */
                     22: BUFFER         *bheadp;                        /* BUFFER listhead      */
                     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.23      deraadt    92:                        long lval;
                     93:                        char *ep;
                     94:
                     95:                        errno = 0;
1.27      vincent    96:                        lval = strtoul(&argv[i][1], &ep, 10);
                     97:                        if (argv[i][1] == '\0' || *ep != '\0')
1.23      deraadt    98:                                goto notnum;
                     99:                        if ((errno == ERANGE &&
                    100:                            (lval == LONG_MAX || lval == LONG_MIN)) ||
                    101:                            (lval > INT_MAX || lval < INT_MIN))
                    102:                                goto notnum;
                    103:                        startrow = (int)lval;
1.24      vincent   104:                } else {
1.23      deraadt   105: notnum:
1.27      vincent   106:                        cp = adjustname(argv[i]);
1.24      vincent   107:                        if (cp != NULL) {
1.27      vincent   108:                                if (nfiles > 0 && nfiles < 3)
                    109:                                        splitwind(0, 1);
1.24      vincent   110:                                curbp = findbuffer(cp);
                    111:                                (void)showbuffer(curbp, curwp, 0);
                    112:                                (void)readin(cp);
                    113:                                if (init_fcn_name)
1.25      vincent   114:                                        init_fcn(0, 1);
1.27      vincent   115:                                nfiles++;
1.24      vincent   116:                        }
1.22      vincent   117:                }
1.1       deraadt   118:        }
1.5       millert   119:
                    120:        /* fake last flags */
                    121:        thisflag = 0;
1.4       millert   122:        for (;;) {
1.1       deraadt   123: #ifndef NO_DPROMPT
1.4       millert   124:                if (epresf == KPROMPT)
                    125:                        eerase();
1.5       millert   126: #endif /* !NO_DPROMPT */
1.17      deraadt   127:                if (winch_flag) {
                    128:                        refresh(0, 0);
                    129:                        winch_flag = 0;
                    130:                }
1.4       millert   131:                update();
                    132:                lastflag = thisflag;
                    133:                thisflag = 0;
1.5       millert   134:
1.4       millert   135:                switch (doin()) {
                    136:                case TRUE:
                    137:                        break;
1.1       deraadt   138:                case ABORT:
1.5       millert   139:                        ewprintf("Quit");
                    140:                        /* and fall through */
1.1       deraadt   141:                case FALSE:
                    142:                default:
1.4       millert   143:                        ttbeep();
1.1       deraadt   144: #ifndef NO_MACRO
1.4       millert   145:                        macrodef = FALSE;
1.5       millert   146: #endif /* !NO_MACRO */
1.4       millert   147:                }
1.1       deraadt   148:        }
                    149: }
                    150:
                    151: /*
                    152:  * Initialize default buffer and window.
                    153:  */
1.7       art       154: static void
1.24      vincent   155: edinit(PF init_fcn)
1.4       millert   156: {
1.5       millert   157:        BUFFER  *bp;
                    158:        MGWIN   *wp;
1.1       deraadt   159:
                    160:        bheadp = NULL;
1.5       millert   161:        bp = bfind("*scratch*", TRUE);          /* Text buffer.          */
1.26      vincent   162:        wp = new_window(bp);
1.21      vincent   163:        if (wp == NULL)
                    164:                panic("Out of memory");
1.4       millert   165:        if (bp == NULL || wp == NULL)
                    166:                panic("edinit");
1.5       millert   167:        curbp = bp;                             /* Current ones.         */
1.1       deraadt   168:        wheadp = wp;
1.4       millert   169:        curwp = wp;
1.5       millert   170:        wp->w_wndp = NULL;                      /* Initialize window.    */
1.1       deraadt   171:        wp->w_linep = wp->w_dotp = bp->b_linep;
1.28    ! vincent   172:        wp->w_ntrows = nrow - 2;                /* 2 = mode, echo. */
        !           173:        wp->w_flag = WFMODE | WFHARD;           /* Full. */
1.24      vincent   174:
                    175:        if (init_fcn)
1.25      vincent   176:                init_fcn(0, 1);
1.1       deraadt   177: }
                    178:
                    179: /*
1.5       millert   180:  * Quit command.  If an argument, always quit.  Otherwise confirm if a buffer
                    181:  * has been changed and not written out.  Normally bound to "C-X C-C".
1.1       deraadt   182:  */
1.4       millert   183: /* ARGSUSED */
1.5       millert   184: int
1.20      vincent   185: quit(int f, int n)
1.1       deraadt   186: {
1.5       millert   187:        int      s;
1.1       deraadt   188:
1.4       millert   189:        if ((s = anycb(FALSE)) == ABORT)
                    190:                return ABORT;
1.1       deraadt   191:        if (s == FALSE
1.4       millert   192:            || eyesno("Some modified buffers exist, really exit") == TRUE) {
1.1       deraadt   193:                vttidy();
1.5       millert   194: #ifdef SYSCLEANUP
1.4       millert   195:                SYSCLEANUP;
1.5       millert   196: #endif /* SYSCLEANUP */
1.1       deraadt   197:                exit(GOOD);
                    198:        }
                    199:        return TRUE;
                    200: }
                    201:
                    202: /*
1.10      mickey    203:  * User abort.  Should be called by any input routine that sees a C-g to abort
1.5       millert   204:  * whatever C-g is aborting these days. Currently does nothing.
1.1       deraadt   205:  */
1.4       millert   206: /* ARGSUSED */
                    207: int
1.20      vincent   208: ctrlg(int f, int n)
1.1       deraadt   209: {
1.24      vincent   210:        return (ABORT);
1.1       deraadt   211: }