contrib/chg/procutil.c
changeset 30692 23ddd43ba866
parent 30691 7adb60660496
child 30693 baee0f47b533
equal deleted inserted replaced
30691:7adb60660496 30692:23ddd43ba866
   157 
   157 
   158 /* This implementation is based on hgext/pager.py (post 369741ef7253)
   158 /* This implementation is based on hgext/pager.py (post 369741ef7253)
   159  * Return 0 if pager is not started, or pid of the pager */
   159  * Return 0 if pager is not started, or pid of the pager */
   160 static pid_t setuppager(const char *pagercmd)
   160 static pid_t setuppager(const char *pagercmd)
   161 {
   161 {
       
   162 	assert(pagerpid == 0);
   162 	if (!pagercmd)
   163 	if (!pagercmd)
   163 		return 0;
   164 		return 0;
   164 
   165 
   165 	int pipefds[2];
   166 	int pipefds[2];
   166 	if (pipe(pipefds) < 0)
   167 	if (pipe(pipefds) < 0)
   175 		if (isatty(fileno(stderr))) {
   176 		if (isatty(fileno(stderr))) {
   176 			if (dup2(pipefds[1], fileno(stderr)) < 0)
   177 			if (dup2(pipefds[1], fileno(stderr)) < 0)
   177 				goto error;
   178 				goto error;
   178 		}
   179 		}
   179 		close(pipefds[1]);
   180 		close(pipefds[1]);
       
   181 		pagerpid = pid;
   180 		return pid;
   182 		return pid;
   181 	} else {
   183 	} else {
   182 		dup2(pipefds[0], fileno(stdin));
   184 		dup2(pipefds[0], fileno(stdin));
   183 		close(pipefds[0]);
   185 		close(pipefds[0]);
   184 		close(pipefds[1]);
   186 		close(pipefds[1]);
   195 	close(pipefds[1]);
   197 	close(pipefds[1]);
   196 	abortmsgerrno("failed to prepare pager");
   198 	abortmsgerrno("failed to prepare pager");
   197 	return 0;
   199 	return 0;
   198 }
   200 }
   199 
   201 
   200 static void waitpager(pid_t pid)
   202 static void waitpager(void)
   201 {
   203 {
       
   204 	if (pagerpid == 0)
       
   205 		return;
       
   206 
   202 	/* close output streams to notify the pager its input ends */
   207 	/* close output streams to notify the pager its input ends */
   203 	fclose(stdout);
   208 	fclose(stdout);
   204 	fclose(stderr);
   209 	fclose(stderr);
   205 	while (1) {
   210 	while (1) {
   206 		pid_t ret = waitpid(pid, NULL, 0);
   211 		pid_t ret = waitpid(pagerpid, NULL, 0);
   207 		if (ret == -1 && errno == EINTR)
   212 		if (ret == -1 && errno == EINTR)
   208 			continue;
   213 			continue;
   209 		break;
   214 		break;
   210 	}
   215 	}
   211 }
   216 }