123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253 |
- #include <features.h>
- #include <bits/kernel-features.h>
- #include <ctype.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>
- #ifndef __ARCH_USE_MMU__
- # define fork vfork
- #endif
- #define __WORDEXP_FULL
- int attribute_hidden __libc_argc;
- char attribute_hidden **__libc_argv;
- 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);
- #define W_CHUNK (100)
- static __inline__ char *w_newword(size_t * actlen, size_t * maxlen)
- {
- *actlen = *maxlen = 0;
- return NULL;
- }
- static __inline__ char *w_addchar(char *buffer, size_t * actlen,
- size_t * maxlen, char ch)
-
- {
- 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;
- }
- #ifndef MAX
- #define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
- #endif
- static char *w_addmem(char *buffer, size_t * actlen, size_t * maxlen,
- const char *str, size_t len)
- {
-
- 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;
- }
- static char *w_addstr(char *buffer, size_t * actlen, size_t * maxlen,
- const char *str)
-
- {
- size_t len;
- assert(str != NULL);
- len = strlen(str);
- return w_addmem(buffer, actlen, maxlen, str, len);
- }
- static int w_addword(wordexp_t * pwordexp, char *word)
- {
- size_t num_p;
- char **new_wordv;
-
- 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;
- }
- static int
- parse_backslash(char **word, size_t * word_length, size_t * max_length,
- const char *words, size_t * offset)
- {
-
- switch (words[1 + *offset]) {
- case 0:
-
- 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 int
- parse_qtd_backslash(char **word, size_t * word_length, size_t * max_length,
- const char *words, size_t * offset)
- {
-
- switch (words[1 + *offset]) {
- case 0:
-
- 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 int
- parse_tilde(char **word, size_t * word_length, size_t * max_length,
- const char *words, size_t * offset, size_t wordc)
- {
-
- 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) {
-
- uid_t uid;
- struct passwd pwd, *tpwd;
- int buflen = 1000;
- char *home;
- char *buffer;
- int result;
-
- 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 {
-
- 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 {
-
- *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 int
- do_parse_glob(const char *glob_word, char **word, size_t * word_length,
- size_t * max_length, wordexp_t * pwordexp, const char *ifs
- )
- {
- int error;
- int match;
- glob_t globbuf;
- error = glob(glob_word, GLOB_NOCHECK, NULL, &globbuf);
- if (error != 0) {
-
- assert(error == GLOB_NOSPACE);
- return WRDE_NOSPACE;
- }
- if (ifs && !*ifs) {
-
- 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 int
- parse_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)
- {
-
- int error = WRDE_NOSPACE;
- int quoted = 0;
- int i;
- wordexp_t glob_list;
- 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])))
-
- break;
-
- 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;
- }
- }
-
- 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;
- }
-
- --*offset;
-
- 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 );
-
- tidy_up:
- wordfree(&glob_list);
- return error;
- }
- static int
- parse_squote(char **word, size_t * word_length, size_t * max_length,
- const char *words, size_t * offset)
- {
-
- 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;
- }
-
- return WRDE_SYNTAX;
- }
- #ifdef __WORDEXP_FULL
- static int eval_expr(char *expr, long int *result);
- static char *_itoa(unsigned long long int value, char *buf)
- {
- sprintf(buf, "%llu", value);
- return buf;
- }
- static int eval_expr_val(char **expr, long int *result)
- {
- int sgn = +1;
- char *digit;
-
- for (digit = *expr; digit && *digit && isspace(*digit); ++digit);
- switch (*digit) {
- case '(':
-
- for (++digit; **expr && **expr != ')'; ++(*expr));
-
- if (!**expr)
- return WRDE_SYNTAX;
- *(*expr)++ = 0;
- if (eval_expr(digit, result))
- return WRDE_SYNTAX;
- return 0;
- case '+':
- ++digit;
- break;
- case '-':
- ++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;
-
- if (eval_expr_val(expr, result) != 0)
- return WRDE_SYNTAX;
- while (**expr) {
-
- 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;
-
- if (eval_expr_multdiv(&expr, result) != 0)
- return WRDE_SYNTAX;
- while (*expr) {
-
- 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 int
- parse_arith(char **word, size_t * word_length, size_t * max_length,
- const char *words, size_t * offset, int flags, int bracket)
- {
-
- 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);
-
- if (error) {
- free(expr);
- return error;
- }
- break;
- case '`':
- (*offset)++;
- error = parse_backtick(&expr, &expr_length, &expr_maxlen,
- words, offset, flags, NULL, NULL, NULL);
-
- 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;
- }
-
- break;
- case ')':
- if (--paren_depth == 0) {
- char result[21];
- long int numresult = 0;
- long long int convertme;
- if (bracket || words[1 + *offset] != ')') {
- free(expr);
- return WRDE_SYNTAX;
- }
- ++(*offset);
-
- 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));
- 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];
- long int numresult = 0;
-
- 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));
- 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;
- }
- }
-
- free(expr);
- return WRDE_SYNTAX;
- }
- static void attribute_noreturn
- exec_comm_child(char *comm, int *fildes, int showerr, int noexec)
- {
- int fd;
- const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
-
- if (noexec)
- args[1] = "-nc";
-
- fd = fildes[1];
- if (likely(fd != STDOUT_FILENO)) {
- dup2(fd, STDOUT_FILENO);
- close(fd);
- }
- #if defined O_CLOEXEC && defined __UCLIBC_LINUX_SPECIFIC__ && defined __ASSUME_PIPE2
- else {
-
- fcntl (fd, F_SETFD, 0);
- }
- #endif
-
- if (showerr == 0) {
- close(STDERR_FILENO);
- fd = open(_PATH_DEVNULL, O_WRONLY);
- if (fd >= 0 && fd != STDERR_FILENO) {
- dup2(fd, 2);
- close(fd);
- }
- }
-
- unsetenv("IFS");
- if (fildes[0] != 1)
- close(fildes[0]);
- execve(_PATH_BSHELL, (char *const *) args, __environ);
-
- abort();
- }
- static int
- exec_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;
-
- if (!comm || !*comm)
- return 0;
- #if defined O_CLOEXEC && defined __UCLIBC_LINUX_SPECIFIC__ && defined __ASSUME_PIPE2
- if (pipe2(fildes, O_CLOEXEC) < 0)
-
- return WRDE_NOSPACE;
- #else
- if (pipe(fildes) < 0)
-
- return WRDE_NOSPACE;
- #endif
- if ((pid = fork()) < 0) {
-
- close(fildes[0]);
- close(fildes[1]);
- return WRDE_NOSPACE;
- }
- if (pid == 0)
- exec_comm_child(comm, fildes, (flags & WRDE_SHOWERR), 0);
-
- close(fildes[1]);
- buffer = alloca(bufsize);
- if (!pwordexp)
-
- {
- 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
-
- {
- int copying = 0;
-
- 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) {
-
- if (strchr(ifs_white, buffer[i]) == NULL) {
-
- if (copying == 2) {
-
- copying = 0;
- continue;
- }
- copying = 0;
-
- } else {
- if (buffer[i] == '\n') {
-
-
- if (copying == 1)
- copying = 3;
- continue;
- } else {
-
-
- if (copying != 1 && copying != 3)
- continue;
-
- copying = 2;
- }
- }
-
- if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
- goto no_space;
- *word = w_newword(word_length, max_length);
- maxnewlines = 0;
-
- } else {
-
- if (copying == 3) {
-
- if (w_addword(pwordexp, *word) == WRDE_NOSPACE)
- goto no_space;
- *word = w_newword(word_length, max_length);
- }
- copying = 1;
- if (buffer[i] == '\n')
- maxnewlines++;
- else
- maxnewlines = 0;
- *word = w_addchar(*word, word_length, max_length,
- buffer[i]);
- if (*word == NULL)
- goto no_space;
- }
- }
- }
- }
-
-
- while (maxnewlines-- != 0 &&
- *word_length > 0 && (*word)[*word_length - 1] == '\n') {
- (*word)[--*word_length] = '\0';
-
- if (*word_length == 0) {
- free(*word);
- *word = w_newword(word_length, max_length);
- break;
- }
- }
- close(fildes[0]);
-
- if (buflen < 1 && status != 0) {
- if ((pid = fork()) < 0) {
-
- 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 int
- parse_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)
- {
-
- int paren_depth = 1;
- int error = 0;
- int quoted = 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 '\'':
- 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) {
-
- if (comm) {
- error = exec_comm(comm, word, word_length, max_length,
- flags, pwordexp, ifs, ifs_white);
- free(comm);
- }
- return error;
- }
-
- break;
- case '(':
- if (!quoted)
- ++paren_depth;
- }
- comm = w_addchar(comm, &comm_length, &comm_maxlen, words[*offset]);
- if (comm == NULL)
- return WRDE_NOSPACE;
- }
-
- free(comm);
- return WRDE_SYNTAX;
- }
- 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)
- {
-
- 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 '`':
-
- 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;
- }
- }
-
- free(comm);
- return WRDE_SYNTAX;
- }
- static int
- parse_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)
- {
-
- 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;
- 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;
-
- if (words[*offset] == '#') {
- seen_hash = 1;
- if (!brace)
- goto envsubst;
- ++*offset;
- }
- if (isalpha(words[*offset]) || words[*offset] == '_') {
-
- 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])) {
-
- 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 = 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) {
-
- switch (words[*offset]) {
- case '}':
-
- 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;
- }
-
- ++*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)
-
- break;
-
- 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;
- }
- }
-
- --(*offset);
- envsubst:
- if (words[start] == '{' && words[*offset] != '}')
- goto syntax;
- if (env == NULL) {
- if (seen_hash) {
-
- buffer[20] = '\0';
- value = _itoa(__libc_argc - 1, buffer);
- seen_hash = 0;
- } else {
-
- *offset = start - 1;
- *word = w_addchar(*word, word_length, max_length, '$');
- return *word ? 0 : WRDE_NOSPACE;
- }
- }
-
- else if (isdigit(env[0])) {
- int n = atoi(env);
- if (n >= __libc_argc)
-
- value = NULL;
- else
-
- value = __libc_argv[n];
- }
-
- else if (special) {
-
- if (*env == '$') {
- buffer[20] = '\0';
- value = _itoa(getpid(), buffer);
- }
-
- else if ((*env == '*' || *env == '@') && seen_hash) {
- buffer[20] = '\0';
- value = _itoa(__libc_argc > 0 ? __libc_argc - 1 : 0,
- buffer);
- *word = w_addstr(*word, word_length, max_length, value);
- free(env);
- free(pattern);
- return *word ? 0 : WRDE_NOSPACE;
- }
-
- else if (*env == '*' || (*env == '@' && !quoted)) {
- size_t plist_len = 0;
- int p;
- char *end;
-
- for (p = 1; __libc_argv[p]; ++p)
- plist_len += strlen(__libc_argv[p]) + 1;
- 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 {
-
- assert(*env == '@' && quoted);
-
- if (__libc_argc == 2)
- value = __libc_argv[1];
- else if (__libc_argc > 2) {
- int p;
-
- 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;
- }
-
- *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)) {
-
- error = WRDE_BADVAL;
- goto do_error;
- }
- if (action != ACT_NONE) {
- int expand_pattern = 0;
-
- switch (action) {
- case ACT_RP_SHORT_LEFT:
- case ACT_RP_LONG_LEFT:
- case ACT_RP_SHORT_RIGHT:
- case ACT_RP_LONG_RIGHT:
-
- expand_pattern = 1;
- break;
- case ACT_NULL_ERROR:
- case ACT_NULL_SUBST:
- case ACT_NULL_ASSIGN:
- if (!value || (!*value && colon_seen))
-
- expand_pattern = 1;
- break;
- case ACT_NONNULL_SUBST:
-
- if (value && (*value || !colon_seen))
- expand_pattern = 1;
- break;
- default:
- assert(!"Unrecognised action!");
- }
- if (expand_pattern) {
-
- char *expanded;
- size_t exp_len;
- size_t exp_maxl;
- char *p;
- int quotes = 0;
- expanded = w_newword(&exp_len, &exp_maxl);
- for (p = pattern; p && *p; p++) {
- size_t _offset;
- switch (*p) {
- case '"':
- if (quotes == 2)
- quotes = 0;
- else if (quotes == 0)
- quotes = 2;
- else
- break;
- continue;
- case '\'':
- if (quotes == 1)
- quotes = 0;
- else if (quotes == 0)
- quotes = 1;
- else
- break;
- continue;
- case '*':
- case '?':
- if (quotes) {
-
- 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);
- free(expanded);
- goto do_error;
- }
- p += _offset;
- continue;
- case '~':
- if (quotes || exp_len)
- break;
- _offset = 0;
- error = parse_tilde(&expanded, &exp_len, &exp_maxl, p,
- &_offset, 0);
- if (error) {
- if (free_value)
- free(value);
- free(expanded);
- goto do_error;
- }
- p += _offset;
- continue;
- case '\\':
- expanded = w_addchar(expanded, &exp_len, &exp_maxl, '\\');
- ++p;
- assert(*p);
- if (expanded == NULL)
- goto no_space;
- }
- expanded = w_addchar(expanded, &exp_len, &exp_maxl, *p);
- if (expanded == NULL)
- goto no_space;
- }
- 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)
-
- break;
- error = 0;
- if (!colon_seen && value)
-
- ;
- 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)
-
- break;
- if (free_value)
- free(value);
- if (!colon_seen && value)
-
- 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)
- free(value);
- value = pattern ? strdup(pattern) : pattern;
- free_value = 1;
- if (pattern && !value)
- goto no_space;
- break;
- }
-
- if (free_value)
- free(value);
- goto success;
- case ACT_NULL_ASSIGN:
- if (value && *value)
-
- break;
- if (!colon_seen && value) {
-
- if (free_value)
- free(value);
- goto success;
- }
- if (free_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));
- if (free_value) {
- assert(value != NULL);
- free(value);
- }
- return *word ? 0 : WRDE_NOSPACE;
- }
- if (value == NULL)
- return 0;
- if (quoted || !pwordexp) {
-
- *word = w_addstr(*word, word_length, max_length, value);
- if (free_value)
- free(value);
- return *word ? 0 : WRDE_NOSPACE;
- } else {
-
- char *value_copy = strdup(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 (field_begin != value_copy) {
- if (w_addword(pwordexp, *word) == WRDE_NOSPACE) {
- free(value_copy);
- goto no_space;
- }
- *word = w_newword(word_length, max_length);
- }
-
- field_begin += strspn(field_begin, ifs_white);
- if (!seen_nonws_ifs && *field_begin == 0)
-
- break;
-
- field_end = field_begin + strcspn(field_begin, ifs);
-
- next_field = field_end + strspn(field_end, ifs_white);
-
- seen_nonws_ifs = 0;
- if (*next_field && strchr(ifs, *next_field)) {
- seen_nonws_ifs = 1;
- next_field++;
- }
-
- *field_end = 0;
-
- *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:
- free(env);
- free(pattern);
- return error;
- }
- #else
- static __inline__ 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)
- {
- return 0;
- }
- #endif
- 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)
- {
-
- 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] == '(') {
-
- 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;
-
- 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;
-
- return parse_arith(word, word_length, max_length, words, offset,
- flags, 1);
- case '{':
- default:
- ++(*offset);
- return parse_param(word, word_length, max_length, words, offset,
- flags, pwordexp, ifs, ifs_white, quoted);
- #else
- default:
- ++(*offset);
- return 0;
- #endif
- }
- }
- 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)
- {
-
- 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);
-
- 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);
-
- 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;
- }
- }
-
- return WRDE_SYNTAX;
- }
- void wordfree(wordexp_t * pwordexp)
- {
-
- 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;
- }
- }
- libc_hidden_def(wordfree)
- 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) {
-
- 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;
- }
- }
-
- ifs = getenv("IFS");
- if (!ifs)
-
- ifs = strcpy(ifs_white, " \t\n");
- else {
- char *ifsch = ifs;
- char *whch = ifs_white;
-
- ifs_white[0] = '\0';
- while (*ifsch != '\0') {
- if ((*ifsch == ' ') || (*ifsch == '\t') || (*ifsch == '\n')) {
-
- 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:
-
- if (strchr(" \t", words[words_offset]) == NULL) {
- char ch = words[words_offset];
-
- if (strchr("\n|&;<>(){}", ch)) {
-
- error = WRDE_BADCHAR;
- goto do_error;
- }
-
- word = w_addchar(word, &word_length, &max_length, ch);
- if (word == NULL) {
- error = WRDE_NOSPACE;
- goto do_error;
- }
- break;
- }
-
- if (word != NULL) {
- error = w_addword(we, word);
- if (error)
- goto do_error;
- }
- word = w_newword(&word_length, &max_length);
- }
-
-
- if (word == NULL)
- return 0;
-
- return w_addword(we, word);
- do_error:
-
- free(word);
- if (error == WRDE_NOSPACE)
- return WRDE_NOSPACE;
- if ((flags & WRDE_APPEND) == 0)
- wordfree(we);
- *we = old_word;
- return error;
- }
|