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

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