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 glib.c.functions;
31 public  import glib.c.types;
32 public  import gtkc.glibtypes;
33 
34 
35 /** */
36 public struct ShellUtils
37 {
38 
39 	/** */
40 	public static GQuark shellErrorQuark()
41 	{
42 		return g_shell_error_quark();
43 	}
44 
45 	/**
46 	 * Parses a command line into an argument vector, in much the same way
47 	 * the shell would, but without many of the expansions the shell would
48 	 * perform (variable expansion, globs, operators, filename expansion,
49 	 * etc. are not supported). The results are defined to be the same as
50 	 * those you would get from a UNIX98 /bin/sh, as long as the input
51 	 * contains none of the unsupported shell expansions. If the input
52 	 * does contain such expansions, they are passed through
53 	 * literally. Possible errors are those from the #G_SHELL_ERROR
54 	 * domain. Free the returned vector with g_strfreev().
55 	 *
56 	 * Params:
57 	 *     commandLine = command line to parse
58 	 *     argvp = return location for array of args
59 	 *
60 	 * Returns: %TRUE on success, %FALSE if error set
61 	 *
62 	 * Throws: GException on failure.
63 	 */
64 	public static bool shellParseArgv(string commandLine, out string[] argvp)
65 	{
66 		int argcp;
67 		char** outargvp = null;
68 		GError* err = null;
69 
70 		auto p = g_shell_parse_argv(Str.toStringz(commandLine), &argcp, &outargvp, &err) != 0;
71 
72 		if (err !is null)
73 		{
74 			throw new GException( new ErrorG(err) );
75 		}
76 
77 		argvp = Str.toStringArray(outargvp, argcp);
78 
79 		return p;
80 	}
81 
82 	/**
83 	 * Quotes a string so that the shell (/bin/sh) will interpret the
84 	 * quoted string to mean @unquoted_string. If you pass a filename to
85 	 * the shell, for example, you should first quote it with this
86 	 * function.  The return value must be freed with g_free(). The
87 	 * quoting style used is undefined (single or double quotes may be
88 	 * used).
89 	 *
90 	 * Params:
91 	 *     unquotedString = a literal string
92 	 *
93 	 * Returns: quoted string
94 	 */
95 	public static string shellQuote(string unquotedString)
96 	{
97 		auto retStr = g_shell_quote(Str.toStringz(unquotedString));
98 
99 		scope(exit) Str.freeString(retStr);
100 		return Str.toString(retStr);
101 	}
102 
103 	/**
104 	 * Unquotes a string as the shell (/bin/sh) would. Only handles
105 	 * quotes; if a string contains file globs, arithmetic operators,
106 	 * variables, backticks, redirections, or other special-to-the-shell
107 	 * features, the result will be different from the result a real shell
108 	 * would produce (the variables, backticks, etc. will be passed
109 	 * through literally instead of being expanded). This function is
110 	 * guaranteed to succeed if applied to the result of
111 	 * g_shell_quote(). If it fails, it returns %NULL and sets the
112 	 * error. The @quoted_string need not actually contain quoted or
113 	 * escaped text; g_shell_unquote() simply goes through the string and
114 	 * unquotes/unescapes anything that the shell would. Both single and
115 	 * double quotes are handled, as are escapes including escaped
116 	 * newlines. The return value must be freed with g_free(). Possible
117 	 * errors are in the #G_SHELL_ERROR domain.
118 	 *
119 	 * Shell quoting rules are a bit strange. Single quotes preserve the
120 	 * literal string exactly. escape sequences are not allowed; not even
121 	 * \' - if you want a ' in the quoted text, you have to do something
122 	 * like 'foo'\''bar'.  Double quotes allow $, `, ", \, and newline to
123 	 * be escaped with backslash. Otherwise double quotes preserve things
124 	 * literally.
125 	 *
126 	 * Params:
127 	 *     quotedString = shell-quoted string
128 	 *
129 	 * Returns: an unquoted string
130 	 *
131 	 * Throws: GException on failure.
132 	 */
133 	public static string shellUnquote(string quotedString)
134 	{
135 		GError* err = null;
136 
137 		auto retStr = g_shell_unquote(Str.toStringz(quotedString), &err);
138 
139 		if (err !is null)
140 		{
141 			throw new GException( new ErrorG(err) );
142 		}
143 
144 		scope(exit) Str.freeString(retStr);
145 		return Str.toString(retStr);
146 	}
147 }