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

Diff for /mandoc/main.c between version 1.256 and 1.261

version 1.256, 2015/11/07 14:22:29 version 1.261, 2016/01/08 02:13:39
Line 27 
Line 27 
 #if HAVE_ERR  #if HAVE_ERR
 #include <err.h>  #include <err.h>
 #endif  #endif
   #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #include <glob.h>  #include <glob.h>
 #include <signal.h>  #include <signal.h>
Line 131  main(int argc, char *argv[])
Line 132  main(int argc, char *argv[])
         int              show_usage;          int              show_usage;
         int              options;          int              options;
         int              use_pager;          int              use_pager;
           int              status, signum;
         int              c;          int              c;
           pid_t            pager_pid, tc_pgid, man_pgid, pid;
   
 #if HAVE_PROGNAME  #if HAVE_PROGNAME
         progname = getprogname();          progname = getprogname();
Line 152  main(int argc, char *argv[])
Line 155  main(int argc, char *argv[])
 #endif  #endif
   
 #if HAVE_PLEDGE  #if HAVE_PLEDGE
         if (pledge("stdio rpath tmppath proc exec flock", NULL) == -1)          if (pledge("stdio rpath tmppath tty proc exec flock", NULL) == -1)
                 err((int)MANDOCLEVEL_SYSERR, "pledge");                  err((int)MANDOCLEVEL_SYSERR, "pledge");
 #endif  #endif
   
Line 296  main(int argc, char *argv[])
Line 299  main(int argc, char *argv[])
                 use_pager = 0;                  use_pager = 0;
   
 #if HAVE_PLEDGE  #if HAVE_PLEDGE
         if (!use_pager && pledge("stdio rpath flock", NULL) == -1)          if (!use_pager)
                 err((int)MANDOCLEVEL_SYSERR, "pledge");                  if (pledge("stdio rpath flock", NULL) == -1)
                           err((int)MANDOCLEVEL_SYSERR, "pledge");
 #endif  #endif
   
         /* Parse arguments. */          /* Parse arguments. */
Line 427  main(int argc, char *argv[])
Line 431  main(int argc, char *argv[])
         /* mandoc(1) */          /* mandoc(1) */
   
 #if HAVE_PLEDGE  #if HAVE_PLEDGE
         if (pledge(use_pager ? "stdio rpath tmppath proc exec" :          if (use_pager) {
             "stdio rpath", NULL) == -1)                  if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
                 err((int)MANDOCLEVEL_SYSERR, "pledge");                          err((int)MANDOCLEVEL_SYSERR, "pledge");
           } else {
                   if (pledge("stdio rpath", NULL) == -1)
                           err((int)MANDOCLEVEL_SYSERR, "pledge");
           }
 #endif  #endif
   
         if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))          if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
Line 527  out:
Line 535  out:
         if (tag_files != NULL) {          if (tag_files != NULL) {
                 fclose(stdout);                  fclose(stdout);
                 tag_write();                  tag_write();
                 waitpid(spawn_pager(tag_files), NULL, 0);                  man_pgid = getpgid(0);
                   tag_files->tcpgid = man_pgid == getpid() ?
                       getpgid(getppid()) : man_pgid;
                   pager_pid = 0;
                   signum = SIGSTOP;
                   for (;;) {
   
                           /* Stop here until moved to the foreground. */
   
                           tc_pgid = tcgetpgrp(STDIN_FILENO);
                           if (tc_pgid != man_pgid) {
                                   if (tc_pgid == pager_pid) {
                                           (void)tcsetpgrp(STDIN_FILENO,
                                               man_pgid);
                                           if (signum == SIGTTIN)
                                                   continue;
                                   } else
                                           tag_files->tcpgid = tc_pgid;
                                   kill(0, signum);
                                   continue;
                           }
   
                           /* Once in the foreground, activate the pager. */
   
                           if (pager_pid) {
                                   (void)tcsetpgrp(STDIN_FILENO, pager_pid);
                                   kill(pager_pid, SIGCONT);
                           } else
                                   pager_pid = spawn_pager(tag_files);
   
                           /* Wait for the pager to stop or exit. */
   
                           while ((pid = waitpid(pager_pid, &status,
                               WUNTRACED)) == -1 && errno == EINTR)
                                   continue;
   
                           if (pid == -1) {
                                   warn("wait");
                                   rc = MANDOCLEVEL_SYSERR;
                                   break;
                           }
                           if (!WIFSTOPPED(status))
                                   break;
   
                           signum = WSTOPSIG(status);
                   }
                 tag_unlink();                  tag_unlink();
         }          }
   
