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

Diff for /mandoc/main.c between version 1.183 and 1.186

version 1.183, 2014/08/22 03:42:18 version 1.186, 2014/08/23 00:34:59
Line 21 
Line 21 
 #include <sys/types.h>  #include <sys/types.h>
   
 #include <assert.h>  #include <assert.h>
   #include <ctype.h>
 #include <errno.h>  #include <errno.h>
   #include <fcntl.h>
 #include <stdio.h>  #include <stdio.h>
 #include <stdint.h>  #include <stdint.h>
 #include <stdlib.h>  #include <stdlib.h>
Line 85  static void    mmsg(enum mandocerr, enum mandoclevel,
Line 87  static void    mmsg(enum mandocerr, enum mandoclevel,
                                 const char *, int, int, const char *);                                  const char *, int, int, const char *);
 static  void              parse(struct curparse *, int,  static  void              parse(struct curparse *, int,
                                 const char *, enum mandoclevel *);                                  const char *, enum mandoclevel *);
   static  enum mandoclevel  passthrough(const char *);
 static  void              spawn_pager(void);  static  void              spawn_pager(void);
 static  int               toptions(struct curparse *, char *);  static  int               toptions(struct curparse *, char *);
 static  void              usage(enum argmode) __attribute__((noreturn));  static  void              usage(enum argmode) __attribute__((noreturn));
Line 104  main(int argc, char *argv[])
Line 107  main(int argc, char *argv[])
         char            *conf_file, *defpaths, *auxpaths;          char            *conf_file, *defpaths, *auxpaths;
         char            *defos;          char            *defos;
 #if HAVE_SQLITE3  #if HAVE_SQLITE3
         struct manpage  *res;          struct manpage  *res, *resp;
         char            **auxargv;  
         size_t           isec, i, sz;          size_t           isec, i, sz;
         int              prio, best_prio;          int              prio, best_prio;
         char             sec;          char             sec;
Line 227  main(int argc, char *argv[])
Line 229  main(int argc, char *argv[])
         if (show_usage)          if (show_usage)
                 usage(search.argmode);                  usage(search.argmode);
   
           /* Postprocess options. */
   
         if (outmode == OUTMODE_DEF) {          if (outmode == OUTMODE_DEF) {
                 switch (search.argmode) {                  switch (search.argmode) {
                 case ARG_FILE:                  case ARG_FILE:
Line 242  main(int argc, char *argv[])
Line 246  main(int argc, char *argv[])
                 }                  }
         }          }
   
           /* Parse arguments. */
   
         argc -= optind;          argc -= optind;
         argv += optind;          argv += optind;
 #if HAVE_SQLITE3  #if HAVE_SQLITE3
         auxargv = NULL;          resp = NULL;
 #endif  #endif
   
           /* Quirk for a man(1) section argument without -s. */
   
           if (search.argmode == ARG_NAME &&
               argv[0] != NULL &&
               isdigit((unsigned char)argv[0][0]) &&
               (argv[0][1] == '\0' || !strcmp(argv[0], "3p"))) {
                   search.sec = argv[0];
                   argv++;
                   argc--;
           }
   
         rc = MANDOCLEVEL_OK;          rc = MANDOCLEVEL_OK;
   
         /* man(1), whatis(1), apropos(1) */          /* man(1), whatis(1), apropos(1) */
Line 264  main(int argc, char *argv[])
Line 281  main(int argc, char *argv[])
                 if( ! mansearch(&search, &paths, argc, argv, &res, &sz))                  if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
                         usage(search.argmode);                          usage(search.argmode);
                 manpath_free(&paths);                  manpath_free(&paths);
                   resp = res;
   
                 /*                  /*
                  * For standard man(1) and -a output mode,                   * For standard man(1) and -a output mode,
Line 273  main(int argc, char *argv[])
Line 291  main(int argc, char *argv[])
   
                 if (outmode == OUTMODE_ONE) {                  if (outmode == OUTMODE_ONE) {
                         argc = 1;                          argc = 1;
                         argv[0] = res[0].file;  
                         argv[1] = NULL;  
                         best_prio = 10;                          best_prio = 10;
                 } else if (outmode == OUTMODE_ALL) {                  } else if (outmode == OUTMODE_ALL)
                         argc = (int)sz;                          argc = (int)sz;
                         argv = auxargv = mandoc_reallocarray(  
                             NULL, sz + 1, sizeof(char *));  
                         argv[argc] = NULL;  
                 }  
   
                 /* Iterate all matching manuals. */                  /* Iterate all matching manuals. */
   
Line 292  main(int argc, char *argv[])
Line 304  main(int argc, char *argv[])
                                 printf("%s - %s\n", res[i].names,                                  printf("%s - %s\n", res[i].names,
                                     res[i].output == NULL ? "" :                                      res[i].output == NULL ? "" :
                                     res[i].output);                                      res[i].output);
                         else if (outmode == OUTMODE_ALL)                          else if (outmode == OUTMODE_ONE) {
                                 argv[i] = res[i].file;  
                         else {  
                                 /* Search for the best section. */                                  /* Search for the best section. */
                                 isec = strcspn(res[i].file, "123456789");                                  isec = strcspn(res[i].file, "123456789");
                                 sec = res[i].file[isec];                                  sec = res[i].file[isec];
Line 304  main(int argc, char *argv[])
Line 314  main(int argc, char *argv[])
                                 if (prio >= best_prio)                                  if (prio >= best_prio)
                                         continue;                                          continue;
                                 best_prio = prio;                                  best_prio = prio;
                                 argv[0] = res[i].file;                                  resp = res + i;
                         }                          }
                 }                  }
   
Line 339  main(int argc, char *argv[])
Line 349  main(int argc, char *argv[])
         if (OUTT_MAN == curp.outtype)          if (OUTT_MAN == curp.outtype)
                 mparse_keep(curp.mp);                  mparse_keep(curp.mp);
   
         if (NULL == *argv)          if (argc == 0)
                 parse(&curp, STDIN_FILENO, "<stdin>", &rc);                  parse(&curp, STDIN_FILENO, "<stdin>", &rc);
   
         while (*argv) {          while (argc) {
                 parse(&curp, -1, *argv, &rc);  #if HAVE_SQLITE3
                   if (resp != NULL) {
                           if (resp->form)
                                   parse(&curp, -1, resp->file, &rc);
                           else
                                   rc = passthrough(resp->file);
                           resp++;
                   } else
   #endif
                           parse(&curp, -1, *argv++, &rc);
                 if (MANDOCLEVEL_OK != rc && curp.wstop)                  if (MANDOCLEVEL_OK != rc && curp.wstop)
                         break;                          break;
                 ++argv;                  argc--;
         }          }
   
         if (curp.outfree)          if (curp.outfree)
Line 359  out:
Line 378  out:
         if (search.argmode != ARG_FILE) {          if (search.argmode != ARG_FILE) {
                 mansearch_free(res, sz);                  mansearch_free(res, sz);
                 mansearch_setup(0);                  mansearch_setup(0);
                 free(auxargv);  
         }          }
 #endif  #endif
   
Line 517  parse(struct curparse *curp, int fd, const char *file,
Line 535  parse(struct curparse *curp, int fd, const char *file,
                 *level = rc;                  *level = rc;
 }  }
   
   static enum mandoclevel
   passthrough(const char *file)
   {
           char             buf[BUFSIZ];
           const char      *syscall;
           ssize_t          nr, nw, off;
           int              fd;
   
           fd = open(file, O_RDONLY);
           if (fd == -1) {
                   syscall = "open";
                   goto fail;
           }
   
           while ((nr = read(fd, buf, BUFSIZ)) != -1 && nr != 0)
                   for (off = 0; off < nr; off += nw)
                           if ((nw = write(STDOUT_FILENO, buf + off,
                               (size_t)(nr - off))) == -1 || nw == 0) {
                                   syscall = "write";
                                   goto fail;
                           }
   
           if (nr == 0) {
                   close(fd);
                   return(MANDOCLEVEL_OK);
           }
   
           syscall = "read";
   fail:
           fprintf(stderr, "%s: %s: SYSERR: %s: %s",
               progname, file, syscall, strerror(errno));
           return(MANDOCLEVEL_SYSERR);
   }
   
 static int  static int
 moptions(int *options, char *arg)  moptions(int *options, char *arg)
 {  {
Line 637  mmsg(enum mandocerr t, enum mandoclevel lvl,
Line 689  mmsg(enum mandocerr t, enum mandoclevel lvl,
 static void  static void
 spawn_pager(void)  spawn_pager(void)
 {  {
         int      fildes[2];  #define MAX_PAGER_ARGS 16
           char            *argv[MAX_PAGER_ARGS];
           const char      *pager;
           char            *cp;
           int              fildes[2];
           int              argc;
   
         if (pipe(fildes) == -1) {          if (pipe(fildes) == -1) {
                 fprintf(stderr, "%s: pipe: %s\n",                  fprintf(stderr, "%s: pipe: %s\n",
Line 659  spawn_pager(void)
Line 716  spawn_pager(void)
                 }                  }
                 return;                  return;
         default:          default:
                 close(fildes[1]);                  break;
                 if (dup2(fildes[0], STDIN_FILENO) == -1) {          }
                         fprintf(stderr, "%s: dup input: %s\n",  
                             progname, strerror(errno));          /* The original process becomes the pager. */
                 } else {  
                         execlp("more", "more", "-s", NULL);          close(fildes[1]);
                         fprintf(stderr, "%s: exec: %s\n",          if (dup2(fildes[0], STDIN_FILENO) == -1) {
                             progname, strerror(errno));                  fprintf(stderr, "%s: dup input: %s\n",
                 }                      progname, strerror(errno));
                 exit((int)MANDOCLEVEL_SYSERR);                  exit((int)MANDOCLEVEL_SYSERR);
         }          }
   
           pager = getenv("MANPAGER");
           if (pager == NULL || *pager == '\0')
                   pager = getenv("PAGER");
           if (pager == NULL || *pager == '\0')
                   pager = "/usr/bin/more -s";
           cp = mandoc_strdup(pager);
   
           /*
            * Parse the pager command into words.
            * Intentionally do not do anything fancy here.
            */
   
           argc = 0;
           while (argc + 1 < MAX_PAGER_ARGS) {
                   argv[argc++] = cp;
                   cp = strchr(cp, ' ');
                   if (cp == NULL)
                           break;
                   *cp++ = '\0';
                   while (*cp == ' ')
                           cp++;
                   if (*cp == '\0')
                           break;
           }
           argv[argc] = NULL;
   
           /* Hand over to the pager. */
   
           execvp(argv[0], argv);
           fprintf(stderr, "%s: exec: %s\n",
               progname, strerror(errno));
           exit((int)MANDOCLEVEL_SYSERR);
 }  }

Legend:
Removed from v.1.183  
changed lines
  Added in v.1.186

CVSweb