ParseCmdSwitches:
PROC [handle: Commander.Handle, literal, ignoreCase, word:
BOOL, transOp: TransOp, fileNamesOK:
BOOL]
RETURNS [ok:
BOOL ←
TRUE, cmdLine:
LIST
OF
ROPE, appendFile:
ROPE ←
NIL] = {
filePatternName: ROPE;
present: BOOL;
cmdLine ← CommandTool.ParseToList[handle, 040C].list;
[present, cmdLine, whoCares] ← GetSwitch["-pattern", cmdLine];
IF present THEN literal ← FALSE;
[present, cmdLine, whoCares] ← GetSwitch["-literal", cmdLine];
IF present THEN literal ← TRUE;
[present, cmdLine, whoCares] ← GetSwitch["-caseSensitive", cmdLine];
IF present THEN ignoreCase ← FALSE;
[present, cmdLine, whoCares] ← GetSwitch["-ignoreCase", cmdLine];
IF present THEN ignoreCase ← TRUE;
[present, cmdLine, whoCares] ← GetSwitch["-wordMatch", cmdLine];
IF present THEN word ← FALSE;
[present, cmdLine, whoCares] ← GetSwitch["-matchAnywhere", cmdLine];
IF present THEN word ← TRUE;
[present, cmdLine, whoCares] ← GetSwitch["-deleteMatched", cmdLine];
IF present THEN transOp ← delete;
[present, cmdLine, whoCares] ← GetSwitch["-selectMatched", cmdLine];
IF present THEN transOp ← select;
[present, cmdLine, whoCares] ← GetSwitch["-replaceMatched", cmdLine];
IF present THEN transOp ← replace;
[present, appendFile, cmdLine, whoCares] ← GetSwitchWithArg["-appendFile", cmdLine];
[present, filePatternName, cmdLine, whoCares] ← GetSwitchWithArg["-filePatterns", cmdLine];
IF SwitchesLeft[cmdLine, stderr] THEN GOTO SyntaxError;
IF present
THEN {
h: Commander.Handle ← NEW[Commander.CommandObject]; -- Hacque.
in:
STREAM ←
FS.StreamOpen[filePatternName !
FS.Error => {
stderr.PutF["Could not open \"%g\"\n", IO.rope[filePatternName]];
GOTO SyntaxError}];
DO
nc: LIST OF ROPE;
h.commandLine ← in.GetLineRope[ ! IO.EndOfStream => EXIT];
IF h.commandLine.Size[] > 0 THEN
[ok, nc, appendFile] ← ParseCmdSwitches[h, literal, ignoreCase, word, transOp, FALSE];
IF ~ok THEN GOTO SyntaxError;
ENDLOOP;
in.Close[];
}
ELSE
IF cmdLine =
NIL
THEN {
stderr.PutF["Missing pattern and replacement in \"%g\"\n", IO.rope[handle.commandLine]];
GOTO SyntaxError;
}
ELSE
IF transOp # delete
AND cmdLine.rest =
NIL
THEN {
stderr.PutF["Missing replacement expression in \"%g\"\n", IO.rope[handle.commandLine]];
GOTO SyntaxError;
}
ELSE
IF transOp = delete
AND cmdLine.rest #
NIL
THEN {
stderr.PutF["Delete mode should not have replacement expression in \"%g\"\n", IO.rope[handle.commandLine]];
GOTO SyntaxError;
}
ELSE
IF transOp = delete
THEN {
ok: BOOL;
program: TransProgram;
[ok, program] ← MakeTransProgram[cmdLine.first, "", literal, word, ignoreCase, transOp];
IF ~ok
THEN
GOTO SyntaxError;
transPrograms ← CONS[program, transPrograms];
cmdLine ← cmdLine.rest;
}
ELSE {
ok: BOOL;
program: TransProgram;
IF cmdLine.rest = NIL THEN GOTO SyntaxError;
[ok, program] ← MakeTransProgram[cmdLine.first, cmdLine.rest.first, literal, word, ignoreCase, transOp];
IF ~ok
THEN
GOTO SyntaxError;
transPrograms ← CONS[program, transPrograms];
cmdLine ← cmdLine.rest.rest;
};
IF cmdLine #
NIL
AND ~fileNamesOK
THEN {
stderr.PutF["File names are not allowed in pattern files: \"%g\"\n", IO.rope[handle.commandLine]];
GOTO SyntaxError;
};
EXITS
SyntaxError => ok ← FALSE;
};