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

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