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: Yes
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  * 	- g_shell_parse_argv
45  * omit signals:
46  * imports:
47  * 	- glib.Str
48  * 	- glib.ErrorG
49  * 	- glib.GException
50  * structWrap:
51  * module aliases:
52  * local aliases:
53  * overrides:
54  */
55 
56 module glib.ShellUtils;
57 
58 public  import gtkc.glibtypes;
59 
60 private import gtkc.glib;
61 private import glib.ConstructionException;
62 
63 
64 private import glib.Str;
65 private import glib.ErrorG;
66 private import glib.GException;
67 
68 
69 
70 
71 /**
72  * Description
73  */
74 public class ShellUtils
75 {
76 	
77 	/**
78 	 * Parses a command line into an argument vector, in much the same way
79 	 * the shell would, but without many of the expansions the shell would
80 	 * perform (variable expansion, globs, operators, filename expansion,
81 	 * etc. are not supported). The results are defined to be the same as
82 	 * those you would get from a UNIX98 /bin/sh, as long as the input
83 	 * contains none of the unsupported shell expansions. If the input
84 	 * does contain such expansions, they are passed through
85 	 * literally. Possible errors are those from the G_SHELL_ERROR
86 	 * domain. Free the returned vector with g_strfreev().
87 	 * Params:
88 	 * commandLine =  command line to parse
89 	 * argcp =  return location for number of args
90 	 * argvp =  return location for array of args
91 	 * Returns: TRUE on success, FALSE if error set
92 	 * Throws: GException on failure.
93 	 */
94 	public static int parseArgv(string commandLine, out int argcp, out string[] argvp)
95 	{
96 		// gboolean g_shell_parse_argv (const gchar *command_line,  gint *argcp,  gchar ***argvp,  GError **error);
97 		GError* err = null;
98 		char** arg = null;
99 		
100 		auto p = g_shell_parse_argv(Str.toStringz(commandLine), &argcp, &arg, &err);
101 		
102 		if (err !is null)
103 		{
104 			throw new GException( new ErrorG(err) );
105 		}
106 		
107 		argvp = Str.toStringArray(arg);
108 		return p;
109 	}
110 	
111 	/**
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 }