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

Diff for /src/usr.bin/less/main.c between version 1.6 and 1.7

version 1.6, 2003/04/05 01:03:35 version 1.7, 2003/04/13 18:26:26
Line 1 
Line 1 
 /*      $OpenBSD$       */  
   
 /*  /*
  * Copyright (c) 1984,1985,1989,1994,1995  Mark Nudelman   * Copyright (C) 1984-2002  Mark Nudelman
  * All rights reserved.  
  *   *
  * Redistribution and use in source and binary forms, with or without   * You may distribute under the terms of either the GNU General Public
  * modification, are permitted provided that the following conditions   * License or the Less License, as specified in the README file.
  * are met:  
  * 1. Redistributions of source code must retain the above copyright  
  *    notice, this list of conditions and the following disclaimer.  
  * 2. Redistributions in binary form must reproduce the above copyright  
  *    notice in the documentation and/or other materials provided with  
  *    the distribution.  
  *   *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY   * For more information about less, or for information on how to
  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE   * contact the author, see the README file.
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR  
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE  
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN  
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
  */   */
   
   
Line 32 
Line 14 
  */   */
   
 #include "less.h"  #include "less.h"
 #include "position.h"  #if MSDOS_COMPILER==WIN32C
   #include <windows.h>
   #endif
   
 public char *   every_first_cmd = NULL;  public char *   every_first_cmd = NULL;
 public int      new_file;  public int      new_file;
Line 41 
Line 25 
 public IFILE    old_ifile = NULL_IFILE;  public IFILE    old_ifile = NULL_IFILE;
 public struct scrpos initial_scrpos;  public struct scrpos initial_scrpos;
 public int      any_display = FALSE;  public int      any_display = FALSE;
   public POSITION start_attnpos = NULL_POSITION;
   public POSITION end_attnpos = NULL_POSITION;
 public int      wscroll;  public int      wscroll;
 public char *   progname;  public char *   progname;
 public int      quitting;  public int      quitting;
 public int      more_mode = 0;  public int      secure;
   public int      dohelp;
   public int      ismore;
   
 extern int      quit_at_eof;  
 extern int      cbufs;  
 extern int      errmsgs;  
 extern int      screen_trashed;  
 extern int      force_open;  
   
 #if LOGFILE  #if LOGFILE
 public int      logfile = -1;  public int      logfile = -1;
 public int      force_logfile = FALSE;  public int      force_logfile = FALSE;
Line 64 
Line 46 
 #endif  #endif
   
 #if TAGS  #if TAGS
 extern char *   tagfile;  extern char *   tags;
 extern char *   tagoption;  extern char *   tagoption;
 extern int      jump_sline;  extern int      jump_sline;
 #endif  #endif
   
   #ifdef WIN32
   static char consoleTitle[256];
   #endif
   
   extern int      missing_cap;
   extern int      know_dumb;
   
   extern char *   __progname;
   
 /*  /*
  * Entry point.   * Entry point.
  */   */
