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

Diff for /mandoc/Attic/apropos.c between version 1.1 and 1.7

version 1.1, 2011/10/06 23:00:54 version 1.7, 2011/10/09 10:46:38
Line 41 
Line 41 
   
 #include "mandoc.h"  #include "mandoc.h"
   
 #define MAXRESULTS       100  #define MAXRESULTS       256
   
 #define TYPE_NAME       0x01  /* Bit-fields.  See mandocdb.8. */
 #define TYPE_FUNCTION   0x02  
 #define TYPE_UTILITY    0x04  
 #define TYPE_INCLUDES   0x08  
 #define TYPE_VARIABLE   0x10  
 #define TYPE_STANDARD   0x20  
 #define TYPE_AUTHOR     0x40  
 #define TYPE_CONFIG     0x80  
 #define TYPE_DESC       0x100  
 #define TYPE_XREF       0x200  
 #define TYPE_PATH       0x400  
 #define TYPE_ENV        0x800  
 #define TYPE_ERR        0x1000  
   
   #define TYPE_NAME         0x01
   #define TYPE_FUNCTION     0x02
   #define TYPE_UTILITY      0x04
   #define TYPE_INCLUDES     0x08
   #define TYPE_VARIABLE     0x10
   #define TYPE_STANDARD     0x20
   #define TYPE_AUTHOR       0x40
   #define TYPE_CONFIG       0x80
   #define TYPE_DESC         0x100
   #define TYPE_XREF         0x200
   #define TYPE_PATH         0x400
   #define TYPE_ENV          0x800
   #define TYPE_ERR          0x1000
   
 enum    match {  enum    match {
         MATCH_SUBSTR = 0,          MATCH_SUBSTR = 0,
         MATCH_REGEX,          MATCH_REGEX,
Line 80  struct opts {
Line 82  struct opts {
   
 struct  type {  struct  type {
         int              mask;          int              mask;
         const char      *name;          const char      *name; /* command-line type name */
 };  };
   
 struct  rec {  struct  rec {
         char            *file;          char            *file; /* file in file-system */
         char            *cat;          char            *cat; /* category (3p, 3, etc.) */
         char            *title;          char            *title; /* title (FOO, etc.) */
         char            *arch;          char            *arch; /* arch (or empty string) */
         char            *desc;          char            *desc; /* description (from Nd) */
         recno_t          rec;          recno_t          rec; /* record in index */
 };  };
   
 struct  res {  struct  res {
Line 101  struct res {
Line 103  struct res {
         char            *title; /* manual section */          char            *title; /* manual section */
         char            *uri; /* formatted uri of file */          char            *uri; /* formatted uri of file */
         recno_t          rec; /* unique id of underlying manual */          recno_t          rec; /* unique id of underlying manual */
           /*
            * Maintain a binary tree for checking the uniqueness of `rec'
            * when adding elements to the results array.
            * Since the results array is dynamic, use offset in the array
            * instead of a pointer to the structure.
            */
           int              lhs;
           int              rhs;
 };  };
   
 struct  state {  struct  state {
Line 108  struct state {
Line 118  struct state {
         DB               *idx; /* index */          DB               *idx; /* index */
         const char       *dbf; /* database name */          const char       *dbf; /* database name */
         const char       *idxf; /* index name */          const char       *idxf; /* index name */
         void            (*err)(const char *);  
         void            (*errx)(const char *, ...);  
 };  };
   
 static  const char * const sorts[SORT__MAX] = {  static  const char * const sorts[SORT__MAX] = {
Line 139  static void  buf_alloc(char **, size_t *, size_t);
Line 147  static void  buf_alloc(char **, size_t *, size_t);
 static  void     buf_dup(struct mchars *, char **, const char *);  static  void     buf_dup(struct mchars *, char **, const char *);
 static  void     buf_redup(struct mchars *, char **,  static  void     buf_redup(struct mchars *, char **,
                         size_t *, const char *);                          size_t *, const char *);
 static  void     error(const char *, ...);  
 static  int      sort_cat(const void *, const void *);  static  int      sort_cat(const void *, const void *);
 static  int      sort_title(const void *, const void *);  static  int      sort_title(const void *, const void *);
 static  void     state_destroy(struct state *);  static  int      state_getrecord(struct state *,
 static  int      state_getrecord(struct state *, recno_t, struct rec *);                          recno_t, struct rec *);
 static  int      state_init(struct state *,  
                         const char *, const char *,  
                         void (*err)(const char *),  
                         void (*errx)(const char *, ...));  
 static  void     state_output(const struct res *, int);  static  void     state_output(const struct res *, int);
 static  void     state_search(struct state *,  static  int      state_search(struct state *,
                         const struct opts *, char *);                          const struct opts *, char *);
   
 static  void     usage(void);  static  void     usage(void);
   
 static  const char       *progname;  static  char    *progname;
   
 int  int
 main(int argc, char *argv[])  main(int argc, char *argv[])
 {  {
         int              ch, i;          BTREEINFO        info;
           int              ch, i, rc;
         const char      *dbf, *idxf;          const char      *dbf, *idxf;
         struct state     state;          struct state     state;
         char            *q, *v;          char            *q, *v;
Line 168  main(int argc, char *argv[])
Line 171  main(int argc, char *argv[])
         extern char     *optarg;          extern char     *optarg;
   
         memset(&opts, 0, sizeof(struct opts));          memset(&opts, 0, sizeof(struct opts));
           memset(&state, 0, sizeof(struct state));
   
         dbf = "mandoc.db";          dbf = "mandoc.db";
         idxf = "mandoc.index";          idxf = "mandoc.index";
         q = NULL;          q = NULL;
           rc = EXIT_FAILURE;
   
         progname = strrchr(argv[0], '/');          progname = strrchr(argv[0], '/');
         if (progname == NULL)          if (progname == NULL)
Line 209  main(int argc, char *argv[])
Line 214  main(int argc, char *argv[])
                         if (i < SORT__MAX)                          if (i < SORT__MAX)
                                 break;                                  break;
   
                         error("%s: Bad sort\n", optarg);                          fprintf(stderr, "%s: Bad sort\n", optarg);
                         return(EXIT_FAILURE);                          return(EXIT_FAILURE);
                 case ('t'):                  case ('t'):
                         while (NULL != (v = strsep(&optarg, ","))) {                          while (NULL != (v = strsep(&optarg, ","))) {
Line 227  main(int argc, char *argv[])
Line 232  main(int argc, char *argv[])
                         if (NULL == v)                          if (NULL == v)
                                 break;                                  break;
   
                         error("%s: Bad type\n", v);                          fprintf(stderr, "%s: Bad type\n", v);
                         return(EXIT_FAILURE);                          return(EXIT_FAILURE);
                 default:                  default:
                         usage();                          usage();
Line 239  main(int argc, char *argv[])
Line 244  main(int argc, char *argv[])
   
         if (0 == argc || '\0' == **argv) {          if (0 == argc || '\0' == **argv) {
                 usage();                  usage();
                 return(EXIT_FAILURE);                  goto out;
         } else          } else
                 q = *argv;                  q = *argv;
   
         if (0 == opts.types)          if (0 == opts.types)
                 opts.types = TYPE_NAME | TYPE_DESC;                  opts.types = TYPE_NAME | TYPE_DESC;
   
         if ( ! state_init(&state, dbf, idxf, perror, error)) {          /*
                 state_destroy(&state);           * Configure databases.
                 return(EXIT_FAILURE);           * The keyword database is a btree that allows for duplicate
            * entries.
            * The index database is a recno.
            */
   
           memset(&info, 0, sizeof(BTREEINFO));
           info.flags = R_DUP;
   
           state.db = dbopen(dbf, O_RDONLY, 0, DB_BTREE, &info);
           if (NULL == state.db) {
                   perror(dbf);
                   goto out;
         }          }
   
         state_search(&state, &opts, q);          state.idx = dbopen(idxf, O_RDONLY, 0, DB_RECNO, NULL);
         state_destroy(&state);          if (NULL == state.idx) {
                   perror(idxf);
                   goto out;
           }
   
         return(EXIT_SUCCESS);          /* Main search function. */
   
           rc = state_search(&state, &opts, q) ?
                   EXIT_SUCCESS : EXIT_FAILURE;
   out:
           if (state.db)
                   (*state.db->close)(state.db);
           if (state.idx)
                   (*state.idx->close)(state.idx);
   
           return(rc);
 }  }
   
 static void  static int
 state_search(struct state *p, const struct opts *opts, char *q)  state_search(struct state *p, const struct opts *opts, char *q)
 {  {
         int              i, len, ch, rflags, dflag;          int              leaf, root, len, ch, dflag, rc;
         struct mchars   *mc;          struct mchars   *mc;
         char            *buf;          char            *buf;
         size_t           bufsz;          size_t           bufsz;
         recno_t          rec;          recno_t          rec;
         uint32_t         fl;          uint32_t         fl;
         DBT              key, val;          DBT              key, val;
         struct res       res[MAXRESULTS];          struct res      *res;
         regex_t          reg;          regex_t          reg;
         regex_t         *regp;          regex_t         *regp;
         char             filebuf[10];          char             filebuf[10];
         struct rec       record;          struct rec       record;
   
           rc = 0;
           root = leaf = -1;
           res = NULL;
         len = 0;          len = 0;
         buf = NULL;          buf = NULL;
         bufsz = 0;          bufsz = 0;
         ch = 0;  
         regp = NULL;          regp = NULL;
   
           /*
            * Configure how we scan through results to see if we match:
            * whether by regexp or exact matches.
            */
   
         switch (opts->match) {          switch (opts->match) {
         case (MATCH_REGEX):          case (MATCH_REGEX):
                 rflags = REG_EXTENDED | REG_NOSUB |                  ch = REG_EXTENDED | REG_NOSUB |
                         (opts->insens ? REG_ICASE : 0);                          (opts->insens ? REG_ICASE : 0);
   
                 if (0 != regcomp(&reg, q, rflags)) {                  if (0 != regcomp(&reg, q, ch)) {
                         error("%s: Bad pattern\n", q);                          fprintf(stderr, "%s: Bad pattern\n", q);
                         return;                          return(0);
                 }                  }
   
                 regp = &reg;                  regp = &reg;
Line 302  state_search(struct state *p, const struct opts *opts,
Line 338  state_search(struct state *p, const struct opts *opts,
                 break;                  break;
         }          }
   
         if (NULL == (mc = mchars_alloc())) {          mc = mchars_alloc();
                 perror(NULL);  
                 exit(EXIT_FAILURE);  
         }  
   
         /*          /*
          * Iterate over the entire keyword database.           * Iterate over the entire keyword database.
Line 314  state_search(struct state *p, const struct opts *opts,
Line 347  state_search(struct state *p, const struct opts *opts,
          * Lastly, add it to the available records.           * Lastly, add it to the available records.
          */           */
   
         while (len < MAXRESULTS) {          while (0 == (ch = (*p->db->seq)(p->db, &key, &val, dflag))) {
                 if ((ch = (*p->db->seq)(p->db, &key, &val, dflag)))  
                         break;  
   
                 dflag = R_NEXT;                  dflag = R_NEXT;
   
                 /*                  /*
Line 328  state_search(struct state *p, const struct opts *opts,
Line 358  state_search(struct state *p, const struct opts *opts,
                  */                   */
   
                 if (key.size < 2 || 8 != val.size) {                  if (key.size < 2 || 8 != val.size) {
                         error("%s: Corrupt database\n", p->dbf);                          fprintf(stderr, "%s: Bad database\n", p->dbf);
                         exit(EXIT_FAILURE);                          goto out;
                 }                  }
   
                 buf_redup(mc, &buf, &bufsz, (char *)key.data);                  buf_redup(mc, &buf, &bufsz, (char *)key.data);
Line 366  state_search(struct state *p, const struct opts *opts,
Line 396  state_search(struct state *p, const struct opts *opts,
                 memcpy(&rec, val.data + 4, sizeof(recno_t));                  memcpy(&rec, val.data + 4, sizeof(recno_t));
   
                 if ( ! state_getrecord(p, rec, &record))                  if ( ! state_getrecord(p, rec, &record))
                         exit(EXIT_FAILURE);                          goto out;
   
                 /* If we're in a different section, skip... */                  /* If we're in a different section, skip... */
   
Line 375  state_search(struct state *p, const struct opts *opts,
Line 405  state_search(struct state *p, const struct opts *opts,
                 if (opts->arch && strcasecmp(opts->arch, record.arch))                  if (opts->arch && strcasecmp(opts->arch, record.arch))
                         continue;                          continue;
   
                 /* FIXME: this needs to be changed.  Ugh.  Linear. */                  /*
                    * Do a binary search to dedupe the results tree of the
                    * same record: we don't print the same file.
                    */
   
                 for (i = 0; i < len; i++)                  for (leaf = root; leaf >= 0; )
                         if (res[i].rec == record.rec)                          if (rec > res[leaf].rec && res[leaf].rhs >= 0)
                                   leaf = res[leaf].rhs;
                           else if (rec < res[leaf].rec && res[leaf].lhs >= 0)
                                   leaf = res[leaf].lhs;
                           else
                                 break;                                  break;
   
                 if (i < len)                  if (leaf >= 0 && res[leaf].rec == rec)
                         continue;                          continue;
   
                   res = mandoc_realloc
                           (res, (len + 1) * sizeof(struct res));
   
                 /*                  /*
                  * Now we have our filename, keywords, types, and all                   * Now we have our filename, keywords, types, and all
                  * other necessary information.                   * other necessary information.
Line 396  state_search(struct state *p, const struct opts *opts,
Line 436  state_search(struct state *p, const struct opts *opts,
   
                 res[len].rec = record.rec;                  res[len].rec = record.rec;
                 res[len].types = fl;                  res[len].types = fl;
                   res[len].lhs = res[len].rhs = -1;
   
                 buf_dup(mc, &res[len].keyword, buf);                  buf_dup(mc, &res[len].keyword, buf);
                 buf_dup(mc, &res[len].uri, filebuf);                  buf_dup(mc, &res[len].uri, filebuf);
Line 403  state_search(struct state *p, const struct opts *opts,
Line 444  state_search(struct state *p, const struct opts *opts,
                 buf_dup(mc, &res[len].arch, record.arch);                  buf_dup(mc, &res[len].arch, record.arch);
                 buf_dup(mc, &res[len].title, record.title);                  buf_dup(mc, &res[len].title, record.title);
                 buf_dup(mc, &res[len].desc, record.desc);                  buf_dup(mc, &res[len].desc, record.desc);
   
                   if (leaf >= 0) {
                           if (record.rec > res[leaf].rec)
                                   res[leaf].rhs = len;
                           else
                                   res[leaf].lhs = len;
                   } else
                           root = len;
   
                 len++;                  len++;
         }          }
   
 send:  
         if (ch < 0) {          if (ch < 0) {
                 perror(p->dbf);                  perror(p->dbf);
                 exit(EXIT_FAILURE);                  goto out;
         }          }
   send:
           /* Sort our results. */
   
         switch (opts->sort) {          if (SORT_CAT == opts->sort)
         case (SORT_CAT):  
                 qsort(res, len, sizeof(struct res), sort_cat);                  qsort(res, len, sizeof(struct res), sort_cat);
                 break;          else
         default:  
                 qsort(res, len, sizeof(struct res), sort_title);                  qsort(res, len, sizeof(struct res), sort_title);
                 break;  
         }  
   
         state_output(res, len);          state_output(res, len);
           rc = 1;
   out:
         for (len-- ; len >= 0; len--) {          for (len-- ; len >= 0; len--) {
                 free(res[len].keyword);                  free(res[len].keyword);
                 free(res[len].title);                  free(res[len].title);
Line 432  send:
Line 480  send:
                 free(res[len].uri);                  free(res[len].uri);
         }          }
   
           free(res);
         free(buf);          free(buf);
         mchars_free(mc);          mchars_free(mc);
   
         if (regp)          if (regp)
                 regfree(regp);                  regfree(regp);
   
           return(rc);
 }  }
   
 /*  /*
Line 450  buf_alloc(char **buf, size_t *bufsz, size_t sz)
Line 501  buf_alloc(char **buf, size_t *bufsz, size_t sz)
                 return;                  return;
   
         *bufsz = sz + 1024;          *bufsz = sz + 1024;
         if (NULL == (*buf = realloc(*buf, *bufsz))) {          *buf = mandoc_realloc(*buf, *bufsz);
                 perror(NULL);  
                 exit(EXIT_FAILURE);  
         }  
 }  }
   
 /*  /*
Line 544  buf_redup(struct mchars *mc, char **buf, 
Line 592  buf_redup(struct mchars *mc, char **buf, 
 }  }
   
 static void  static void
 error(const char *fmt, ...)  
 {  
         va_list          ap;  
   
         va_start(ap, fmt);  
         vfprintf(stderr, fmt, ap);  
         va_end(ap);  
 }  
   
 static void  
 state_output(const struct res *res, int sz)  state_output(const struct res *res, int sz)
 {  {
         int              i;          int              i;
Line 580  usage(void)
Line 618  usage(void)
 }  }
   
 static int  static int
 state_init(struct state *p,  
                 const char *dbf, const char *idxf,  
                 void (*err)(const char *),  
                 void (*errx)(const char *, ...))  
 {  
         BTREEINFO        info;  
   
         memset(p, 0, sizeof(struct state));  
         memset(&info, 0, sizeof(BTREEINFO));  
   
         info.flags = R_DUP;  
   
         p->dbf = dbf;  
         p->idxf = idxf;  
         p->err = err;  
   
         p->db = dbopen(p->dbf, O_RDONLY, 0, DB_BTREE, &info);  
         if (NULL == p->db) {  
                 (*err)(p->dbf);  
                 return(0);  
         }  
   
         p->idx = dbopen(p->idxf, O_RDONLY, 0, DB_RECNO, NULL);  
         if (NULL == p->idx) {  
                 (*err)(p->idxf);  
                 return(0);  
         }  
   
         return(1);  
 }  
   
 static void  
 state_destroy(struct state *p)  
 {  
   
         if (p->db)  
                 (*p->db->close)(p->db);  
         if (p->idx)  
                 (*p->idx->close)(p->idx);  
 }  
   
 static int  
 state_getrecord(struct state *p, recno_t rec, struct rec *rp)  state_getrecord(struct state *p, recno_t rec, struct rec *rp)
 {  {
         DBT             key, val;          DBT             key, val;
Line 633  state_getrecord(struct state *p, recno_t rec, struct r
Line 629  state_getrecord(struct state *p, recno_t rec, struct r
   
         rc = (*p->idx->get)(p->idx, &key, &val, 0);          rc = (*p->idx->get)(p->idx, &key, &val, 0);
         if (rc < 0) {          if (rc < 0) {
                 (*p->err)(p->idxf);                  perror(p->idxf);
                 return(0);                  return(0);
         } else if (rc > 0) {          } else if (rc > 0)
                 (*p->errx)("%s: Corrupt index\n", p->idxf);                  goto err;
                 return(0);  
         }  
   
         rp->file = (char *)val.data;          rp->file = (char *)val.data;
         if ((sz = strlen(rp->file) + 1) >= val.size) {          if ((sz = strlen(rp->file) + 1) >= val.size)
                 (*p->errx)("%s: Corrupt index\n", p->idxf);                  goto err;
                 return(0);  
         }  
   
         rp->cat = (char *)val.data + (int)sz;          rp->cat = (char *)val.data + (int)sz;
         if ((sz += strlen(rp->cat) + 1) >= val.size) {          if ((sz += strlen(rp->cat) + 1) >= val.size)
                 (*p->errx)("%s: Corrupt index\n", p->idxf);                  goto err;
                 return(0);  
         }  
   
         rp->title = (char *)val.data + (int)sz;          rp->title = (char *)val.data + (int)sz;
         if ((sz += strlen(rp->title) + 1) >= val.size) {          if ((sz += strlen(rp->title) + 1) >= val.size)
                 (*p->errx)("%s: Corrupt index\n", p->idxf);                  goto err;
                 return(0);  
         }  
   
         rp->arch = (char *)val.data + (int)sz;          rp->arch = (char *)val.data + (int)sz;
         if ((sz += strlen(rp->arch) + 1) >= val.size) {          if ((sz += strlen(rp->arch) + 1) >= val.size)
                 (*p->errx)("%s: Corrupt index\n", p->idxf);                  goto err;
                 return(0);  
         }  
   
         rp->desc = (char *)val.data + (int)sz;          rp->desc = (char *)val.data + (int)sz;
         rp->rec = rec;          rp->rec = rec;
         return(1);          return(1);
   err:
           fprintf(stderr, "%s: Corrupt index\n", p->idxf);
           return(0);
 }  }
   
 static int  static int

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

CVSweb