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

Diff for /src/usr.bin/less/charset.c between version 1.1.1.1 and 1.1.1.2

version 1.1.1.1, 1996/09/21 05:39:41 version 1.1.1.2, 2003/04/13 18:21:21
Line 1 
Line 1 
 /*  /*
  * 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 36 
Line 20 
 #include <ctype.h>  #include <ctype.h>
 #endif  #endif
   
   public int utf_mode = 0;
   
 /*  /*
  * Predefined character sets,   * Predefined character sets,
  * selected by the LESSCHARSET environment variable.   * selected by the LESSCHARSET environment variable.
  */   */
 struct charset {  struct charset {
         char *name;          char *name;
           int *p_flag;
         char *desc;          char *desc;
 } charsets[] = {  } charsets[] = {
         { "ascii",      "8bcccbcc18b95.b"               },          { "ascii",      NULL,       "8bcccbcc18b95.b" },
         { "latin1",     "8bcccbcc18b95.33b."            },          { "dos",        NULL,       "8bcccbcc12bc5b223.b" },
         { "dos",        "8bcccbcc12bc5b95.b."           },          { "ebcdic",     NULL,       "5bc6bcc7bcc41b.9b7.9b5.b..8b6.10b6.b9.7b9.8b8.17b3.3b9.7b9.8b8.6b10.b.b.b." },
         { "koi8-r",     "8bcccbcc18b95.b128."           },          { "IBM-1047",   NULL,       "4cbcbc3b9cbccbccbb4c6bcc5b3cbbc4bc4bccbc191.b" },
         { "next",       "8bcccbcc18b95.bb125.bb"        },          { "iso8859",    NULL,       "8bcccbcc18b95.33b." },
         { NULL }          { "koi8-r",     NULL,       "8bcccbcc18b95.b128." },
           { "next",       NULL,       "8bcccbcc18b95.bb125.bb" },
           { "utf-8",      &utf_mode,  "8bcccbcc18b." },
           { NULL, NULL, NULL }
 };  };
   
   struct cs_alias {
           char *name;
           char *oname;
   } cs_aliases[] = {
           { "latin1",     "iso8859" },
           { "latin9",     "iso8859" },
           { NULL, NULL }
   };
   
 #define IS_BINARY_CHAR  01  #define IS_BINARY_CHAR  01
 #define IS_CONTROL_CHAR 02  #define IS_CONTROL_CHAR 02
   
Line 136 
Line 135 
         register char *name;          register char *name;
 {  {
         register struct charset *p;          register struct charset *p;
           register struct cs_alias *a;
   
         if (name == NULL || *name == '\0')          if (name == NULL || *name == '\0')
                 return (0);                  return (0);
   
           /* First see if the name is an alias. */
           for (a = cs_aliases;  a->name != NULL;  a++)
           {
                   if (strcmp(name, a->name) == 0)
                   {
                           name = a->oname;
                           break;
                   }
           }
   
         for (p = charsets;  p->name != NULL;  p++)          for (p = charsets;  p->name != NULL;  p++)
         {          {
                 if (strcmp(name, p->name) == 0)                  if (strcmp(name, p->name) == 0)
                 {                  {
                         ichardef(p->desc);                          ichardef(p->desc);
                           if (p->p_flag != NULL)
                                   *(p->p_flag) = 1;
                         return (1);                          return (1);
                 }                  }
         }          }
Line 152 
Line 164 
         error("invalid charset name", NULL_PARG);          error("invalid charset name", NULL_PARG);
         quit(QUIT_ERROR);          quit(QUIT_ERROR);
         /*NOTREACHED*/          /*NOTREACHED*/
           return (0);
 }  }
   
 #if HAVE_LOCALE  #if HAVE_LOCALE
Line 163 
Line 176 
 {  {
         register int c;          register int c;
   
         setlocale(LC_CTYPE, "");          setlocale(LC_ALL, "");
         for (c = 0;  c < sizeof(chardef);  c++)          for (c = 0;  c < (int) sizeof(chardef);  c++)
         {          {
                 if (isprint(c))                  if (isprint(c))
                         chardef[c] = 0;                          chardef[c] = 0;
Line 211 
Line 224 
 {  {
         register char *s;          register char *s;
   
         s = getenv("LESSBINFMT");          s = lgetenv("LESSBINFMT");
         setbinfmt(s);          setbinfmt(s);
   
         /*          /*
          * See if environment variable LESSCHARSET is defined.           * See if environment variable LESSCHARSET is defined.
          */           */
         s = getenv("LESSCHARSET");          s = lgetenv("LESSCHARSET");
         if (icharset(s))          if (icharset(s))
                 return;                  return;
         /*          /*
          * LESSCHARSET is not defined: try LESSCHARDEF.           * LESSCHARSET is not defined: try LESSCHARDEF.
          */           */
         s = getenv("LESSCHARDEF");          s = lgetenv("LESSCHARDEF");
         if (s != NULL && *s != '\0')          if (s != NULL && *s != '\0')
         {          {
                 ichardef(s);                  ichardef(s);
                 return;                  return;
         }          }
   
   #if HAVE_STRSTR
           /*
            * Check whether LC_ALL, LC_CTYPE or LANG look like UTF-8 is used.
            */
           if ((s = lgetenv("LC_ALL")) != NULL ||
               (s = lgetenv("LC_CTYPE")) != NULL ||
               (s = lgetenv("LANG")) != NULL)
           {
                   if (strstr(s, "UTF-8") != NULL || strstr(s, "utf-8") != NULL)
                           if (icharset("utf-8"))
                                   return;
           }
   #endif
   
 #if HAVE_LOCALE  #if HAVE_LOCALE
         /*          /*
          * Use setlocale.           * Use setlocale.
          */           */
         ilocale();          ilocale();
 #else  #else
   #if MSDOS_COMPILER
         /*          /*
          * Default to "ascii".           * Default to "dos".
          */           */
         (void) icharset("ascii");          (void) icharset("dos");
   #else
           /*
            * Default to "latin1".
            */
           (void) icharset("latin1");
 #endif  #endif
   #endif
 }  }
   
 /*  /*
Line 247 
Line 282 
  */   */
         public int          public int
 binary_char(c)  binary_char(c)
         int c;          unsigned char c;
 {  {
         c &= 0377;          c &= 0377;
         return (chardef[c] & IS_BINARY_CHAR);          return (chardef[c] & IS_BINARY_CHAR);
Line 279 
Line 314 
                 sprintf(buf, "%c", c);                  sprintf(buf, "%c", c);
         else if (c == ESC)          else if (c == ESC)
                 sprintf(buf, "ESC");                  sprintf(buf, "ESC");
         else if (c < 128 && !control_char(c ^ 0100))  #if IS_EBCDIC_HOST
                 sprintf(buf, "^%c", c ^ 0100);          else if (!binary_char(c) && c < 64)
                   sprintf(buf, "^%c",
                   /*
                    * This array roughly inverts CONTROL() #defined in less.h,
                    * and should be kept in sync with CONTROL() and IBM-1047.
                    */
                   "@ABC.I.?...KLMNO"
                   "PQRS.JH.XY.."
                   "\\]^_"
                   "......W[.....EFG"
                   "..V....D....TU.Z"[c]);
   #else
           else if (c < 128 && !control_char(c ^ 0100))
                   sprintf(buf, "^%c", c ^ 0100);
   #endif
         else          else
                 sprintf(buf, binfmt, c);                  sprintf(buf, binfmt, c);
         return (buf);          return (buf);

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.2