[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / less

Annotation of src/usr.bin/less/main.c, Revision 1.8

1.1       etheisen    1: /*
1.7       millert     2:  * Copyright (C) 1984-2002  Mark Nudelman
1.1       etheisen    3:  *
1.7       millert     4:  * You may distribute under the terms of either the GNU General Public
                      5:  * License or the Less License, as specified in the README file.
1.1       etheisen    6:  *
1.7       millert     7:  * For more information about less, or for information on how to
                      8:  * contact the author, see the README file.
1.1       etheisen    9:  */
                     10:
                     11:
                     12: /*
                     13:  * Entry point, initialization, miscellaneous routines.
                     14:  */
                     15:
                     16: #include "less.h"
1.7       millert    17: #if MSDOS_COMPILER==WIN32C
                     18: #include <windows.h>
                     19: #endif
1.1       etheisen   20:
                     21: public char *  every_first_cmd = NULL;
                     22: public int     new_file;
                     23: public int     is_tty;
                     24: public IFILE   curr_ifile = NULL_IFILE;
                     25: public IFILE   old_ifile = NULL_IFILE;
                     26: public struct scrpos initial_scrpos;
                     27: public int     any_display = FALSE;
1.7       millert    28: public POSITION        start_attnpos = NULL_POSITION;
                     29: public POSITION        end_attnpos = NULL_POSITION;
1.1       etheisen   30: public int     wscroll;
                     31: public char *  progname;
                     32: public int     quitting;
1.7       millert    33: public int     secure;
                     34: public int     ismore;
1.1       etheisen   35:
                     36: #if LOGFILE
                     37: public int     logfile = -1;
                     38: public int     force_logfile = FALSE;
                     39: public char *  namelogfile = NULL;
                     40: #endif
                     41:
                     42: #if EDITOR
                     43: public char *  editor;
                     44: public char *  editproto;
                     45: #endif
                     46:
                     47: #if TAGS
1.7       millert    48: extern char *  tags;
1.1       etheisen   49: extern char *  tagoption;
                     50: extern int     jump_sline;
                     51: #endif
                     52:
1.7       millert    53: #ifdef WIN32
                     54: static char consoleTitle[256];
                     55: #endif
1.1       etheisen   56:
1.7       millert    57: extern int     missing_cap;
                     58: extern int     know_dumb;
                     59:
                     60: extern char *  __progname;
1.1       etheisen   61:
                     62: /*
                     63:  * Entry point.
                     64:  */
                     65: int
                     66: main(argc, argv)
                     67:        int argc;
                     68:        char *argv[];
                     69: {
                     70:        IFILE ifile;
1.7       millert    71:        char *s;
1.1       etheisen   72:
                     73: #ifdef __EMX__
                     74:        _response(&argc, &argv);
                     75:        _wildcard(&argc, &argv);
                     76: #endif
                     77:
                     78:        progname = *argv++;
1.7       millert    79:        argc--;
                     80:
                     81:        secure = 0;
                     82:        s = lgetenv("LESSSECURE");
                     83:        if (s != NULL && *s != '\0')
                     84:                secure = 1;
                     85:
                     86: #ifdef WIN32
                     87:        if (getenv("HOME") == NULL)
                     88:        {
                     89:                /*
                     90:                 * If there is no HOME environment variable,
                     91:                 * try the concatenation of HOMEDRIVE + HOMEPATH.
                     92:                 */
                     93:                char *drive = getenv("HOMEDRIVE");
                     94:                char *path  = getenv("HOMEPATH");
                     95:                if (drive != NULL && path != NULL)
                     96:                {
                     97:                        size_t len = strlen(drive) + strlen(path) + 6;
                     98:                        char *env = (char *) ecalloc(len, sizeof(char));
                     99:                        strlcpy(env, "HOME=", len);
                    100:                        strlcat(env, drive, len);
                    101:                        strlcat(env, path, len);
                    102:                        putenv(env);
                    103:                }
                    104:        }
                    105:        GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
                    106: #endif /* WIN32 */
1.1       etheisen  107:
                    108:        /*
                    109:         * Process command line arguments and LESS environment arguments.
                    110:         * Command line arguments override environment arguments.
                    111:         */
1.7       millert   112:        ismore = !strcmp(__progname, "more");
                    113:        is_tty = isatty(1);
1.1       etheisen  114:        get_term();
                    115:        init_cmds();
                    116:        init_prompt();
                    117:        init_charset();
1.7       millert   118:        init_line();
1.1       etheisen  119:        init_option();
1.7       millert   120:        if (ismore) {
1.2       etheisen  121:                scan_option("-E");
                    122:                scan_option("-G");
1.7       millert   123:                scan_option("-L");
                    124:                s = lgetenv("MORE");
1.2       etheisen  125:        } else
1.7       millert   126:                s = lgetenv("LESS");
                    127:        if (s != NULL)
                    128:                scan_option(save(s));
1.1       etheisen  129:
1.7       millert   130: #define        isoptstring(s)  (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
                    131:        while (argc > 0 && (isoptstring(*argv) || isoptpending()))
1.1       etheisen  132:        {
1.7       millert   133:                s = *argv++;
                    134:                argc--;
                    135:                if (strcmp(s, "--") == 0)
1.3       deraadt   136:                        break;
1.7       millert   137:                scan_option(s);
1.3       deraadt   138:        }
1.1       etheisen  139: #undef isoptstring
                    140:
                    141:        if (isoptpending())
                    142:        {
                    143:                /*
                    144:                 * Last command line option was a flag requiring a
                    145:                 * following string, but there was no following string.
                    146:                 */
                    147:                nopendopt();
                    148:                quit(QUIT_OK);
                    149:        }
                    150:
                    151: #if EDITOR
1.7       millert   152:        editor = lgetenv("VISUAL");
1.1       etheisen  153:        if (editor == NULL || *editor == '\0')
                    154:        {
1.7       millert   155:                editor = lgetenv("EDITOR");
1.1       etheisen  156:                if (editor == NULL || *editor == '\0')
                    157:                        editor = EDIT_PGM;
                    158:        }
1.7       millert   159:        editproto = lgetenv("LESSEDIT");
1.1       etheisen  160:        if (editproto == NULL || *editproto == '\0')
                    161:                editproto = "%E ?lm+%lm. %f";
                    162: #endif
                    163:
                    164:        /*
                    165:         * Call get_ifile with all the command line filenames
                    166:         * to "register" them with the ifile system.
                    167:         */
                    168:        ifile = NULL_IFILE;
1.7       millert   169:        while (argc-- > 0)
1.1       etheisen  170:        {
1.7       millert   171:                char *filename;
                    172: #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
1.1       etheisen  173:                /*
                    174:                 * Because the "shell" doesn't expand filename patterns,
                    175:                 * treat each argument as a filename pattern rather than
                    176:                 * a single filename.
                    177:                 * Expand the pattern and iterate over the expanded list.
                    178:                 */
                    179:                struct textlist tlist;
                    180:                char *gfilename;
                    181:
1.7       millert   182:                gfilename = lglob(*argv++);
1.1       etheisen  183:                init_textlist(&tlist, gfilename);
                    184:                filename = NULL;
                    185:                while ((filename = forw_textlist(&tlist, filename)) != NULL)
1.7       millert   186:                {
                    187:                        (void) get_ifile(filename, ifile);
                    188:                        ifile = prev_ifile(NULL_IFILE);
                    189:                }
1.1       etheisen  190:                free(gfilename);
                    191: #else
1.7       millert   192:                filename = shell_quote(*argv);
                    193:                if (filename == NULL)
                    194:                        filename = *argv;
                    195:                argv++;
                    196:                (void) get_ifile(filename, ifile);
                    197:                ifile = prev_ifile(NULL_IFILE);
1.1       etheisen  198: #endif
                    199:        }
                    200:        /*
                    201:         * Set up terminal, etc.
                    202:         */
                    203:        if (!is_tty)
                    204:        {
                    205:                /*
                    206:                 * Output is not a tty.
                    207:                 * Just copy the input file(s) to output.
                    208:                 */
1.7       millert   209:                SET_BINARY(1);
1.1       etheisen  210:                if (nifile() == 0)
                    211:                {
                    212:                        if (edit_stdin() == 0)
                    213:                                cat_file();
                    214:                } else if (edit_first() == 0)
                    215:                {
                    216:                        do {
                    217:                                cat_file();
                    218:                        } while (edit_next(1) == 0);
                    219:                }
                    220:                quit(QUIT_OK);
                    221:        }
                    222:
1.7       millert   223:        if (missing_cap && !know_dumb && !ismore)
                    224:                error("WARNING: terminal is not fully functional", NULL_PARG);
1.1       etheisen  225:        init_mark();
1.7       millert   226:        open_getchr();
1.1       etheisen  227:        raw_mode(1);
                    228:        init_signals(1);
                    229:
                    230:        /*
                    231:         * Select the first file to examine.
                    232:         */
                    233: #if TAGS
1.7       millert   234:        if (tagoption != NULL || strcmp(tags, "-") == 0)
1.1       etheisen  235:        {
                    236:                /*
                    237:                 * A -t option was given.
                    238:                 * Verify that no filenames were also given.
                    239:                 * Edit the file selected by the "tags" search,
                    240:                 * and search for the proper line in the file.
                    241:                 */
                    242:                if (nifile() > 0)
                    243:                {
                    244:                        error("No filenames allowed with -t option", NULL_PARG);
                    245:                        quit(QUIT_ERROR);
                    246:                }
                    247:                findtag(tagoption);
1.7       millert   248:                if (edit_tagfile())  /* Edit file which contains the tag */
1.1       etheisen  249:                        quit(QUIT_ERROR);
                    250:                /*
                    251:                 * Search for the line which contains the tag.
                    252:                 * Set up initial_scrpos so we display that line.
                    253:                 */
                    254:                initial_scrpos.pos = tagsearch();
                    255:                if (initial_scrpos.pos == NULL_POSITION)
                    256:                        quit(QUIT_ERROR);
                    257:                initial_scrpos.ln = jump_sline;
                    258:        } else
                    259: #endif
                    260:        if (nifile() == 0)
                    261:        {
                    262:                if (edit_stdin())  /* Edit standard input */
                    263:                        quit(QUIT_ERROR);
                    264:        } else
                    265:        {
                    266:                if (edit_first())  /* Edit first valid file in cmd line */
                    267:                        quit(QUIT_ERROR);
                    268:        }
                    269:
                    270:        init();
                    271:        commands();
                    272:        quit(QUIT_OK);
                    273:        /*NOTREACHED*/
1.7       millert   274:        return (0);
1.1       etheisen  275: }
                    276:
                    277: /*
                    278:  * Copy a string to a "safe" place
                    279:  * (that is, to a buffer allocated by calloc).
                    280:  */
                    281:        public char *
                    282: save(s)
                    283:        char *s;
                    284: {
1.7       millert   285:        register char *p;
1.6       deraadt   286:        size_t len;
1.1       etheisen  287:
1.7       millert   288:        len = strlen(s)+1, sizeof(char);
1.6       deraadt   289:        p = (char *) ecalloc(len, sizeof(char));
                    290:        strlcpy(p, s, len);
1.1       etheisen  291:        return (p);
                    292: }
                    293:
                    294: /*
                    295:  * Allocate memory.
                    296:  * Like calloc(), but never returns an error (NULL).
                    297:  */
                    298:        public VOID_POINTER
                    299: ecalloc(count, size)
                    300:        int count;
                    301:        unsigned int size;
                    302: {
1.7       millert   303:        register VOID_POINTER p;
1.1       etheisen  304:
                    305:        p = (VOID_POINTER) calloc(count, size);
                    306:        if (p != NULL)
                    307:                return (p);
                    308:        error("Cannot allocate memory", NULL_PARG);
                    309:        quit(QUIT_ERROR);
                    310:        /*NOTREACHED*/
1.7       millert   311:        return (NULL);
1.1       etheisen  312: }
                    313:
                    314: /*
                    315:  * Skip leading spaces in a string.
                    316:  */
                    317:        public char *
                    318: skipsp(s)
1.7       millert   319:        register char *s;
1.1       etheisen  320: {
                    321:        while (*s == ' ' || *s == '\t')
                    322:                s++;
                    323:        return (s);
                    324: }
                    325:
                    326: /*
1.7       millert   327:  * See how many characters of two strings are identical.
                    328:  * If uppercase is true, the first string must begin with an uppercase
                    329:  * character; the remainder of the first string may be either case.
                    330:  */
                    331:        public int
                    332: sprefix(ps, s, uppercase)
                    333:        char *ps;
                    334:        char *s;
                    335:        int uppercase;
                    336: {
                    337:        register int c;
                    338:        register int sc;
                    339:        register int len = 0;
                    340:
                    341:        for ( ;  *s != '\0';  s++, ps++)
                    342:        {
                    343:                c = *ps;
                    344:                if (uppercase)
                    345:                {
                    346:                        if (len == 0 && SIMPLE_IS_LOWER(c))
                    347:                                return (-1);
                    348:                        if (SIMPLE_IS_UPPER(c))
                    349:                                c = SIMPLE_TO_LOWER(c);
                    350:                }
                    351:                sc = *s;
                    352:                if (len > 0 && SIMPLE_IS_UPPER(sc))
                    353:                        sc = SIMPLE_TO_LOWER(sc);
                    354:                if (c != sc)
                    355:                        break;
                    356:                len++;
                    357:        }
                    358:        return (len);
                    359: }
                    360:
                    361: /*
1.1       etheisen  362:  * Exit the program.
                    363:  */
                    364:        public void
                    365: quit(status)
                    366:        int status;
                    367: {
                    368:        static int save_status;
                    369:
                    370:        /*
                    371:         * Put cursor at bottom left corner, clear the line,
                    372:         * reset the terminal modes, and exit.
                    373:         */
                    374:        if (status < 0)
                    375:                status = save_status;
                    376:        else
                    377:                save_status = status;
                    378:        quitting = 1;
                    379:        edit((char*)NULL);
1.7       millert   380:        if (any_display && is_tty)
1.1       etheisen  381:                clear_bot();
                    382:        deinit();
                    383:        flush();
                    384:        raw_mode(0);
1.7       millert   385: #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
1.1       etheisen  386:        /*
                    387:         * If we don't close 2, we get some garbage from
                    388:         * 2's buffer when it flushes automatically.
                    389:         * I cannot track this one down  RB
                    390:         * The same bug shows up if we use ^C^C to abort.
                    391:         */
                    392:        close(2);
                    393: #endif
1.7       millert   394: #if WIN32
                    395:        SetConsoleTitle(consoleTitle);
                    396: #endif
                    397:        close_getchr();
1.1       etheisen  398:        exit(status);
                    399: }