File: all_argv.c

package info (click to toggle)
xmpi 2.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,232 kB
  • ctags: 1,656
  • sloc: ansic: 13,738; sh: 1,799; makefile: 233
file content (543 lines) | stat: -rw-r--r-- 10,459 bytes parent folder | download
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
 * Copyright 1998-1999, University of Notre Dame.
 * Authors: Brian W. Barrett, Arun F. Rodrigues, Jeffrey M. Squyres,
 * 	 and Andrew Lumsdaine
 *
 * This file is part of XMPI
 *
 * You should have received a copy of the License Agreement for XMPI 
 * along with the software; see the file LICENSE.  If not, contact 
 * Office of Research, University of Notre Dame, Notre Dame, IN 46556.
 *
 * Permission to modify the code and to distribute modified code is
 * granted, provided the text of this NOTICE is retained, a notice that
 * the code was modified is included with the above COPYRIGHT NOTICE and
 * with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
 * file is distributed with the modified code.
 *
 * LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
 * By way of example, but not limitation, Licensor MAKES NO
 * REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
 * PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
 * OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
 * OR OTHER RIGHTS.
 *
 * Additional copyrights may follow.

 *
 *	$Id: all_argv.c,v 1.4 1999/11/08 06:20:19 bbarrett Exp $
 *
 *	Function:	- argument vector library
 */

#include <stdlib.h>
#include <string.h>

#include "lam.h"

#define ARGSIZE		128

/*
 * global functions
 */
int			sfh_argv_add();
int			sfh_argv_count();
unint			sfh_argv_len();
void			sfh_argv_free();
char			**sfh_argv_break();
char			**sfh_argv_dup();
char			*sfh_argv_glue();
char			**sfh_argv_break_quoted();
char			*sfh_argv_unquote();

/*
 *	sfh_argv_add
 *
 *	Function:	- extends an argv with a new arg
 *	Accepts:	- argc (value returned)
 *			- argv (value returned)
 *			- new arg
 *	Returns:	- 0 or -1 (error)
 */
int
sfh_argv_add(int *argc, char ***argv, XMPI_CONST char *arg)
{
/*
 * Create new argv.
 */
	if (*argv == 0) {
		*argv = (char **) malloc(2 * sizeof(char *));
		if (*argv == 0) return(-1);
		(*argv)[0] = 0;
		(*argv)[1] = 0;
	}
/*
 * Extend existing argv.
 */
	else {
		*argv = (char **) realloc((char *) *argv,
				(unsigned) (*argc + 2) * sizeof(char *));
		if (*argv == 0) return(-1);
	}

	(*argv)[*argc] = malloc((unsigned) strlen(arg) + 1);
	if ((*argv)[*argc] == 0) return(-1);

	strcpy((*argv)[*argc], arg);
	*argc = *argc + 1;
	(*argv)[*argc] = 0;
	return(0);
}

/*
 *	sfh_argv_free
 *
 *	Function:	- frees memory associated with argv
 *	Accepts:	- argv
 */
void
sfh_argv_free(char **argv)
{
	char		**p;

	if (argv == 0) return;

	for (p = argv; *p; ++p) {
		free(*p);
	}

	free((char *) argv);
}

/*
 *	sfh_argv_break
 * 
 *	Function:	- breaks single string into argv
 *			- allocates space for argv structure
 *			  which must be freed by the caller
 *	Accepts:	- argument string
 *			- arg delimiter within string
 *	Returns:	- argv or NULL (error)
 */
char **
sfh_argv_break(XMPI_CONST char *args, int delim)
{
	char		arg[ARGSIZE];	/* single argument */
	char		**argv = 0;	/* argument structure ptr */
	XMPI_CONST char	*p;		/* walk through args */
	char		*argtemp;	/* temporary single argument */
	int		argc = 0;	/* # arguments */
	unint		arglen;		/* single argument length */

	while (*args) {
		p = args;
		arglen = 0;

		while ((*p != '\0') && (*p != delim)) {
			p++;
			arglen++;
		}
/*
 * zero length argument, skip
 */
		if (args == p) {
			p++;
		}
/*
 * tail argument, add straight from the original string
 */
		else if (*p == '\0') {

			if (sfh_argv_add(&argc, &argv, args)) return(0);
		}
/*
 * long argument, malloc buffer, copy and add
 */
		else if (arglen > (ARGSIZE - 1)) {
			argtemp = malloc((unsigned) (arglen + 1));
			if (argtemp == 0) return(0);

			strncpy(argtemp, args, arglen);
			argtemp[arglen] = '\0';

			if (sfh_argv_add(&argc, &argv, argtemp)) {
				free(argtemp);
				return(0);
			}

			free(argtemp);
		}
/*
 * short argument, copy to buffer and add
 */
		else {
			strncpy(arg, args, arglen);
			arg[arglen] = '\0';

			if (sfh_argv_add(&argc, &argv, arg)) return(0);
		}

		args = p;
	}

	return(argv);
}

