[BACK]Return to chars.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/chars.c between version 1.19 and 1.34

version 1.19, 2010/06/01 11:47:28 version 1.34, 2011/03/22 10:13:01
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
    * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
Line 24 
Line 25 
 #include <string.h>  #include <string.h>
   
 #include "mandoc.h"  #include "mandoc.h"
 #include "chars.h"  #include "out.h"
   
 #define PRINT_HI         126  #define PRINT_HI         126
 #define PRINT_LO         32  #define PRINT_LO         32
Line 33  struct ln {
Line 34  struct ln {
         struct ln        *next;          struct ln        *next;
         const char       *code;          const char       *code;
         const char       *ascii;          const char       *ascii;
         const char       *html;          int               unicode;
         size_t            codesz;  
         size_t            asciisz;  
         size_t            htmlsz;  
         int               type;          int               type;
 #define CHARS_CHAR       (1 << 0)  #define CHARS_CHAR       (1 << 0)
 #define CHARS_STRING     (1 << 1)  #define CHARS_STRING     (1 << 1)
 #define CHARS_BOTH       (CHARS_CHAR | CHARS_STRING)  #define CHARS_BOTH       (CHARS_CHAR | CHARS_STRING)
 };  };
   
 #define LINES_MAX         370  #define LINES_MAX         351
   
 #define CHAR(w, x, y, z, a, b) \  #define CHAR(in, ch, code) \
         { NULL, (w), (y), (a), (x), (z), (b), CHARS_CHAR },          { NULL, (in), (ch), (code), CHARS_CHAR },
 #define STRING(w, x, y, z, a, b) \  #define STRING(in, ch, code) \
         { NULL, (w), (y), (a), (x), (z), (b), CHARS_STRING },          { NULL, (in), (ch), (code), CHARS_STRING },
 #define BOTH(w, x, y, z, a, b) \  #define BOTH(in, ch, code) \
         { NULL, (w), (y), (a), (x), (z), (b), CHARS_BOTH },          { NULL, (in), (ch), (code), CHARS_BOTH },
   
 #define CHAR_TBL_START    static struct ln lines[LINES_MAX] = {  #define CHAR_TBL_START    static struct ln lines[LINES_MAX] = {
 #define CHAR_TBL_END      };  #define CHAR_TBL_END      };
   
 #include "chars.in"  #include "chars.in"
   
 struct  tbl {  struct  ctab {
         enum chars        type;          enum chars        type;
         struct ln       **htab;          struct ln       **htab;
 };  };
   
 static  inline int        match(const struct ln *,  static  inline int        match(const struct ln *,
                                 const char *, size_t, int);                                  const char *, size_t, int);
 static  const char       *find(struct tbl *, const char *,  static  const struct ln  *find(struct ctab *, const char *, size_t, int);
                                 size_t, size_t *, int);  
   
   
 void  void
 chars_free(void *arg)  chars_free(void *arg)
 {  {
         struct tbl      *tab;          struct ctab     *tab;
   
         tab = (struct tbl *)arg;          tab = (struct ctab *)arg;
   
         free(tab->htab);          free(tab->htab);
         free(tab);          free(tab);
Line 83  chars_free(void *arg)
Line 80  chars_free(void *arg)
 void *  void *
 chars_init(enum chars type)  chars_init(enum chars type)
 {  {
         struct tbl       *tab;          struct ctab      *tab;
         struct ln       **htab;          struct ln       **htab;
         struct ln        *pp;          struct ln        *pp;
         int               i, hash;          int               i, hash;
Line 95  chars_init(enum chars type)
Line 92  chars_init(enum chars type)
          * (they're in-line re-ordered during lookup).           * (they're in-line re-ordered during lookup).
          */           */
   
         tab = malloc(sizeof(struct tbl));          tab = mandoc_malloc(sizeof(struct ctab));
         if (NULL == tab) {          htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
                 perror(NULL);  
                 exit(EXIT_FAILURE);  
         }  
   
         htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));  
         if (NULL == htab) {  
                 perror(NULL);  
                 exit(EXIT_FAILURE);  
         }  
   
         for (i = 0; i < LINES_MAX; i++) {          for (i = 0; i < LINES_MAX; i++) {
                 hash = (int)lines[i].code[0] - PRINT_LO;                  hash = (int)lines[i].code[0] - PRINT_LO;
   
Line 126  chars_init(enum chars type)
Line 114  chars_init(enum chars type)
 }  }
   
   
   /*
    * Special character to Unicode codepoint.
    */
   int
   chars_spec2cp(void *arg, const char *p, size_t sz)
   {
           const struct ln *ln;
   
           ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
           if (NULL == ln)
                   return(-1);
           return(ln->unicode);
   }
   
   
   /*
    * Reserved word to Unicode codepoint.
    */
   int
   chars_res2cp(void *arg, const char *p, size_t sz)
   {
           const struct ln *ln;
   
           ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
           if (NULL == ln)
                   return(-1);
           return(ln->unicode);
   }
   
   
   /*
    * Numbered character to literal character,
    * represented as a null-terminated string for additional safety.
    */
 const char *  const char *
 chars_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz)  chars_num2char(const char *p, size_t sz)
 {  {
           int               i;
           static char       c[2];
   
         return(find((struct tbl *)arg, p, sz, rsz, CHARS_CHAR));          if (sz > 3)
                   return(NULL);
           i = atoi(p);
           if (i < 0 || i > 255)
                   return(NULL);
           c[0] = (char)i;
           c[1] = '\0';
           return(c);
 }  }
   
   
   /*
    * Special character to string array.
    */
 const char *  const char *
 chars_a2res(void *arg, const char *p, size_t sz, size_t *rsz)  chars_spec2str(void *arg, const char *p, size_t sz, size_t *rsz)
 {  {
           const struct ln *ln;
   
         return(find((struct tbl *)arg, p, sz, rsz, CHARS_STRING));          ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
           if (NULL == ln)
                   return(NULL);
   
           *rsz = strlen(ln->ascii);
           return(ln->ascii);
 }  }
   
   
 static const char *  /*
 find(struct tbl *tab, const char *p, size_t sz, size_t *rsz, int type)   * Reserved word to string array.
    */
   const char *
   chars_res2str(void *arg, const char *p, size_t sz, size_t *rsz)
 {  {
           const struct ln *ln;
   
           ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
           if (NULL == ln)
                   return(NULL);
   
           *rsz = strlen(ln->ascii);
           return(ln->ascii);
   }
   
   
   static const struct ln *
   find(struct ctab *tab, const char *p, size_t sz, int type)
   {
         struct ln        *pp, *prev;          struct ln        *pp, *prev;
         struct ln       **htab;          struct ln       **htab;
         int               hash;          int               hash;
   
         assert(p);          assert(p);
         assert(sz > 0);          if (0 == sz)
                   return(NULL);
   
         if (p[0] < PRINT_LO || p[0] > PRINT_HI)          if (p[0] < PRINT_LO || p[0] > PRINT_HI)
                 return(NULL);                  return(NULL);
Line 179  find(struct tbl *tab, const char *p, size_t sz, size_t
Line 237  find(struct tbl *tab, const char *p, size_t sz, size_t
                         htab[hash] = pp;                          htab[hash] = pp;
                 }                  }
   
                 if (CHARS_HTML == tab->type) {                  return(pp);
                         *rsz = pp->htmlsz;  
                         return(pp->html);  
                 }  
                 *rsz = pp->asciisz;  
                 return(pp->ascii);  
         }          }
   
         return(NULL);          return(NULL);
Line 197  match(const struct ln *ln, const char *p, size_t sz, i
Line 250  match(const struct ln *ln, const char *p, size_t sz, i
   
         if ( ! (ln->type & type))          if ( ! (ln->type & type))
                 return(0);                  return(0);
         if (ln->codesz != sz)          if (strncmp(ln->code, p, sz))
                 return(0);                  return(0);
         return(0 == strncmp(ln->code, p, sz));          return('\0' == ln->code[(int)sz]);
 }  }

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.34

CVSweb