FreeBSD/contrib/nvi/ex/ex_cscope.c
/*
 * run_cscope --
 *	Fork off the cscope process.
 */
run_cscope
Fork off the cscope process.
run_cscope Fork off the cscope process .
static int
run_cscope(SCR *sp, CSC *csc, char *dbname)
{
	int to_cs[2], from_cs[2];
	char *cmd;
	to_cs[0] = to_cs[1] = from_cs[0] = from_cs[1] = -1;
	if (pipe(to_cs) < 0 || pipe(from_cs) < 0) {
		msgq(sp, M_SYSERR, "pipe");
		goto err;
	}
	switch (csc->pid = vfork()) {
		char *dn, *dbn;
	case -1:
		msgq(sp, M_SYSERR, "vfork");
err:		if (to_cs[0] != -1)
			(void)close(to_cs[0]);
		if (to_cs[1] != -1)
			(void)close(to_cs[1]);
		if (from_cs[0] != -1)
			(void)close(from_cs[0]);
		if (from_cs[1] != -1)
			(void)close(from_cs[1]);
		return (1);
	case 0:				
		(void)dup2(to_cs[0], STDIN_FILENO);
		(void)dup2(from_cs[1], STDOUT_FILENO);
		(void)dup2(from_cs[1], STDERR_FILENO);
		(void)close(to_cs[1]);
		(void)close(from_cs[0]);
#define	CSCOPE_CMD_FMT		"cd %s && exec cscope -dl -f %s"
		if ((dn = quote(csc->dname)) == NULL)
			goto nomem;
		if ((dbn = quote(dbname)) == NULL) {
			free(dn);
			goto nomem;
		}
		(void)asprintf(&cmd, CSCOPE_CMD_FMT, dn, dbn);
		free(dbn);
		free(dn);
		if (cmd == NULL) {
nomem:			msgq(sp, M_SYSERR, NULL);
			_exit (1);
		}
		(void)execl(_PATH_BSHELL, "sh", "-c", cmd, (char *)NULL);
		msgq_str(sp, M_SYSERR, cmd, "execl: %s");
		free(cmd);
		_exit (127);
	default:			
		(void)close(to_cs[0]);
		(void)close(from_cs[1]);
		csc->to_fd = to_cs[1];
		csc->to_fp = fdopen(to_cs[1], "w");
		csc->from_fd = from_cs[0];
		csc->from_fp = fdopen(from_cs[0], "r");
		break;
	}
	return (0);
}
static int run_cscope ( SCR * sp , CSC * csc , char * dbname ) { int to_cs [ 2 ] , from_cs [ 2 ] ; char * cmd ; to_cs [ 0 ] = to_cs [ 1 ] = from_cs [ 0 ] = from_cs [ 1 ] = -1 ; if ( pipe ( to_cs ) < 0 || pipe ( from_cs ) < 0 ) { msgq ( sp , M_SYSERR , " " ) ; goto err ; } switch ( csc -> pid = vfork ( ) ) { char * dn , * dbn ; case -1 : msgq ( sp , M_SYSERR , " " ) ; err : if ( to_cs [ 0 ] != -1 ) ( void ) close ( to_cs [ 0 ] ) ; if ( to_cs [ 1 ] != -1 ) ( void ) close ( to_cs [ 1 ] ) ; if ( from_cs [ 0 ] != -1 ) ( void ) close ( from_cs [ 0 ] ) ; if ( from_cs [ 1 ] != -1 ) ( void ) close ( from_cs [ 1 ] ) ; return ( 1 ) ; case 0 : ( void ) dup2 ( to_cs [ 0 ] , STDIN_FILENO ) ; ( void ) dup2 ( from_cs [ 1 ] , STDOUT_FILENO ) ; ( void ) dup2 ( from_cs [ 1 ] , STDERR_FILENO ) ; ( void ) close ( to_cs [ 1 ] ) ; ( void ) close ( from_cs [ 0 ] ) ; #define CSCOPE_CMD_FMT 		"cd %s && exec cscope -dl -f %s" if ( ( dn = quote ( csc -> dname ) ) == NULL ) goto nomem ; if ( ( dbn = quote ( dbname ) ) == NULL ) { free ( dn ) ; goto nomem ; } ( void ) asprintf ( & cmd , CSCOPE_CMD_FMT , dn , dbn ) ; free ( dbn ) ; free ( dn ) ; if ( cmd == NULL ) { nomem : msgq ( sp , M_SYSERR , NULL ) ; _exit ( 1 ) ; } ( void ) execl ( _PATH_BSHELL , " " , " " , cmd , ( char * ) NULL ) ; msgq_str ( sp , M_SYSERR , cmd , " " ) ; free ( cmd ) ; _exit ( 127 ) ; default : ( void ) close ( to_cs [ 0 ] ) ; ( void ) close ( from_cs [ 1 ] ) ; csc -> to_fd = to_cs [ 1 ] ; csc -> to_fp = fdopen ( to_cs [ 1 ] , " " ) ; csc -> from_fd = from_cs [ 0 ] ; csc -> from_fp = fdopen ( from_cs [ 0 ] , " " ) ; break ; } return ( 0 ) ; }
run_cscope
      Fork off the cscope process.
run_cscope Fork off the cscope process .
/*
	 * Cscope reads from to_cs[0] and writes to from_cs[1]; vi reads from
	 * from_cs[0] and writes to to_cs[1].
	 */ /* child: run cscope. */ /* Close unused file descriptors. */ /* Run the cscope command. */ /* NOTREACHED */ /* parent. */ /* Close unused file descriptors. */ /*
		 * Save the file descriptors for later duplication, and
		 * reopen as streams.
		 */