2 * Shell utility functions.
5 * Gonzalo Paniagua Javier (gonzalo@novell.com
7 * (C) 2006 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 split_cmdline (const gchar
*cmdline
, GPtrArray
*array
, GError
**error
)
36 gboolean in_quote
= FALSE
;
37 gboolean escaped
= FALSE
;
38 gchar quote_char
= '\0';
41 str
= g_string_new ("");
42 ptr
= (gchar
*) cmdline
;
43 while ((c
= *ptr
++) != '\0') {
46 if (!g_ascii_isspace (c
))
47 g_string_append_c (str
, c
);
48 } else if (in_quote
) {
49 if (c
== quote_char
) {
52 g_ptr_array_add (array
, g_string_free (str
, FALSE
));
53 str
= g_string_new ("");
55 g_string_append_c (str
, c
);
57 } else if (g_ascii_isspace (c
)) {
59 g_ptr_array_add (array
, g_string_free (str
, FALSE
));
60 str
= g_string_new ("");
62 } else if (c
== '\\') {
64 } else if (c
== '\'' || c
== '"') {
68 g_string_append_c (str
, c
);
74 *error
= g_error_new (G_LOG_DOMAIN
, 0, "Unfinished escape.");
75 g_string_free (str
, TRUE
);
81 *error
= g_error_new (G_LOG_DOMAIN
, 0, "Unfinished quote.");
82 g_string_free (str
, TRUE
);
87 g_ptr_array_add (array
, g_string_free (str
, FALSE
));
89 g_string_free (str
, TRUE
);
91 g_ptr_array_add (array
, NULL
);
96 g_shell_parse_argv (const gchar
*command_line
, gint
*argcp
, gchar
***argvp
, GError
**error
)
102 g_return_val_if_fail (command_line
, FALSE
);
103 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
105 array
= g_ptr_array_new();
106 if (split_cmdline (command_line
, array
, error
)) {
107 g_ptr_array_add (array
, NULL
);
108 g_strfreev ((gchar
**) array
->pdata
);
109 g_ptr_array_free (array
, FALSE
);
114 argv
= (gchar
**) array
->pdata
;
118 g_ptr_array_free (array
, FALSE
);
123 *argcp
= array
->len
- 1;
132 g_ptr_array_free (array
, FALSE
);
137 g_shell_quote (const gchar
*unquoted_string
)
139 //g_error ("Not implemented");
140 return g_strdup (unquoted_string
);
145 g_shell_unquote (const gchar
*quoted_string
, GError
**error
)
147 // g_error ("Not implemented");
148 return g_strdup (quoted_string
);