Home | History | Annotate | Line # | Download | only in diff
      1 /*-
      2  * Copyright (c) 2017 Baptiste Daroussin <bapt (at) FreeBSD.org>
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer
     10  *    in this position and unchanged.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 #include <sys/wait.h>
     29 
     30 #include <err.h>
     31 #include <paths.h>
     32 #include <signal.h>
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <unistd.h>
     36 
     37 #include "pr.h"
     38 #include "diff.h"
     39 #include "xmalloc.h"
     40 
     41 #define _PATH_PR "/usr/bin/pr"
     42 
     43 struct pr *
     44 start_pr(char *file1, char *file2)
     45 {
     46 	int pfd[2];
     47 	pid_t pid;
     48 	char *header;
     49 	struct pr *pr;
     50 
     51 	pr = xcalloc(1, sizeof(*pr));
     52 
     53 	xasprintf(&header, "%s %s %s", diffargs, file1, file2);
     54 	signal(SIGPIPE, SIG_IGN);
     55 	fflush(stdout);
     56 	rewind(stdout);
     57 	if (pipe(pfd) == -1)
     58 		err(2, "pipe");
     59 	switch ((pid = fork())) {
     60 	case -1:
     61 		status |= 2;
     62 		free(header);
     63 		err(2, "No more processes");
     64 	case 0:
     65 		/* child */
     66 		if (pfd[0] != STDIN_FILENO) {
     67 			dup2(pfd[0], STDIN_FILENO);
     68 			close(pfd[0]);
     69 		}
     70 		close(pfd[1]);
     71 		execl(_PATH_PR, _PATH_PR, "-h", header, (char *)0);
     72 		_exit(127);
     73 	default:
     74 
     75 		/* parent */
     76 		if (pfd[1] != STDOUT_FILENO) {
     77 			pr->ostdout = dup(STDOUT_FILENO);
     78 			dup2(pfd[1], STDOUT_FILENO);
     79 			close(pfd[1]);
     80 		}
     81 		close(pfd[0]);
     82 		rewind(stdout);
     83 		free(header);
     84 		pr->kq = kqueue();
     85 		if (pr->kq == -1)
     86 			err(2, "kqueue");
     87 		pr->e = xmalloc(sizeof(struct kevent));
     88 		EV_SET(pr->e, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
     89 		if (kevent(pr->kq, pr->e, 1, NULL, 0, NULL) == -1)
     90 			err(2, "kevent");
     91 	}
     92 	return (pr);
     93 }
     94 
     95 /* close the pipe to pr and restore stdout */
     96 void
     97 stop_pr(struct pr *pr)
     98 {
     99 	if (pr == NULL)
    100 		return;
    101 
    102 	fflush(stdout);
    103 	if (pr->ostdout != STDOUT_FILENO) {
    104 		close(STDOUT_FILENO);
    105 		dup2(pr->ostdout, STDOUT_FILENO);
    106 		close(pr->ostdout);
    107 	}
    108 	if (kevent(pr->kq, NULL, 0, pr->e, 1, NULL) == -1)
    109 		err(2, "kevent");
    110 	close(pr->kq);
    111 	free(pr);
    112 }
    113