Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fixargv.c « fixargv « samples « mingw « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50c24cd068b7c89ca48a7e4f83d704bbc624cc93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * fixargv.c
 *
 * A special function which "fixes" an argv array by replacing arguments
 * that need quoting with quoted versions.
 *
 * NOTE: In order to be reasonably consistent there is some misuse of the
 *       const keyword here-- which leads to compilation warnings. These
 *       should be ok to ignore.
 *
 * This is a sample distributed as part of the Mingw32 package.
 *
 * Contributors:
 *  Created by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
 *
 *  THIS SOFTWARE IS NOT COPYRIGHTED
 *
 *  This source code is offered for use in the public domain. You may
 *  use, modify or distribute it freely.
 *
 *  This code is distributed in the hope that it will be useful but
 *  WITHOUT ANY WARRANTY. ALL WARRENTIES, EXPRESS OR IMPLIED ARE HEREBY
 *  DISCLAMED. This includes but is not limited to warrenties of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * $Revision$
 * $Author$
 * $Date$
 *
 */

#include <string.h>
#include "fixargv.h"

/*
 * This takes a single string and fixes it, enclosing it in quotes if it
 * contains any spaces and/or escaping the quotes it contains.
 */
char*
fix_arg (const char* szArg)
{
	int	nQuoteAll;	/* Does the whole arg need quoting? */
	int	nBkSlRun;	/* How may backslashes in a row? */
	char*	sz;
	char*	szNew;
	size_t	sizeLen;

	nQuoteAll = 0;
	nBkSlRun = 0;
	sz = szArg;
	sizeLen = 1;

	/* First we figure out how much bigger the new string has to be
	 * than the old one. */
	while (*sz != '\0')
	{
		/*
		 * Arguments containing whitespace of wildcards will be
		 * quoted to preserve tokenization and/or those special
		 * characters (i.e. wildcarding will NOT be done at the
		 * other end-- they will get the * and ? characters as is).
		 * TODO: Is this the best way? Do we want to enable wildcards?
		 *       If so, when?
		 */
		if (!nQuoteAll &&
		    (*sz == ' ' || *sz == '\t' || *sz == '*' || *sz == '?'))
		{
			nQuoteAll = 1;
		}
		else if (*sz == '\\')
		{
			nBkSlRun++;
		}
		else
		{
			if (*sz == '\"')
			{
				sizeLen += nBkSlRun + 1;
			}
			nBkSlRun = 0;
		}

		sizeLen++;
		sz++;
	}

	if (nQuoteAll)
	{
		sizeLen += 2;
	}

	/*
	 * Make a new string big enough.
	 */
	szNew = (char*) malloc (sizeLen);
	if (!szNew)
	{
		return NULL;
	}
	sz = szNew;

	/* First enclosing quote for fully quoted args. */
	if (nQuoteAll)
	{
		*sz = '\"';
		sz++;
	}

	/*
	 * Go through the string putting backslashes in front of quotes,
	 * and doubling all backslashes immediately in front of quotes.
	 */
	nBkSlRun = 0;
	while (*szArg != '\0')
	{
		if (*szArg == '\\')
		{
			nBkSlRun++;
		}
		else
		{
			if (*szArg == '\"')
			{
				while (nBkSlRun > 0)
				{
					*sz = '\\';
					sz++;
					nBkSlRun--;
				}
				*sz = '\\';
				sz++;
			}
			nBkSlRun = 0;
		}

		*sz = *szArg;
		sz++;
		szArg++;
	}

	/* Closing quote for fully quoted args. */
	if (nQuoteAll)
	{
		*sz = '\"';
		sz++;
	}

	*sz = '\0';
	return szNew;
}

/*
 * Takes argc and argv and returns a new argv with escaped members. Pass
 * this fixed argv (along with the old one) to free_fixed_argv after
 * you finish with it. Pass in an argc of -1 and make sure the argv vector
 * ends with a null pointer to have fix_argv count the arguments for you.
 */
char* const*
fix_argv (int argc, char* const* szaArgv)
{
	char**	szaNew;
	char*	sz;
	int	i;

	if (!szaArgv)
	{
		return NULL;
	}

	/*
	 * Count the arguments if asked.
	 */
	if (argc == -1)
	{
		for (i = 0; szaArgv[i]; i++)
			;

		argc = i;
	}

	/*
	 * If there are no args or only one arg then do no escaping.
	 */
	if (argc < 2)
	{
		return szaArgv;
	}

	for (i = 1, szaNew = NULL; i < argc; i++)
	{
		sz = szaArgv[i];

		/*
		 * If an argument needs fixing, then fix it.
		 */
		if (strpbrk (sz, "\" \t*?"))
		{
			/*
			 * If we haven't created a new argv list already
			 * then make one.
			 */
			if (!szaNew)
			{
				szaNew = (char**) malloc ((argc + 1) *
					sizeof (char*));
				if (!szaNew)
				{
					return NULL;
				}

				/*
				 * Copy previous args from old to new.
				 */
				memcpy (szaNew, szaArgv, sizeof(char*) * i);
			}

			/*
			 * Now do the fixing.
			 */
			szaNew[i] = fix_arg (sz);
			if (!szaNew[i])
			{
				/* Fixing failed, free up and return error. */
				free_fixed_argv (szaNew, szaArgv);
				return NULL;
			}
		}
		else if (szaNew)
		{
			szaNew[i] = sz;
		}
	}

	if (szaNew)
	{
		/* If we have created a new argv list then we might as well
		 * terminate it nicely. (And we depend on it in
		 * free_fixed_argv.) */
		szaNew[argc] = NULL;
	}
	else
	{
		/* If we didn't create a new argv list then return the
		 * original. */
		return szaArgv;
	}

	return szaNew;
}

void
free_fixed_argv (char* const* szaFixed, char* const* szaOld)
{
	char* const*	sza;

	/*
	 * Check for error conditions. Also note that if no corrections
	 * were required the fixed argv will actually be the same as
	 * the old one, and we don't need to do anything.
	 */
	if (!szaFixed || !szaOld || szaFixed == szaOld)
	{
		return;
	}

	/*
	 * Go through all members of the argv list. If any of the
	 * members in the fixed list are different from the old
	 * list we free those members.
	 * NOTE: The first member is never modified, so we don't need to
	 * check.
	 */
	sza = szaFixed + 1;
	szaOld++;
	while (*sza)
	{
		if (*sza != *szaOld)
		{
			free (*sza);
		}
		sza++;
		szaOld++;
	}

	/*
	 * Now we can free the array of char pointers itself.
	 */
	free (szaFixed);
}