/*
 * CDE - Common Desktop Environment
 *
 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
 *
 * These libraries and programs are free software; you can
 * redistribute them and/or modify them under the terms of the GNU
 * Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * These libraries and programs are distributed in the hope that
 * they will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with these libraries and programs; if not, write
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301 USA
 */
/* $XConsortium: cmdopen.c /main/3 1995/11/01 18:07:32 rswiston $ */
/***************************************************************
*                                                              *
*                      AT&T - PROPRIETARY                      *
*                                                              *
*        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF        *
*                    AT&T BELL LABORATORIES                    *
*         AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN         *
*            ACCORDANCE WITH APPLICABLE AGREEMENTS             *
*                                                              *
*          Copyright (c) 1994 AT&T Bell Laboratories           *
*              Unpublished & Not for Publication               *
*                     All Rights Reserved                      *
*                                                              *
*       The copyright notice above does not evidence any       *
*      actual or intended publication of such source code      *
*                                                              *
*               This software was created by the               *
*           Software Engineering Research Department           *
*                    AT&T Bell Laboratories                    *
*                                                              *
*               For further information contact                *
*                   advsoft@research.att.com                   *
*                 Randy Hackbarth 908-582-5245                 *
*                  Dave Belanger 908-582-7427                  *
*                                                              *
***************************************************************/

/* : : generated by proto : : */

#line 1

#if !defined(__PROTO__)
#if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
#if defined(__cplusplus)
#define __MANGLE__	"C"
#else
#define __MANGLE__
#endif
#define __STDARG__
#define __PROTO__(x)	x
#define __OTORP__(x)
#define __PARAM__(n,o)	n
#if !defined(__STDC__) && !defined(__cplusplus)
#if !defined(c_plusplus)
#define const
#endif
#define signed
#define void		int
#define volatile
#define __V_		char
#else
#define __V_		void
#endif
#else
#define __PROTO__(x)	()
#define __OTORP__(x)	x
#define __PARAM__(n,o)	o
#define __MANGLE__
#define __V_		char
#define const
#define signed
#define void		int
#define volatile
#endif
#if defined(__cplusplus) || defined(c_plusplus)
#define __VARARG__	...
#else
#define __VARARG__
#endif
#if defined(__STDARG__)
#define __VA_START__(p,a)	va_start(p,a)
#else
#define __VA_START__(p,a)	va_start(p)
#endif
#endif

#line 27
#include "cmdlib.h"

Cmd_t*		cmds;

int
cmdopen __PARAM__((const char* cmd, char** argv, char** envv, int* redv, const char* mode), (cmd, argv, envv, redv, mode)) __OTORP__(const char* cmd; char** argv; char** envv; int* redv; const char* mode;)
#line 33
{
	Cmd_t*	o;
	Proc_t*	p;
	int*	r;
	long*	m;
	long*	modv;
	long	flags = 0;

	for (o = cmds; o; o = o->next)
		if (!o->proc) break;
	if (!o && !(o = newof(0, Cmd_t, 1, 0)))
		return(-1);
	if (mode) while (*mode) switch (*mode++)
	{
	case 'a':
		flags |= PROC_ARGMOD;
		break;
	case 'b':
		flags |= PROC_READ|PROC_WRITE;
		break;
	case 'd':
		flags |= PROC_DAEMON;
		break;
	case 'e':
		flags |= PROC_ENVCLEAR;
		break;
	case 'g':
		flags |= PROC_GID;
		break;
	case 'o':
		flags |= PROC_OVERLAY;
		break;
	case 'p':
		flags |= PROC_PARANOID;
		break;
	case 'r':
		flags |= PROC_READ;
		break;
	case 's':
		flags |= PROC_PRIVELEGED;
		break;
	case 'u':
		flags |= PROC_UID;
		break;
	case 'w':
		flags |= PROC_WRITE;
		break;
	default:
		return(-1);
	}
	if (r = redv)
	{
		while (*r >= 0) r += 2;
		if (!(modv = newof(0, long, (r - redv) / 2 + 1, 0))) return(-1);
		m = modv;
		for (r = redv; *r >= 0; r += 2)
			*m++ = PROC_FD_DUP(*(r + 1), *r, PROC_FD_CHILD);
		*m = 0;
	}
	else modv = 0;
	p = procopen(cmd, argv, envv, modv, flags);
	if (modv) free(modv);
	if (!p) return(-1);
	if (p->rfd >= 0) o->fd = p->rfd;
	else if (p->wfd >= 0) o->fd = p->wfd;
	else o->fd = dup(0);
	o->proc = p;
	return(o->fd);
}
