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