📄 iwordexp.c
字号:
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_word (value ? strlen (value) : 0, ¶m_length[20], 10, 0)); 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;}static intinternal_functionparse_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; 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); }}static intinternal_functionparse_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 intinternal_functionparse_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. */voidiwordfree (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() */intiwordexp (const char *words, wordexp_t *pwordexp, 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 = *pwordexp; if (flags & WRDE_REUSE) { /* Minimal implementation of WRDE_REUSE for now */ iwordfree (pwordexp); old_word.we_wordv = NULL; } if ((flags & WRDE_APPEND) == 0) { pwordexp->we_wordc = 0; if (flags & WRDE_DOOFFS) { pwordexp->we_wordv = calloc (1 + pwordexp->we_offs, sizeof (char *)); if (pwordexp->we_wordv == NULL) { error = WRDE_NOSPACE; goto do_error; } } else { pwordexp->we_wordv = calloc (1, sizeof (char *)); if (pwordexp->we_wordv == NULL) { error = WRDE_NOSPACE; goto do_error; } pwordexp->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, pwordexp, 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, pwordexp, ifs, ifs_white); if (error) goto do_error; break; case '"': ++words_offset; error = parse_dquote (&word, &word_length, &max_length, words, &words_offset, flags, pwordexp, ifs, ifs_white); if (error) goto do_error; if (!word_length) { error = w_addword (pwordexp, 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 (pwordexp, NULL); if (error) return error; } break; case '~': error = parse_tilde (&word, &word_length, &max_length, words, &words_offset, pwordexp->we_wordc); if (error) goto do_error; break; case '*': case '[': case '?': error = parse_glob (&word, &word_length, &max_length, words, &words_offset, flags, pwordexp, 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 (pwordexp, 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 (pwordexp, word);do_error: /* Error: * free memory used (unless error is WRDE_NOSPACE), and * set pwordexp members back to what they were. */ if (word != NULL) free (word); if (error == WRDE_NOSPACE) return WRDE_NOSPACE; if ((flags & WRDE_APPEND) == 0) iwordfree (pwordexp); *pwordexp = old_word; return error;}static char *itoa (unsigned long long int value, char *buflim, unsigned int base, int upper_case){/* This actually only does base 10 but it'll do for the purpose of wordexp */char s[64];char *begin;sprintf(s, "%lld", value);begin = (char *)((long)buflim - strlen(s));strcpy(begin, s);return begin;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -