| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254 | /* vi: set sw=4 ts=4: *//* POSIX.2 wordexp implementation.   Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.   This file is part of the GNU C Library.   Contributed by Tim Waugh <tim@cyberelk.demon.co.uk>.   The GNU C Library is free software; you can redistribute it and/or   modify it under the terms of the GNU Library General Public License as   published by the Free Software Foundation; either version 2 of the   License, or (at your option) any later version.   The GNU C Library is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   Library General Public License for more details.   You should have received a copy of the GNU Library General Public   License along with the GNU C Library; see the file COPYING.LIB.  If not,   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,   Boston, MA 02111-1307, USA.  */#define mempcpy __mempcpy#define stpcpy __stpcpy#define strndup __strndup#define strspn __strspn#define unsetenv __unsetenv#define waitpid __waitpid#define kill __kill#define getuid __getuid#define execve __execve#define _GNU_SOURCE#include <sys/cdefs.h>#include <sys/types.h>#include <sys/wait.h>#include <fcntl.h>#include <paths.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <pwd.h>#include <errno.h>#include <assert.h>#include <fnmatch.h>#include <glob.h>#include <wordexp.h>#define __WORDEXP_FULL//#undef __WORDEXP_FULL/* * This is a recursive-descent-style word expansion routine. *//* These variables are defined and initialized in the startup code.  *///extern int __libc_argc;//extern char **__libc_argv;/* FIXME!!!! */int __libc_argc;char **__libc_argv;/* Some forward declarations */static int parse_dollars(char **word, size_t * word_length,						 size_t * max_length, const char *words,						 size_t * offset, int flags, wordexp_t * pwordexp,						 const char *ifs, const char *ifs_white,						 int quoted);static int parse_backtick(char **word, size_t * word_length,						  size_t * max_length, const char *words,						  size_t * offset, int flags, wordexp_t * pwordexp,						  const char *ifs, const char *ifs_white);static int parse_dquote(char **word, size_t * word_length,						size_t * max_length, const char *words,						size_t * offset, int flags, wordexp_t * pwordexp,						const char *ifs, const char *ifs_white);/* The w_*() functions manipulate word lists. */#define W_CHUNK	(100)/* Result of w_newword will be ignored if it's the last word. */static inline char *w_newword(size_t * actlen, size_t * maxlen){	*actlen = *maxlen = 0;	return NULL;}/* Add a character to the buffer, allocating room for it if needed.  */static inline char *w_addchar(char *buffer, size_t * actlen,							  size_t * maxlen, char ch)	 /* (lengths exclude trailing zero) */{	if (*actlen == *maxlen) {		char *old_buffer = buffer;		assert(buffer == NULL || *maxlen != 0);		*maxlen += W_CHUNK;		buffer = realloc(buffer, 1 + *maxlen);		if (buffer == NULL)			free(old_buffer);	}	if (buffer != NULL) {		buffer[*actlen] = ch;		buffer[++(*actlen)] = '\0';	}	return buffer;}#define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )static char *w_addmem(char *buffer, size_t * actlen, size_t * maxlen,					  const char *str, size_t len){	/* Add a string to the buffer, allocating room for it if needed.	 */	if (*actlen + len > *maxlen) {		char *old_buffer = buffer;		assert(buffer == NULL || *maxlen != 0);		*maxlen += MAX(2 * len, W_CHUNK);		buffer = realloc(old_buffer, 1 + *maxlen);		if (buffer == NULL)			free(old_buffer);	}	if (buffer != NULL) {		*((char *) mempcpy(&buffer[*actlen], str, len)) = '\0';		*actlen += len;	}	return buffer;}/* Add a string to the buffer, allocating room for it if needed.  */static char *w_addstr(char *buffer, size_t * actlen, size_t * maxlen,					  const char *str)	 /* (lengths exclude trailing zero) */{	size_t len;	assert(str != NULL);		/* w_addstr only called from this file */	len = __strlen(str);	return w_addmem(buffer, actlen, maxlen, str, len);}/* Add a word to the wordlist */static int w_addword(wordexp_t * pwordexp, char *word){	size_t num_p;	char **new_wordv;	/* Internally, NULL acts like "".  Convert NULLs to "" before	 * the caller sees them.	 */	if (word == NULL) {		word = __strdup("");		if (word == NULL)			goto no_space;	}	num_p = 2 + pwordexp->we_wordc + pwordexp->we_offs;	new_wordv = realloc(pwordexp->we_wordv, sizeof(char *) * num_p);	if (new_wordv != NULL) {		pwordexp->we_wordv = new_wordv;		pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc++] = word;		pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc] = NULL;		return 0;	}  no_space:	return WRDE_NOSPACE;}/* The parse_*() functions should leave *offset being the offset in 'words' * to the last character processed. */static intparse_backslash(char **word, size_t * word_length, size_t * max_length,				const char *words, size_t * offset){	/* We are poised _at_ a backslash, not in quotes */	switch (words[1 + *offset]) {	case 0:		/* Backslash is last character of input words */		return WRDE_SYNTAX;	case '\n':		++(*offset);		break;	default:		*word = w_addchar(*word, word_length, max_length, words[1 + *offset]);		if (*word == NULL)			return WRDE_NOSPACE;		++(*offset);		break;	}	return 0;}static intparse_qtd_backslash(char **word, size_t * word_length, size_t * max_length,					const char *words, size_t * offset){	/* We are poised _at_ a backslash, inside quotes */	switch (words[1 + *offset]) {	case 0:		/* Backslash is last character of input words */		return WRDE_SYNTAX;	case '\n':		++(*offset);		break;	case '$':	case '`':	case '"':	case '\\':		*word =			w_addchar(*word, word_length, max_length, words[1 + *offset]);		if (*word == NULL)			return WRDE_NOSPACE;		++(*offset);		break;	default:		*word = w_addchar(*word, word_length, max_length, words[*offset]);		if (*word != NULL)			*word =				w_addchar(*word, word_length, max_length,						  words[1 + *offset]);		if (*word == NULL)			return WRDE_NOSPACE;		++(*offset);		break;	}	return 0;}static intparse_tilde(char **word, size_t * word_length, size_t * max_length,			const char *words, size_t * offset, size_t wordc){	/* We are poised _at_ a tilde */	size_t i;	if (*word_length != 0) {		if (!((*word)[*word_length - 1] == '=' && wordc == 0)) {			if (!((*word)[*word_length - 1] == ':'				  && __strchr(*word, '=') && wordc == 0)) {				*word = w_addchar(*word, word_length, max_length, '~');				return *word ? 0 : WRDE_NOSPACE;			}		}	}	for (i = 1 + *offset; words[i]; i++) {		if (words[i] == ':' || words[i] == '/' || words[i] == ' ' ||			words[i] == '\t' || words[i] == 0)			break;		if (words[i] == '\\') {			*word = w_addchar(*word, word_length, max_length, '~');			return *word ? 0 : WRDE_NOSPACE;		}	}	if (i == 1 + *offset) {		/* Tilde appears on its own */		uid_t uid;		struct passwd pwd, *tpwd;		int buflen = 1000;		char *home;		char *buffer;		int result;		/* POSIX.2 says ~ expands to $HOME and if HOME is unset the		   results are unspecified.  We do a lookup on the uid if		   HOME is unset. */		home = __getenv("HOME");		if (home != NULL) {			*word = w_addstr(*word, word_length, max_length, home);			if (*word == NULL)				return WRDE_NOSPACE;		} else {			uid = getuid();			buffer = alloca(buflen);			while ((result = getpwuid_r(uid, &pwd, buffer, buflen, &tpwd)) 					!= 0 && errno == ERANGE) 			{				buflen += 1000;				buffer = alloca(buflen);			}			if (result == 0 && tpwd != NULL && pwd.pw_dir != NULL) {				*word = w_addstr(*word, word_length, max_length, pwd.pw_dir);				if (*word == NULL)					return WRDE_NOSPACE;			} else {				*word = w_addchar(*word, word_length, max_length, '~');				if (*word == NULL)					return WRDE_NOSPACE;			}		}	} else {		/* Look up user name in database to get home directory */		char *user = strndup(&words[1 + *offset], i - (1 + *offset));		struct passwd pwd, *tpwd;		int buflen = 1000;		char *buffer = alloca(buflen);		int result;		while ((result = getpwnam_r(user, &pwd, buffer, buflen, &tpwd)) != 0			   && errno == ERANGE) {			buflen += 1000;			buffer = alloca(buflen);		}		if (result == 0 && tpwd != NULL && pwd.pw_dir)			*word = w_addstr(*word, word_length, max_length, pwd.pw_dir);		else {			/* (invalid login name) */			*word = w_addchar(*word, word_length, max_length, '~');			if (*word != NULL)				*word = w_addstr(*word, word_length, max_length, user);		}		*offset = i - 1;	}	return *word ? 0 : WRDE_NOSPACE;}static intdo_parse_glob(const char *glob_word, char **word, size_t * word_length,			  size_t * max_length, wordexp_t * pwordexp, const char *ifs,			  const char *ifs_white){	int error;	int match;	glob_t globbuf;	error = glob(glob_word, GLOB_NOCHECK, NULL, &globbuf);	if (error != 0) {		/* We can only run into memory problems.  */		assert(error == GLOB_NOSPACE);		return WRDE_NOSPACE;	}	if (ifs && !*ifs) {		/* No field splitting allowed. */		assert(globbuf.gl_pathv[0] != NULL);		*word = w_addstr(*word, word_length, max_length, globbuf.gl_pathv[0]);		for (match = 1; match < globbuf.gl_pathc && *word != NULL; ++match) {			*word = w_addchar(*word, word_length, max_length, ' ');			if (*word != NULL)				*word = w_addstr(*word, word_length, max_length,								 globbuf.gl_pathv[match]);		}		globfree(&globbuf);		return *word ? 0 : WRDE_NOSPACE;	}	assert(ifs == NULL || *ifs != '\0');	if (*word != NULL) {		free(*word);		*word = w_newword(word_length, max_length);	}	for (match = 0; match < globbuf.gl_pathc; ++match) {		char *matching_word = __strdup(globbuf.gl_pathv[match]);		if (matching_word == NULL || w_addword(pwordexp, matching_word)) {			globfree(&globbuf);			return WRDE_NOSPACE;		}	}	globfree(&globbuf);	return 0;}static intparse_glob(char **word, size_t * word_length, size_t * max_length,		   const char *words, size_t * offset, int flags,		   wordexp_t * pwordexp, const char *ifs, const char *ifs_white){	/* We are poised just after a '*', a '[' or a '?'. */	int error = WRDE_NOSPACE;	int quoted = 0;				/* 1 if singly-quoted, 2 if doubly */	int i;	wordexp_t glob_list;		/* List of words to glob */	glob_list.we_wordc = 0;	glob_list.we_wordv = NULL;	glob_list.we_offs = 0;	for (; words[*offset] != '\0'; ++*offset) {		if ((ifs && __strchr(ifs, words[*offset])) ||			(!ifs && __strchr(" \t\n", words[*offset])))			/* Reached IFS */			break;		/* Sort out quoting */		if (words[*offset] == '\'') {			if (quoted == 0) {				quoted = 1;				continue;			} else if (quoted == 1) {				quoted = 0;				continue;			}		} else if (words[*offset] == '"') {			if (quoted == 0) {				quoted = 2;				continue;			} else if (quoted == 2) {				quoted = 0;				continue;			}		}		/* Sort out other special characters */		if (quoted != 1 && words[*offset] == '$') {			error = parse_dollars(word, word_length, max_length, words,								  offset, flags, &glob_list, ifs,								  ifs_white, quoted == 2);			if (error)				goto tidy_up;			continue;		} else if (words[*offset] == '\\') {			if (quoted)				error = parse_qtd_backslash(word, word_length, max_length,											words, offset);			else				error = parse_backslash(word, word_length, max_length,										words, offset);			if (error)				goto tidy_up;			continue;		}		*word = w_addchar(*word, word_length, max_length, words[*offset]);		if (*word == NULL)			goto tidy_up;	}	/* Don't forget to re-parse the character we stopped at. */	--*offset;	/* Glob the words */	error = w_addword(&glob_list, *word);	*word = w_newword(word_length, max_length);	for (i = 0; error == 0 && i < glob_list.we_wordc; i++)		error = do_parse_glob(glob_list.we_wordv[i], word, word_length,							  max_length, pwordexp, ifs, ifs_white);	/* Now tidy up */  tidy_up:	wordfree(&glob_list);	return error;}static intparse_squote(char **word, size_t * word_length, size_t * max_length,			 const char *words, size_t * offset){	/* We are poised just after a single quote */	for (; words[*offset]; ++(*offset)) {		if (words[*offset] != '\'') {			*word = w_addchar(*word, word_length, max_length, words[*offset]);			if (*word == NULL)				return WRDE_NOSPACE;		} else			return 0;	}	/* Unterminated string */	return WRDE_SYNTAX;}#ifdef __WORDEXP_FULLstatic int eval_expr(char *expr, long int *result);static char *_itoa(unsigned long long int value, char *buflim){	sprintf(buflim, "%llu", value);	return buflim;}/* Functions to evaluate an arithmetic expression */static int eval_expr_val(char **expr, long int *result){	int sgn = +1;	char *digit;	/* Skip white space */	for (digit = *expr; digit && *digit && isspace(*digit); ++digit);	switch (*digit) {	case '(':		/* Scan for closing paren */		for (++digit; **expr && **expr != ')'; ++(*expr));		/* Is there one? */		if (!**expr)			return WRDE_SYNTAX;		*(*expr)++ = 0;		if (eval_expr(digit, result))			return WRDE_SYNTAX;		return 0;	case '+':					/* Positive value */		++digit;		break;	case '-':					/* Negative value */		++digit;		sgn = -1;		break;	default:		if (!isdigit(*digit))			return WRDE_SYNTAX;	}	*result = 0;	for (; *digit && isdigit(*digit); ++digit)		*result = (*result * 10) + (*digit - '0');	*expr = digit;	*result *= sgn;	return 0;}static int eval_expr_multdiv(char **expr, long int *result){	long int arg;	/* Read a Value */	if (eval_expr_val(expr, result) != 0)		return WRDE_SYNTAX;	while (**expr) {		/* Skip white space */		for (; *expr && **expr && isspace(**expr); ++(*expr));		if (**expr == '*') {			++(*expr);			if (eval_expr_val(expr, &arg) != 0)				return WRDE_SYNTAX;			*result *= arg;		} else if (**expr == '/') {			++(*expr);			if (eval_expr_val(expr, &arg) != 0)				return WRDE_SYNTAX;			*result /= arg;		} else			break;	}	return 0;}static int eval_expr(char *expr, long int *result){	long int arg;	/* Read a Multdiv */	if (eval_expr_multdiv(&expr, result) != 0)		return WRDE_SYNTAX;	while (*expr) {		/* Skip white space */		for (; expr && *expr && isspace(*expr); ++expr);		if (*expr == '+') {			++expr;			if (eval_expr_multdiv(&expr, &arg) != 0)				return WRDE_SYNTAX;			*result += arg;		} else if (*expr == '-') {			++expr;			if (eval_expr_multdiv(&expr, &arg) != 0)				return WRDE_SYNTAX;			*result -= arg;		} else			break;	}	return 0;}static intparse_arith(char **word, size_t * word_length, size_t * max_length,			const char *words, size_t * offset, int flags, int bracket){	/* We are poised just after "$((" or "$[" */	int error;	int paren_depth = 1;	size_t expr_length;	size_t expr_maxlen;	char *expr;	expr = w_newword(&expr_length, &expr_maxlen);	for (; words[*offset]; ++(*offset)) {		switch (words[*offset]) {		case '$':			error = parse_dollars(&expr, &expr_length, &expr_maxlen,								  words, offset, flags, NULL, NULL, NULL,								  1);			/* The ``1'' here is to tell parse_dollars not to			 * split the fields.			 */			if (error) {				free(expr);				return error;			}			break;		case '`':			(*offset)++;			error = parse_backtick(&expr, &expr_length, &expr_maxlen,								   words, offset, flags, NULL, NULL, NULL);			/* The first NULL here is to tell parse_backtick not to			 * split the fields.			 */			if (error) {				free(expr);				return error;			}			break;		case '\\':			error = parse_qtd_backslash(&expr, &expr_length, &expr_maxlen,										words, offset);			if (error) {				free(expr);				return error;			}			/* I think that a backslash within an			 * arithmetic expansion is bound to			 * cause an error sooner or later anyway though.			 */			break;		case ')':			if (--paren_depth == 0) {				char result[21];	/* 21 = ceil(log10(2^64)) + 1 */				long int numresult = 0;				long long int convertme;				if (bracket || words[1 + *offset] != ')') {					free(expr);					return WRDE_SYNTAX;				}				++(*offset);				/* Go - evaluate. */				if (*expr && eval_expr(expr, &numresult) != 0) {					free(expr);					return WRDE_SYNTAX;				}				if (numresult < 0) {					convertme = -numresult;					*word = w_addchar(*word, word_length, max_length, '-');					if (!*word) {						free(expr);						return WRDE_NOSPACE;					}				} else					convertme = numresult;				result[20] = '\0';				*word = w_addstr(*word, word_length, max_length,								 _itoa(convertme, &result[20]));				free(expr);				return *word ? 0 : WRDE_NOSPACE;			}			expr =				w_addchar(expr, &expr_length, &expr_maxlen,						  words[*offset]);			if (expr == NULL)				return WRDE_NOSPACE;			break;		case ']':			if (bracket && paren_depth == 1) {				char result[21];	/* 21 = ceil(log10(2^64)) + 1 */				long int numresult = 0;				/* Go - evaluate. */				if (*expr && eval_expr(expr, &numresult) != 0) {					free(expr);					return WRDE_SYNTAX;				}				result[20] = '\0';				*word = w_addstr(*word, word_length, max_length,								 _itoa(numresult, &result[20]));				free(expr);				return *word ? 0 : WRDE_NOSPACE;			}			free(expr);			return WRDE_SYNTAX;		case '\n':		case ';':		case '{':		case '}':			free(expr);			return WRDE_BADCHAR;		case '(':			++paren_depth;		default:			expr =				w_addchar(expr, &expr_length, &expr_maxlen,						  words[*offset]);			if (expr == NULL)				return WRDE_NOSPACE;		}	}	/* Premature end */	free(expr);	return WRDE_SYNTAX;}/* Function called by child process in exec_comm() */static voidexec_comm_child(char *comm, int *fildes, int showerr, int noexec){	const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };	/* Execute the command, or just check syntax? */	if (noexec)		args[1] = "-nc";	/* Redirect output.  */	dup2(fildes[1], 1);	__close(fildes[1]);	/* Redirect stderr to /dev/null if we have to.  */	if (showerr == 0) {		int fd;		__close(2);		fd = __open(_PATH_DEVNULL, O_WRONLY);		if (fd >= 0 && fd != 2) {			dup2(fd, 2);			__close(fd);		}	}	/* Make sure the subshell doesn't field-split on our behalf. */	unsetenv("IFS");	__close(fildes[0]);	execve(_PATH_BSHELL, (char *const *) args, __environ);	/* Bad.  What now?  */	abort();}/* Function to execute a command and retrieve the results *//* pwordexp contains NULL if field-splitting is forbidden */static intexec_comm(char *comm, char **word, size_t * word_length,		  size_t * max_length, int flags, wordexp_t * pwordexp,		  const char *ifs, const char *ifs_white){	int fildes[2];	int bufsize = 128;	int buflen;	int i;	int status = 0;	size_t maxnewlines = 0;	char *buffer;	pid_t pid;	/* Don't fork() unless necessary */	if (!comm || !*comm)		return 0;	if (pipe(fildes))		/* Bad */		return WRDE_NOSPACE;	if ((pid = fork()) < 0) {		/* Bad */		__close(fildes[0]);		__close(fildes[1]);		return WRDE_NOSPACE;	}	if (pid == 0)		exec_comm_child(comm, fildes, (flags & WRDE_SHOWERR), 0);	/* Parent */	__close(fildes[1]);	buffer = alloca(bufsize);	if (!pwordexp)		/* Quoted - no field splitting */	{		while (1) {			if ((buflen = __read(fildes[0], buffer, bufsize)) < 1) {				if (waitpid(pid, &status, WNOHANG) == 0)					continue;				if ((buflen = __read(fildes[0], buffer, bufsize)) < 1)					break;			}			maxnewlines += buflen;			*word = w_addmem(*word, word_length, max_length, buffer, buflen);			if (*word == NULL)				goto no_space;		}	} else		/* Not quoted - split fields */	{		int copying = 0;		/* 'copying' is:		 *  0 when searching for first character in a field not IFS white space		 *  1 when copying the text of a field		 *  2 when searching for possible non-whitespace IFS		 *  3 when searching for non-newline after copying field		 */		while (1) {			if ((buflen = __read(fildes[0], buffer, bufsize)) < 1) {				if (waitpid(pid, &status, WNOHANG) == 0)					continue;				if ((buflen = __read(fildes[0], buffer, bufsize)) < 1)					break;			}			for (i = 0; i < buflen; ++i) {				if (__strchr(ifs, buffer[i]) != NULL) {					/* Current character is IFS */					if (__strchr(ifs_white, buffer[i]) == NULL) {						/* Current character is IFS but not whitespace */						if (copying == 2) {							/*            current character							 *                   |							 *                   V							 * eg: text<space><comma><space>moretext							 *							 * So, strip whitespace IFS (like at the start)							 */							copying = 0;							continue;						}						copying = 0;						/* fall through and delimit field.. */					} else {						if (buffer[i] == '\n') {							/* Current character is (IFS) newline */							/* If copying a field, this is the end of it,							   but maybe all that's left is trailing newlines.							   So start searching for a non-newline. */							if (copying == 1)								copying = 3;							continue;						} else {							/* Current character is IFS white space, but							   not a newline */							/* If not either copying a field or searching							   for non-newline after a field, ignore it */							if (copying != 1 && copying != 3)								continue;							/* End of field (search for non-ws IFS afterwards) */							copying = 2;						}					}					/* First IFS white space (non-newline), or IFS non-whitespace.					 * Delimit the field.  Nulls are converted by w_addword. */					if (w_addword(pwordexp, *word) == WRDE_NOSPACE)						goto no_space;					*word = w_newword(word_length, max_length);					maxnewlines = 0;					/* fall back round the loop.. */				} else {					/* Not IFS character */					if (copying == 3) {						/* Nothing but (IFS) newlines since the last field,						   so delimit it here before starting new word */						if (w_addword(pwordexp, *word) == WRDE_NOSPACE)							goto no_space;						*word = w_newword(word_length, max_length);					}					copying = 1;					if (buffer[i] == '\n')	/* happens if newline not in IFS */						maxnewlines++;					else						maxnewlines = 0;					*word = w_addchar(*word, word_length, max_length,									  buffer[i]);					if (*word == NULL)						goto no_space;				}			}		}	}	/* Chop off trailing newlines (required by POSIX.2)  */	/* Ensure we don't go back further than the beginning of the	   substitution (i.e. remove maxnewlines bytes at most) */	while (maxnewlines-- != 0 &&		   *word_length > 0 && (*word)[*word_length - 1] == '\n') {		(*word)[--*word_length] = '\0';		/* If the last word was entirely newlines, turn it into a new word		 * which can be ignored if there's nothing following it. */		if (*word_length == 0) {			free(*word);			*word = w_newword(word_length, max_length);			break;		}	}	__close(fildes[0]);	/* Check for syntax error (re-execute but with "-n" flag) */	if (buflen < 1 && status != 0) {		if ((pid = fork()) < 0) {			/* Bad */			return WRDE_NOSPACE;		}		if (pid == 0) {			fildes[0] = fildes[1] = -1;			exec_comm_child(comm, fildes, 0, 1);		}		if (waitpid(pid, &status, 0) == pid && status != 0)			return WRDE_SYNTAX;	}	return 0;  no_space:	kill(pid, SIGKILL);	waitpid(pid, NULL, 0);	__close(fildes[0]);	return WRDE_NOSPACE;}static intparse_comm(char **word, size_t * word_length, size_t * max_length,		   const char *words, size_t * offset, int flags,		   wordexp_t * pwordexp, const char *ifs, const char *ifs_white){	/* We are poised just after "$(" */	int paren_depth = 1;	int error = 0;	int quoted = 0;				/* 1 for singly-quoted, 2 for doubly-quoted */	size_t comm_length;	size_t comm_maxlen;	char *comm = w_newword(&comm_length, &comm_maxlen);	for (; words[*offset]; ++(*offset)) {		switch (words[*offset]) {		case '\'':			if (quoted == 0)				quoted = 1;			else if (quoted == 1)				quoted = 0;			break;		case '"':			if (quoted == 0)				quoted = 2;			else if (quoted == 2)				quoted = 0;			break;		case ')':			if (!quoted && --paren_depth == 0) {				/* Go -- give script to the shell */				if (comm) {					error = exec_comm(comm, word, word_length, max_length,									  flags, pwordexp, ifs, ifs_white);					free(comm);				}				return error;			}			/* This is just part of the script */			break;		case '(':			if (!quoted)				++paren_depth;		}		comm = w_addchar(comm, &comm_length, &comm_maxlen, words[*offset]);		if (comm == NULL)			return WRDE_NOSPACE;	}	/* Premature end */	if (comm)		free(comm);	return WRDE_SYNTAX;}static intparse_backtick(char **word, size_t * word_length, size_t * max_length,			   const char *words, size_t * offset, int flags,			   wordexp_t * pwordexp, const char *ifs,			   const char *ifs_white){	/* We are poised just after "`" */	int error;	int squoting = 0;	size_t comm_length;	size_t comm_maxlen;	char *comm = w_newword(&comm_length, &comm_maxlen);	for (; words[*offset]; ++(*offset)) {		switch (words[*offset]) {		case '`':			/* Go -- give the script to the shell */			error = exec_comm(comm, word, word_length, max_length, flags,							  pwordexp, ifs, ifs_white);			free(comm);			return error;		case '\\':			if (squoting) {				error = parse_qtd_backslash(&comm, &comm_length, &comm_maxlen,										words, offset);				if (error) {					free(comm);					return error;				}				break;			}			++(*offset);			error = parse_backslash(&comm, &comm_length, &comm_maxlen, words,								offset);			if (error) {				free(comm);				return error;			}			break;		case '\'':			squoting = 1 - squoting;		default:			comm = w_addchar(comm, &comm_length, &comm_maxlen,						  words[*offset]);			if (comm == NULL)				return WRDE_NOSPACE;		}	}	/* Premature end */	free(comm);	return WRDE_SYNTAX;}static intparse_param(char **word, size_t * word_length, size_t * max_length,			const char *words, size_t * offset, int flags,			wordexp_t * pwordexp, const char *ifs, const char *ifs_white,			int quoted){	/* We are poised just after "$" */	enum action {		ACT_NONE,		ACT_RP_SHORT_LEFT = '#',		ACT_RP_LONG_LEFT = 'L',		ACT_RP_SHORT_RIGHT = '%',		ACT_RP_LONG_RIGHT = 'R',		ACT_NULL_ERROR = '?',		ACT_NULL_SUBST = '-',		ACT_NONNULL_SUBST = '+',		ACT_NULL_ASSIGN = '='	};	size_t env_length;	size_t env_maxlen;	size_t pat_length;	size_t pat_maxlen;	size_t start = *offset;	char *env;	char *pattern;	char *value = NULL;	enum action action = ACT_NONE;	int depth = 0;	int colon_seen = 0;	int seen_hash = 0;	int free_value = 0;	int pattern_is_quoted = 0;	/* 1 for singly-quoted, 2 for doubly-quoted */	int error;	int special = 0;	char buffer[21];	int brace = words[*offset] == '{';	env = w_newword(&env_length, &env_maxlen);	pattern = w_newword(&pat_length, &pat_maxlen);	if (brace)		++ * offset;	/* First collect the parameter name. */	if (words[*offset] == '#') {		seen_hash = 1;		if (!brace)			goto envsubst;		++*offset;	}	if (isalpha(words[*offset]) || words[*offset] == '_') {		/* Normal parameter name. */		do {			env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);			if (env == NULL)				goto no_space;		}		while (isalnum(words[++*offset]) || words[*offset] == '_');	} else if (isdigit(words[*offset])) {		/* Numeric parameter name. */		special = 1;		do {			env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);			if (env == NULL)				goto no_space;			if (!brace)				goto envsubst;		}		while (isdigit(words[++*offset]));	} else if (__strchr("*@$", words[*offset]) != NULL) {		/* Special parameter. */		special = 1;		env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);		if (env == NULL)			goto no_space;		++*offset;	} else {		if (brace)			goto syntax;	}	if (brace) {		/* Check for special action to be applied to the value. */		switch (words[*offset]) {		case '}':			/* Evaluate. */			goto envsubst;		case '#':			action = ACT_RP_SHORT_LEFT;			if (words[1 + *offset] == '#') {				++*offset;				action = ACT_RP_LONG_LEFT;			}			break;		case '%':			action = ACT_RP_SHORT_RIGHT;			if (words[1 + *offset] == '%') {				++*offset;				action = ACT_RP_LONG_RIGHT;			}			break;		case ':':			if (__strchr("-=?+", words[1 + *offset]) == NULL)				goto syntax;			colon_seen = 1;			action = words[++*offset];			break;		case '-':		case '=':		case '?':		case '+':			action = words[*offset];			break;		default:			goto syntax;		}		/* Now collect the pattern, but don't expand it yet. */		++*offset;		for (; words[*offset]; ++(*offset)) {			switch (words[*offset]) {			case '{':				if (!pattern_is_quoted)					++depth;				break;			case '}':				if (!pattern_is_quoted) {					if (depth == 0)						goto envsubst;					--depth;				}				break;			case '\\':				if (pattern_is_quoted)					/* Quoted; treat as normal character. */					break;				/* Otherwise, it's an escape: next character is literal. */				if (words[++*offset] == '\0')					goto syntax;				pattern = w_addchar(pattern, &pat_length, &pat_maxlen, '\\');				if (pattern == NULL)					goto no_space;				break;			case '\'':				if (pattern_is_quoted == 0)					pattern_is_quoted = 1;				else if (pattern_is_quoted == 1)					pattern_is_quoted = 0;				break;			case '"':				if (pattern_is_quoted == 0)					pattern_is_quoted = 2;				else if (pattern_is_quoted == 2)					pattern_is_quoted = 0;				break;			}			pattern = w_addchar(pattern, &pat_length, &pat_maxlen,								words[*offset]);			if (pattern == NULL)				goto no_space;		}	}	/* End of input string -- remember to reparse the character that we	 * stopped at.  */	--(*offset);  envsubst:	if (words[start] == '{' && words[*offset] != '}')		goto syntax;	if (env == NULL) {		if (seen_hash) {			/* $# expands to the number of positional parameters */			buffer[20] = '\0';			value = _itoa(__libc_argc - 1, &buffer[20]);			seen_hash = 0;		} else {			/* Just $ on its own */			*offset = start - 1;			*word = w_addchar(*word, word_length, max_length, '$');			return *word ? 0 : WRDE_NOSPACE;		}	}	/* Is it a numeric parameter? */	else if (isdigit(env[0])) {		int n = atoi(env);		if (n >= __libc_argc)			/* Substitute NULL. */			value = NULL;		else			/* Replace with appropriate positional parameter. */			value = __libc_argv[n];	}	/* Is it a special parameter? */	else if (special) {		/* Is it `$$'? */		if (*env == '$') {			buffer[20] = '\0';			value = _itoa(__getpid(), &buffer[20]);		}		/* Is it `${#*}' or `${#@}'? */		else if ((*env == '*' || *env == '@') && seen_hash) {			buffer[20] = '\0';			value = _itoa(__libc_argc > 0 ? __libc_argc - 1 : 0,							   &buffer[20]);			*word = w_addstr(*word, word_length, max_length, value);			free(env);			if (pattern)				free(pattern);			return *word ? 0 : WRDE_NOSPACE;		}		/* Is it `$*' or `$@' (unquoted) ? */		else if (*env == '*' || (*env == '@' && !quoted)) {			size_t plist_len = 0;			int p;			char *end;			/* Build up value parameter by parameter (copy them) */			for (p = 1; __libc_argv[p]; ++p)				plist_len += __strlen(__libc_argv[p]) + 1;	/* for space */			value = malloc(plist_len);			if (value == NULL)				goto no_space;			end = value;			*end = 0;			for (p = 1; __libc_argv[p]; ++p) {				if (p > 1)					*end++ = ' ';				end = stpcpy(end, __libc_argv[p]);			}			free_value = 1;		} else {			/* Must be a quoted `$@' */			assert(*env == '@' && quoted);			/* Each parameter is a separate word ("$@") */			if (__libc_argc == 2)				value = __libc_argv[1];			else if (__libc_argc > 2) {				int p;				/* Append first parameter to current word. */				value = w_addstr(*word, word_length, max_length,								 __libc_argv[1]);				if (value == NULL || w_addword(pwordexp, value))					goto no_space;				for (p = 2; __libc_argv[p + 1]; p++) {					char *newword = __strdup(__libc_argv[p]);					if (newword == NULL || w_addword(pwordexp, newword))						goto no_space;				}				/* Start a new word with the last parameter. */				*word = w_newword(word_length, max_length);				value = __libc_argv[p];			} else {				free(env);				free(pattern);				return 0;			}		}	} else		value = __getenv(env);	if (value == NULL && (flags & WRDE_UNDEF)) {		/* Variable not defined. */		error = WRDE_BADVAL;		goto do_error;	}	if (action != ACT_NONE) {		int expand_pattern = 0;		/* First, find out if we need to expand pattern (i.e. if we will		 * use it). */		switch (action) {		case ACT_RP_SHORT_LEFT:		case ACT_RP_LONG_LEFT:		case ACT_RP_SHORT_RIGHT:		case ACT_RP_LONG_RIGHT:			/* Always expand for these. */			expand_pattern = 1;			break;		case ACT_NULL_ERROR:		case ACT_NULL_SUBST:		case ACT_NULL_ASSIGN:			if (!value || (!*value && colon_seen))				/* If param is unset, or set but null and a colon has been seen,				   the expansion of the pattern will be needed. */				expand_pattern = 1;			break;		case ACT_NONNULL_SUBST:			/* Expansion of word will be needed if parameter is set and not null,			   or set null but no colon has been seen. */			if (value && (*value || !colon_seen))				expand_pattern = 1;			break;		default:			assert(!"Unrecognised action!");		}		if (expand_pattern) {			/* We need to perform tilde expansion, parameter expansion,			   command substitution, and arithmetic expansion.  We also			   have to be a bit careful with wildcard characters, as			   pattern might be given to fnmatch soon.  To do this, we			   convert quotes to escapes. */			char *expanded;			size_t exp_len;			size_t exp_maxl;			char *p;			int quoted = 0;		/* 1: single quotes; 2: double */			expanded = w_newword(&exp_len, &exp_maxl);			for (p = pattern; p && *p; p++) {				size_t offset;				switch (*p) {				case '"':					if (quoted == 2)						quoted = 0;					else if (quoted == 0)						quoted = 2;					else						break;					continue;				case '\'':					if (quoted == 1)						quoted = 0;					else if (quoted == 0)						quoted = 1;					else						break;					continue;				case '*':				case '?':					if (quoted) {						/* Convert quoted wildchar to escaped wildchar. */						expanded = w_addchar(expanded, &exp_len,											 &exp_maxl, '\\');						if (expanded == NULL)							goto no_space;					}					break;				case '$':					offset = 0;					error = parse_dollars(&expanded, &exp_len, &exp_maxl, p,									  &offset, flags, NULL, NULL, NULL, 1);					if (error) {						if (free_value)							free(value);						if (expanded)							free(expanded);						goto do_error;					}					p += offset;					continue;				case '~':					if (quoted || exp_len)						break;					offset = 0;					error = parse_tilde(&expanded, &exp_len, &exp_maxl, p,										&offset, 0);					if (error) {						if (free_value)							free(value);						if (expanded)							free(expanded);						goto do_error;					}					p += offset;					continue;				case '\\':					expanded = w_addchar(expanded, &exp_len, &exp_maxl, '\\');					++p;					assert(*p);	/* checked when extracted initially */					if (expanded == NULL)						goto no_space;				}				expanded = w_addchar(expanded, &exp_len, &exp_maxl, *p);				if (expanded == NULL)					goto no_space;			}			if (pattern)				free(pattern);			pattern = expanded;		}		switch (action) {		case ACT_RP_SHORT_LEFT:		case ACT_RP_LONG_LEFT:		case ACT_RP_SHORT_RIGHT:		case ACT_RP_LONG_RIGHT:		{			char *p;			char c;			char *end;			if (value == NULL || pattern == NULL || *pattern == '\0')				break;			end = value + __strlen(value);			switch (action) {			case ACT_RP_SHORT_LEFT:				for (p = value; p <= end; ++p) {					c = *p;					*p = '\0';					if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {						*p = c;						if (free_value) {							char *newval = __strdup(p);							if (newval == NULL) {								free(value);								goto no_space;							}							free(value);							value = newval;						} else							value = p;						break;					}					*p = c;				}				break;			case ACT_RP_LONG_LEFT:				for (p = end; p >= value; --p) {					c = *p;					*p = '\0';					if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {						*p = c;						if (free_value) {							char *newval = __strdup(p);							if (newval == NULL) {								free(value);								goto no_space;							}							free(value);							value = newval;						} else							value = p;						break;					}					*p = c;				}				break;			case ACT_RP_SHORT_RIGHT:				for (p = end; p >= value; --p) {					if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {						char *newval;						newval = malloc(p - value + 1);						if (newval == NULL) {							if (free_value)								free(value);							goto no_space;						}						*(char *) mempcpy(newval, value, p - value) = '\0';						if (free_value)							free(value);						value = newval;						free_value = 1;						break;					}				}				break;			case ACT_RP_LONG_RIGHT:				for (p = value; p <= end; ++p) {					if (fnmatch(pattern, p, 0) != FNM_NOMATCH) {						char *newval;						newval = malloc(p - value + 1);						if (newval == NULL) {							if (free_value)								free(value);							goto no_space;						}						*(char *) mempcpy(newval, value, p - value) = '\0';						if (free_value)							free(value);						value = newval;						free_value = 1;						break;					}				}				break;			default:				break;			}			break;		}		case ACT_NULL_ERROR:			if (value && *value)				/* Substitute parameter */				break;			error = 0;			if (!colon_seen && value)				/* Substitute NULL */				;			else if (*pattern)				fprintf(stderr, "%s: %s\n", env, pattern);			else {				fprintf(stderr, "%s: parameter null or not set\n", env);				error = WRDE_BADVAL;			}			if (free_value)				free(value);			goto do_error;		case ACT_NULL_SUBST:			if (value && *value)				/* Substitute parameter */				break;			if (free_value && value)				free(value);			if (!colon_seen && value)				/* Substitute NULL */				goto success;			value = pattern ? __strdup(pattern) : pattern;			free_value = 1;			if (pattern && !value)				goto no_space;			break;		case ACT_NONNULL_SUBST:			if (value && (*value || !colon_seen)) {				if (free_value && value)					free(value);				value = pattern ? __strdup(pattern) : pattern;				free_value = 1;				if (pattern && !value)					goto no_space;				break;			}			/* Substitute NULL */			if (free_value)				free(value);			goto success;		case ACT_NULL_ASSIGN:			if (value && *value)				/* Substitute parameter */				break;			if (!colon_seen && value) {				/* Substitute NULL */				if (free_value)					free(value);				goto success;			}			if (free_value && value)				free(value);			value = pattern ? __strdup(pattern) : pattern;			free_value = 1;			if (pattern && !value)				goto no_space;			setenv(env, value, 1);			break;		default:			assert(!"Unrecognised action!");		}	}	free(env);	env = NULL;	free(pattern);	pattern = NULL;	if (seen_hash) {		char param_length[21];		param_length[20] = '\0';		*word = w_addstr(*word, word_length, max_length,						 _itoa(value ? __strlen(value) : 0,									¶m_length[20]));		if (free_value) {			assert(value != NULL);			free(value);		}		return *word ? 0 : WRDE_NOSPACE;	}	if (value == NULL)		return 0;	if (quoted || !pwordexp) {		/* Quoted - no field split */		*word = w_addstr(*word, word_length, max_length, value);		if (free_value)			free(value);		return *word ? 0 : WRDE_NOSPACE;	} else {		/* Need to field-split */		char *value_copy = __strdup(value);	/* Don't modify value */		char *field_begin = value_copy;		int seen_nonws_ifs = 0;		if (free_value)			free(value);		if (value_copy == NULL)			goto no_space;		do {			char *field_end = field_begin;			char *next_field;			/* If this isn't the first field, start a new word */			if (field_begin != value_copy) {				if (w_addword(pwordexp, *word) == WRDE_NOSPACE) {					free(value_copy);					goto no_space;				}				*word = w_newword(word_length, max_length);			}			/* Skip IFS whitespace before the field */			field_begin += strspn(field_begin, ifs_white);			if (!seen_nonws_ifs && *field_begin == 0)				/* Nothing but whitespace */				break;			/* Search for the end of the field */			field_end = field_begin + strcspn(field_begin, ifs);			/* Set up pointer to the character after end of field and			   skip whitespace IFS after it. */			next_field = field_end + strspn(field_end, ifs_white);			/* Skip at most one non-whitespace IFS character after the field */			seen_nonws_ifs = 0;			if (*next_field && __strchr(ifs, *next_field)) {				seen_nonws_ifs = 1;				next_field++;			}			/* Null-terminate it */			*field_end = 0;			/* Tag a copy onto the current word */			*word = w_addstr(*word, word_length, max_length, field_begin);			if (*word == NULL && *field_begin != '\0') {				free(value_copy);				goto no_space;			}			field_begin = next_field;		}		while (seen_nonws_ifs || *field_begin);		free(value_copy);	}	return 0;  success:	error = 0;	goto do_error;  no_space:	error = WRDE_NOSPACE;	goto do_error;  syntax:	error = WRDE_SYNTAX;  do_error:	if (env)		free(env);	if (pattern)		free(pattern);	return error;}#elsestatic inline intparse_backtick(char **word, size_t * word_length, size_t * max_length,			   const char *words, size_t * offset, int flags,			   wordexp_t * pwordexp, const char *ifs,			   const char *ifs_white){	return 0;}#endifstatic intparse_dollars(char **word, size_t * word_length, size_t * max_length,			  const char *words, size_t * offset, int flags,			  wordexp_t * pwordexp, const char *ifs, const char *ifs_white,			  int quoted){	/* We are poised _at_ "$" */	switch (words[1 + *offset]) {	case '"':	case '\'':	case 0:		*word = w_addchar(*word, word_length, max_length, '$');		return *word ? 0 : WRDE_NOSPACE;#ifdef __WORDEXP_FULL	case '(':		if (words[2 + *offset] == '(') {			/* Differentiate between $((1+3)) and $((echo);(ls)) */			int i = 3 + *offset;			int depth = 0;			while (words[i] && !(depth == 0 && words[i] == ')')) {				if (words[i] == '(')					++depth;				else if (words[i] == ')')					--depth;				++i;			}			if (words[i] == ')' && words[i + 1] == ')') {				(*offset) += 3;				/* Call parse_arith -- 0 is for "no brackets" */				return parse_arith(word, word_length, max_length, words,								   offset, flags, 0);			}		}		if (flags & WRDE_NOCMD)			return WRDE_CMDSUB;		(*offset) += 2;		return parse_comm(word, word_length, max_length, words, offset,						  flags, quoted ? NULL : pwordexp, ifs, ifs_white);	case '[':		(*offset) += 2;		/* Call parse_arith -- 1 is for "brackets" */		return parse_arith(word, word_length, max_length, words, offset,						   flags, 1);	case '{':	default:		++(*offset);			/* parse_param needs to know if "{" is there */		return parse_param(word, word_length, max_length, words, offset,						   flags, pwordexp, ifs, ifs_white, quoted);#else	default:		++(*offset);			/* parse_param needs to know if "{" is there */		return 0;#endif	}}static intparse_dquote(char **word, size_t * word_length, size_t * max_length,			 const char *words, size_t * offset, int flags,			 wordexp_t * pwordexp, const char *ifs, const char *ifs_white){	/* We are poised just after a double-quote */	int error;	for (; words[*offset]; ++(*offset)) {		switch (words[*offset]) {		case '"':			return 0;		case '$':			error = parse_dollars(word, word_length, max_length, words, offset,							  flags, pwordexp, ifs, ifs_white, 1);			/* The ``1'' here is to tell parse_dollars not to			 * split the fields.  It may need to, however ("$@").			 */			if (error)				return error;			break;		case '`':			if (flags & WRDE_NOCMD)				return WRDE_CMDSUB;			++(*offset);			error = parse_backtick(word, word_length, max_length, words,								   offset, flags, NULL, NULL, NULL);			/* The first NULL here is to tell parse_backtick not to			 * split the fields.			 */			if (error)				return error;			break;		case '\\':			error = parse_qtd_backslash(word, word_length, max_length, words,									offset);			if (error)				return error;			break;		default:			*word = w_addchar(*word, word_length, max_length, words[*offset]);			if (*word == NULL)				return WRDE_NOSPACE;		}	}	/* Unterminated string */	return WRDE_SYNTAX;}/* * wordfree() is to be called after pwordexp is finished with. */void wordfree(wordexp_t * pwordexp){	/* wordexp can set pwordexp to NULL */	if (pwordexp && pwordexp->we_wordv) {		char **wordv = pwordexp->we_wordv;		for (wordv += pwordexp->we_offs; *wordv; ++wordv)			free(*wordv);		free(pwordexp->we_wordv);		pwordexp->we_wordv = NULL;	}}/* * wordexp() */int wordexp(const char *words, wordexp_t * we, int flags){	size_t words_offset;	size_t word_length;	size_t max_length;	char *word = w_newword(&word_length, &max_length);	int error;	char *ifs;	char ifs_white[4];	wordexp_t old_word = *we;	if (flags & WRDE_REUSE) {		/* Minimal implementation of WRDE_REUSE for now */		wordfree(we);		old_word.we_wordv = NULL;	}	if ((flags & WRDE_APPEND) == 0) {		we->we_wordc = 0;		if (flags & WRDE_DOOFFS) {			we->we_wordv = calloc(1 + we->we_offs, sizeof(char *));			if (we->we_wordv == NULL) {				error = WRDE_NOSPACE;				goto do_error;			}		} else {			we->we_wordv = calloc(1, sizeof(char *));			if (we->we_wordv == NULL) {				error = WRDE_NOSPACE;				goto do_error;			}			we->we_offs = 0;		}	}	/* Find out what the field separators are.	 * There are two types: whitespace and non-whitespace.	 */	ifs = __getenv("IFS");	if (!ifs)		/* IFS unset - use <space><tab><newline>. */		ifs = __strcpy(ifs_white, " \t\n");	else {		char *ifsch = ifs;		char *whch = ifs_white;		/* Start off with no whitespace IFS characters */		ifs_white[0] = '\0';		while (*ifsch != '\0') {			if ((*ifsch == ' ') || (*ifsch == '\t') || (*ifsch == '\n')) {				/* Whitespace IFS.  See first whether it is already in our				   collection.  */				char *runp = ifs_white;				while (runp < whch && *runp != '\0' && *runp != *ifsch)					++runp;				if (runp == whch)					*whch++ = *ifsch;			}			++ifsch;		}		*whch = '\0';	}	for (words_offset = 0; words[words_offset]; ++words_offset)		switch (words[words_offset]) {		case '\\':			error = parse_backslash(&word, &word_length, &max_length, words,								&words_offset);			if (error)				goto do_error;			break;		case '$':			error = parse_dollars(&word, &word_length, &max_length, words,								  &words_offset, flags, we, ifs, ifs_white,								  0);			if (error)				goto do_error;			break;		case '`':			if (flags & WRDE_NOCMD) {				error = WRDE_CMDSUB;				goto do_error;			}			++words_offset;			error = parse_backtick(&word, &word_length, &max_length, words,								   &words_offset, flags, we, ifs,								   ifs_white);			if (error)				goto do_error;			break;		case '"':			++words_offset;			error = parse_dquote(&word, &word_length, &max_length, words,								 &words_offset, flags, we, ifs, ifs_white);			if (error)				goto do_error;			if (!word_length) {				error = w_addword(we, NULL);				if (error)					return error;			}			break;		case '\'':			++words_offset;			error = parse_squote(&word, &word_length, &max_length, words,								 &words_offset);			if (error)				goto do_error;			if (!word_length) {				error = w_addword(we, NULL);				if (error)					return error;			}			break;		case '~':			error = parse_tilde(&word, &word_length, &max_length, words,								&words_offset, we->we_wordc);			if (error)				goto do_error;			break;		case '*':		case '[':		case '?':			error = parse_glob(&word, &word_length, &max_length, words,							   &words_offset, flags, we, ifs, ifs_white);			if (error)				goto do_error;			break;		default:			/* Is it a word separator? */			if (__strchr(" \t", words[words_offset]) == NULL) {				char ch = words[words_offset];				/* Not a word separator -- but is it a valid word char? */				if (__strchr("\n|&;<>(){}", ch)) {					/* Fail */					error = WRDE_BADCHAR;					goto do_error;				}				/* "Ordinary" character -- add it to word */				word = w_addchar(word, &word_length, &max_length, ch);				if (word == NULL) {					error = WRDE_NOSPACE;					goto do_error;				}				break;			}			/* If a word has been delimited, add it to the list. */			if (word != NULL) {				error = w_addword(we, word);				if (error)					goto do_error;			}			word = w_newword(&word_length, &max_length);		}	/* End of string */	/* There was a word separator at the end */	if (word == NULL)			/* i.e. w_newword */		return 0;	/* There was no field separator at the end */	return w_addword(we, word);  do_error:	/* Error:	 *  free memory used (unless error is WRDE_NOSPACE), and	 *  set we members back to what they were.	 */	if (word != NULL)		free(word);	if (error == WRDE_NOSPACE)		return WRDE_NOSPACE;	if ((flags & WRDE_APPEND) == 0)		wordfree(we);	*we = old_word;	return error;}
 |