1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.ShellUtils;
26 
27 private import glib.ErrorG;
28 private import glib.GException;
29 private import glib.Str;
30 private import gtkc.glib;
31 public  import gtkc.glibtypes;
32 
33 
34 public struct ShellUtils
35 {
36 	/**
37 	 */
38 
39 	public static GQuark shellErrorQuark()
40 	{
41 		return g_shell_error_quark();
42 	}
43 
44 	/**
45 	 * Parses a command line into an argument vector, in much the same way
46 	 * the shell would, but without many of the expansions the shell would
47 	 * perform (variable expansion, globs, operators, filename expansion,
48 	 * etc. are not supported). The results are defined to be the same as
49 	 * those you would get from a UNIX98 /bin/sh, as long as the input
50 	 * contains none of the unsupported shell expansions. If the input
51 	 * does contain such expansions, they are passed through
52 	 * literally. Possible errors are those from the #G_SHELL_ERROR
53 	 * domain. Free the returned vector with g_strfreev().
54 	 *
55 	 * Params:
56 	 *     commandLine = command line to parse
57 	 *     argcp = return location for number of args, or %NULL
58 	 *     argvp = return
59 	 *         location for array of args, or %NULL
60 	 *
61 	 * Return: %TRUE on success, %FALSE if error set
62 	 *
63 	 * Throws: GException on failure.
64 	 */
65 	public static bool shellParseArgv(string commandLine, out string[] argvp)
66 	{
67 		int argcp;
68 		char** outargvp = null;
69 		GError* err = null;
70 		
71 		auto p = g_shell_parse_argv(Str.toStringz(commandLine), &argcp, &outargvp, &err) != 0;
72 		
73 		if (err !is null)
74 		{
75 			throw new GException( new ErrorG(err) );
76 		}
77 		
78 		argvp = Str.toStringArray(outargvp, argcp);
79 		
80 		return p;
81 	}
82 
83 	/**
84 	 * Quotes a string so that the shell (/bin/sh) will interpret the
85 	 * quoted string to mean @unquoted_string. If you pass a filename to
86 	 * the shell, for example, you should first quote it with this
87 	 * function.  The return value must be freed with g_free(). The
88 	 * quoting style used is undefined (single or double quotes may be
89 	 * used).
90 	 *
91 	 * Params:
92 	 *     unquotedString = a literal string
93 	 *
94 	 * Return: quoted string
95 	 */
96 	public static string shellQuote(string unquotedString)
97 	{
98 		return Str.toString(g_shell_quote(Str.toStringz(unquotedString)));
99 	}
100 
101 	/**
102 	 * Unquotes a string as the shell (/bin/sh) would. Only handles
103 	 * quotes; if a string contains file globs, arithmetic operators,
104 	 * variables, backticks, redirections, or other special-to-the-shell
105 	 * features, the result will be different from the result a real shell
106 	 * would produce (the variables, backticks, etc. will be passed
107 	 * through literally instead of being expanded). This function is
108 	 * guaranteed to succeed if applied to the result of
109 	 * g_shell_quote(). If it fails, it returns %NULL and sets the
110 	 * error. The @quoted_string need not actually contain quoted or
111 	 * escaped text; g_shell_unquote() simply goes through the string and
112 	 * unquotes/unescapes anything that the shell would. Both single and
113 	 * double quotes are handled, as are escapes including escaped
114 	 * newlines. The return value must be freed with g_free(). Possible
115 	 * errors are in the #G_SHELL_ERROR domain.
116 	 *
117 	 * Shell quoting rules are a bit strange. Single quotes preserve the
118 	 * literal string exactly. escape sequences are not allowed; not even
119 	 * \' - if you want a ' in the quoted text, you have to do something
120 	 * like 'foo'\''bar'.  Double quotes allow $, `, ", \, and newline to
121 	 * be escaped with backslash. Otherwise double quotes preserve things
122 	 * literally.
123 	 *
124 	 * Params:
125 	 *     quotedString = shell-quoted string
126 	 *
127 	 * Return: an unquoted string
128 	 *
129 	 * Throws: GException on failure.
130 	 */
131 	public static string shellUnquote(string quotedString)
132 	{
133 		GError* err = null;
134 		
135 		auto p = g_shell_unquote(Str.toStringz(quotedString), &err);
136 		
137 		if (err !is null)
138 		{
139 			throw new GException( new ErrorG(err) );
140 		}
141 		
142 		return Str.toString(p);
143 	}
144 }