Line 80 
Line 69 
         char *argv[];          char *argv[];
 {  {
         IFILE ifile;          IFILE ifile;
         extern char *__progname;          char *s;
   
 #ifdef __EMX__  #ifdef __EMX__
         _response(&argc, &argv);          _response(&argc, &argv);
Line 88 
Line 77 
 #endif  #endif
   
         progname = *argv++;          progname = *argv++;
           argc--;
   
           secure = 0;
           s = lgetenv("LESSSECURE");
           if (s != NULL && *s != '\0')
                   secure = 1;
   
   #ifdef WIN32
           if (getenv("HOME") == NULL)
           {
                   /*
                    * If there is no HOME environment variable,
                    * try the concatenation of HOMEDRIVE + HOMEPATH.
                    */
                   char *drive = getenv("HOMEDRIVE");
                   char *path  = getenv("HOMEPATH");
                   if (drive != NULL && path != NULL)
                   {
                           size_t len = strlen(drive) + strlen(path) + 6;
                           char *env = (char *) ecalloc(len, sizeof(char));
                           strlcpy(env, "HOME=", len);
                           strlcat(env, drive, len);
                           strlcat(env, path, len);
                           putenv(env);
                   }
           }
           GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
   #endif /* WIN32 */
   
         /*          /*
          * Process command line arguments and LESS environment arguments.           * Process command line arguments and LESS environment arguments.
          * Command line arguments override environment arguments.           * Command line arguments override environment arguments.
          */           */
         if (strcmp(__progname, "more") == 0)          ismore = !strcmp(__progname, "more");
                 more_mode = 1;          is_tty = isatty(1);
   
         get_term();          get_term();
         init_cmds();          init_cmds();
         init_prompt();          init_prompt();
         init_charset();          init_charset();
           init_line();
         init_option();          init_option();
           if (ismore) {
         if (more_mode) {  
                 scan_option("-E");                  scan_option("-E");
                 scan_option("-m");  
                 scan_option("-G");                  scan_option("-G");
                 scan_option(getenv("MORE"));                  scan_option("-L");
                   s = lgetenv("MORE");
         } else          } else
                 scan_option(getenv("LESS"));                  s = lgetenv("LESS");
           if (s != NULL)
                   scan_option(save(s));
   
 #if GNU_OPTIONS  
         /*  
          * Special case for "less --help" and "less --version".  
          */  
         if (argc == 2)  
         {  
                 if (strcmp(argv[0], "--help") == 0)  
                         scan_option("-?");  
                 if (strcmp(argv[0], "--version") == 0)  
                         scan_option("-V");  
         }  
 #endif  
 #define isoptstring(s)  (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')  #define isoptstring(s)  (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
         while (--argc > 0 && (isoptstring(argv[0]) || isoptpending())) {          while (argc > 0 && (isoptstring(*argv) || isoptpending()))
                 if (strcmp(argv[0], "--") == 0) {          {
                         argv++;                  s = *argv++;
                         argc--;                  argc--;
                   if (strcmp(s, "--") == 0)
                         break;                          break;
                 }                  scan_option(s);
                 scan_option(*argv++);  
         }          }
 #undef isoptstring  #undef isoptstring
   
Line 144 
Line 150 
         }          }
   
 #if EDITOR  #if EDITOR
         editor = getenv("VISUAL");          editor = lgetenv("VISUAL");
         if (editor == NULL || *editor == '\0')          if (editor == NULL || *editor == '\0')
         {          {
                 editor = getenv("EDITOR");                  editor = lgetenv("EDITOR");
                 if (editor == NULL || *editor == '\0')                  if (editor == NULL || *editor == '\0')
                         editor = EDIT_PGM;                          editor = EDIT_PGM;
         }          }
         editproto = getenv("LESSEDIT");          editproto = lgetenv("LESSEDIT");
         if (editproto == NULL || *editproto == '\0')          if (editproto == NULL || *editproto == '\0')
                 editproto = "%E ?lm+%lm. %f";                  editproto = "%E ?lm+%lm. %f";
 #endif  #endif
