12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249 |
- #define mempcpy __mempcpy
- #define strndup __strndup
- #define strspn __strspn
- #define unsetenv __unsetenv
- #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
- int __libc_argc;
- char **__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;
- }
- #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)
- {
-
- 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,
- const char *ifs_white)
- {
- 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, ifs_white);
-
- 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 *buflim)
- {
- sprintf(buflim, "%llu", value);
- return buflim;
- }
- 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[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];
- 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[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;
- }
- }
-
- free(expr);
- return WRDE_SYNTAX;
- }
- static void
- exec_comm_child(char *comm, int *fildes, int showerr, int noexec)
- {
- const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
-
- if (noexec)
- args[1] = "-nc";
-
- dup2(fildes[1], 1);
- __close(fildes[1]);
-
- if (showerr == 0) {
- int fd;
- __close(2);
- fd = __open(_PATH_DEVNULL, O_WRONLY);
- if (fd >= 0 && fd != 2) {
- dup2(fd, 2);
- __close(fd);
- }
- }
-
- unsetenv("IFS");
- __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 (pipe(fildes))
-
- return WRDE_NOSPACE;
- 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;
- }
-
- if (comm)
- 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[20]);
- 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[20]);
- }
-
- 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;
- }
-
- 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 quoted = 0;
- 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) {
-
- 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);
- 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)
-
- 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 && 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 && 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 && 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) {
-
- *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:
- if (env)
- free(env);
- if (pattern)
- 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;
- }
- }
- 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:
-
- if (word != NULL)
- free(word);
- if (error == WRDE_NOSPACE)
- return WRDE_NOSPACE;
- if ((flags & WRDE_APPEND) == 0)
- wordfree(we);
- *we = old_word;
- return error;
- }
|