/*
 *	sfh_argv_count
 * 
 *	Function:	- count the # of entries in argv
 *	Accepts:	- argv
 *	Returns:	- # of entries
 */
int
sfh_argv_count(char **argv)
{
	char		**p;
	int		i;

	if (argv == 0) return(0);

	for (i = 0, p = argv; *p; i++, p++);

	return(i);
}

/*
 *	sfh_argv_glue
 * 
 *	Function:	- glues argv into single string
 *			- delimiter and max string length specified
 *			- allocates space for string which must be
 *			  freed by the caller
 *	Accepts:	- argv
 *			- arg delimiter within string
 *			- max string length (or 0 meaning full argv)
 *	Returns:	- string or NULL (error)
 */
char *
sfh_argv_glue(char **argv, int delim, unint maxlen)
{
	char		**p;		/* favorite pointer */
	char		*pp;		/* another pointer */
	char		*str;		/* glued string */
	unint		str_len = 0;	/* string length */
	unint		i;		/* favorite index */
/*
 * Find the total string length in argv including delimiters.
 * The last delimiter is replaced by the NULL character.
 */
	for (p = argv; *p; p++) {
		str_len += strlen(*p) + 1;
	}

	if ((maxlen > 0) && (maxlen + 1 < str_len)) {
		str_len = maxlen + 1;
	}
/*
 * Allocate the string.
 */
	if ((str = malloc((unsigned) str_len)) == 0) return(0);
/*
 * Loop filling in the string.
 */
	str[--str_len] = '\0';
	p = argv;
	pp = *p;

	for (i = 0; i < str_len; i++) {

		if (*pp == '\0') {
/*
 * End of a string, fill in a delimiter and go to the next string.
 */
			str[i] = (char) delim;
			p++;
			pp = *p;
		} else {
			str[i] = *pp++;
		}
	}

	return(str);
}

/*
 *	sfh_argv_len
 * 
 *	Function:	- count the bytes consumed by an argv
 *	Accepts:	- argv
 *	Returns:	- length
 */
unint
sfh_argv_len(char **argv)
{
	char		**p;
	unint		length;		/* argv length */

	if (argv == 0) return(0);

	length = sizeof(char *);

	for (p = argv; *p; p++) {
		length += strlen(*p) + 1 + sizeof(char *);
	}

	return(length);
}

/*
 *	sfh_argv_dup
 *
 *	Function:	- create a duplicate copy of an argv
 *	Accepts:	- argv
 *	Returns:	- duplicate argv or NULL
 */
char **
sfh_argv_dup(char **argv)
{
	char		**dupv = 0;	/* duplicate argv */
	int		dupc = 0;	/* dupv count */

	if (argv == 0) return(0);

	while (*argv != 0) {

		if (sfh_argv_add(&dupc, &dupv, *argv)) {
			sfh_argv_free(dupv);
			return(0);
		}

		argv++;
	}

	return(dupv);
}

#define Q 		quotes[0]
#define SQ 		quotes[1]
#define DQ 		quotes[2]

static void
unquote(char *unquoted, char *arg, char *end, char quotes[])
{
	char  		*p;
	int         	insq = 0;
	int		indq = 0;

	p = arg;
	while (p < end) {
		if (*p == SQ) {
			if (insq) {
				insq = 0;
				p++;
			} else if (indq) {
				*unquoted++ = *p++;
			} else {
				insq = 1;
				p++;
			}
		} else if (*p == DQ) {
			if (insq) {
				*unquoted++ = *p++;
			} else if (indq) {
				indq = 0;
				p++;
			} else {
				indq = 1;
				p++;
			}
		} else if (*p == Q) {
			if (*(p+1) == Q || *(p+1) == DQ) {
				*unquoted++ = *(p+1);
				p += 2;
			} else {
				*unquoted++ = *p++;
			}
		} else {
			*unquoted++ = *p++;
		}
	}

	*unquoted = 0;
}