Line 671  parse(struct curparse *curp, int fd, const char *file)
Line 724  parse(struct curparse *curp, int fd, const char *file)
         /* Begin by parsing the file itself. */          /* Begin by parsing the file itself. */
   
         assert(file);          assert(file);
         assert(fd >= -1);          assert(fd > 0);
   
         rctmp = mparse_readfd(curp->mp, fd, file);          rctmp = mparse_readfd(curp->mp, fd, file);
           if (fd != STDIN_FILENO)
                   close(fd);
         if (rc < rctmp)          if (rc < rctmp)
                 rc = rctmp;                  rc = rctmp;
   
Line 774  passthrough(const char *file, int fd, int synopsis_onl
Line 829  passthrough(const char *file, int fd, int synopsis_onl
   
         FILE            *stream;          FILE            *stream;
         const char      *syscall;          const char      *syscall;
         char            *line;          char            *line, *cp;
         size_t           len, off;          size_t           linesz;
         ssize_t          nw;  
         int              print;          int              print;
   
         fflush(stdout);          line = NULL;
           linesz = 0;
   
         if ((stream = fdopen(fd, "r")) == NULL) {          if ((stream = fdopen(fd, "r")) == NULL) {
                 close(fd);                  close(fd);
Line 788  passthrough(const char *file, int fd, int synopsis_onl
Line 843  passthrough(const char *file, int fd, int synopsis_onl
         }          }
   
         print = 0;          print = 0;
         while ((line = fgetln(stream, &len)) != NULL) {          while (getline(&line, &linesz, stream) != -1) {
                   cp = line;
                 if (synopsis_only) {                  if (synopsis_only) {
                         if (print) {                          if (print) {
                                 if ( ! isspace((unsigned char)*line))                                  if ( ! isspace((unsigned char)*cp))
                                         goto done;                                          goto done;
                                 while (len &&                                  while (isspace((unsigned char)*cp))
                                     isspace((unsigned char)*line)) {                                          cp++;
                                         line++;  
                                         len--;  
                                 }  
                         } else {                          } else {
                                 if ((len == sizeof(synb) &&                                  if (strcmp(cp, synb) == 0 ||
                                      ! strncmp(line, synb, len - 1)) ||                                      strcmp(cp, synr) == 0)
                                     (len == sizeof(synr) &&  
                                      ! strncmp(line, synr, len - 1)))  
                                         print = 1;                                          print = 1;
                                 continue;                                  continue;
                         }                          }
                 }                  }
                 for (off = 0; off < len; off += nw)                  if (fputs(cp, stdout)) {
                         if ((nw = write(STDOUT_FILENO, line + off,                          fclose(stream);
                             len - off)) == -1 || nw == 0) {                          syscall = "fputs";
                                 fclose(stream);                          goto fail;
                                 syscall = "write";                  }
                                 goto fail;  
                         }  
         }          }
   
         if (ferror(stream)) {          if (ferror(stream)) {
                 fclose(stream);                  fclose(stream);
                 syscall = "fgetln";                  syscall = "getline";
                 goto fail;                  goto fail;
         }          }
   
 done:  done:
           free(line);
         fclose(stream);          fclose(stream);
         return;          return;
   
 fail:  fail:
           free(line);
         warn("%s: SYSERR: %s", file, syscall);          warn("%s: SYSERR: %s", file, syscall);
         if (rc < MANDOCLEVEL_SYSERR)          if (rc < MANDOCLEVEL_SYSERR)
                 rc = MANDOCLEVEL_SYSERR;                  rc = MANDOCLEVEL_SYSERR;
Line 1020  spawn_pager(struct tag_files *tag_files)
Line 1071  spawn_pager(struct tag_files *tag_files)
         case -1:          case -1:
                 err((int)MANDOCLEVEL_SYSERR, "fork");                  err((int)MANDOCLEVEL_SYSERR, "fork");
         case 0:          case 0:
                   /* Set pgrp in both parent and child to avoid racing exec. */
                   (void)setpgid(0, 0);
                 break;                  break;
         default:          default:
                   (void)setpgid(pager_pid, 0);
                   (void)tcsetpgrp(STDIN_FILENO, pager_pid);
 #if HAVE_PLEDGE  #if HAVE_PLEDGE
                 if (pledge("stdio rpath tmppath", NULL) == -1)                  if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
                         err((int)MANDOCLEVEL_SYSERR, "pledge");                          err((int)MANDOCLEVEL_SYSERR, "pledge");
 #endif  #endif
                   tag_files->pager_pid = pager_pid;
                 return pager_pid;                  return pager_pid;
         }          }
   

Legend:
Removed from v.1.256  
changed lines
  Added in v.1.261

CVSweb