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

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