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

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