home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / LES177AS.ZIP / CHARSET.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-14  |  3.8 KB  |  214 lines

  1. /*
  2.  * Functions to define the character set
  3.  * and do things specific to the character set.
  4.  */
  5.  
  6. #include "less.h"
  7.  
  8. /*
  9.  * Predefined character sets,
  10.  * selected by the LESSCHARSET environment variable.
  11.  */
  12. struct charset {
  13.     char *name;
  14.     char *desc;
  15. } charsets[] = {
  16.     { "ascii",    "8bcccbcc18b95.b"    },
  17.     { "latin1",    "8bcccbcc18b95.33b."    },
  18.     { "ibmpc",    "8bcccbcc18b."    },
  19.     { NULL }
  20. };
  21.  
  22. #define    IS_BINARY_CHAR    01
  23. #define    IS_CONTROL_CHAR    02
  24.  
  25. static char chardef[256];
  26. static char *binfmt = "\\%o";
  27. #ifdef TURBOC
  28. public int binattr = BOLD2;
  29. #else
  30. public int binattr = BLINKING;
  31. #endif
  32.  
  33. extern char *getenv();
  34.  
  35. /*
  36.  * Define a charset, given a description string.
  37.  * The string consists of 256 letters,
  38.  * one for each character in the charset.
  39.  * If the string is shorter than 256 letters, missing letters
  40.  * are taken to be identical to the last one.
  41.  * A decimal number followed by a letter is taken to be a 
  42.  * repetition of the letter.
  43.  *
  44.  * Each letter is one of:
  45.  *    . normal character
  46.  *    b binary character
  47.  *    c control character
  48.  */
  49.     static void
  50. ichardef(s)
  51.     char *s;
  52. {
  53.     register char *cp;
  54.     register int n;
  55.     register char v;
  56.  
  57.     n = 0;
  58.     cp = chardef;
  59.     while (*s != '\0')
  60.     {
  61.         switch (*s++)
  62.         {
  63.         case '.':
  64.             v = 0;
  65.             break;
  66.         case 'c':
  67.             v = IS_CONTROL_CHAR;
  68.             break;
  69.         case 'b':
  70.             v = IS_BINARY_CHAR|IS_CONTROL_CHAR;
  71.             break;
  72.  
  73.         case '0': case '1': case '2': case '3': case '4':
  74.         case '5': case '6': case '7': case '8': case '9':
  75.             n = (10 * n) + (s[-1] - '0');
  76.             continue;
  77.  
  78.         default:
  79.             error("invalid chardef", NULL_PARG);
  80.             quit(1);
  81.             /*NOTREACHED*/
  82.         }
  83.  
  84.         do
  85.         {
  86.             if (cp >= chardef + sizeof(chardef))
  87.             {
  88.                 error("chardef longer than 256", NULL_PARG);
  89.                 quit(1);
  90.                 /*NOTREACHED*/
  91.             }
  92.             *cp++ = v;
  93.         } while (--n > 0);
  94.         n = 0;
  95.     }
  96.  
  97.     while (cp < chardef + sizeof(chardef))
  98.         *cp++ = v;
  99. }
  100.  
  101. /*
  102.  * Define a charset, given a charset name.
  103.  * The valid charset names are listed in the "charsets" array.
  104.  */
  105.     static int
  106. icharset(name)
  107.     register char *name;
  108. {
  109.     register struct charset *p;
  110.  
  111.     if (name == NULL || *name == '\0')
  112.         return (0);
  113.  
  114.     for (p = charsets;  p->name != NULL;  p++)
  115.     {
  116.         if (strcmp(name, p->name) == 0)
  117.         {
  118.             ichardef(p->desc);
  119.             return (1);
  120.         }
  121.     }
  122.  
  123.     error("invalid charset name", NULL_PARG);
  124.     quit(1);
  125.     return (0);    /*NOTREACHED*/
  126. }
  127.  
  128. /*
  129.  * Initialize charset data structures.
  130.  */
  131.     public void
  132. init_charset()
  133. {
  134.     register char *s;
  135.  
  136.     /*
  137.      * Try environment variable LESSCHARSET.
  138.      * If LESSCHARSET is not set, try LESSCHARDEF.
  139.      * If LESSCHARDEF is not set, default to "ascii" charset.
  140.      */
  141.     s = getenv("LESSCHARSET");
  142.     if (icharset(s))
  143.         return;
  144.  
  145.     s = getenv("LESSCHARDEF");
  146.     if (s != NULL && *s != '\0')
  147.     {
  148.         ichardef(s);
  149.         return;
  150.     }
  151. #ifdef TURBOC
  152.     (void) icharset("ibmpc");
  153. #else
  154.     (void) icharset("ascii");
  155. #endif
  156.  
  157.     s = getenv("LESSBINFMT");
  158.     if (s != NULL && *s != '\0')
  159.     {
  160.         if (*s == '*')
  161.         {
  162.             switch (s[1])
  163.             {
  164.             case 'd':  binattr = BOLD;      break;
  165.             case 'k':  binattr = BLINKING;  break;
  166.             case 'u':  binattr = UNDERLINE; break;
  167.             default:   binattr = NORMAL;    break;
  168.             }
  169.             s += 2;
  170.         }
  171.         if (*s != '\0')
  172.             binfmt = s;
  173.     }
  174. }
  175.  
  176. /*
  177.  * Is a given character a "binary" character?
  178.  */
  179.     public int
  180. binary_char(c)
  181.     int c;
  182. {
  183.     return (chardef[c] & IS_BINARY_CHAR);
  184. }
  185.  
  186. /*
  187.  * Is a given character a "control" character?
  188.  */
  189.     public int
  190. control_char(c)
  191.     int c;
  192. {
  193.     return (chardef[c] & IS_CONTROL_CHAR);
  194. }
  195.  
  196. /*
  197.  * Return the printable form of a character.
  198.  * For example, in the "ascii" charset '\3' is printed as "^C".
  199.  */
  200.     public char *
  201. prchar(c)
  202.     int c;
  203. {
  204.     static char buf[8];
  205.  
  206.     if (!control_char(c))
  207.         sprintf(buf, "%c", c);
  208.     else if (!control_char(c ^ 0100))
  209.         sprintf(buf, "^%c", c ^ 0100);
  210.     else
  211.         sprintf(buf, binfmt, c);
  212.     return (buf);
  213. }
  214.