Line 161 
Line 167 
          * to "register" them with the ifile system.           * to "register" them with the ifile system.
          */           */
         ifile = NULL_IFILE;          ifile = NULL_IFILE;
         while (--argc >= 0)          if (dohelp)
                   ifile = get_ifile(FAKE_HELPFILE, ifile);
           while (argc-- > 0)
         {          {
 #if MSOFTC || OS2                  char *filename;
   #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
                 /*                  /*
                  * Because the "shell" doesn't expand filename patterns,                   * Because the "shell" doesn't expand filename patterns,
                  * treat each argument as a filename pattern rather than                   * treat each argument as a filename pattern rather than
Line 172 
Line 181 
                  */                   */
                 struct textlist tlist;                  struct textlist tlist;
                 char *gfilename;                  char *gfilename;
                 char *filename;  
   
                 gfilename = glob(*argv++);                  gfilename = lglob(*argv++);
                 init_textlist(&tlist, gfilename);                  init_textlist(&tlist, gfilename);
                 filename = NULL;                  filename = NULL;
                 while ((filename = forw_textlist(&tlist, filename)) != NULL)                  while ((filename = forw_textlist(&tlist, filename)) != NULL)
                         ifile = get_ifile(filename, ifile);                  {
                           (void) get_ifile(filename, ifile);
                           ifile = prev_ifile(NULL_IFILE);
                   }
                 free(gfilename);                  free(gfilename);
 #else  #else
                 ifile = get_ifile(*argv++, ifile);                  filename = shell_quote(*argv);
                   if (filename == NULL)
                           filename = *argv;
                   argv++;
                   (void) get_ifile(filename, ifile);
                   ifile = prev_ifile(NULL_IFILE);
 #endif  #endif
         }          }
         /*          /*
          * Set up terminal, etc.           * Set up terminal, etc.
          */           */
         is_tty = isatty(1);  
         if (!is_tty)          if (!is_tty)
         {          {
                 /*                  /*
                  * Output is not a tty.                   * Output is not a tty.
                  * Just copy the input file(s) to output.                   * Just copy the input file(s) to output.
                  */                   */
                   SET_BINARY(1);
                 if (nifile() == 0)                  if (nifile() == 0)
                 {                  {
                         if (edit_stdin() == 0)                          if (edit_stdin() == 0)
Line 207 
Line 223 
                 quit(QUIT_OK);                  quit(QUIT_OK);
         }          }
   
           if (missing_cap && !know_dumb && !ismore)
                   error("WARNING: terminal is not fully functional", NULL_PARG);
         init_mark();          init_mark();
         raw_mode(1);  
         open_getchr();          open_getchr();
           raw_mode(1);
         init_signals(1);          init_signals(1);
   
         /*          /*
          * Select the first file to examine.           * Select the first file to examine.
          */           */
 #if TAGS  #if TAGS
         if (tagoption != NULL)          if (tagoption != NULL || strcmp(tags, "-") == 0)
         {          {
                 /*                  /*
                  * A -t option was given.                   * A -t option was given.
Line 230 
Line 248 
                         quit(QUIT_ERROR);                          quit(QUIT_ERROR);
                 }                  }
                 findtag(tagoption);                  findtag(tagoption);
                 if (tagfile == NULL)                  if (edit_tagfile())  /* Edit file which contains the tag */
                         quit(QUIT_ERROR);                          quit(QUIT_ERROR);
                 if (edit(tagfile))  /* Edit file which contains the tag */  
                         quit(QUIT_ERROR);  
                 /*                  /*
                  * Search for the line which contains the tag.                   * Search for the line which contains the tag.
                  * Set up initial_scrpos so we display that line.                   * Set up initial_scrpos so we display that line.
Line 258 
Line 274 
         commands();          commands();
         quit(QUIT_OK);          quit(QUIT_OK);
         /*NOTREACHED*/          /*NOTREACHED*/
           return (0);
 }  }
   
 /*  /*
  * Copy a string, truncating to the specified length if necessary.  
  * Unlike strncpy(), the resulting string is guaranteed to be null-terminated.  
  */  
         public void  
 strtcpy(to, from, len)  
         char *to;  
         char *from;  
         unsigned int len;  
 {  
         strncpy(to, from, len);  
         to[len-1] = '\0';  
 }  
   
 /*  
  * Copy a string to a "safe" place   * Copy a string to a "safe" place
  * (that is, to a buffer allocated by calloc).   * (that is, to a buffer allocated by calloc).
  */   */
Line 282 
Line 285 
 save(s)  save(s)
         char *s;          char *s;
 {  {
         char *p;          register char *p;
         size_t len;          size_t len;
   
         len = strlen(s)+1;          len = strlen(s)+1, sizeof(char);
         p = (char *) ecalloc(len, sizeof(char));          p = (char *) ecalloc(len, sizeof(char));
         strlcpy(p, s, len);          strlcpy(p, s, len);
         return (p);          return (p);
Line 300 
Line 303 
         int count;          int count;
         unsigned int size;          unsigned int size;
 {  {
         VOID_POINTER p;          register VOID_POINTER p;
   
         p = (VOID_POINTER) calloc(count, size);          p = (VOID_POINTER) calloc(count, size);
         if (p != NULL)          if (p != NULL)
Line 308 
Line 311 
         error("Cannot allocate memory", NULL_PARG);          error("Cannot allocate memory", NULL_PARG);
         quit(QUIT_ERROR);          quit(QUIT_ERROR);
         /*NOTREACHED*/          /*NOTREACHED*/
           return (NULL);
 }  }
   
 /*  /*
Line 315 
Line 319 
  */   */
         public char *          public char *
 skipsp(s)  skipsp(s)
         char *s;          register char *s;
 {  {
         while (*s == ' ' || *s == '\t')          while (*s == ' ' || *s == '\t')
                 s++;                  s++;
Line 323 
Line 327 
 }  }
   
 /*  /*
    * See how many characters of two strings are identical.
    * If uppercase is true, the first string must begin with an uppercase
    * character; the remainder of the first string may be either case.
    */
           public int
   sprefix(ps, s, uppercase)
           char *ps;
           char *s;
           int uppercase;
   {
           register int c;
           register int sc;
           register int len = 0;
   
           for ( ;  *s != '\0';  s++, ps++)
           {
                   c = *ps;
                   if (uppercase)
                   {
                           if (len == 0 && SIMPLE_IS_LOWER(c))
                                   return (-1);
                           if (SIMPLE_IS_UPPER(c))
                                   c = SIMPLE_TO_LOWER(c);
                   }
                   sc = *s;
                   if (len > 0 && SIMPLE_IS_UPPER(sc))
                           sc = SIMPLE_TO_LOWER(sc);
                   if (c != sc)
                           break;
                   len++;
           }
           return (len);
   }
   
   /*
  * Exit the program.   * Exit the program.
  */   */
         public void          public void
Line 341 
Line 380 
                 save_status = status;                  save_status = status;
         quitting = 1;          quitting = 1;
         edit((char*)NULL);          edit((char*)NULL);
         if (is_tty && any_display)          if (any_display && is_tty)
                 clear_bot();                  clear_bot();
         deinit();          deinit();
         flush();          flush();
         raw_mode(0);          raw_mode(0);
 #if MSOFTC  #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
         /*          /*
          * If we don't close 2, we get some garbage from           * If we don't close 2, we get some garbage from
          * 2's buffer when it flushes automatically.           * 2's buffer when it flushes automatically.
Line 355 
Line 394 
          */           */
         close(2);          close(2);
 #endif  #endif
   #if WIN32
           SetConsoleTitle(consoleTitle);
   #endif
           close_getchr();
         exit(status);          exit(status);
 }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7