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
59 	 *         location for array of args
60 	 *
61 	 * Returns: %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 	 * Returns: quoted string
95 	 */
96 	public static string shellQuote(string unquotedString)
97 	{
98 		auto retStr = g_shell_quote(Str.toStringz(unquotedString));
99 
100 		scope(exit) Str.freeString(retStr);
101 		return Str.toString(retStr);
102 	}
103 
104 	/**
105 	 * Unquotes a string as the shell (/bin/sh) would. Only handles
106 	 * quotes; if a string contains file globs, arithmetic operators,
107 	 * variables, backticks, redirections, or other special-to-the-shell
108 	 * features, the result will be different from the result a real shell
109 	 * would produce (the variables, backticks, etc. will be passed
110 	 * through literally instead of being expanded). This function is
111 	 * guaranteed to succeed if applied to the result of
112 	 * g_shell_quote(). If it fails, it returns %NULL and sets the
113 	 * error. The @quoted_string need not actually contain quoted or
114 	 * escaped text; g_shell_unquote() simply goes through the string and
115 	 * unquotes/unescapes anything that the shell would. Both single and
116 	 * double quotes are handled, as are escapes including escaped
117 	 * newlines. The return value must be freed with g_free(). Possible
118 	 * errors are in the #G_SHELL_ERROR domain.
119 	 *
120 	 * Shell quoting rules are a bit strange. Single quotes preserve the
121 	 * literal string exactly. escape sequences are not allowed; not even
122 	 * \' - if you want a ' in the quoted text, you have to do something
123 	 * like 'foo'\''bar'.  Double quotes allow $, `, ", \, and newline to
124 	 * be escaped with backslash. Otherwise double quotes preserve things
125 	 * literally.
126 	 *
127 	 * Params:
128 	 *     quotedString = shell-quoted string
129 	 *
130 	 * Returns: an unquoted string
131 	 *
132 	 * Throws: GException on failure.
133 	 */
134 	public static string shellUnquote(string quotedString)
135 	{
136 		GError* err = null;
137 
138 		auto retStr = g_shell_unquote(Str.toStringz(quotedString), &err);
139 
140 		if (err !is null)
141 		{
142 			throw new GException( new ErrorG(err) );
143 		}
144 
145 		scope(exit) Str.freeString(retStr);
146 		return Str.toString(retStr);
147 	}
148 }