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  * Conversion parameters:
26  * inFile  = glib-Shell-related-Utilities.html
27  * outPack = glib
28  * outFile = ShellUtils
29  * strct   = 
30  * realStrct=
31  * ctorStrct=
32  * clss    = ShellUtils
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_shell_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.Str
47  * 	- glib.ErrorG
48  * 	- glib.GException
49  * structWrap:
50  * module aliases:
51  * local aliases:
52  * overrides:
53  */
54 
55 module glib.ShellUtils;
56 
57 public  import gtkc.glibtypes;
58 
59 private import gtkc.glib;
60 private import glib.ConstructionException;
61 
62 
63 private import glib.Str;
64 private import glib.ErrorG;
65 private import glib.GException;
66 
67 
68 
69 
70 /**
71  */
72 public class ShellUtils
73 {
74 	
75 	/**
76 	 */
77 	
78 	/**
79 	 * Parses a command line into an argument vector, in much the same way
80 	 * the shell would, but without many of the expansions the shell would
81 	 * perform (variable expansion, globs, operators, filename expansion,
82 	 * etc. are not supported). The results are defined to be the same as
83 	 * those you would get from a UNIX98 /bin/sh, as long as the input
84 	 * contains none of the unsupported shell expansions. If the input
85 	 * does contain such expansions, they are passed through
86 	 * literally. Possible errors are those from the G_SHELL_ERROR
87 	 * domain. Free the returned vector with g_strfreev().
88 	 * Params:
89 	 * commandLine = command line to parse
90 	 * argvp = return location for array of args. [out][array length=argcp zero-terminated=1]
91 	 * Returns: TRUE on success, FALSE if error set
92 	 * Throws: GException on failure.
93 	 */
94 	public static int parseArgv(string commandLine, out string[] argvp)
95 	{
96 		// gboolean g_shell_parse_argv (const gchar *command_line,  gint *argcp,  gchar ***argvp,  GError **error);
97 		char** outargvp = null;
98 		int argcp;
99 		GError* err = null;
100 		
101 		auto p = g_shell_parse_argv(Str.toStringz(commandLine), &argcp, &outargvp, &err);
102 		
103 		if (err !is null)
104 		{
105 			throw new GException( new ErrorG(err) );
106 		}
107 		
108 		argvp = null;
109 		foreach ( cstr; outargvp[0 .. argcp] )
110 		{
111 			argvp ~= Str.toString(cstr);
112 		}
113 		return p;
114 	}
115 	
116 	/**
117 	 * Quotes a string so that the shell (/bin/sh) will interpret the
118 	 * quoted string to mean unquoted_string. If you pass a filename to
119 	 * the shell, for example, you should first quote it with this
120 	 * function. The return value must be freed with g_free(). The
121 	 * quoting style used is undefined (single or double quotes may be
122 	 * used).
123 	 * Params:
124 	 * unquotedString = a literal string
125 	 * Returns: quoted string
126 	 */
127 	public static string quote(string unquotedString)
128 	{
129 		// gchar * g_shell_quote (const gchar *unquoted_string);
130 		return Str.toString(g_shell_quote(Str.toStringz(unquotedString)));
131 	}
132 	
133 	/**
134 	 * Unquotes a string as the shell (/bin/sh) would. Only handles
135 	 * quotes; if a string contains file globs, arithmetic operators,
136 	 * variables, backticks, redirections, or other special-to-the-shell
137 	 * features, the result will be different from the result a real shell
138 	 * would produce (the variables, backticks, etc. will be passed
139 	 * through literally instead of being expanded). This function is
140 	 * guaranteed to succeed if applied to the result of
141 	 * g_shell_quote(). If it fails, it returns NULL and sets the
142 	 * error. The quoted_string need not actually contain quoted or
143 	 * escaped text; g_shell_unquote() simply goes through the string and
144 	 * unquotes/unescapes anything that the shell would. Both single and
145 	 * double quotes are handled, as are escapes including escaped
146 	 * newlines. The return value must be freed with g_free(). Possible
147 	 * errors are in the G_SHELL_ERROR domain.
148 	 * Shell quoting rules are a bit strange. Single quotes preserve the
149 	 * literal string exactly. escape sequences are not allowed; not even
150 	 * \' - if you want a ' in the quoted text, you have to do something
151 	 * like 'foo'\''bar'. Double quotes allow $, `, ", \, and newline to
152 	 * be escaped with backslash. Otherwise double quotes preserve things
153 	 * literally.
154 	 * Params:
155 	 * quotedString = shell-quoted string
156 	 * Returns: an unquoted string
157 	 * Throws: GException on failure.
158 	 */
159 	public static string unquote(string quotedString)
160 	{
161 		// gchar * g_shell_unquote (const gchar *quoted_string,  GError **error);
162 		GError* err = null;
163 		
164 		auto p = g_shell_unquote(Str.toStringz(quotedString), &err);
165 		
166 		if (err !is null)
167 		{
168 			throw new GException( new ErrorG(err) );
169 		}
170 		
171 		return Str.toString(p);
172 	}
173 }