xref: /illumos-gate/usr/src/cmd/xargs/xargs.c (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
23  * Copyright 2012 DEY Storage Systems, Inc.
24  * Copyright (c) 2018, Joyent, Inc.
25  *
26  * Portions of this file developed by DEY Storage Systems, Inc. are licensed
27  * under the terms of the Common Development and Distribution License (CDDL)
28  * version 1.0 only.  The use of subsequent versions of the License are
29  * is specifically prohibited unless those terms are not in conflict with
30  * version 1.0 of the License.  You can find this license on-line at
31  * http://www.illumos.org/license/CDDL
32  */
33 /*
34  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
35  * Use is subject to license terms.
36  */
37 
38 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
39 /*	  All Rights Reserved	*/
40 
41 
42 #include <stdio.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <stdlib.h>
50 #include <limits.h>
51 #include <wchar.h>
52 #include <locale.h>
53 #include <langinfo.h>
54 #include <stropts.h>
55 #include <poll.h>
56 #include <errno.h>
57 #include <stdarg.h>
58 #include <sys/fork.h>
59 #include "getresponse.h"
60 
61 #define	HEAD	0
62 #define	TAIL	1
63 #define	FALSE 0
64 #define	TRUE 1
65 #define	MAXSBUF 255
66 #define	MAXIBUF 512
67 #define	MAXINSERTS 5
68 #define	BUFSIZE LINE_MAX
69 #define	MAXARGS 255
70 #define	INSPAT_STR	"{}"	/* default replstr string for -[Ii]	*/
71 #define	FORK_RETRY	5
72 
73 #define	QBUF_STARTLEN 255  /* start size of growable string buffer */
74 #define	QBUF_INC 100	   /* how much to grow a growable string by */
75 
76 /* We use these macros to help make formatting look "consistent" */
77 #define	EMSG(s)		ermsg(gettext(s "\n"))
78 #define	EMSG2(s, a)	ermsg(gettext(s "\n"), a)
79 #define	PERR(s)		perror(gettext("xargs: " s))
80 
81 /* Some common error messages */
82 
83 #define	LIST2LONG	"Argument list too long"
84 #define	ARG2LONG	"A single argument was greater than %d bytes"
85 #define	MALLOCFAIL	"Memory allocation failure"
86 #define	CORRUPTFILE	"Corrupt input file"
87 #define	WAITFAIL	"Wait failure"
88 #define	CHILDSIG	"Child killed with signal %d"
89 #define	CHILDFAIL	"Command could not continue processing data"
90 #define	FORKFAIL	"Could not fork child"
91 #define	EXECFAIL	"Could not exec command"
92 #define	MISSQUOTE	"Missing quote"
93 #define	BADESCAPE	"Incomplete escape"
94 #define	IBUFOVERFLOW	"Insert buffer overflow"
95 #define	NOCHILDSLOT	"No free child slot available"
96 
97 #define	_(x)	gettext(x)
98 
99 static wctype_t	blank;
100 static char	*arglist[MAXARGS+1];
101 static char	argbuf[BUFSIZE * 2 + 1];
102 static char	lastarg[BUFSIZE + 1];
103 static char	**ARGV = arglist;
104 static char	*LEOF = "_";
105 static char	*INSPAT = INSPAT_STR;
106 static char	ins_buf[MAXIBUF];
107 static char	*p_ibuf;
108 
109 static struct inserts {
110 	char	**p_ARGV;	/* where to put newarg ptr in arg list */
111 	char	*p_skel;	/* ptr to arg template */
112 } saveargv[MAXINSERTS];
113 
114 static int	PROMPT = -1;
115 static int	BUFLIM = BUFSIZE;
116 static int	MAXPROCS = 1;
117 static int	N_ARGS = 0;
118 static int	N_args = 0;
119 static int	N_lines = 0;
120 static int	DASHX = FALSE;
121 static int	MORE = TRUE;
122 static int	PER_LINE = FALSE;
123 static int	LINE_CONT = FALSE;
124 static int	EAT_LEAD = FALSE;
125 static int	ERR = FALSE;
126 static int	OK = TRUE;
127 static int	LEGAL = FALSE;
128 static int	TRACE = FALSE;
129 static int	INSERT = FALSE;
130 static int	ZERO = FALSE;
131 static int	linesize = 0;
132 static int	ibufsize = 0;
133 static int	exitstat = 0;	/* our exit status			*/
134 static int	mac;		/* modified argc, after parsing		*/
135 static char	**mav;		/* modified argv, after parsing		*/
136 static int	n_inserts;	/* # of insertions.			*/
137 static pid_t	*procs;		/* pids of children			*/
138 static int	n_procs;	/* # of child processes.		*/
139 
140 /* our usage message:							*/
141 #define	USAGEMSG "Usage: xargs: [-t] [-p] [-0] [-e[eofstr]] [-E eofstr] "\
142 	"[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-P maxprocs] "\
143 	"[-s size] [cmd [args ...]]\n"
144 
145 static int	echoargs();
146 static wint_t	getwchr(char *, size_t *);
147 static void	lcall(char *sub, char **subargs);
148 static void	addibuf(struct inserts *p);
149 static void	ermsg(char *messages, ...);
150 static char	*addarg(char *arg);
151 static void	store_str(char **, char *, size_t);
152 static char	*getarg(char *);
153 static char	*insert(char *pattern, char *subst);
154 static void	usage();
155 static void	parseargs();
156 static int	procs_find(pid_t child);
157 static void	procs_store(pid_t child);
158 static boolean_t procs_delete(pid_t child);
159 static pid_t	procs_waitpid(boolean_t blocking, int *stat_loc);
160 static void	procs_wait(boolean_t blocking);
161 
162 int
163 main(int argc, char **argv)
164 {
165 	int	j;
166 	long	l;
167 	struct inserts *psave;
168 	int c;
169 	int	initsize;
170 	char	*cmdname, **initlist;
171 	char	*arg;
172 	char	*next;
173 	char	*eptr;
174 
175 	/* initialization */
176 	blank = wctype("blank");
177 	n_inserts = 0;
178 	psave = saveargv;
179 	(void) setlocale(LC_ALL, "");
180 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D		*/
181 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't		*/
182 #endif
183 	(void) textdomain(TEXT_DOMAIN);
184 	if (init_yes() < 0) {
185 		ermsg(_(ERR_MSG_INIT_YES), strerror(errno));
186 		exit(1);
187 	}
188 
189 	parseargs(argc, argv);
190 
191 	/* handling all of xargs arguments:				*/
192 	while ((c = getopt(mac, mav, "0tpe:E:I:i:L:l:n:P:s:x")) != EOF) {
193 		switch (c) {
194 		case '0':
195 			ZERO = TRUE;
196 			break;
197 
198 		case 't':	/* -t: turn trace mode on		*/
199 			TRACE = TRUE;
200 			break;
201 
202 		case 'p':	/* -p: turn on prompt mode.		*/
203 			if ((PROMPT = open("/dev/tty", O_RDONLY)) == -1) {
204 				PERR("can't read from tty for -p");
205 			} else {
206 				TRACE = TRUE;
207 			}
208 			break;
209 
210 		case 'e':
211 			/*
212 			 * -e[eofstr]: set/disable end-of-file.
213 			 * N.B. that an argument *isn't* required here; but
214 			 * parseargs forced an argument if not was given.  The
215 			 * forced argument is the default...
216 			 */
217 			LEOF = optarg; /* can be empty */
218 			break;
219 
220 		case 'E':
221 			/*
222 			 * -E eofstr: change end-of-file string.
223 			 * eofstr *is* required here, but can be empty:
224 			 */
225 			LEOF = optarg;
226 			break;
227 
228 		case 'I':
229 			/* -I replstr: Insert mode. replstr *is* required. */
230 			INSERT = PER_LINE = LEGAL = EAT_LEAD = TRUE;
231 			LINE_CONT = FALSE;
232 			N_ARGS = 0;
233 			INSPAT = optarg;
234 			if (*optarg == '\0') {
235 				ermsg(_("Option requires an argument: -%c\n"),
236 				    c);
237 			}
238 			break;
239 
240 		case 'i':
241 			/*
242 			 * -i [replstr]: insert mode, with *optional* replstr.
243 			 * N.B. that an argument *isn't* required here; if
244 			 * it's not given, then the string INSPAT_STR will
245 			 * be assumed.
246 			 *
247 			 * Since getopts(3C) doesn't handle the case of an
248 			 * optional variable argument at all, we have to
249 			 * parse this by hand:
250 			 */
251 
252 			INSERT = PER_LINE = LEGAL = EAT_LEAD = TRUE;
253 			LINE_CONT = FALSE;
254 			N_ARGS = 0;
255 			if ((optarg != NULL) && (*optarg != '\0')) {
256 				INSPAT = optarg;
257 			} else {
258 				/*
259 				 * here, there is no next argument. so
260 				 * we reset INSPAT to the INSPAT_STR.
261 				 * we *have* to do this, as -i/I may have
262 				 * been given previously, and XCU4 requires
263 				 * that only "the last one specified takes
264 				 * effect".
265 				 */
266 				INSPAT = INSPAT_STR;
267 			}
268 			break;
269 
270 		case 'L':
271 			/*
272 			 * -L number: # of times cmd is executed
273 			 * number *is* required here:
274 			 */
275 			PER_LINE = LINE_CONT = TRUE;
276 			N_ARGS = 0;
277 			INSERT = EAT_LEAD = FALSE;
278 			if ((PER_LINE = atoi(optarg)) <= 0) {
279 				ermsg(_("#lines must be positive int: %s\n"),
280 				    optarg);
281 			}
282 			break;
283 
284 		case 'l':
285 			/*
286 			 * -l [number]: # of times cmd is executed
287 			 * N.B. that an argument *isn't* required here; if
288 			 * it's not given, then 1 is assumed.
289 			 *
290 			 * parseargs handles the optional arg processing.
291 			 */
292 
293 			PER_LINE = LINE_CONT = LEGAL = TRUE;
294 			N_ARGS = 0;
295 			INSERT = EAT_LEAD = FALSE;
296 
297 			if ((optarg != NULL) && (*optarg != '\0')) {
298 				if ((PER_LINE = atoi(optarg)) <= 0)
299 					PER_LINE = 1;
300 			}
301 			break;
302 
303 		case 'n':	/* -n number: # stdin args		*/
304 			/*
305 			 * -n number: # stdin args.
306 			 * number *is* required here:
307 			 */
308 			if ((N_ARGS = atoi(optarg)) <= 0) {
309 				ermsg(_("#args must be positive int: %s\n"),
310 				    optarg);
311 			} else {
312 				LEGAL = DASHX || N_ARGS == 1;
313 				INSERT = PER_LINE = LINE_CONT = FALSE;
314 			}
315 			break;
316 
317 		case 'P':	/* -P maxprocs: # of child processses	*/
318 			errno = 0;
319 			l = strtol(optarg, &eptr, 10);
320 			if (*eptr != '\0' || errno != 0) {
321 				ermsg(_("failed to parse maxprocs (-P): %s\n"),
322 				    optarg);
323 				break;
324 			}
325 
326 			if (l < 0) {
327 				ermsg(_("maximum number of processes (-P) "
328 				    "cannot be negative\n"));
329 				break;
330 			}
331 
332 			/*
333 			 * Come up with an upper bound that'll probably fit in
334 			 * memory.
335 			 */
336 			if (l == 0 || l > ((INT_MAX / sizeof (pid_t) >> 1))) {
337 				l = INT_MAX / sizeof (pid_t) >> 1;
338 			}
339 			MAXPROCS = (int)l;
340 			break;
341 
342 		case 's':	/* -s size: set max size of each arg list */
343 			BUFLIM = atoi(optarg);
344 			if (BUFLIM > BUFSIZE || BUFLIM <= 0) {
345 				ermsg(_("0 < max-cmd-line-size <= %d: %s\n"),
346 				    BUFSIZE, optarg);
347 			}
348 			break;
349 
350 		case 'x':	/* -x: terminate if args > size limit	*/
351 			DASHX = LEGAL = TRUE;
352 			break;
353 
354 		default:
355 			/*
356 			 * bad argument. complain and get ready to die.
357 			 */
358 			usage();
359 			exit(2);
360 			break;
361 		}
362 	}
363 
364 	/*
365 	 * if anything called ermsg(), something screwed up, so
366 	 * we exit early.
367 	 */
368 	if (OK == FALSE) {
369 		usage();
370 		exit(2);
371 	}
372 
373 	/*
374 	 * we're finished handling xargs's options, so now pick up
375 	 * the command name (if any), and it's options.
376 	 */
377 
378 
379 	mac -= optind;	/* dec arg count by what we've processed	*/
380 	mav += optind;	/* inc to current mav				*/
381 
382 	procs = calloc(MAXPROCS, sizeof (pid_t));
383 	if (procs == NULL) {
384 		PERR(MALLOCFAIL);
385 		exit(1);
386 	}
387 
388 	if (mac <= 0) {	/* if there're no more args to process,	*/
389 		cmdname = "/usr/bin/echo";	/* our default command	*/
390 		*ARGV++ = addarg(cmdname);	/* use the default cmd.	*/
391 	} else {	/* otherwise keep parsing rest of the string.	*/
392 		/*
393 		 * note that we can't use getopts(3C), and *must* parse
394 		 * this by hand, as we don't know apriori what options the
395 		 * command will take.
396 		 */
397 		cmdname = *mav;	/* get the command name	*/
398 
399 
400 		/* pick up the remaining args from the command line:	*/
401 		while ((OK == TRUE) && (mac-- > 0)) {
402 			/*
403 			 * while we haven't crapped out, and there's
404 			 * work to do:
405 			 */
406 			if (INSERT && ! ERR) {
407 				if (strstr(*mav, INSPAT) != NULL) {
408 					if (++n_inserts > MAXINSERTS) {
409 						ermsg(_("too many args "
410 						    "with %s\n"), INSPAT);
411 						ERR = TRUE;
412 					}
413 					psave->p_ARGV = ARGV;
414 					(psave++)->p_skel = *mav;
415 				}
416 			}
417 			*ARGV++ = addarg(*mav++);
418 		}
419 	}
420 
421 	/* pick up args from standard input */
422 
423 	initlist = ARGV;
424 	initsize = linesize;
425 	lastarg[0] = '\0';
426 
427 	while (OK) {
428 		N_args = 0;
429 		N_lines = 0;
430 		ARGV = initlist;
431 		linesize = initsize;
432 		next = argbuf;
433 
434 		while (MORE || (lastarg[0] != '\0')) {
435 			int l;
436 
437 			if (*lastarg != '\0') {
438 				arg = strcpy(next, lastarg);
439 				*lastarg = '\0';
440 			} else if ((arg = getarg(next)) == NULL) {
441 				break;
442 			}
443 
444 			l = strlen(arg) + 1;
445 			linesize += l;
446 			next += l;
447 
448 			/* Inserts are handled specially later. */
449 			if ((n_inserts == 0) && (linesize >= BUFLIM)) {
450 				/*
451 				 * Legal indicates hard fail if the list is
452 				 * truncated due to size.  So fail, or if we
453 				 * cannot create any list because it would be
454 				 * too big.
455 				 */
456 				if (LEGAL || N_args == 0) {
457 					EMSG(LIST2LONG);
458 					procs_wait(B_TRUE);
459 					exit(2);
460 					/* NOTREACHED */
461 				}
462 
463 				/*
464 				 * Otherwise just save argument for later.
465 				 */
466 				(void) strcpy(lastarg, arg);
467 				break;
468 			}
469 
470 			*ARGV++ = arg;
471 
472 			N_args++;
473 
474 			if ((PER_LINE && (N_lines >= PER_LINE)) ||
475 			    (N_ARGS && (N_args >= N_ARGS))) {
476 				break;
477 			}
478 
479 
480 			if ((ARGV - arglist) == MAXARGS) {
481 				break;
482 			}
483 		}
484 
485 		*ARGV = NULL;
486 		if (N_args == 0) {
487 			/* Reached the end with no more work. */
488 			break;
489 		}
490 
491 		/* insert arg if requested */
492 
493 		if (!ERR && INSERT) {
494 
495 			p_ibuf = ins_buf;
496 			ARGV--;
497 			j = ibufsize = 0;
498 			for (psave = saveargv; ++j <= n_inserts; ++psave) {
499 				addibuf(psave);
500 				if (ERR)
501 					break;
502 			}
503 		}
504 		*ARGV = NULL;
505 
506 		if (n_inserts > 0) {
507 			/*
508 			 * if we've done any insertions, re-calculate the
509 			 * linesize. bomb out if we've exceeded our length.
510 			 */
511 			linesize = 0;
512 			for (ARGV = arglist; *ARGV != NULL; ARGV++) {
513 				linesize += strlen(*ARGV) + 1;
514 			}
515 			if (linesize >= BUFLIM) {
516 				EMSG(LIST2LONG);
517 				procs_wait(B_TRUE);
518 				exit(2);
519 				/* NOTREACHED */
520 			}
521 		}
522 
523 		/* exec command */
524 
525 		if (!ERR) {
526 			if (!MORE &&
527 			    (PER_LINE && N_lines == 0 || N_ARGS && N_args == 0))
528 				exit(exitstat);
529 			OK = TRUE;
530 			j = TRACE ? echoargs() : TRUE;
531 			if (j) {
532 				/*
533 				 * for xcu4, all invocations of cmdname must
534 				 * return 0, in order for us to return 0.
535 				 * so if we have a non-zero status here,
536 				 * quit immediately.
537 				 */
538 				(void) lcall(cmdname, arglist);
539 			}
540 		}
541 	}
542 
543 	procs_wait(B_TRUE);
544 
545 	if (OK)
546 		return (exitstat);
547 
548 	/*
549 	 * if exitstat was set, to match XCU4 complience,
550 	 * return that value, otherwise, return 1.
551 	 */
552 	return (exitstat ? exitstat : 1);
553 }
554 
555 static char *
556 addarg(char *arg)
557 {
558 	linesize += (strlen(arg) + 1);
559 	return (arg);
560 }
561 
562 
563 static void
564 store_str(char **buffer, char *str, size_t len)
565 {
566 	(void) memcpy(*buffer, str, len);
567 	(*buffer)[len] = '\0';
568 	*buffer += len;
569 }
570 
571 
572 static char *
573 getarg(char *arg)
574 {
575 	char	*xarg = arg;
576 	wchar_t	c = 0;
577 	char	mbc[MB_LEN_MAX];
578 	size_t	len;
579 	int	escape = 0;
580 	int	inquote = 0;
581 	int	last = 0;
582 
583 	arg[0] = '\0';
584 
585 	while (MORE) {
586 
587 		len = 0;
588 		last = c;
589 		c = getwchr(mbc, &len);
590 
591 		if (((arg - xarg) + len) > BUFLIM) {
592 			EMSG2(ARG2LONG, BUFLIM);
593 			exit(2);
594 			ERR = TRUE;
595 			return (NULL);
596 		}
597 
598 		switch (c) {
599 		case '\n':
600 			if (ZERO) {
601 				store_str(&arg, mbc, len);
602 				continue;
603 			}
604 			/*
605 			 * NB: Some other versions rip off all of the trailing
606 			 * blanks.  The spec only claims that this should
607 			 * be done for a single blank.  We follow the spec.
608 			 */
609 			if (LINE_CONT && iswctype(last, blank)) {
610 				len = 0;
611 				*arg = 0;
612 				continue;
613 			}
614 			/* FALLTHRU */
615 
616 		case '\0':
617 		case WEOF:	/* Note WEOF == EOF */
618 
619 			if (escape) {
620 				EMSG(BADESCAPE);
621 				ERR = TRUE;
622 				return (NULL);
623 			}
624 			if (inquote) {
625 				EMSG(MISSQUOTE);
626 				ERR = TRUE;
627 				return (NULL);
628 			}
629 
630 			N_lines++;
631 			break;
632 
633 		case '"':
634 			if (ZERO || escape || (inquote == 1)) {
635 				/* treat it literally */
636 				escape = 0;
637 				store_str(&arg, mbc, len);
638 
639 			} else if (inquote == 2) {
640 				/* terminating double quote */
641 				inquote = 0;
642 
643 			} else {
644 				/* starting quoted string */
645 				inquote = 2;
646 			}
647 			continue;
648 
649 		case '\'':
650 			if (ZERO || escape || (inquote == 2)) {
651 				/* treat it literally */
652 				escape = 0;
653 				store_str(&arg, mbc, len);
654 
655 			} else if (inquote == 1) {
656 				/* terminating single quote */
657 				inquote = 0;
658 
659 			} else {
660 				/* starting quoted string */
661 				inquote = 1;
662 			}
663 			continue;
664 
665 		case '\\':
666 			/*
667 			 * Any unquoted character can be escaped by
668 			 * preceding it with a backslash.
669 			 */
670 			if (ZERO || inquote || escape) {
671 				escape = 0;
672 				store_str(&arg, mbc, len);
673 			} else {
674 				escape = 1;
675 			}
676 			continue;
677 
678 		default:
679 			/* most times we will just want to store it */
680 			if (inquote || escape || ZERO || !iswctype(c, blank)) {
681 				escape = 0;
682 				store_str(&arg, mbc, len);
683 				continue;
684 			}
685 			if (EAT_LEAD && last == 0) {
686 				c = 0;		/* Roll it back */
687 				continue;
688 			}
689 			if (PER_LINE) {
690 				store_str(&arg, mbc, len);
691 				continue;
692 			}
693 
694 			/* unquoted blank without special handling */
695 			break;
696 		}
697 
698 		/*
699 		 * At this point we are processing a complete argument.
700 		 */
701 		if (strcmp(xarg, LEOF) == 0 && *LEOF != '\0') {
702 			MORE = FALSE;
703 			return (NULL);
704 		}
705 		if (c == WEOF) {
706 			MORE = FALSE;
707 		}
708 		if (xarg[0] == '\0')
709 			continue;
710 		break;
711 	}
712 
713 	return (xarg[0] == '\0' ? NULL : xarg);
714 }
715 
716 /*
717  * ermsg():	print out an error message, and indicate failure globally.
718  *
719  *	Assumes that message has already been gettext()'d. It would be
720  *	nice if we could just do the gettext() here, but we can't, since
721  *	since xgettext(1) wouldn't be able to pick up our error message.
722  */
723 /* PRINTFLIKE1 */
724 static void
725 ermsg(char *messages, ...)
726 {
727 	va_list	ap;
728 
729 	va_start(ap, messages);
730 
731 	(void) fprintf(stderr, "xargs: ");
732 	(void) vfprintf(stderr, messages, ap);
733 
734 	va_end(ap);
735 	OK = FALSE;
736 }
737 
738 static int
739 echoargs(void)
740 {
741 	char	**anarg;
742 	char	**tanarg;	/* tmp ptr			*/
743 	int	i;
744 	char	reply[LINE_MAX];
745 
746 	tanarg = anarg = arglist-1;
747 
748 	/*
749 	 * write out each argument, separated by a space. the tanarg
750 	 * nonsense is for xcu4 testsuite compliance - so that an
751 	 * extra space isn't echoed after the last argument.
752 	 */
753 	while (*++anarg) {		/* while there's an argument	*/
754 		++tanarg;		/* follow anarg			*/
755 		(void) write(2, *anarg, strlen(*anarg));
756 
757 		if (*++tanarg) {	/* if there's another argument:	*/
758 			(void) write(2, " ", 1); /* add a space		*/
759 			--tanarg;	/* reset back to anarg		*/
760 		}
761 	}
762 	if (PROMPT == -1) {
763 		(void) write(2, "\n", 1);
764 		return (TRUE);
765 	}
766 
767 	(void) write(2, "?...", 4);	/* ask the user for input	*/
768 
769 	for (i = 0; i < LINE_MAX && read(PROMPT, &reply[i], 1) > 0; i++) {
770 		if (reply[i] == '\n') {
771 			if (i == 0)
772 				return (FALSE);
773 			break;
774 		}
775 	}
776 	if (i < LINE_MAX)
777 		reply[i] = '\0';
778 	else
779 		reply[LINE_MAX - 1] = '\0';
780 
781 	/* flush remainder of line if necessary */
782 	if (i == LINE_MAX) {
783 		char	bitbucket;
784 
785 		while ((read(PROMPT, &bitbucket, 1) > 0) && (bitbucket != '\n'))
786 			;
787 	}
788 
789 	return (yes_check(reply));
790 }
791 
792 
793 static char *
794 insert(char *pattern, char *subst)
795 {
796 	static char	buffer[MAXSBUF+1];
797 	int		len, ipatlen;
798 	char	*pat;
799 	char	*bufend;
800 	char	*pbuf;
801 
802 	len = strlen(subst);
803 	ipatlen = strlen(INSPAT) - 1;
804 	pat = pattern - 1;
805 	pbuf = buffer;
806 	bufend = &buffer[MAXSBUF];
807 
808 	while (*++pat) {
809 		if (strncmp(pat, INSPAT, ipatlen + 1) == 0) {
810 			if (pbuf + len >= bufend) {
811 				break;
812 			} else {
813 				(void) strcpy(pbuf, subst);
814 				pat += ipatlen;
815 				pbuf += len;
816 			}
817 		} else {
818 			*pbuf++ = *pat;
819 			if (pbuf >= bufend)
820 				break;
821 		}
822 	}
823 
824 	if (!*pat) {
825 		*pbuf = '\0';
826 		return (buffer);
827 	} else {
828 		ermsg(gettext("Maximum argument size with insertion via %s's "
829 		    "exceeded\n"), INSPAT);
830 		ERR = TRUE;
831 		return (NULL);
832 	}
833 }
834 
835 
836 static void
837 addibuf(struct inserts	*p)
838 {
839 	char	*newarg, *skel, *sub;
840 	int		l;
841 
842 	skel = p->p_skel;
843 	sub = *ARGV;
844 	newarg = insert(skel, sub);
845 	if (ERR)
846 		return;
847 
848 	l = strlen(newarg) + 1;
849 	if ((ibufsize += l) > MAXIBUF) {
850 		EMSG(IBUFOVERFLOW);
851 		ERR = TRUE;
852 	}
853 	(void) strcpy(p_ibuf, newarg);
854 	*(p->p_ARGV) = p_ibuf;
855 	p_ibuf += l;
856 }
857 
858 
859 /*
860  * getwchr():	get the next wide character.
861  * description:
862  *	we get the next character from stdin.  This returns WEOF if no
863  *	character is present.  If ZERO is set, it gets a single byte instead
864  *	a wide character.
865  */
866 static wint_t
867 getwchr(char *mbc, size_t *sz)
868 {
869 	size_t		i;
870 	int		c;
871 	wchar_t		wch;
872 
873 	i = 0;
874 	while (i < MB_CUR_MAX) {
875 
876 		if ((c = fgetc(stdin)) == EOF) {
877 
878 			if (i == 0) {
879 				/* TRUE EOF has been reached */
880 				return (WEOF);
881 			}
882 
883 			/*
884 			 * We have some characters in our buffer still so it
885 			 * must be an invalid character right before EOF.
886 			 */
887 			break;
888 		}
889 		mbc[i++] = (char)c;
890 
891 		/* If this succeeds then we are done */
892 		if (ZERO) {
893 			*sz = i;
894 			return ((char)c);
895 		}
896 		if (mbtowc(&wch, mbc, i) != -1) {
897 			*sz = i;
898 			return ((wint_t)wch);
899 		}
900 	}
901 
902 	/*
903 	 * We have now encountered an illegal character sequence.
904 	 * There is nothing much we can do at this point but
905 	 * return an error.  If we attempt to recover we may in fact
906 	 * return garbage as arguments, from the customer's point
907 	 * of view.  After all what if they are feeding us a file
908 	 * generated in another locale?
909 	 */
910 	errno = EILSEQ;
911 	PERR(CORRUPTFILE);
912 	exit(1);
913 	/* NOTREACHED */
914 }
915 
916 
917 static void
918 lcall(char *sub, char **subargs)
919 {
920 	int	retry = 0;
921 	pid_t	child;
922 
923 	for (;;) {
924 		switch (child = forkx(FORK_NOSIGCHLD)) {
925 		default:
926 			procs_store(child);
927 			/*
928 			 * Note, if we have used up all of our slots, then this
929 			 * call may end up blocking.
930 			 */
931 			procs_wait(B_FALSE);
932 			return;
933 		case 0:
934 			(void) execvp(sub, subargs);
935 			PERR(EXECFAIL);
936 			if (errno == EACCES)
937 				exit(126);
938 			exit(127);
939 			/* NOTREACHED */
940 		case -1:
941 			if (errno != EAGAIN && retry++ < FORK_RETRY) {
942 				PERR(FORKFAIL);
943 				exit(123);
944 			}
945 			(void) sleep(1);
946 		}
947 	}
948 }
949 
950 /*
951  * Return the index of child in the procs array.
952  */
953 static int
954 procs_find(pid_t child)
955 {
956 	int	i;
957 
958 	for (i = 0; i < MAXPROCS; i++) {
959 		if (procs[i] == child) {
960 			return (i);
961 		}
962 	}
963 
964 	return (-1);
965 }
966 
967 static void
968 procs_store(pid_t child)
969 {
970 	int	i;
971 
972 	i = procs_find(0);
973 	if (i < 0) {
974 		EMSG(NOCHILDSLOT);
975 		exit(1);
976 	}
977 	procs[i] = child;
978 	n_procs++;
979 }
980 
981 static boolean_t
982 procs_delete(pid_t child)
983 {
984 	int	i;
985 
986 	i = procs_find(child);
987 	if (i < 0) {
988 		return (B_FALSE);
989 	}
990 
991 	procs[i] = (pid_t)0;
992 	n_procs--;
993 
994 	return (B_TRUE);
995 }
996 
997 static pid_t
998 procs_waitpid(boolean_t blocking, int *stat_loc)
999 {
1000 	pid_t	child;
1001 	int	options;
1002 
1003 	if (n_procs == 0) {
1004 		errno = ECHILD;
1005 		return (-1);
1006 	}
1007 
1008 	options = 0;
1009 	if (!blocking) {
1010 		options |= WNOHANG;
1011 	}
1012 
1013 	while ((child = waitpid((pid_t)-1, stat_loc, options)) > 0) {
1014 		if (procs_delete(child)) {
1015 			break;
1016 		}
1017 	}
1018 
1019 	return (child);
1020 }
1021 
1022 static void
1023 procs_wait(boolean_t blocking)
1024 {
1025 	pid_t	child;
1026 	int	stat_loc;
1027 
1028 	/*
1029 	 * If we currently have filled all of our slots, then we need to block
1030 	 * further execution.
1031 	 */
1032 	if (n_procs >= MAXPROCS)
1033 		blocking = B_TRUE;
1034 	while ((child = procs_waitpid(blocking, &stat_loc)) > 0) {
1035 		if (WIFSIGNALED(stat_loc)) {
1036 			EMSG2(CHILDSIG, WTERMSIG(stat_loc));
1037 			exit(125);
1038 			/* NOTREACHED */
1039 		} else if ((WEXITSTATUS(stat_loc) & 0377) == 0377) {
1040 			EMSG(CHILDFAIL);
1041 			exit(124);
1042 			/* NOTREACHED */
1043 		} else {
1044 			exitstat |= WEXITSTATUS(stat_loc);
1045 		}
1046 	}
1047 
1048 	if (child == (pid_t)(-1) && errno != ECHILD) {
1049 		EMSG(WAITFAIL);
1050 		exit(122);
1051 		/* NOTREACHED */
1052 	}
1053 }
1054 
1055 static void
1056 usage()
1057 {
1058 	ermsg(_(USAGEMSG));
1059 	OK = FALSE;
1060 }
1061 
1062 
1063 
1064 /*
1065  * parseargs():		modify the args
1066  *	since the -e, -i and -l flags all take optional subarguments,
1067  *	and getopts(3C) is clueless about this nonsense, we change the
1068  *	our local argument count and strings to separate this out,
1069  *	and make it easier to handle via getopts(3c).
1070  *
1071  *	-e	-> "-e ""
1072  *	-e3	-> "-e "3"
1073  *	-Estr	-> "-E "str"
1074  *	-i	-> "-i "{}"
1075  *	-irep	-> "-i "rep"
1076  *	-l	-> "-l "1"
1077  *	-l10	-> "-l "10"
1078  *
1079  *	since the -e, -i and -l flags all take optional subarguments,
1080  */
1081 static void
1082 parseargs(int ac, char **av)
1083 {
1084 	int i;			/* current argument			*/
1085 	int cflag;		/* 0 = not processing cmd arg		*/
1086 
1087 	if ((mav = malloc((ac * 2 + 1) * sizeof (char *))) == NULL) {
1088 		PERR(MALLOCFAIL);
1089 		exit(1);
1090 	}
1091 
1092 	/* for each argument, see if we need to change things:		*/
1093 	for (i = mac = cflag = 0; (av[i] != NULL) && i < ac; i++, mac++) {
1094 		if ((mav[mac] = strdup(av[i])) == NULL) {
1095 			PERR(MALLOCFAIL);
1096 			exit(1);
1097 		}
1098 
1099 		/* -- has been found or argument list is fully processes */
1100 		if (cflag)
1101 			continue;
1102 
1103 		/*
1104 		 * if we're doing special processing, and we've got a flag
1105 		 */
1106 		else if ((av[i][0] == '-') && (av[i][1] != '\0')) {
1107 			char	*def;
1108 
1109 			switch (av[i][1]) {
1110 			case	'e':
1111 				def = ""; /* -e with no arg turns off eof */
1112 				goto process_special;
1113 			case	'i':
1114 				def = INSPAT_STR;
1115 				goto process_special;
1116 			case	'l':
1117 				def = "1";
1118 process_special:
1119 				/*
1120 				 * if there's no sub-option, we *must* add
1121 				 * a default one. this is because xargs must
1122 				 * be able to distinguish between a valid
1123 				 * suboption, and a command name.
1124 				 */
1125 				if (av[i][2] == '\0') {
1126 					mav[++mac] = strdup(def);
1127 				} else {
1128 					/* clear out our version: */
1129 					mav[mac][2] = '\0';
1130 					mav[++mac] = strdup(&av[i][2]);
1131 				}
1132 				if (mav[mac] == NULL) {
1133 					PERR(MALLOCFAIL);
1134 					exit(1);
1135 				}
1136 				break;
1137 
1138 			/* flags with required subarguments:		*/
1139 
1140 			/*
1141 			 * there are two separate cases here. either the
1142 			 * flag can have the normal XCU4 handling
1143 			 * (of the form: -X subargument); or it can have
1144 			 * the old solaris 2.[0-4] handling (of the
1145 			 * form: -Xsubargument). in order to maintain
1146 			 * backwards compatibility, we must support the
1147 			 * latter case. we handle the latter possibility
1148 			 * first so both the old solaris way of handling
1149 			 * and the new XCU4 way of handling things are allowed.
1150 			 */
1151 			case	'n':	/* FALLTHROUGH			*/
1152 			case	'P':	/* FALLTHROUGH			*/
1153 			case	's':	/* FALLTHROUGH			*/
1154 			case	'E':	/* FALLTHROUGH			*/
1155 			case	'I':	/* FALLTHROUGH			*/
1156 			case	'L':
1157 				/*
1158 				 * if the second character isn't null, then
1159 				 * the user has specified the old syntax.
1160 				 * we move the subargument into our
1161 				 * mod'd argument list.
1162 				 */
1163 				if (av[i][2] != '\0') {
1164 					/* first clean things up:	*/
1165 					mav[mac][2] = '\0';
1166 
1167 					/* now add the separation:	*/
1168 					++mac;	/* inc to next mod'd arg */
1169 					if ((mav[mac] = strdup(&av[i][2])) ==
1170 					    NULL) {
1171 						PERR(MALLOCFAIL);
1172 						exit(1);
1173 					}
1174 					break;
1175 				}
1176 				i++;
1177 				mac++;
1178 
1179 				if (av[i] == NULL) {
1180 					mav[mac] = NULL;
1181 					return;
1182 				}
1183 				if ((mav[mac] = strdup(av[i])) == NULL) {
1184 					PERR(MALLOCFAIL);
1185 					exit(1);
1186 				}
1187 				break;
1188 
1189 			/* flags */
1190 			case 'p' :
1191 			case 't' :
1192 			case 'x' :
1193 			case '0' :
1194 				break;
1195 
1196 			case '-' :
1197 			default:
1198 				/*
1199 				 * here we've hit the cmd argument. so
1200 				 * we'll stop special processing, as the
1201 				 * cmd may have a "-i" etc., argument,
1202 				 * and we don't want to add a "" to it.
1203 				 */
1204 				cflag = 1;
1205 				break;
1206 			}
1207 		} else if (i > 0) {	/* if we're not the 1st arg	*/
1208 			/*
1209 			 * if it's not a flag, then it *must* be the cmd.
1210 			 * set cflag, so we don't mishandle the -[eil] flags.
1211 			 */
1212 			cflag = 1;
1213 		}
1214 	}
1215 
1216 	mav[mac] = NULL;
1217 }
1218