12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664 |
- /*++
- Copyright (c) 2013 Minoca Corp. All Rights Reserved
- Module Name:
- sedfunc.c
- Abstract:
- This module implements the actual editing functions for the sed utility.
- Author:
- Evan Green 12-Jul-2013
- Environment:
- POSIX
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "sed.h"
- #include <assert.h>
- #include <ctype.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include "../swlib.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // Define the number of columns to print when displaying with the 'l' (ell)
- // command.
- //
- #define SED_PRINT_COLUMNS 80
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- INT
- SedExecuteGroup (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteAppend (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteBranchOrTest (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteDeleteAndPrintText (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteDelete (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteHoldSpaceToPattern (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecutePatternSpaceToHold (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecutePrint (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecutePrintEscapedText (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteMoveToNextLine (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteWritePatternSpace (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteQuit (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteSubstitute (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteWriteFile (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteExchangePatternAndHold (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteSubstituteCharacters (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteNop (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- INT
- SedExecuteWriteLineNumber (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- PSED_EXECUTE_FUNCTION SedFunctionTable[SedFunctionCount] = {
- NULL,
- SedExecuteGroup,
- SedExecuteAppend,
- SedExecuteBranchOrTest,
- SedExecuteDeleteAndPrintText,
- SedExecuteDelete,
- SedExecuteDelete,
- SedExecuteHoldSpaceToPattern,
- SedExecuteHoldSpaceToPattern,
- SedExecutePatternSpaceToHold,
- SedExecutePatternSpaceToHold,
- SedExecutePrint,
- SedExecutePrintEscapedText,
- SedExecuteMoveToNextLine,
- SedExecuteMoveToNextLine,
- SedExecuteWritePatternSpace,
- SedExecuteWritePatternSpace,
- SedExecuteQuit,
- SedExecuteAppend,
- SedExecuteSubstitute,
- SedExecuteBranchOrTest,
- SedExecuteWriteFile,
- SedExecuteExchangePatternAndHold,
- SedExecuteSubstituteCharacters,
- SedExecuteNop,
- SedExecuteWriteLineNumber,
- SedExecuteNop,
- };
- //
- // ------------------------------------------------------------------ Functions
- //
- INT
- SedExecuteGroup (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a group command.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- assert(Command->Function.Type == SedFunctionGroup);
- //
- // Set the next command to be the first child.
- //
- if (LIST_EMPTY(&(Command->Function.U.ChildList)) == FALSE) {
- Context->NextCommand = LIST_VALUE(Command->Function.U.ChildList.Next,
- SED_COMMAND,
- ListEntry);
- }
- return 0;
- }
- INT
- SedExecuteAppend (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes "print text at line end" or "read file" command.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- PSED_APPEND_ENTRY AppendEntry;
- assert((Command->Function.Type == SedFunctionPrintTextAtLineEnd) ||
- (Command->Function.Type == SedFunctionReadFile));
- if (Command->Function.U.StringArgument == NULL) {
- return 0;
- }
- //
- // Allocate and fill out an append entry structure.
- //
- AppendEntry = malloc(sizeof(SED_APPEND_ENTRY));
- if (AppendEntry == NULL) {
- return ENOMEM;
- }
- AppendEntry->Type = Command->Function.Type;
- AppendEntry->StringOrPath = Command->Function.U.StringArgument;
- //
- // Stick the command on the end of the append list.
- //
- INSERT_BEFORE(&(AppendEntry->ListEntry), &(Context->AppendList));
- return 0;
- }
- INT
- SedExecuteBranchOrTest (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a branch command or a test command.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- int Match;
- PSED_STRING Name;
- PSED_COMMAND NextCommand;
- assert((Command->Function.Type == SedFunctionBranch) ||
- (Command->Function.Type == SedFunctionTest));
- Name = Command->Function.U.StringArgument;
- //
- // For tests, check the test result, which indicates whether anything has
- // been substituted since the last line was read from the input or the
- // last test command.
- //
- if (Command->Function.Type == SedFunctionTest) {
- if (Context->TestResult == FALSE) {
- return 0;
- }
- Context->TestResult = FALSE;
- }
- //
- // Loop looking for an entry with the given label.
- //
- NextCommand = Command;
- Command = &(Context->HeadCommand);
- while (NextCommand != NULL) {
- if ((Command->Function.Type == SedFunctionGroup) &&
- (LIST_EMPTY(&(Command->Function.U.ChildList)) == FALSE)) {
- NextCommand = LIST_VALUE(Command->Function.U.ChildList.Next,
- SED_COMMAND,
- ListEntry);
- } else {
- //
- // Fill in the next command by moving on to the next sibling, or up
- // the chain if necessary. Stop if the head command is reached.
- //
- while (NextCommand != &(Context->HeadCommand)) {
- //
- // If there's a sibling, go to it.
- //
- if (NextCommand->ListEntry.Next !=
- &(NextCommand->Parent->Function.U.ChildList)) {
- NextCommand = LIST_VALUE(NextCommand->ListEntry.Next,
- SED_COMMAND,
- ListEntry);
- break;
- //
- // Move up to the parent.
- //
- } else {
- NextCommand = NextCommand->Parent;
- }
- }
- }
- if (NextCommand == &(Context->HeadCommand)) {
- NextCommand = NULL;
- }
- //
- // Check it out if this is a label.
- //
- if (Command->Function.Type == SedFunctionLabel) {
- //
- // If they're both null, this is a match.
- //
- if ((Name == NULL) &&
- (Command->Function.U.StringArgument == NULL)) {
- break;
- }
- //
- // If they're both not null, test to see if the strings are equal.
- //
- if ((Name != NULL) &&
- (Command->Function.U.StringArgument != NULL)) {
- Match = strcmp(Name->Data,
- Command->Function.U.StringArgument->Data);
- if (Match == 0) {
- break;
- }
- }
- }
- //
- // Move on to the next command.
- //
- Command = NextCommand;
- }
- //
- // If the label wasn't found, it branches to the end of the script, which
- // is null.
- //
- assert((Command == NULL) || (Command->Function.Type == SedFunctionLabel));
- Context->NextCommand = Command;
- return 0;
- }
- INT
- SedExecuteDeleteAndPrintText (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a "delete and print text", which deletes the current
- pattern space. For a 0 or 1 address match, it prints the text. For a 2
- address match, it prints the text at the end of the range.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- assert(Command->Function.Type == SedFunctionDeleteAndPrintText);
- //
- // Delete the pattern space.
- //
- assert(Context->PatternSpace->Size != 0);
- Context->PatternSpace->Data[0] = '\0';
- Context->PatternSpace->Size = 1;
- //
- // Print the text if this isn't a 2-address form or if the active flag is
- // off in the two address form.
- //
- if ((Command->AddressCount < 2) || (Command->Active == FALSE)) {
- if (Command->Function.U.StringArgument != NULL) {
- SedPrint(Context, Command->Function.U.StringArgument->Data, EOF);
- }
- }
- Context->NextCommand = NULL;
- Context->SkipPrint = TRUE;
- return 0;
- }
- INT
- SedExecuteDelete (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a "delete pattern space and start next cycle" command
- or a "delete pattern space up to the next newline and start next cycle"
- command.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- PSED_STRING Pattern;
- UINTN Size;
- assert((Command->Function.Type == SedFunctionDelete) ||
- (Command->Function.Type == SedFunctionDeleteToNewline));
- Pattern = Context->PatternSpace;
- assert(Pattern->Size != 0);
- //
- // Delete up to the next newline in the pattern space.
- //
- if (Command->Function.Type == SedFunctionDeleteToNewline) {
- Size = 0;
- while ((Size < Pattern->Size) && (Pattern->Data[Size] != '\n')) {
- Size += 1;
- }
- if (Size == Pattern->Size) {
- Pattern->Data[0] = '\0';
- Pattern->Size = 1;
- } else {
- SwStringRemoveRegion(Pattern->Data, &(Pattern->Size), 0, Size + 1);
- }
- //
- // Just delete the whole line.
- //
- } else {
- Pattern->Data[0] = '\0';
- Pattern->Size = 1;
- }
- //
- // If there's nothing left, go to the end of this cycle.
- //
- if (Pattern->Size == 1) {
- Context->NextCommand = NULL;
- Context->SkipPrint = TRUE;
- //
- // Move to the top of the cycle with the input that's there.
- //
- } else {
- Context->NextCommand = LIST_VALUE(
- Context->HeadCommand.Function.U.ChildList.Next,
- SED_COMMAND,
- ListEntry);
- }
- return 0;
- }
- INT
- SedExecuteHoldSpaceToPattern (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a "replace pattern space with hold space" or
- "append newline plus hold space to pattern space" command.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- PSED_STRING Hold;
- PSED_STRING Pattern;
- BOOL Result;
- assert((Command->Function.Type == SedFunctionReplacePatternWithHold) ||
- (Command->Function.Type == SedFunctionAppendHoldToPattern));
- Hold = Context->HoldSpace;
- Pattern = Context->PatternSpace;
- assert(Hold->Size != 0);
- assert(Pattern->Size != 0);
- //
- // If appending, add a newline.
- //
- if (Command->Function.Type == SedFunctionAppendHoldToPattern) {
- Result = SedAppendString(Pattern, "\n", 1);
- if (Result == FALSE) {
- return ENOMEM;
- }
- //
- // If replacing, delete the pattern space.
- //
- } else {
- Pattern->Data[0] = '\0';
- Pattern->Size = 1;
- }
- //
- // Now append the hold space.
- //
- Result = SedAppendString(Pattern, Hold->Data, Hold->Size);
- if (Result == FALSE) {
- return ENOMEM;
- }
- return 0;
- }
- INT
- SedExecutePatternSpaceToHold (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a "replace hold space with pattern space" or
- "append newline plus pattern space to hold space" command.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- PSED_STRING Hold;
- PSED_STRING Pattern;
- BOOL Result;
- assert((Command->Function.Type == SedFunctionReplaceHoldWithPattern) ||
- (Command->Function.Type == SedFunctionAppendPatternToHold));
- Hold = Context->HoldSpace;
- Pattern = Context->PatternSpace;
- assert(Hold->Size != 0);
- assert(Pattern->Size != 0);
- //
- // If appending, add a newline.
- //
- if (Command->Function.Type == SedFunctionAppendPatternToHold) {
- Result = SedAppendString(Hold, "\n", 1);
- if (Result == FALSE) {
- return ENOMEM;
- }
- //
- // If replacing, delete the pattern space.
- //
- } else {
- Hold->Data[0] = '\0';
- Hold->Size = 1;
- }
- //
- // Now append the pattern space.
- //
- Result = SedAppendString(Hold, Pattern->Data, Pattern->Size);
- if (Result == FALSE) {
- return ENOMEM;
- }
- return 0;
- }
- INT
- SedExecutePrint (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a print (i) command.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- assert(Command->Function.Type == SedFunctionPrintText);
- if (Command->Function.U.StringArgument != NULL) {
- SedPrint(Context, Command->Function.U.StringArgument->Data, '\n');
- }
- return 0;
- }
- INT
- SedExecutePrintEscapedText (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a print (i) command.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- CHAR Character;
- ULONG Column;
- UINTN Index;
- PSED_STRING Pattern;
- assert(Command->Function.Type == SedFunctionWritePatternEscaped);
- Pattern = Context->PatternSpace;
- assert(Pattern->Size != 0);
- Column = 0;
- for (Index = 0; Index < Pattern->Size - 1; Index += 1) {
- Character = Pattern->Data[Index];
- //
- // If it's printable, just print it out straight up.
- //
- if (isprint(Character)) {
- if (Column >= SED_PRINT_COLUMNS) {
- printf("\\\n");
- Column = 0;
- }
- printf("%c", Character);
- Column += 1;
- //
- // Print out an escape sequence if it's one of the common ones.
- //
- } else if ((Character == '\\') || (Character == '\a') ||
- (Character == '\b') || (Character == '\f') ||
- (Character == '\r') || (Character == '\v') ||
- (Character == '\n')) {
- if (Column + 1 >= SED_PRINT_COLUMNS) {
- printf("\\\n");
- Column = 0;
- }
- if (Character == '\\') {
- Character = '\\';
- } else if (Character == '\a') {
- Character = 'a';
- } else if (Character == '\b') {
- Character = 'b';
- } else if (Character == '\f') {
- Character = 'f';
- } else if (Character == '\r') {
- Character = 'r';
- } else if (Character == '\t') {
- Character = 't';
- } else if (Character == '\v') {
- Character = 'v';
- } else if (Character == '\n') {
- Character = 'n';
- }
- printf("\\%c", Character);
- Column += 2;
- //
- // It's a weird character, print out its octal representation.
- //
- } else {
- if (Column + 3 > SED_PRINT_COLUMNS) {
- printf("\\\n");
- Column = 0;
- }
- printf("\\%03o", Character);
- }
- }
- printf("$\n");
- Context->StandardOut.LineTerminated = TRUE;
- return 0;
- }
- INT
- SedExecuteMoveToNextLine (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a "move to next line" command, which breaks out of
- the current cycle and moves to the next.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- CHAR Character;
- PSED_STRING Pattern;
- INT Status;
- assert((Command->Function.Type == SedFunctionMoveToNextLine) ||
- (Command->Function.Type == SedFunctionAppendNextLine));
- Pattern = Context->PatternSpace;
- if (Command->Function.Type == SedFunctionMoveToNextLine) {
- //
- // If directed, print the pattern space.
- //
- if (Context->PrintLines != FALSE) {
- SedPrint(Context, Pattern->Data, Context->LineTerminator);
- }
- //
- // Clear the current pattern space.
- //
- Pattern->Data[0] = '\0';
- Pattern->Size = 1;
- } else {
- Character = '\n';
- Status = SedAppendString(Pattern, &Character, 1);
- if (Status == FALSE) {
- return ENOMEM;
- }
- }
- //
- // Append the next line.
- //
- Status = SedReadLine(Context);
- if (Status != 0) {
- return Status;
- }
- //
- // If there was no more input, then move to the end of the script.
- //
- if (Context->Done != FALSE) {
- Context->NextCommand = NULL;
- Context->SkipPrint = TRUE;
- }
- return 0;
- }
- INT
- SedExecuteWritePatternSpace (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a command to write all (p) or part (P) of the pattern
- space to standard out.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- UINTN Index;
- PSED_STRING Pattern;
- assert((Command->Function.Type == SedFunctionWritePattern) ||
- (Command->Function.Type == SedFunctionWritePatternToNewline));
- Pattern = Context->PatternSpace;
- assert(Pattern->Size != 0);
- if (Command->Function.Type == SedFunctionWritePattern) {
- printf("%s\n", Pattern->Data);
- } else {
- Index = 0;
- while ((Index < Pattern->Size) && (Pattern->Data[Index] != '\n')) {
- Index += 1;
- }
- if (Index == Pattern->Size) {
- SedWrite(&(Context->StandardOut),
- Pattern->Data,
- Pattern->Size - 1,
- '\n');
- } else {
- SedWrite(&(Context->StandardOut),
- Pattern->Data,
- Index,
- '\n');
- }
- }
- return 0;
- }
- INT
- SedExecuteQuit (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a quit command.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- Context->Quit = TRUE;
- return 0;
- }
- INT
- SedExecuteSubstitute (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a substitute command.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- UINTN InputEnd;
- UINTN InputStart;
- ULONG Occurrence;
- PSED_STRING Pattern;
- UINTN PatternOffset;
- PSTR Replace;
- ULONG ReplaceEnd;
- PSED_STRING Replacement;
- CHAR ReplacementCharacter;
- UINTN ReplacementIndex;
- UINTN ReplacementSize;
- ULONG ReplaceStart;
- int Result;
- LONG Subexpression;
- PSED_SUBSTITUTE Substitute;
- BOOL SubstitutionMade;
- UINTN TotalEndOffset;
- UINTN TotalStartOffset;
- BOOL WasBackslash;
- Pattern = Context->PatternSpace;
- ReplaceEnd = 0;
- Replacement = NULL;
- ReplaceStart = 0;
- Substitute = &(Command->Function.U.Substitute);
- SubstitutionMade = FALSE;
- assert(Command->Function.Type == SedFunctionSubstitute);
- //
- // Loop making subsitutions.
- //
- Occurrence = 0;
- PatternOffset = 0;
- while (PatternOffset < Pattern->Size) {
- Result = regexec(&(Substitute->Expression),
- Pattern->Data + PatternOffset,
- Substitute->MatchCount,
- Substitute->Matches,
- 0);
- //
- // If there was no match, stop now.
- //
- if (Result != 0) {
- break;
- }
- //
- // If there's a specific occurrence number and this isn't it, continue
- // on.
- //
- Occurrence += 1;
- if ((Substitute->OccurrenceNumber != 0) &&
- (Substitute->OccurrenceNumber != Occurrence)) {
- PatternOffset += Substitute->Matches[0].rm_eo;
- continue;
- }
- //
- // Generate the replacement. Start off by creating a copy of the
- // replacement template.
- //
- Replacement = SedCreateString(Substitute->Replacement->Data,
- Substitute->Replacement->Size,
- TRUE);
- //
- // Loop through the replacement converting as necessary.
- //
- ReplacementIndex = 0;
- WasBackslash = FALSE;
- Replace = Replacement->Data;
- ReplacementCharacter = 0;
- InputStart = 0;
- InputEnd = 0;
- while (ReplacementIndex < Replacement->Size) {
- if (WasBackslash != FALSE) {
- if (*Replace == '\\') {
- ReplacementCharacter = '\\';
- } else if (*Replace == 'a') {
- ReplacementCharacter = '\a';
- } else if (*Replace == 'b') {
- ReplacementCharacter = '\b';
- } else if (*Replace == 'f') {
- ReplacementCharacter = '\f';
- } else if (*Replace == 'r') {
- ReplacementCharacter = '\r';
- } else if (*Replace == 't') {
- ReplacementCharacter = '\t';
- } else if (*Replace == 'v') {
- ReplacementCharacter = '\v';
- } else if (*Replace == 'n') {
- ReplacementCharacter = '\n';
- } else if (isdigit(*Replace)) {
- Subexpression = *Replace - '0';
- if ((Substitute->Matches[Subexpression].rm_so != -1) &&
- (Substitute->Matches[Subexpression].rm_eo != -1)) {
- InputStart = Substitute->Matches[Subexpression].rm_so;
- InputEnd = Substitute->Matches[Subexpression].rm_eo;
- } else {
- InputStart = 0;
- InputEnd = 0;
- }
- ReplaceStart = ReplacementIndex - 1;
- ReplaceEnd = ReplacementIndex + 1;
- } else {
- ReplacementCharacter = *Replace;
- }
- if (ReplacementCharacter != 0) {
- Result = SwStringReplaceRegion(&(Replacement->Data),
- &(Replacement->Size),
- &(Replacement->Capacity),
- ReplacementIndex - 1,
- ReplacementIndex + 1,
- &ReplacementCharacter,
- 2);
- if (Result == FALSE) {
- Result = ENOMEM;
- goto ExecuteSubstituteEnd;
- }
- //
- // Back up since new characters just shifted down.
- //
- ReplacementIndex -= 1;
- Replace = Replacement->Data + ReplacementIndex;
- ReplacementCharacter = 0;
- }
- //
- // If it wasn't a backslash, the only special character is an
- // ampersand, which matches the whole thing.
- //
- } else {
- if (*Replace == '&') {
- assert((Substitute->Matches[0].rm_so != -1) &&
- (Substitute->Matches[0].rm_eo != -1));
- InputStart = Substitute->Matches[0].rm_so;
- InputEnd = Substitute->Matches[0].rm_eo;
- ReplaceStart = ReplacementIndex;
- ReplaceEnd = ReplacementIndex + 1;
- }
- }
- //
- // If someone requested a replacement with part of the input
- // string, go for it.
- //
- if (ReplaceStart != ReplaceEnd) {
- assert((InputStart + PatternOffset < Pattern->Size) &&
- (InputEnd + PatternOffset <= Pattern->Size));
- //
- // The pattern offset is added because the regular expression
- // was searched with the input plus the pattern offset.
- //
- TotalStartOffset = InputStart + PatternOffset;
- Result = SwStringReplaceRegion(&(Replacement->Data),
- &(Replacement->Size),
- &(Replacement->Capacity),
- ReplaceStart,
- ReplaceEnd,
- Pattern->Data + TotalStartOffset,
- InputEnd - InputStart + 1);
- if (Result == FALSE) {
- Result = ENOMEM;
- goto ExecuteSubstituteEnd;
- }
- //
- // Reset the current index as the string was just moved around
- // and may have been replaced entirely.
- //
- ReplacementIndex = ReplaceStart + (InputEnd - InputStart) - 1;
- Replace = Replacement->Data + ReplacementIndex;
- ReplaceStart = 0;
- ReplaceEnd = 0;
- }
- if (*Replace == '\\') {
- WasBackslash = !WasBackslash;
- } else {
- WasBackslash = FALSE;
- }
- Replace += 1;
- ReplacementIndex += 1;
- }
- //
- // Now that the replacement string has finally been created, replace
- // the portion of the pattern that matched with the replacement string.
- // Again, the matches are all relative to the pattern offset.
- //
- ReplacementSize = Replacement->Size;
- assert((ReplacementSize != 0) &&
- (Substitute->Matches[0].rm_so != -1) &&
- (Substitute->Matches[0].rm_eo != -1));
- TotalStartOffset = Substitute->Matches[0].rm_so + PatternOffset;
- TotalEndOffset = Substitute->Matches[0].rm_eo + PatternOffset;
- Result = SwStringReplaceRegion(&(Pattern->Data),
- &(Pattern->Size),
- &(Pattern->Capacity),
- TotalStartOffset,
- TotalEndOffset,
- Replacement->Data,
- ReplacementSize);
- if (Result == FALSE) {
- Result = ENOMEM;
- goto ExecuteSubstituteEnd;
- }
- //
- // Move to the end of the replacement for the next substitution.
- //
- PatternOffset = TotalStartOffset + ReplacementSize - 1;
- SubstitutionMade = TRUE;
- SedDestroyString(Replacement);
- Replacement = NULL;
- //
- // If the global flag is off, stop.
- //
- if ((Substitute->Flags & SED_SUBSTITUTE_FLAG_GLOBAL) == 0) {
- break;
- }
- //
- // Handle an empty match.
- //
- if (Substitute->Matches[0].rm_so == Substitute->Matches[0].rm_eo) {
- //
- // If the match was empty and the end of the string, then stop, as
- // the end of the string is just going to match empty again.
- //
- if (PatternOffset == Pattern->Size - 1) {
- break;
- //
- // If the replacement was empty, then move the pattern forward
- // to prevent replacing empty with empty forever.
- //
- } else if (ReplacementSize == 1) {
- PatternOffset += 1;
- }
- }
- }
- //
- // If a substitution was made and the caller wants it printed or written
- // to a file, do that now.
- //
- if ((SubstitutionMade != FALSE) &&
- ((Substitute->Flags & SED_SUBSTITUTE_FLAG_PRINT) != 0)) {
- SedPrint(Context, Pattern->Data, Context->LineTerminator);
- }
- if ((SubstitutionMade != FALSE) &&
- ((Substitute->Flags & SED_SUBSTITUTE_FLAG_WRITE) != 0)) {
- Result = SedWrite(Substitute->WriteFile,
- Pattern->Data,
- Pattern->Size - 1,
- Context->LineTerminator);
- if (Result != 0) {
- goto ExecuteSubstituteEnd;
- }
- }
- //
- // Mark if a substitution was made for any future test commands.
- //
- Context->TestResult = SubstitutionMade;
- Result = 0;
- ExecuteSubstituteEnd:
- if (Replacement != NULL) {
- SedDestroyString(Replacement);
- }
- return Result;
- }
- INT
- SedExecuteWriteFile (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a "write to file" command, which writes the pattern
- space plus a newline.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- INT Result;
- PSED_WRITE_FILE WriteFile;
- assert(Command->Function.Type == SedFunctionWriteFile);
- assert(Context->PatternSpace->Size != 0);
- WriteFile = Command->Function.U.WriteFile;
- Result = SedWrite(WriteFile,
- Context->PatternSpace->Data,
- Context->PatternSpace->Size - 1,
- Context->LineTerminator);
- return Result;
- }
- INT
- SedExecuteExchangePatternAndHold (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes the "exchange pattern space and hold space" command,
- which does exactly what it sounds like.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- PSED_STRING Swap;
- assert(Command->Function.Type == SedFunctionExchangePatternAndHold);
- Swap = Context->PatternSpace;
- Context->PatternSpace = Context->HoldSpace;
- Context->HoldSpace = Swap;
- return 0;
- }
- INT
- SedExecuteSubstituteCharacters (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes the "substitute characters" command, which replaces
- every occurrence of a character found in the first set with the replacement
- character specified by the second set.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- UINTN CharacterCount;
- UINTN CharacterIndex;
- PSTR Characters;
- PSTR Pattern;
- PSTR Replacement;
- assert(Command->Function.Type == SedFunctionSubstituteCharacters);
- Pattern = Context->PatternSpace->Data;
- CharacterCount =
- Command->Function.U.CharacterSubstitute.Characters->Size - 1;
- assert(CharacterCount ==
- Command->Function.U.CharacterSubstitute.Replacement->Size - 1);
- Characters = Command->Function.U.CharacterSubstitute.Characters->Data;
- Replacement = Command->Function.U.CharacterSubstitute.Replacement->Data;
- while (*Pattern != '\0') {
- for (CharacterIndex = 0;
- CharacterIndex < CharacterCount;
- CharacterIndex += 1) {
- if (*Pattern == Characters[CharacterIndex]) {
- *Pattern = Replacement[CharacterIndex];
- assert(*Pattern != '\0');
- break;
- }
- }
- Pattern += 1;
- }
- return 0;
- }
- INT
- SedExecuteNop (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a "no-op" command, which does nothing.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- assert((Command->Function.Type == SedFunctionNop) ||
- (Command->Function.Type == SedFunctionLabel));
- return 0;
- }
- INT
- SedExecuteWriteLineNumber (
- PSED_CONTEXT Context,
- PSED_COMMAND Command
- )
- /*++
- Routine Description:
- This routine executes a "write line number", which prints out the decimal
- line number of the input plus a newline.
- Arguments:
- Context - Supplies a pointer to the application context.
- Command - Supplies a pointer to the command to execute.
- Return Value:
- 0 on success.
- Non-zero error code on failure.
- --*/
- {
- CHAR LineNumberBuffer[100];
- assert(Command->Function.Type == SedFunctionWriteLineNumber);
- snprintf(LineNumberBuffer,
- sizeof(LineNumberBuffer),
- "%lld",
- Context->LineNumber);
- SedPrint(Context, LineNumberBuffer, '\n');
- return 0;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|