12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993 |
- /*++
- Copyright (c) 2013 Minoca Corp. All Rights Reserved
- Module Name:
- builtin.c
- Abstract:
- This module implements support for the builtin shell utilities.
- Author:
- Evan Green 20-Aug-2013
- Environment:
- POSIX
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "sh.h"
- #include "shparse.h"
- #include "../swiss.h"
- #include <assert.h>
- #include <ctype.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <getopt.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include "../swlib.h"
- //
- // --------------------------------------------------------------------- Macros
- //
- //
- // This macro returns non-zero if the given node type is a for, while, or until
- // loop.
- //
- #define SHELL_LOOP_NODE(_NodeType) \
- (((_NodeType) == ShellNodeFor) || ((_NodeType) == ShellNodeWhile) || \
- ((_NodeType) == ShellNodeUntil))
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define SHELL_READ_INITIAL_STRING_SIZE 32
- #define SHELL_MAX_OPTION_INDEX_LENGTH 12
- #define RUBOUT_CHARACTER 0x7F
- //
- // Define the default builtin path used by the command builtin.
- //
- #define SHELL_COMMAND_BUILTIN_PATH "/bin:/usr/bin:/usr/local/bin"
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- INT
- ShBuiltinBreak (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinContinue (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinReturn (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinExit (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinBreakOrContinue (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments,
- BOOL Break
- );
- INT
- ShBuiltinReturnOrExit (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments,
- BOOL Exit
- );
- INT
- ShBuiltinNop (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinFalse (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinDot (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinExec (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinRead (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinShift (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinTimes (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinUmask (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinGetopts (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinCommand (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinType (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- );
- INT
- ShBuiltinTypeOrCommand (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments,
- BOOL IsType
- );
- INT
- ShClassifyCommand (
- PSHELL Shell,
- PSTR Command,
- BOOL Verbose
- );
- INT
- ShGetNextOption (
- PSHELL Shell,
- ULONG ArgumentCount,
- PSTR *Arguments,
- PINT ArgumentIndex,
- PSTR Options,
- PCHAR Option,
- PSTR *OptionArgument,
- PBOOL EndOfOptions
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Stores the expected index for the next call to the built-in option
- // processing.
- //
- INT ShOptionsIndex;
- //
- // Stores the string index of the next option character to process.
- //
- ULONG ShNextOptionCharacter;
- //
- // Stores whether or not a "--" end of arguments marker has been seen by the
- // built-in get options command.
- //
- BOOL ShSeenDoubleDash;
- //
- // Define the shell reserved keywords.
- //
- PSTR ShReservedWords[] = {
- "if",
- "then",
- "else",
- "elif",
- "fi",
- "do",
- "done",
- "case",
- "esac",
- "while",
- "until",
- "for",
- "{",
- "}",
- "!",
- "in",
- NULL
- };
- //
- // ------------------------------------------------------------------ Functions
- //
- PSHELL_BUILTIN_COMMAND
- ShIsBuiltinCommand (
- PSTR Command
- )
- /*++
- Routine Description:
- This routine determines if the given command name is a built in command,
- and returns a pointer to the command function if it is.
- Arguments:
- Command - Supplies the null terminated string of the command.
- Return Value:
- Returns a pointer to the command entry point function if the given string
- is a built-in command.
- NULL if the command is not a built-in command.
- --*/
- {
- PSHELL_BUILTIN_COMMAND EntryPoint;
- EntryPoint = NULL;
- switch (*Command) {
- case ':':
- if (*(Command + 1) == '\0') {
- EntryPoint = ShBuiltinNop;
- }
- break;
- case 'a':
- if (strcmp(Command + 1, "lias") == 0) {
- EntryPoint = ShBuiltinAlias;
- }
- break;
- case 'b':
- if (strcmp(Command + 1, "reak") == 0) {
- EntryPoint = ShBuiltinBreak;
- }
- break;
- case 'c':
- if (strcmp(Command + 1, "d") == 0) {
- EntryPoint = ShBuiltinCd;
- } else if (strcmp(Command + 1, "ommand") == 0) {
- EntryPoint = ShBuiltinCommand;
- } else if (strcmp(Command + 1, "ontinue") == 0) {
- EntryPoint = ShBuiltinContinue;
- }
- break;
- case '.':
- if (*(Command + 1) == '\0') {
- EntryPoint = ShBuiltinDot;
- }
- break;
- case 'e':
- if (strcmp(Command + 1, "val") == 0) {
- EntryPoint = ShBuiltinEval;
- } else if (strcmp(Command + 1, "xec") == 0) {
- EntryPoint = ShBuiltinExec;
- } else if (strcmp(Command + 1, "xit") == 0) {
- EntryPoint = ShBuiltinExit;
- } else if (strcmp(Command + 1, "xport") == 0) {
- EntryPoint = ShBuiltinExport;
- }
- break;
- case 'f':
- if (strcmp(Command + 1, "alse") == 0) {
- EntryPoint = ShBuiltinFalse;
- }
- break;
- case 'g':
- if (strcmp(Command + 1, "etopts") == 0) {
- EntryPoint = ShBuiltinGetopts;
- }
- break;
- case 'l':
- if (strcmp(Command + 1, "ocal") == 0) {
- EntryPoint = ShBuiltinLocal;
- }
- break;
- case 'p':
- if (strcmp(Command + 1, "wd") == 0) {
- EntryPoint = ShBuiltinPwd;
- }
- break;
- case 'r':
- if (strcmp(Command + 1, "ead") == 0) {
- EntryPoint = ShBuiltinRead;
- } else if (strcmp(Command + 1, "eadonly") == 0) {
- EntryPoint = ShBuiltinReadOnly;
- } else if (strcmp(Command + 1, "eturn") == 0) {
- EntryPoint = ShBuiltinReturn;
- }
- break;
- case 's':
- if (strcmp(Command + 1, "et") == 0) {
- EntryPoint = ShBuiltinSet;
- } else if (strcmp(Command + 1, "hift") == 0) {
- EntryPoint = ShBuiltinShift;
- }
- break;
- case 't':
- if (strcmp(Command + 1, "imes") == 0) {
- EntryPoint = ShBuiltinTimes;
- } else if (strcmp(Command + 1, "rap") == 0) {
- EntryPoint = ShBuiltinTrap;
- } else if (strcmp(Command + 1, "rue") == 0) {
- EntryPoint = ShBuiltinNop;
- } else if (strcmp(Command + 1, "ype") == 0) {
- EntryPoint = ShBuiltinType;
- }
- break;
- case 'u':
- if (strcmp(Command + 1, "mask") == 0) {
- EntryPoint = ShBuiltinUmask;
- } else if (strcmp(Command + 1, "nalias") == 0) {
- EntryPoint = ShBuiltinUnalias;
- } else if (strcmp(Command + 1, "nset") == 0) {
- EntryPoint = ShBuiltinUnset;
- }
- break;
- default:
- break;
- }
- return EntryPoint;
- }
- INT
- ShRunBuiltinCommand (
- PSHELL Shell,
- PSHELL_BUILTIN_COMMAND Command,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine runs a shell builtin command.
- Arguments:
- Shell - Supplies a pointer to the shell.
- Command - Supplies a pointer to the command function to run.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- Return Value:
- 0 on success.
- 1 on failure.
- --*/
- {
- INT Result;
- //
- // Run the command.
- //
- Result = Command(Shell, ArgumentCount, Arguments);
- fflush(NULL);
- return Result;
- }
- INT
- ShBuiltinEval (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the eval command, which collects all the parameters
- together separated by spaces and reexecutes them in the shell.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- Return Value:
- Returns the return value of the command it executes.
- --*/
- {
- INT ArgumentIndex;
- UINTN ArgumentSize;
- PSTR Input;
- UINTN InputIndex;
- UINTN InputSize;
- ULONG OldOptions;
- SHELL_LEXER_STATE OriginalLexer;
- BOOL Result;
- INT ReturnValue;
- Input = NULL;
- if (ArgumentCount < 2) {
- return 0;
- }
- //
- // Loop through once to figure out how big the input buffer needs to be.
- //
- InputSize = 0;
- for (ArgumentIndex = 1; ArgumentIndex < ArgumentCount; ArgumentIndex += 1) {
- ArgumentSize = strlen(Arguments[ArgumentIndex]);
- if (ArgumentSize == 0) {
- continue;
- }
- InputSize += ArgumentSize + 1;
- }
- if (InputSize == 0) {
- return 0;
- }
- //
- // Create the buffer consisting of all the strings separated by spaces.
- //
- Input = malloc(InputSize);
- if (Input == NULL) {
- ReturnValue = ENOMEM;
- goto BuiltinEvalEnd;
- }
- InputIndex = 0;
- for (ArgumentIndex = 1; ArgumentIndex < ArgumentCount; ArgumentIndex += 1) {
- ArgumentSize = strlen(Arguments[ArgumentIndex]);
- if (ArgumentSize == 0) {
- continue;
- }
- memcpy(Input + InputIndex, Arguments[ArgumentIndex], ArgumentSize);
- InputIndex += ArgumentSize;
- if (ArgumentIndex == ArgumentCount - 1) {
- Input[InputIndex] = '\0';
- } else {
- Input[InputIndex] = ' ';
- }
- InputIndex += 1;
- }
- assert(InputIndex == InputSize);
- //
- // Save the original lexer and re-initialize the lexer for this new file.
- //
- memcpy(&OriginalLexer, &(Shell->Lexer), sizeof(SHELL_LEXER_STATE));
- Result = ShInitializeLexer(&(Shell->Lexer), NULL, Input, InputSize);
- if (Result == FALSE) {
- memcpy(&(Shell->Lexer), &OriginalLexer, sizeof(SHELL_LEXER_STATE));
- goto BuiltinEvalEnd;
- }
- OldOptions = Shell->Options;
- Shell->Options &= ~SHELL_OPTION_PRINT_PROMPTS;
- Shell->Options |= SHELL_OPTION_INPUT_BUFFER_ONLY;
- //
- // Run the commands.
- //
- Result = ShExecute(Shell, &ReturnValue);
- //
- // Turn the print prompts flag back on if it was set before.
- //
- Shell->Options &= ~SHELL_OPTION_INPUT_BUFFER_ONLY;
- Shell->Options |= OldOptions &
- (SHELL_OPTION_PRINT_PROMPTS |
- SHELL_OPTION_INPUT_BUFFER_ONLY);
- //
- // Restore the original lexer.
- //
- ShDestroyLexer(&(Shell->Lexer));
- memcpy(&(Shell->Lexer), &OriginalLexer, sizeof(SHELL_LEXER_STATE));
- if ((Result == FALSE) && (ReturnValue == 0)) {
- ReturnValue = 1;
- }
- BuiltinEvalEnd:
- if (Input != NULL) {
- free(Input);
- }
- return ReturnValue;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- INT
- ShBuiltinBreak (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the builtin break statement.
- Arguments:
- Shell - Supplies a pointer to the shell being run in.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument.
- Return Value:
- Returns the integer return value from the built-in command, which will be
- placed in the node on top of the execution stack. This may not be the node
- that executed this command, as this command may have popped things off the
- execution stack (such as a return, break, or continue will do).
- --*/
- {
- return ShBuiltinBreakOrContinue(Shell, ArgumentCount, Arguments, TRUE);
- }
- INT
- ShBuiltinContinue (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the builtin break statement.
- Arguments:
- Shell - Supplies a pointer to the shell being run in.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument.
- Return Value:
- Returns the integer return value from the built-in command, which will be
- placed in the node on top of the execution stack. This may not be the node
- that executed this command, as this command may have popped things off the
- execution stack (such as a return, break, or continue will do).
- --*/
- {
- return ShBuiltinBreakOrContinue(Shell, ArgumentCount, Arguments, FALSE);
- }
- INT
- ShBuiltinReturn (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the builtin return statement.
- Arguments:
- Shell - Supplies a pointer to the shell being run in.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument.
- Return Value:
- Returns the integer return value from the built-in command, which will be
- placed in the node on top of the execution stack. This may not be the node
- that executed this command, as this command may have popped things off the
- execution stack (such as a return, break, or continue will do).
- --*/
- {
- return ShBuiltinReturnOrExit(Shell, ArgumentCount, Arguments, FALSE);
- }
- INT
- ShBuiltinExit (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the builtin exit statement.
- Arguments:
- Shell - Supplies a pointer to the shell being run in.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument.
- Return Value:
- Returns the integer return value from the built-in command, which will be
- placed in the node on top of the execution stack. This may not be the node
- that executed this command, as this command may have popped things off the
- execution stack (such as a return, break, or continue will do).
- --*/
- {
- return ShBuiltinReturnOrExit(Shell, ArgumentCount, Arguments, TRUE);
- }
- INT
- ShBuiltinBreakOrContinue (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments,
- BOOL Break
- )
- /*++
- Routine Description:
- This routine implements the guts of the built in break and continue
- statements.
- Arguments:
- Shell - Supplies a pointer to the shell being run in.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument.
- Break - Supplies a boolean indicating if this is a break command (TRUE) or
- a continue command (FALSE).
- Return Value:
- Returns the integer return value from the built-in command, which will be
- placed in the node on top of the execution stack. This may not be the node
- that executed this command, as this command may have popped things off the
- execution stack (such as a return, break, or continue will do).
- --*/
- {
- PSTR AfterScan;
- PLIST_ENTRY CurrentEntry;
- PSHELL_EXECUTION_NODE DestinationLoop;
- ULONG LoopCount;
- PSHELL_EXECUTION_NODE Node;
- //
- // Get the argument to how many loops to exit if there is one.
- //
- LoopCount = 1;
- if (ArgumentCount > 2) {
- return 1;
- } else if (ArgumentCount == 2) {
- LoopCount = strtoul(Arguments[1], &AfterScan, 10);
- if ((LoopCount < 1) || (*AfterScan != '\0')) {
- PRINT_ERROR("sh: break: Invalid count\n");
- return 1;
- }
- }
- assert(LoopCount >= 1);
- if (LIST_EMPTY(&(Shell->ExecutionStack)) != FALSE) {
- return 0;
- }
- //
- // Get the node corresponding to loop N, or if loop N is greater than the
- // number of loops, then just get the outermost loop.
- //
- DestinationLoop = NULL;
- CurrentEntry = Shell->ExecutionStack.Next;
- while (CurrentEntry != &(Shell->ExecutionStack)) {
- Node = LIST_VALUE(CurrentEntry, SHELL_EXECUTION_NODE, ListEntry);
- CurrentEntry = CurrentEntry->Next;
- if (SHELL_LOOP_NODE(Node->Node->Type)) {
- DestinationLoop = Node;
- LoopCount -= 1;
- if (LoopCount == 0) {
- break;
- }
- }
- }
- //
- // If there were no loops on the whole stack, then just return happily.
- //
- if (DestinationLoop == NULL) {
- return 0;
- }
- //
- // Remove nodes up until the destination loop.
- //
- CurrentEntry = Shell->ExecutionStack.Next;
- CurrentEntry = Shell->ExecutionStack.Next;
- while (CurrentEntry != &(Shell->ExecutionStack)) {
- Node = LIST_VALUE(CurrentEntry, SHELL_EXECUTION_NODE, ListEntry);
- CurrentEntry = CurrentEntry->Next;
- //
- // If this is the destination node, it's removed for breaks, but not
- // for continues.
- //
- if (Node == DestinationLoop) {
- if (Break == FALSE) {
- break;
- }
- }
- //
- // Don't worry about freeing the node, as all the functions on this
- // execution stack are also on the real stack. They'll notice they
- // were removed and return immediately.
- //
- LIST_REMOVE(&(Node->ListEntry));
- Node->ListEntry.Next = NULL;
- //
- // Stop if this is the destination node.
- //
- if (Node == DestinationLoop) {
- break;
- }
- }
- return 0;
- }
- INT
- ShBuiltinReturnOrExit (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments,
- BOOL Exit
- )
- /*++
- Routine Description:
- This routine implements the return and exit functions.
- Arguments:
- Shell - Supplies a pointer to the shell being run in.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument.
- Exit - Supplies a boolean indicating if this is an exit command (TRUE) or
- a return command (FALSE).
- Return Value:
- Returns the integer return value from the built-in command, which will be
- placed in the node on top of the execution stack. This may not be the node
- that executed this command, as this command may have popped things off the
- execution stack (such as a return, break, or continue will do).
- --*/
- {
- PSTR AfterScan;
- PLIST_ENTRY CurrentEntry;
- PSHELL_EXECUTION_NODE Node;
- LONG ReturnValue;
- //
- // Get the return value argument if there is one.
- //
- ReturnValue = Shell->LastReturnValue;
- if (ArgumentCount >= 2) {
- ReturnValue = strtoul(Arguments[1], &AfterScan, 10);
- if (*AfterScan != '\0') {
- PRINT_ERROR("sh: return: invalid argument '%s'\n", Arguments[1]);
- ReturnValue = Shell->LastReturnValue;
- }
- }
- if (LIST_EMPTY(&(Shell->ExecutionStack)) != FALSE) {
- return ReturnValue;
- }
- //
- // Remove nodes up until either the first function for return statements or
- // until there are none for exit statements.
- //
- CurrentEntry = Shell->ExecutionStack.Next;
- CurrentEntry = Shell->ExecutionStack.Next;
- while (CurrentEntry != &(Shell->ExecutionStack)) {
- Node = LIST_VALUE(CurrentEntry, SHELL_EXECUTION_NODE, ListEntry);
- CurrentEntry = CurrentEntry->Next;
- LIST_REMOVE(&(Node->ListEntry));
- Node->ListEntry.Next = NULL;
- //
- // If this was an executing function and it's a return statement, then
- // stop here.
- //
- if ((Exit == FALSE) && (Node->Node->Type == ShellNodeFunction) &&
- ((Node->Flags & SHELL_EXECUTION_BODY) != 0)) {
- break;
- }
- }
- if (Exit != FALSE) {
- Shell->Exited = TRUE;
- }
- return ReturnValue;
- }
- INT
- ShBuiltinNop (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the no-op colon (:) command. It also doubles as the
- true command.
- Arguments:
- Shell - Supplies a pointer to the shell being run in.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument.
- Return Value:
- 0 always.
- --*/
- {
- return 0;
- }
- INT
- ShBuiltinFalse (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the builtin false command, which just fails
- everything.
- Arguments:
- Shell - Supplies a pointer to the shell being run in.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument.
- Return Value:
- 1 always.
- --*/
- {
- return 1;
- }
- INT
- ShBuiltinDot (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the dot command, which executes commands from the
- given file in the current environment.
- Arguments:
- Shell - Supplies a pointer to the shell being run in.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- Return Value:
- 0 always.
- --*/
- {
- PSTR FullCommandPath;
- ULONG FullCommandPathSize;
- FILE *NewFile;
- INT NewFileDescriptor;
- INT NewFileDescriptorHigh;
- SHELL_LEXER_STATE OriginalLexer;
- ULONG OriginalOptions;
- BOOL Result;
- INT ReturnValue;
- NewFile = NULL;
- NewFileDescriptor = -1;
- NewFileDescriptorHigh = -1;
- if (ArgumentCount < 2) {
- return 0;
- }
- //
- // Find the command.
- //
- ReturnValue = 0;
- Result = ShLocateCommand(Shell,
- Arguments[1],
- strlen(Arguments[1]) + 1,
- FALSE,
- &FullCommandPath,
- &FullCommandPathSize,
- &ReturnValue);
- if (Result == FALSE) {
- goto BuiltinDotEnd;
- }
- if (ReturnValue != 0) {
- if (ReturnValue == SHELL_ERROR_OPEN) {
- PRINT_ERROR("sh: %s: Command not found.\n", Arguments[1]);
- } else if (ReturnValue == SHELL_ERROR_EXECUTE) {
- PRINT_ERROR("sh: %s: Permission denied.\n", Arguments[1]);
- }
- goto BuiltinDotEnd;
- }
- //
- // Open up the new file to be read for commands. Make sure it's out of the
- // user file descriptor range.
- //
- NewFileDescriptor = open(FullCommandPath, O_RDONLY | O_BINARY);
- if (NewFileDescriptor < 0) {
- SwPrintError(errno, FullCommandPath, "Unable to open");
- ReturnValue = SHELL_ERROR_OPEN;
- goto BuiltinDotEnd;
- }
- if (NewFileDescriptor >= SHELL_MINIMUM_FILE_DESCRIPTOR) {
- NewFileDescriptorHigh = NewFileDescriptor;
- NewFileDescriptor = -1;
- } else {
- NewFileDescriptorHigh = ShDup(Shell, NewFileDescriptor, FALSE);
- if (NewFileDescriptorHigh < 0) {
- SwPrintError(errno, FullCommandPath, "Unable to dup");
- ReturnValue = SHELL_ERROR_OPEN;
- goto BuiltinDotEnd;
- }
- assert(NewFileDescriptorHigh >= SHELL_MINIMUM_FILE_DESCRIPTOR);
- close(NewFileDescriptor);
- NewFileDescriptor = -1;
- }
- NewFile = fdopen(NewFileDescriptorHigh, "rb");
- if (NewFile == NULL) {
- SwPrintError(errno, FullCommandPath, "Unable to Open");
- ReturnValue = SHELL_ERROR_OPEN;
- goto BuiltinDotEnd;
- }
- NewFileDescriptorHigh = -1;
- //
- // Save the original lexer and re-initialize the lexer for this new file.
- //
- memcpy(&OriginalLexer, &(Shell->Lexer), sizeof(SHELL_LEXER_STATE));
- Result = ShInitializeLexer(&(Shell->Lexer), NewFile, NULL, 0);
- if (Result == FALSE) {
- memcpy(&(Shell->Lexer), &OriginalLexer, sizeof(SHELL_LEXER_STATE));
- goto BuiltinDotEnd;
- }
- OriginalOptions = Shell->Options &
- (SHELL_OPTION_PRINT_PROMPTS |
- SHELL_OPTION_INTERACTIVE |
- SHELL_OPTION_RAW_INPUT |
- SHELL_OPTION_INPUT_BUFFER_ONLY);
- Shell->Options &= ~OriginalOptions;
- Shell->LastReturnValue = 0;
- //
- // Run the commands.
- //
- Result = ShExecute(Shell, &ReturnValue);
- Shell->Options |= OriginalOptions;
- //
- // Restore the original lexer.
- //
- ShDestroyLexer(&(Shell->Lexer));
- memcpy(&(Shell->Lexer), &OriginalLexer, sizeof(SHELL_LEXER_STATE));
- if ((Result == FALSE) && (ReturnValue == 0)) {
- ReturnValue = 1;
- }
- BuiltinDotEnd:
- if ((FullCommandPath != NULL) && (FullCommandPath != Arguments[1])) {
- free(FullCommandPath);
- }
- if (NewFileDescriptor >= 0) {
- close(NewFileDescriptor);
- }
- if (NewFileDescriptorHigh >= 0) {
- close(NewFileDescriptorHigh);
- }
- return ReturnValue;
- }
- INT
- ShBuiltinExec (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the exec command, which makes the current shell
- into the given program.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- Return Value:
- 0 if the command ran successfully. In this case the shell's exited flag
- will be set.
- Returns an error code if the app failed to launch.
- --*/
- {
- PSHELL_ACTIVE_REDIRECT ActiveRedirect;
- PSHELL_BUILTIN_COMMAND BuiltinCommand;
- PSHELL_EXECUTION_NODE ExecutionNode;
- PSTR FullCommandPath;
- ULONG FullCommandPathSize;
- INT Result;
- INT ReturnValue;
- FullCommandPath = NULL;
- //
- // If there are no arguments, pull off any active redirection entries so
- // they are not undone when the command finishes.
- //
- if (ArgumentCount <= 1) {
- ExecutionNode = LIST_VALUE(Shell->ExecutionStack.Next,
- SHELL_EXECUTION_NODE,
- ListEntry);
- assert(ExecutionNode->Node->Type == ShellNodeSimpleCommand);
- while (LIST_EMPTY(&(ExecutionNode->ActiveRedirectList)) == FALSE) {
- ActiveRedirect = LIST_VALUE(ExecutionNode->ActiveRedirectList.Next,
- SHELL_ACTIVE_REDIRECT,
- ListEntry);
- LIST_REMOVE(&(ActiveRedirect->ListEntry));
- INSERT_BEFORE(&(ActiveRedirect->ListEntry),
- &(Shell->ActiveRedirectList));
- }
- return 0;
- }
- Arguments += 1;
- ArgumentCount -= 1;
- //
- // Check to see if this is a builtin command, and run it if it is.
- //
- BuiltinCommand = ShIsBuiltinCommand(Arguments[0]);
- if (BuiltinCommand != NULL) {
- ReturnValue = ShRunBuiltinCommand(Shell,
- BuiltinCommand,
- ArgumentCount,
- Arguments);
- Shell->Exited = TRUE;
- Shell->SkipExitSignal = TRUE;
- goto BuiltinExecEnd;
- } else {
- //
- // If fork is supported, then actually try to exec the item.
- //
- ReturnValue = 0;
- if (SwForkSupported != FALSE) {
- Result = ShLocateCommand(Shell,
- Arguments[0],
- strlen(Arguments[0]) + 1,
- TRUE,
- &FullCommandPath,
- &FullCommandPathSize,
- &ReturnValue);
- if (Result == FALSE) {
- if (ReturnValue == 0) {
- ReturnValue = 1;
- }
- goto BuiltinExecEnd;
- }
- if (ReturnValue != 0) {
- if (ReturnValue == SHELL_ERROR_OPEN) {
- PRINT_ERROR("sh: %s: Command not found.\n", Arguments[0]);
- } else if (ReturnValue == SHELL_ERROR_EXECUTE) {
- PRINT_ERROR("sh: %s: Permission denied.\n", Arguments[0]);
- }
- Shell->ReturnValue = ReturnValue;
- goto BuiltinExecEnd;
- }
- fflush(NULL);
- //
- // Execute the destination image. If this fails, exit immediately
- // anyway.
- //
- ShRestoreOriginalSignalDispositions();
- ReturnValue = SwExec(FullCommandPath, Arguments, ArgumentCount);
- ShSetAllSignalDispositions(Shell);
- SwPrintError(ReturnValue, FullCommandPath, "Failed to exec");
- Shell->ReturnValue = ReturnValue;
- //
- // If fork is not supported, then subshells never forked, and this
- // process needs to unwind back up to that. Run the command, then
- // go back up to the previous subshell.
- //
- } else {
- Result = ShRunCommand(Shell,
- Arguments[0],
- Arguments,
- ArgumentCount,
- FALSE,
- &ReturnValue);
- if (Result == 0) {
- ShOsConvertExitStatus(&ReturnValue);
- Shell->ReturnValue = ReturnValue;
- Shell->Exited = TRUE;
- Shell->SkipExitSignal = TRUE;
- goto BuiltinExecEnd;
- } else {
- SwPrintError(Result, Arguments[0], "Failed to exec");
- ReturnValue = 1;
- }
- }
- }
- BuiltinExecEnd:
- if ((FullCommandPath != NULL) && (FullCommandPath != Arguments[0])) {
- free(FullCommandPath);
- }
- return ReturnValue;
- }
- INT
- ShBuiltinRead (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the read command, which reads a line from standard
- in, splits it, and assigns variable names given on the command line to the
- given fields.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- Return Value:
- 0 if the command ran successfully. In this case the shell's exited flag
- will be set.
- Returns an error code if the app failed to launch.
- --*/
- {
- PSTR Argument;
- INT ArgumentIndex;
- UINTN ArgumentSize;
- UCHAR Character;
- BOOL EndOfFileDetected;
- SHELL_EXPANSION_RANGE Expansion;
- LIST_ENTRY ExpansionList;
- PSTR Field;
- ULONG FieldCount;
- PSTR *Fields;
- ULONG FieldSize;
- BOOL IgnoreBackslash;
- PSTR Line;
- UINTN LineCapacity;
- UINTN LineSize;
- PSTR NewBuffer;
- BOOL Result;
- INT Status;
- BOOL WasBackslash;
- assert(ArgumentCount != 0);
- EndOfFileDetected = FALSE;
- Fields = NULL;
- //
- // Skip over the "read" argument.
- //
- Arguments += 1;
- ArgumentCount -= 1;
- IgnoreBackslash = FALSE;
- if ((ArgumentCount != 0) && (strcmp(Arguments[0], "-r") == 0)) {
- IgnoreBackslash = TRUE;
- Arguments += 1;
- ArgumentCount -= 1;
- }
- //
- // Read a line of input.
- //
- LineCapacity = SHELL_READ_INITIAL_STRING_SIZE;
- Line = malloc(LineCapacity);
- if (Line == NULL) {
- Status = ENOMEM;
- goto BuiltinReadEnd;
- }
- LineSize = 0;
- WasBackslash = FALSE;
- while (TRUE) {
- do {
- Status = read(STDIN_FILENO, &Character, 1);
- } while ((Status < 0) && (errno == EINTR));
- if (Status < 0) {
- Status = errno;
- ShPrintTrace(Shell, "sh: Failed read: %s.\n", strerror(Status));
- goto BuiltinReadEnd;
- }
- if (Status == 0) {
- EndOfFileDetected = TRUE;
- break;
- }
- if (Character == '\n') {
- //
- // A backslash followed by a newline is a line continuation.
- // Remove the backslash from the input line.
- //
- if (WasBackslash != FALSE) {
- continue;
- } else {
- //
- // Remove any carriage returns that may have strayed along.
- //
- if ((LineSize != 0) && (Line[LineSize - 1] == '\r')) {
- LineSize -= 1;
- }
- break;
- }
- //
- // A backslash followed by any character preserves the literal meaning
- // of that character. Remove the backslash from the input line.
- //
- } else if (WasBackslash != FALSE) {
- LineSize -= 1;
- }
- //
- // Allocate more space for the line if needed. Always have enough
- // space for the terminator as well.
- //
- if (LineSize + 2 >= LineCapacity) {
- LineCapacity *= 2;
- NewBuffer = realloc(Line, LineCapacity);
- if (NewBuffer == NULL) {
- Status = ENOMEM;
- goto BuiltinReadEnd;
- }
- Line = NewBuffer;
- }
- Line[LineSize] = Character;
- LineSize += 1;
- //
- // Keep track of whether or not the previous character was a backslash,
- // but only if backslashes are not being ignored.
- //
- if ((Character == '\\') && (IgnoreBackslash == FALSE)) {
- WasBackslash = !WasBackslash;
- } else {
- WasBackslash = FALSE;
- }
- }
- //
- // Terminate the line.
- //
- Line[LineSize] = '\0';
- LineSize += 1;
- //
- // Split the line into fields.
- //
- INITIALIZE_LIST_HEAD(&ExpansionList);
- Expansion.Type = ShellExpansionFieldSplit;
- Expansion.Index = 0;
- Expansion.Length = LineSize;
- INSERT_BEFORE(&(Expansion.ListEntry), &ExpansionList);
- Result = ShFieldSplit(Shell,
- &Line,
- &LineSize,
- &ExpansionList,
- ArgumentCount,
- &Fields,
- &FieldCount);
- if (Result == FALSE) {
- Status = 1;
- goto BuiltinReadEnd;
- }
- //
- // Assign every argument to the field.
- //
- for (ArgumentIndex = 0; ArgumentIndex < ArgumentCount; ArgumentIndex += 1) {
- Argument = Arguments[ArgumentIndex];
- ArgumentSize = strlen(Argument) + 1;
- if (ShIsName(Argument, ArgumentSize) == FALSE) {
- PRINT_ERROR("read: Invalid variable name '%s'.\n", Argument);
- Status = EINVAL;
- goto BuiltinReadEnd;
- }
- if (ArgumentIndex < FieldCount) {
- Field = Fields[ArgumentIndex];
- } else {
- Field = "";
- }
- FieldSize = strlen(Field) + 1;
- Result = ShSetVariable(Shell,
- Argument,
- ArgumentSize,
- Field,
- FieldSize);
- if (Result == FALSE) {
- PRINT_ERROR("read: Unable to set variable '%s'.\n", Argument);
- Status = 1;
- goto BuiltinReadEnd;
- }
- }
- Status = 0;
- if (EndOfFileDetected != FALSE) {
- Status = 1;
- }
- BuiltinReadEnd:
- if (Fields != NULL) {
- free(Fields);
- }
- if (Line != NULL) {
- free(Line);
- }
- return Status;
- }
- INT
- ShBuiltinShift (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the shift command, which chomps away at the
- positional arguments.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments on the command line. The
- only valid values are 1 and 2 (only one optional argument expected).
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- Return Value:
- 1 on failure.
- 0 on success.
- --*/
- {
- PSTR AfterScan;
- PSHELL_ARGUMENT Argument;
- PLIST_ENTRY ArgumentList;
- PLIST_ENTRY CurrentEntry;
- ULONG ShellArgumentCount;
- ULONG ShiftCount;
- ULONG ShiftIndex;
- ShellArgumentCount = 0;
- ArgumentList = ShGetCurrentArgumentList(Shell);
- //
- // Loop through once to count arguments.
- //
- CurrentEntry = ArgumentList->Next;
- while (CurrentEntry != ArgumentList) {
- Argument = LIST_VALUE(CurrentEntry, SHELL_ARGUMENT, ListEntry);
- CurrentEntry = CurrentEntry->Next;
- ShellArgumentCount += 1;
- }
- //
- // Convert the optional argument to a shift count.
- //
- ShiftCount = 1;
- if (ArgumentCount > 1) {
- ShiftCount = strtol(Arguments[1], &AfterScan, 10);
- if ((AfterScan == Arguments[1]) || (*AfterScan != '\0')) {
- PRINT_ERROR("shift: Illegal number %s.\n", Arguments[1]);
- return 1;
- }
- }
- //
- // Don't overextend.
- //
- if (ShiftCount > ShellArgumentCount) {
- PRINT_ERROR("shift: Can't shift by %d, only %d arguments.\n",
- ShiftCount,
- ShellArgumentCount);
- return 1;
- }
- //
- // Pull arguments off the list.
- //
- for (ShiftIndex = 0; ShiftIndex < ShiftCount; ShiftIndex += 1) {
- assert(LIST_EMPTY(ArgumentList) == FALSE);
- Argument = LIST_VALUE(ArgumentList->Next, SHELL_ARGUMENT, ListEntry);
- LIST_REMOVE(&(Argument->ListEntry));
- if (Argument->Name != NULL) {
- free(Argument->Name);
- }
- free(Argument);
- }
- return 0;
- }
- INT
- ShBuiltinTimes (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the times command, which prints execution
- statistics about the shell and its children.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- Return Value:
- 0 on success.
- 1 on failure.
- --*/
- {
- BOOL Result;
- SHELL_PROCESS_TIMES Times;
- Result = ShGetExecutionTimes(&Times);
- if (Result == FALSE) {
- return 1;
- }
- //
- // Floats are for losers.
- //
- printf("%I64dm%d.%06ds %I64dm%d.%06ds\n%I64dm%d.%06ds %I64dm%d.%06ds\n",
- Times.ShellUserMinutes,
- Times.ShellUserMicroseconds / 1000000,
- Times.ShellUserMicroseconds % 1000000,
- Times.ShellSystemMinutes,
- Times.ShellSystemMicroseconds / 1000000,
- Times.ShellSystemMicroseconds % 1000000,
- Times.ChildrenUserMinutes,
- Times.ChildrenUserMicroseconds / 1000000,
- Times.ChildrenUserMicroseconds % 1000000,
- Times.ChildrenSystemMinutes,
- Times.ChildrenSystemMicroseconds / 1000000,
- Times.ChildrenSystemMicroseconds % 1000000);
- return 0;
- }
- INT
- ShBuiltinUmask (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the uname builtin command, which changes the
- umask of the process the current shell is running in.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- Return Value:
- 0 on success.
- 1 on failure.
- --*/
- {
- PSTR Argument;
- INT ArgumentIndex;
- mode_t Mask;
- PSTR ModeString;
- mode_t OriginalMask;
- BOOL Result;
- BOOL Symbolic;
- Symbolic = FALSE;
- if (ArgumentCount > 3) {
- fprintf(stderr, "usage: umask [-S] [mask]\n");
- return 1;
- }
- ModeString = NULL;
- for (ArgumentIndex = 1; ArgumentIndex < ArgumentCount; ArgumentIndex += 1) {
- Argument = Arguments[ArgumentIndex];
- if (strcmp(Argument, "-S") == 0) {
- Symbolic = TRUE;
- } else {
- ModeString = Argument;
- }
- }
- OriginalMask = umask(0);
- umask(OriginalMask);
- //
- // If a mode string was supplied, create the and set the new mask.
- //
- if (ModeString != NULL) {
- Mask = OriginalMask;
- Result = SwParseFilePermissionsString(ModeString, FALSE, &Mask);
- if (Result == FALSE) {
- fprintf(stderr,
- "umask: Could not parse mode string '%s'.\n",
- ModeString);
- return 1;
- }
- umask(Mask);
- //
- // If there's no mode string, print the current mask out.
- //
- } else {
- if (Symbolic != FALSE) {
- //
- // Go through the motions to print out something pretty.
- //
- Mask = OriginalMask;
- printf("u=");
- if ((Mask & S_IRUSR) == 0) {
- fputc('r', stdout);
- }
- if ((Mask & S_IWUSR) == 0) {
- fputc('w', stdout);
- }
- if ((Mask & S_IXUSR) == 0) {
- fputc('x', stdout);
- }
- printf(",g=");
- if ((Mask & S_IRGRP) == 0) {
- fputc('r', stdout);
- }
- if ((Mask & S_IWGRP) == 0) {
- fputc('w', stdout);
- }
- if ((Mask & S_IXGRP) == 0) {
- fputc('x', stdout);
- }
- printf(",o=");
- if ((Mask & S_IROTH) == 0) {
- fputc('r', stdout);
- }
- if ((Mask & S_IWOTH) == 0) {
- fputc('w', stdout);
- }
- if ((Mask & S_IXOTH) == 0) {
- fputc('x', stdout);
- }
- printf("\n");
- //
- // Just print out the octal value.
- //
- } else {
- printf("%04o\n", OriginalMask);
- }
- }
- return 0;
- }
- INT
- ShBuiltinGetopts (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the getopts builtin command, which parses
- positional parameters or the supplied arguments for command.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument.
- Return Value:
- 0 on success.
- Returns greater than zero if an error occurred.
- --*/
- {
- PSTR AfterScan;
- PSTR Argument;
- ULONG ArgumentIndex;
- INT BytesConverted;
- BOOL EndOfOptions;
- PSTR ExpandedArguments;
- UINTN ExpandedArgumentsSize;
- CHAR NewOptionIndex[SHELL_MAX_OPTION_INDEX_LENGTH];
- CHAR NewOptionValueBuffer[2];
- PSTR NewOptionVariable;
- PSTR OptionArgument;
- CHAR OptionCharacter;
- PSTR OptionIndexString;
- ULONG OptionsArgumentCount;
- PSTR *OptionsArguments;
- INT OptionsIndex;
- PSTR OptionsString;
- BOOL Result;
- INT ReturnValue;
- INT Status;
- BOOL UsingPositionalParameters;
- ReturnValue = 0;
- //
- // There are no options to the getopts utility, but eat up arguments
- // looking for bad options.
- //
- for (ArgumentIndex = 1; ArgumentIndex < ArgumentCount; ArgumentIndex += 1) {
- Argument = Arguments[ArgumentIndex];
- //
- // Break out if a non-argument is seen.
- //
- if (*Argument != '-') {
- break;
- }
- //
- // Break out and advance the argument index if the "--" is seen.
- //
- Argument += 1;
- if (strcmp(Argument, "-") == 0) {
- ArgumentIndex += 1;
- break;
- }
- //
- // Anything else is an error.
- //
- fprintf(stderr, "getopts: invalid option '-%c'.\n", *Argument);
- fprintf(stderr, "usage: getopts optstring name [arg...]\n");
- return 2;
- }
- //
- // Argument index holds the position of the option string. If there aren't
- // at least two parameters remaining, then exit.
- //
- if ((ArgumentCount - ArgumentIndex) < 2) {
- fprintf(stderr, "usage: getopts optstring name [arg...]\n");
- return 2;
- }
- OptionsString = Arguments[ArgumentIndex];
- NewOptionVariable = Arguments[ArgumentIndex + 1];
- ArgumentIndex += 2;
- //
- // Try to get the option index. If it's not there or invalid, then reset to
- // 1.
- //
- OptionsIndex = 1;
- Result = ShGetVariable(Shell,
- SHELL_OPTION_INDEX,
- sizeof(SHELL_OPTION_INDEX),
- &OptionIndexString,
- NULL);
- if (Result != FALSE) {
- OptionsIndex = (int)strtol(OptionIndexString, &AfterScan, 10);
- if ((OptionIndexString == AfterScan) || (*AfterScan != '\0')) {
- OptionsIndex = 1;
- }
- }
- //
- // If the options index is less than 1, then reset it.
- //
- if (OptionsIndex < 1) {
- ShOptionsIndex = OptionsIndex;
- OptionsIndex = 1;
- }
- //
- // Reset if the caller tried to manipluate the option index.
- //
- if (ShOptionsIndex != OptionsIndex) {
- ShOptionsIndex = OptionsIndex;
- ShNextOptionCharacter = 0;
- ShSeenDoubleDash = FALSE;
- }
- //
- // If arguments are present, those are preferred to the positional
- // parameters.
- //
- UsingPositionalParameters = FALSE;
- if (ArgumentIndex != ArgumentCount) {
- OptionsArgumentCount = ArgumentCount - ArgumentIndex;
- OptionsArguments = &(Arguments[ArgumentIndex]);
- } else {
- Result = ShPerformExpansions(Shell,
- "$@",
- sizeof("$@"),
- 0,
- &ExpandedArguments,
- &ExpandedArgumentsSize,
- &OptionsArguments,
- &OptionsArgumentCount);
- if (Result == FALSE) {
- ReturnValue += 1;
- goto BuiltinGetoptsEnd;
- }
- UsingPositionalParameters = TRUE;
- }
- //
- // The options index is off by one because the arguments array does not
- // have the command as the first entry.
- //
- OptionsIndex -= 1;
- //
- // Get the next option using the built-in parser rather than the C library
- // getopts() routine. Things are handled slightly differently with regards
- // to the option string.
- //
- Status = ShGetNextOption(Shell,
- OptionsArgumentCount,
- OptionsArguments,
- &OptionsIndex,
- OptionsString,
- &OptionCharacter,
- &OptionArgument,
- &EndOfOptions);
- if (Status == 0) {
- ReturnValue += 1;
- goto BuiltinGetoptsEnd;
- }
- //
- // Shift the options index back as it is stored considering the command as
- // 0.
- //
- OptionsIndex += 1;
- ShOptionsIndex = OptionsIndex;
- //
- // Update the environment variables.
- //
- BytesConverted = snprintf(NewOptionIndex,
- SHELL_MAX_OPTION_INDEX_LENGTH,
- "%d",
- OptionsIndex);
- if (BytesConverted < 0) {
- ReturnValue += 1;
- goto BuiltinGetoptsEnd;
- }
- Result = ShSetVariable(Shell,
- SHELL_OPTION_INDEX,
- sizeof(SHELL_OPTION_INDEX),
- NewOptionIndex,
- BytesConverted + 1);
- if (Result == FALSE) {
- ReturnValue += 1;
- goto BuiltinGetoptsEnd;
- }
- NewOptionValueBuffer[0] = OptionCharacter;
- NewOptionValueBuffer[1] = '\0';
- BytesConverted = 1;
- Result = ShSetVariable(Shell,
- NewOptionVariable,
- strlen(NewOptionVariable) + 1,
- NewOptionValueBuffer,
- BytesConverted + 1);
- if (Result == FALSE) {
- ReturnValue += 1;
- goto BuiltinGetoptsEnd;
- }
- if (OptionArgument != NULL) {
- Result = ShSetVariable(Shell,
- SHELL_OPTION_ARGUMENT,
- sizeof(SHELL_OPTION_ARGUMENT),
- OptionArgument,
- strlen(OptionArgument) + 1);
- if (Result == FALSE) {
- ReturnValue += 1;
- goto BuiltinGetoptsEnd;
- }
- } else {
- ShUnsetVariableOrFunction(Shell,
- SHELL_OPTION_ARGUMENT,
- sizeof(SHELL_OPTION_ARGUMENT),
- ShellUnsetDefault);
- }
- //
- // If the end of options was reached, return a non-zero value.
- //
- if (EndOfOptions != FALSE) {
- ReturnValue += 1;
- }
- BuiltinGetoptsEnd:
- if (OptionArgument != NULL) {
- free(OptionArgument);
- }
- if (UsingPositionalParameters != FALSE) {
- if (ExpandedArguments != NULL) {
- free(ExpandedArguments);
- }
- if (OptionsArguments != NULL) {
- free(OptionsArguments);
- }
- }
- return ReturnValue;
- }
- INT
- ShBuiltinCommand (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the command builtin command, which runs the given
- command without invoking shell functions.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- Return Value:
- 0 on success.
- 1 on failure.
- --*/
- {
- INT Result;
- Result = ShBuiltinTypeOrCommand(Shell, ArgumentCount, Arguments, FALSE);
- return Result;
- }
- INT
- ShBuiltinType (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments
- )
- /*++
- Routine Description:
- This routine implements the 'type' builtin command, which describes the
- given commands.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- Return Value:
- 0 on success.
- 1 on failure.
- --*/
- {
- INT Result;
- Result = ShBuiltinTypeOrCommand(Shell, ArgumentCount, Arguments, TRUE);
- return Result;
- }
- INT
- ShBuiltinTypeOrCommand (
- PSHELL Shell,
- INT ArgumentCount,
- PSTR *Arguments,
- BOOL IsType
- )
- /*++
- Routine Description:
- This routine implements the command or type builtins, which run or describe
- a command.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments on the command line.
- Arguments - Supplies the array of pointers to strings representing each
- argument. Arguments can be -v to unset a variable (the default), or -f
- to unset a function. This is followed by the variable or function name.
- IsType - Supplies a boolean indicating whether this is the type builtin
- function (TRUE) or the command builtin function (FALSE).
- Return Value:
- 0 on success.
- 1 on failure.
- --*/
- {
- PSTR Argument;
- INT ArgumentIndex;
- PSHELL_BUILTIN_COMMAND BuiltinCommand;
- BOOL DefaultPath;
- PSTR OriginalPath;
- UINTN OriginalPathSize;
- BOOL PrintPath;
- INT ReturnValue;
- INT TotalReturnValue;
- BOOL Verbose;
- DefaultPath = FALSE;
- OriginalPath = NULL;
- PrintPath = FALSE;
- Verbose = FALSE;
- TotalReturnValue = 0;
- //
- // Type acts just like command -v.
- //
- if (IsType != FALSE) {
- Verbose = TRUE;
- ArgumentIndex = 1;
- //
- // Handle the arguments for the command command.
- //
- } else {
- for (ArgumentIndex = 1;
- ArgumentIndex < ArgumentCount;
- ArgumentIndex += 1) {
- Argument = Arguments[ArgumentIndex];
- if (*Argument != '-') {
- break;
- }
- Argument += 1;
- if (*Argument == '-') {
- break;
- }
- while (*Argument != '\0') {
- switch (*Argument) {
- case 'p':
- DefaultPath = TRUE;
- break;
- case 'v':
- PrintPath = TRUE;
- break;
- case 'V':
- Verbose = TRUE;
- break;
- default:
- fprintf(stderr, "command: Invalid option %c.\n", *Argument);
- break;
- }
- Argument += 1;
- }
- }
- if (PrintPath != FALSE) {
- Verbose = FALSE;
- }
- }
- Arguments += ArgumentIndex;
- ArgumentCount -= ArgumentIndex;
- //
- // If the command is empty, don't do much.
- //
- if ((ArgumentCount == 0) || (strlen(Arguments[0]) == 0)) {
- return 0;
- }
- if (DefaultPath != FALSE) {
- ShGetVariable(Shell,
- SHELL_PATH,
- sizeof(SHELL_PATH),
- &OriginalPath,
- &OriginalPathSize);
- OriginalPath = SwStringDuplicate(OriginalPath, OriginalPathSize);
- ShSetVariableWithProperties(Shell,
- SHELL_PATH,
- sizeof(SHELL_PATH),
- SHELL_COMMAND_BUILTIN_PATH,
- sizeof(SHELL_COMMAND_BUILTIN_PATH),
- TRUE,
- FALSE,
- TRUE);
- }
- if ((Verbose != FALSE) || (PrintPath != FALSE)) {
- //
- // Loop over all the arguments, though if this is not the 'type'
- // builtin this will break after the first iteration.
- //
- while (ArgumentCount != 0) {
- ReturnValue = ShClassifyCommand(Shell, Arguments[0], Verbose);
- if (ReturnValue != 0) {
- TotalReturnValue = ReturnValue;
- }
- if (IsType == FALSE) {
- break;
- }
- Arguments += 1;
- ArgumentCount -= 1;
- }
- //
- // Really run the command.
- //
- } else {
- BuiltinCommand = ShIsBuiltinCommand(Arguments[0]);
- if (BuiltinCommand != NULL) {
- ReturnValue = ShRunBuiltinCommand(Shell,
- BuiltinCommand,
- ArgumentCount,
- Arguments);
- } else {
- ShRunCommand(Shell,
- Arguments[0],
- Arguments,
- ArgumentCount,
- FALSE,
- &ReturnValue);
- }
- }
- if ((TotalReturnValue == 0) && (ReturnValue != 0)) {
- TotalReturnValue = ReturnValue;
- }
- if (OriginalPath != NULL) {
- ShSetVariable(Shell,
- SHELL_PATH,
- sizeof(SHELL_PATH),
- OriginalPath,
- OriginalPathSize);
- }
- return TotalReturnValue;
- }
- INT
- ShClassifyCommand (
- PSHELL Shell,
- PSTR Command,
- BOOL Verbose
- )
- /*++
- Routine Description:
- This routine classifies and prints the classification for the given command.
- Arguments:
- Shell - Supplies a pointer to the shell.
- Command - Supplies a pointer to the command to classify.
- Verbose - Supplies a boolean indicating whether to print the verbose
- description or just the command path (or name).
- Return Value:
- 0 on success.
- 127 if the command could not be found.
- --*/
- {
- PSHELL_ALIAS Alias;
- PSHELL_BUILTIN_COMMAND BuiltinCommand;
- PSTR FullCommandPath;
- ULONG FullCommandPathSize;
- PSHELL_FUNCTION Function;
- PSTR ReservedWord;
- UINTN ReservedWordIndex;
- BOOL Result;
- INT ReturnValue;
- //
- // First look to see if it is a reserved word.
- //
- ReservedWordIndex = 0;
- while (ShReservedWords[ReservedWordIndex] != NULL) {
- ReservedWord = ShReservedWords[ReservedWordIndex];
- if (strcmp(Command, ReservedWord) == 0) {
- if (Verbose != FALSE) {
- printf("%s is a shell keyword\n", Command);
- } else {
- printf("%s\n", Command);
- }
- return 0;
- }
- ReservedWordIndex += 1;
- }
- BuiltinCommand = ShIsBuiltinCommand(Command);
- if (BuiltinCommand != NULL) {
- ReturnValue = 0;
- if (Verbose != FALSE) {
- printf("%s is a shell builtin\n", Command);
- } else {
- printf("%s\n", Command);
- }
- return 0;
- }
- //
- // Then look to see if it is an alias.
- //
- Alias = ShLookupAlias(Shell, Command, strlen(Command) + 1);
- if (Alias != NULL) {
- if (Verbose != FALSE) {
- printf("%s is an alias for %s\n", Command, Alias->Value);
- } else {
- printf("alias %s='%s'\n", Command, Alias->Value);
- }
- return 0;
- }
- //
- // Look to see if this is a function.
- //
- Function = ShGetFunction(Shell, Command, strlen(Command) + 1);
- if (Function != NULL) {
- if (Verbose != FALSE) {
- printf("%s is a shell function\n", Command);
- } else {
- printf("%s\n", Command);
- }
- return 0;
- }
- //
- // Attempt to locate the command in the path.
- //
- ReturnValue = 0;
- FullCommandPath = NULL;
- Result = ShLocateCommand(Shell,
- Command,
- strlen(Command) + 1,
- TRUE,
- &FullCommandPath,
- &FullCommandPathSize,
- &ReturnValue);
- if (Result == FALSE) {
- ReturnValue = SHELL_ERROR_OPEN;
- }
- if (ReturnValue != 0) {
- if (Verbose != FALSE) {
- ShPrintTrace(Shell, "sh: %s: Command not found.\n", Command);
- }
- } else {
- if (Verbose != FALSE) {
- printf("%s is %s\n", Command, FullCommandPath);
- } else {
- printf("%s\n", FullCommandPath);
- }
- }
- if ((FullCommandPath != NULL) && (FullCommandPath != Command)) {
- free(FullCommandPath);
- }
- return ReturnValue;
- }
- INT
- ShGetNextOption (
- PSHELL Shell,
- ULONG ArgumentCount,
- PSTR *Arguments,
- PINT ArgumentIndex,
- PSTR Options,
- PCHAR Option,
- PSTR *OptionArgument,
- PBOOL EndOfOptions
- )
- /*++
- Routine Description:
- This routine gets the next option from the list of arguments. Starting at
- the given argument index, it determines if that argument string begins with
- an option.
- Arguments:
- Shell - Supplies a pointer to the shell.
- ArgumentCount - Supplies the number of arguments in the arguments array.
- Arguments - Supplies the arguments to parse. The first array item is the
- first argument (e.g. positional parameter 1). On return it will contain
- the index of the next argument to parse.
- ArgumentIndex - Supplies the index into the argument array that should be
- analyzed for an option.
- Options - Supplies the options string.
- Option - Supplies a pointer that receives the parsed option.
- OptionArgument - Supplies a pointer that receives the option's argument
- string. The caller is responsible for releasing this memory.
- EndOfOptions - Supplies a pointer that receives a boolean indicating
- whether or not the end of options has been reached.
- Return Value:
- 1 on success.
- 0 on failure.
- --*/
- {
- PSTR Argument;
- CHAR CharacterString[2];
- PSTR OptionError;
- BOOL Result;
- BOOL StartsWithColon;
- *OptionArgument = NULL;
- *EndOfOptions = FALSE;
- //
- // If the argument index is beyond the bounds of the array, then return as
- // if the end of the options was reached.
- //
- if (*ArgumentIndex >= ArgumentCount) {
- *ArgumentIndex = ArgumentCount;
- *Option = '?';
- ShNextOptionCharacter = 0;
- *EndOfOptions = TRUE;
- return 1;
- }
- //
- // If the next option character value is 0, then this argument has not been
- // processed.
- //
- Argument = Arguments[*ArgumentIndex];
- if (ShNextOptionCharacter == 0) {
- //
- // If the argument does not start with a dash, then this is the end of
- // the arguments.
- //
- if (*Argument != '-') {
- *Option = '?';
- *EndOfOptions = TRUE;
- return 1;
- }
- Argument += 1;
- //
- // If this is the "--" argument, then it's also the end of the line.
- //
- if ((*Argument == '-') && (*(Argument + 1) == '\0')) {
- *Option = '?';
- //
- // If the "--" has not yet been seen, then the index needs to jump
- // to the next argument.
- //
- if (ShSeenDoubleDash == FALSE) {
- *ArgumentIndex += 1;
- ShSeenDoubleDash = TRUE;
- }
- *EndOfOptions = TRUE;
- return 1;
- }
- //
- // Ok. There might be some options in this argument, start looking at
- // the second character.
- //
- ShNextOptionCharacter = 1;
- } else {
- Argument += ShNextOptionCharacter;
- }
- assert(*Argument != '\0');
- StartsWithColon = FALSE;
- if (*Options == ':') {
- StartsWithColon = TRUE;
- Options += 1;
- }
- //
- // Loop over every acceptible option.
- //
- while (*Options != '\0') {
- //
- // Keep looking if they are not equal.
- //
- if ((!isalnum(*Options)) || (*Argument != *Options)) {
- Options += 1;
- continue;
- }
- //
- // They're equal, look to see if the next character is a colon.
- //
- *Option = *Options;
- Options += 1;
- ShNextOptionCharacter += 1;
- Argument += 1;
- //
- // If no argument is required, then work here is done.
- //
- if (*Options != ':') {
- //
- // If the next character of the argument is the terminator, then
- // up the index and reset the option character.
- //
- if (*Argument == '\0') {
- ShNextOptionCharacter = 0;
- *ArgumentIndex += 1;
- }
- return 1;
- }
- Options += 1;
- //
- // An argument is required. Optional arguments are not supported for
- // the built-in shell option parsing. If the next character of the
- // argument is not null, then the argument is the remainder.
- //
- ShNextOptionCharacter = 0;
- if (*Argument != '\0') {
- *OptionArgument = strdup(Argument);
- if (*OptionArgument == NULL) {
- return 0;
- }
- *ArgumentIndex += 1;
- return 1;
- }
- //
- // It must be the next argument. If there is no next argument, that's a
- // problem.
- //
- if (*ArgumentIndex >= (ArgumentCount - 1)) {
- *ArgumentIndex += 1;
- if (StartsWithColon != FALSE) {
- CharacterString[0] = *Option;
- CharacterString[1] = '\0';
- *OptionArgument = strdup(CharacterString);
- if (*OptionArgument == NULL) {
- return 0;
- }
- *Option = ':';
- } else {
- assert(*OptionArgument == NULL);
- Result = ShGetVariable(Shell,
- SHELL_OPTION_ERROR,
- sizeof(SHELL_OPTION_ERROR),
- &OptionError,
- NULL);
- if ((Result == FALSE) || (strcmp(OptionError, "0") != 0)) {
- fprintf(stderr,
- "%s: option -%c requires an argument.\n",
- Shell->CommandName,
- *Option);
- }
- *Option = '?';
- }
- return 1;
- }
- *ArgumentIndex += 1;
- *OptionArgument = strdup(Arguments[*ArgumentIndex]);
- if (*OptionArgument == NULL) {
- return 0;
- }
- *ArgumentIndex += 1;
- return 1;
- }
- //
- // The argument doesn't match any of the acceptable options.
- //
- if (StartsWithColon != FALSE) {
- CharacterString[0] = *Option;
- CharacterString[1] = '\0';
- *OptionArgument = strdup(CharacterString);
- if (*OptionArgument == NULL) {
- return 0;
- }
- } else {
- assert(*OptionArgument == NULL);
- Result = ShGetVariable(Shell,
- SHELL_OPTION_ERROR,
- sizeof(SHELL_OPTION_ERROR),
- &OptionError,
- NULL);
- if ((Result == FALSE) || (strcmp(OptionError, "0") != 0)) {
- fprintf(stderr,
- "%s: Invalid option -%c.\n",
- Shell->CommandName,
- *Argument);
- }
- }
- //
- // Skip to the next option. It could be in the next argument or next
- // character.
- //
- Argument += 1;
- if (*Argument == '\0') {
- ShNextOptionCharacter = 0;
- *ArgumentIndex += 1;
- } else {
- ShNextOptionCharacter += 1;
- }
- *Option = '?';
- return 1;
- }
|