/*
 *	sfh_argv_break_quoted
 * 
 *	Function:	- breaks single possibly quoted string into argv
 *			- allocates space for argv structure
 *			  which must be freed by the caller
 *	Accepts:	- argument string
 *			- arg delimiter within string
 *			- array of quotes (quote, single quote, double quote)
 *	Returns:	- argv or NULL (error)
 */
char **
sfh_argv_break_quoted(char *args, int delim, char quotes[])
{
	char		arg[ARGSIZE];	/* single argument */
	char		**argv = 0;	/* argument structure ptr */
	char	        *p;		/* walk through args */
	char		*argtemp;	/* temporary single argument */
	int		argc = 0;	/* # arguments */
	unint		arglen;		/* single argument length */
	int             insq;		/* inside single quotes */
	int		indq;		/* inside double quotes */
 
	while (*args) {
		p = args;
		arglen = 0;
		indq = insq = 0;

		while (*p) {
			if (*p == SQ) {
				if (insq) {
					insq = 0;
					p++;
				} else if (indq) {
					arglen++;
					p++;
				} else {
					insq = 1;
					p++;
				}
			} else if (*p == DQ) {
				if (insq) {
					arglen++;
					p++;
				} else if (indq) {
					indq = 0;
					p++;
				} else {
					indq = 1;
					p++;
				}
			} else if (*p == delim) {
				if (insq || indq) {
					arglen++;
					p++;
				} else {
					break;
				}
			} else if (*p == Q) {
				if (*(p+1) == Q || *(p+1) == DQ) {
					arglen++;
					p += 2;
				} else {
					arglen++;
					p++;
				}
			} else {
				arglen++;
				p++;
			}
		}
/*
 * zero length argument, skip
 */
		if (args == p) {
			p++;
		}
/*
 * long argument, malloc buffer, copy and add
 */
		else if (arglen > (ARGSIZE - 1)) {
			argtemp = malloc((unsigned) (arglen + 1));
			if (argtemp == 0) return(0);

			unquote(argtemp, args, p, quotes);
			if (sfh_argv_add(&argc, &argv, argtemp)) {
				free(argtemp);
				return(0);
			}

			free(argtemp);
		}
/*
 * short argument, copy to buffer and add
 */
		else {
			unquote(arg, args, p, quotes); 
			if (sfh_argv_add(&argc, &argv, arg)) return(0);
		}

		args = p;
	}

	return(argv);
}

/*
 *	sfh_argv_quote
 * 
 *	Function:	- quote string
 *			- allocates space for quoted string
 *			  which must be freed by the caller
 *	Accepts:	- string
 *			- array of quotes (quote, single quote, double quote)
 *	Returns:	- quoted string or NULL (error)
 */
char *
sfh_argv_quote(XMPI_CONST char *s, char quotes[])
{
	char        	*qs;
	int         	len = strlen(s);
	int         	nq = 0;
	int         	i, j;

	/* find number of \ quotes needed */
	for (i = 0; i < len; i++) {
		if (s[i] == DQ || s[i] == Q) {
			nq++;
		}
	}

	qs = malloc(len+nq+3);
	qs[0] = DQ;

	for (j = 1, i = 0; i < len; i++) {
		if (s[i] == DQ) {
			qs[j++] = Q;
			qs[j++] = DQ;
		} else if (s[i] == Q) {
			qs[j++] = Q;
			qs[j++] = Q;
		} else {
			qs[j++] = s[i];
		}
	}

	qs[j++] = DQ;
	qs[j] = 0;

	return(qs);
}


/*
 * backwards compatibility
 */
int addarg(int *argc, char ***argv, char *arg)
{ return(sfh_argv_add(argc, argv, arg)); }

int argvadd(int *argc, char ***argv, char *arg)
{ return(sfh_argv_add(argc, argv, arg)); }

void argvfree(char **argv)
{ sfh_argv_free(argv); }

char **argvbreak(char *args, int delim)
{ return(sfh_argv_break(args, delim)); }

int argvcount(char **argv)
{ return(sfh_argv_count(argv)); }

char *argvglue(char **argv, int delim, unint maxlen)
{ return(sfh_argv_glue(argv, delim, maxlen)); }

unint argvlen(char **argv)
{ return(sfh_argv_len(argv)); }

char **argvdup(char **argv)
{ return(sfh_argv_dup(argv)); }