xref: /freebsd/contrib/dialog/argv.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*
2  * $Id: argv.c,v 1.1 2011/03/02 09:56:39 tom Exp $
3  *
4  *  argv - Reusable functions for argv-parsing.
5  *
6  *  Copyright 2011	Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License, version 2.1
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this program; if not, write to
19  *	Free Software Foundation, Inc.
20  *	51 Franklin St., Fifth Floor
21  *	Boston, MA 02110, USA.
22  */
23 
24 #include <dialog.h>
25 #include <string.h>
26 
27 /*
28  * Convert a string to an argv[], returning a char** index (which must be
29  * freed by the caller).  The string is modified (replacing gaps between
30  * tokens with nulls).
31  */
32 char **
33 dlg_string_to_argv(char *blob)
34 {
35     size_t n;
36     int pass;
37     size_t length = strlen(blob);
38     char **result = 0;
39 
40     for (pass = 0; pass < 2; ++pass) {
41 	bool inparm = FALSE;
42 	bool quoted = FALSE;
43 	char *param = blob;
44 	size_t count = 0;
45 
46 	for (n = 0; n < length; ++n) {
47 	    if (quoted && blob[n] == '"') {
48 		quoted = FALSE;
49 	    } else if (blob[n] == '"') {
50 		quoted = TRUE;
51 		if (!inparm) {
52 		    if (pass)
53 			result[count] = param;
54 		    ++count;
55 		    inparm = TRUE;
56 		}
57 	    } else if (blob[n] == '\\') {
58 		if (quoted && !isspace(UCH(blob[n + 1]))) {
59 		    if (!inparm) {
60 			if (pass)
61 			    result[count] = param;
62 			++count;
63 			inparm = TRUE;
64 		    }
65 		    if (pass) {
66 			*param++ = blob[n];
67 			*param++ = blob[n + 1];
68 		    }
69 		}
70 		++n;
71 	    } else if (!quoted && isspace(UCH(blob[n]))) {
72 		inparm = FALSE;
73 		if (pass) {
74 		    *param++ = '\0';
75 		}
76 	    } else {
77 		if (!inparm) {
78 		    if (pass)
79 			result[count] = param;
80 		    ++count;
81 		    inparm = TRUE;
82 		}
83 		if (pass) {
84 		    *param++ = blob[n];
85 		}
86 	    }
87 	}
88 
89 	if (!pass) {
90 	    if (count) {
91 		result = dlg_calloc(char *, count + 1);
92 		assert_ptr(result, "string_to_argv");
93 	    } else {
94 		break;		/* no tokens found */
95 	    }
96 	} else {
97 	    *param = '\0';
98 	}
99     }
100     return result;
101 }
102 
103 /*
104  * Count the entries in an argv list.
105  */
106 int
107 dlg_count_argv(char **argv)
108 {
109     int result = 0;
110 
111     if (argv != 0) {
112 	while (argv[result] != 0)
113 	    ++result;
114     }
115     return result;
116 }
117 
118 int
119 dlg_eat_argv(int *argcp, char ***argvp, int start, int count)
120 {
121     int k;
122 
123     *argcp -= count;
124     for (k = start; k <= *argcp; k++)
125 	(*argvp)[k] = (*argvp)[k + count];
126     (*argvp)[*argcp] = 0;
127     return TRUE;
128 }
129