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

Diff for /mandoc/chars.c between version 1.1 and 1.15

version 1.1, 2009/09/17 07:41:28 version 1.15, 2010/01/05 19:51:10
Line 14 
Line 14 
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */   */
   #ifdef HAVE_CONFIG_H
   #include "config.h"
   #endif
   
 #include <assert.h>  #include <assert.h>
 #include <err.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   
 #include "chars.h"  #include "chars.h"
   
 #define ASCII_PRINT_HI   126  #define PRINT_HI         126
 #define ASCII_PRINT_LO   32  #define PRINT_LO         32
   
 struct  ln {  struct  ln {
         struct ln        *next;          struct ln        *next;
         const char       *code;          const char       *code;
         const char       *out;          const char       *ascii;
           const char       *html;
         size_t            codesz;          size_t            codesz;
         size_t            outsz;          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       (0x03)  #define CHARS_BOTH       (CHARS_CHAR | CHARS_STRING)
 };  };
   
 #define LINES_MAX         266  #define LINES_MAX         350
   
 #define CHAR(w, x, y, z) \  #define CHAR(w, x, y, z, a, b) \
         { NULL, (w), (y), (x), (z), CHARS_CHAR },          { NULL, (w), (y), (a), (x), (z), (b), CHARS_CHAR },
 #define STRING(w, x, y, z) \  #define STRING(w, x, y, z, a, b) \
         { NULL, (w), (y), (x), (z), CHARS_STRING },          { NULL, (w), (y), (a), (x), (z), (b), CHARS_STRING },
 #define BOTH(w, x, y, z) \  #define BOTH(w, x, y, z, a, b) \
         { NULL, (w), (y), (x), (z), CHARS_BOTH },          { NULL, (w), (y), (a), (x), (z), (b), CHARS_BOTH },
   
 static  struct ln lines[LINES_MAX] = {  #define CHAR_TBL_START    static struct ln lines[LINES_MAX] = {
   #define CHAR_TBL_END      };
   
 #include "chars.in"  #include "chars.in"
 };  
   
 struct  tbl {  struct  tbl {
           enum chars        type;
         struct ln       **htab;          struct ln       **htab;
 };  };
   
Line 71  chars_free(void *arg)
Line 79  chars_free(void *arg)
 }  }
   
   
 /* ARGSUSED */  
 void *  void *
 chars_init(enum chars type)  chars_init(enum chars type)
 {  {
Line 87  chars_init(enum chars type)
Line 94  chars_init(enum chars type)
          * (they're in-line re-ordered during lookup).           * (they're in-line re-ordered during lookup).
          */           */
   
         if (NULL == (tab = malloc(sizeof(struct tbl))))          tab = malloc(sizeof(struct tbl));
                 err(1, "malloc");          if (NULL == tab) {
                   perror(NULL);
                   exit(EXIT_FAILURE);
           }
   
         htab = calloc(ASCII_PRINT_HI - ASCII_PRINT_LO + 1,          htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
                         sizeof(struct ln **));          if (NULL == htab) {
                   perror(NULL);
                   exit(EXIT_FAILURE);
           }
   
         if (NULL == htab)  
                 err(1, "malloc");  
   
         for (i = 0; i < LINES_MAX; i++) {          for (i = 0; i < LINES_MAX; i++) {
                 assert(lines[i].codesz > 0);                  hash = (int)lines[i].code[0] - PRINT_LO;
                 assert(lines[i].code);  
                 assert(lines[i].out);  
   
                 hash = (int)lines[i].code[0] - ASCII_PRINT_LO;  
   
                 if (NULL == (pp = htab[hash])) {                  if (NULL == (pp = htab[hash])) {
                         htab[hash] = &lines[i];                          htab[hash] = &lines[i];
                         continue;                          continue;
Line 110  chars_init(enum chars type)
Line 116  chars_init(enum chars type)
   
                 for ( ; pp->next; pp = pp->next)                  for ( ; pp->next; pp = pp->next)
                         /* Scan ahead. */ ;                          /* Scan ahead. */ ;
   
                 pp->next = &lines[i];                  pp->next = &lines[i];
         }          }
   
         tab->htab = htab;          tab->htab = htab;
           tab->type = type;
         return(tab);          return(tab);
 }  }
   
Line 145  find(struct tbl *tab, const char *p, size_t sz, size_t
Line 151  find(struct tbl *tab, const char *p, size_t sz, size_t
         assert(p);          assert(p);
         assert(sz > 0);          assert(sz > 0);
   
         if (p[0] < ASCII_PRINT_LO || p[0] > ASCII_PRINT_HI)          if (p[0] < PRINT_LO || p[0] > PRINT_HI)
                 return(NULL);                  return(NULL);
   
         /*          /*
Line 154  find(struct tbl *tab, const char *p, size_t sz, size_t
Line 160  find(struct tbl *tab, const char *p, size_t sz, size_t
          * to optimise for repeat hits.           * to optimise for repeat hits.
          */           */
   
         hash = (int)p[0] - ASCII_PRINT_LO;          hash = (int)p[0] - PRINT_LO;
         htab = tab->htab;          htab = tab->htab;
   
         if (NULL == (pp = htab[hash]))          if (NULL == (pp = htab[hash]))
Line 163  find(struct tbl *tab, const char *p, size_t sz, size_t
Line 169  find(struct tbl *tab, const char *p, size_t sz, size_t
         if (NULL == pp->next) {          if (NULL == pp->next) {
                 if ( ! match(pp, p, sz, type))                  if ( ! match(pp, p, sz, type))
                         return(NULL);                          return(NULL);
                 *rsz = pp->outsz;  
                 return(pp->out);                  if (CHARS_HTML == tab->type) {
                           *rsz = pp->htmlsz;
                           return(pp->html);
                   }
                   *rsz = pp->asciisz;
                   return(pp->ascii);
         }          }
   
         for (prev = NULL; pp; pp = pp->next) {          for (prev = NULL; pp; pp = pp->next) {
Line 173  find(struct tbl *tab, const char *p, size_t sz, size_t
Line 184  find(struct tbl *tab, const char *p, size_t sz, size_t
                         continue;                          continue;
                 }                  }
   
                 /* Re-order the hash chain. */  
   
                 if (prev) {                  if (prev) {
                         prev->next = pp->next;                          prev->next = pp->next;
                         pp->next = htab[hash];                          pp->next = htab[hash];
                         htab[hash] = pp;                          htab[hash] = pp;
                 }                  }
   
                 *rsz = pp->outsz;                  if (CHARS_HTML == tab->type) {
                 return(pp->out);                          *rsz = pp->htmlsz;
                           return(pp->html);
                   }
                   *rsz = pp->asciisz;
                   return(pp->ascii);
         }          }
   
         return(NULL);          return(NULL);

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.15

CVSweb