xref: /illumos-gate/usr/src/lib/libmail/common/setup_exec.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <sys/types.h>
31 #include <ctype.h>
32 #include "libmail.h"
33 
34 #define	TRUE	1
35 #define	FALSE	0
36 
37 char **
38 setup_exec(char *s)
39 {
40 	char	*p = s, *q;
41 	static char	*argvec[256]; /* is this enough? */
42 	int	i = 0;
43 	int	stop;
44 	int	ignorespace = FALSE;
45 
46 	/* Parse up string into arg. vec. for subsequent exec. Assume */
47 	/* whitespace delimits args. Any non-escaped double quotes will */
48 	/* be used to group multiple whitespace-delimited tokens into */
49 	/* a single exec arg. */
50 	p = skipspace(p);
51 	while (*p) {
52 		q = p;
53 		stop = FALSE;
54 		while (*q && (stop == FALSE)) {
55 		again:
56 			switch (*q) {
57 			case '\\':
58 				/* Slide command string 1 char to left */
59 				strmove(q, q+1);
60 				break;
61 			case '"':
62 				ignorespace = ((ignorespace == TRUE) ?
63 				    FALSE : TRUE);
64 				/* Slide command string 1 char to left */
65 				strmove(q, q+1);
66 				goto again;
67 			default:
68 				if (isspace((int)*q) &&
69 				    (ignorespace == FALSE)) {
70 					stop = TRUE;
71 					continue;
72 				}
73 				break;
74 			}
75 			q++;
76 		}
77 		if (*q == '\0') {
78 			argvec[i++] = p;
79 			break;
80 		}
81 		*q++ = '\0';
82 		argvec[i++] = p;
83 		p = skipspace(q);
84 	}
85 	argvec[i] = NULL;
86 	if (i == 0) {
87 		return (NULL);
88 	}
89 	return (argvec);
90 }
91