xref: /freebsd/contrib/dialog/argv.c (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
1 /*
2  * $Id: argv.c,v 1.2 2012/11/30 20:28:23 tom Exp $
3  *
4  *  argv - Reusable functions for argv-parsing.
5  *
6  *  Copyright 2011,2012	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 (pass) {
60 			*param++ = blob[n];
61 			*param++ = blob[n + 1];
62 		    }
63 		}
64 		++n;
65 	    } else if (!quoted && isspace(UCH(blob[n]))) {
66 		inparm = FALSE;
67 		if (pass) {
68 		    *param++ = '\0';
69 		}
70 	    } else {
71 		if (!inparm) {
72 		    if (pass)
73 			result[count] = param;
74 		    ++count;
75 		    inparm = TRUE;
76 		}
77 		if (pass) {
78 		    *param++ = blob[n];
79 		}
80 	    }
81 	}
82 
83 	if (!pass) {
84 	    if (count) {
85 		result = dlg_calloc(char *, count + 1);
86 		assert_ptr(result, "string_to_argv");
87 	    } else {
88 		break;		/* no tokens found */
89 	    }
90 	} else {
91 	    *param = '\0';
92 	}
93     }
94     return result;
95 }
96 
97 /*
98  * Count the entries in an argv list.
99  */
100 int
101 dlg_count_argv(char **argv)
102 {
103     int result = 0;
104 
105     if (argv != 0) {
106 	while (argv[result] != 0)
107 	    ++result;
108     }
109     return result;
110 }
111 
112 int
113 dlg_eat_argv(int *argcp, char ***argvp, int start, int count)
114 {
115     int k;
116 
117     *argcp -= count;
118     for (k = start; k <= *argcp; k++)
119 	(*argvp)[k] = (*argvp)[k + count];
120     (*argvp)[*argcp] = 0;
121     return TRUE;
122 }
123