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

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