⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordexp.c

📁 Axis 221 camera embedded programing interface
💻 C
📖 第 1 页 / 共 4 页
字号:
			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,									&param_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;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -