Răsfoiți Sursa

ISSigTool: Add --quiet/-q and --help/-? options.

Jordan Russell 4 luni în urmă
părinte
comite
1ae2524487
1 a modificat fișierele cu 51 adăugiri și 17 ștergeri
  1. 51 17
      Projects/ISSigTool.dpr

+ 51 - 17
Projects/ISSigTool.dpr

@@ -33,6 +33,7 @@ uses
 var
 var
   Options: record
   Options: record
     KeyFile: String;
     KeyFile: String;
+    Quiet: Boolean;
   end;
   end;
 
 
 procedure RaiseFatalError(const Msg: String);
 procedure RaiseFatalError(const Msg: String);
@@ -45,6 +46,28 @@ begin
   raise Exception.CreateFmt(Msg, Args);
   raise Exception.CreateFmt(Msg, Args);
 end;
 end;
 
 
+procedure Print(const S: String; const IncludeNewLine: Boolean = True);
+begin
+  if IncludeNewLine then
+    Writeln(S)
+  else
+    Write(S);
+end;
+
+procedure PrintUnlessQuiet(const S: String;
+  const IncludeNewLine: Boolean = True);
+begin
+  if not Options.Quiet then
+    Print(S, IncludeNewLine);
+end;
+
+procedure PrintFmtUnlessQuiet(const S: String; const Args: array of const;
+  const IncludeNewLine: Boolean = True);
+begin
+  if not Options.Quiet then
+    Print(Format(S, Args), IncludeNewLine);
+end;
+
 function CalcFileHash(const AFile: TFile): TSHA256Digest;
 function CalcFileHash(const AFile: TFile): TSHA256Digest;
 var
 var
   Buf: array[0..$FFFF] of Byte;
   Buf: array[0..$FFFF] of Byte;
@@ -85,7 +108,7 @@ begin
     ISSigExportPublicKeyText(Key, PublicKeyText);
     ISSigExportPublicKeyText(Key, PublicKeyText);
     ISSigSaveTextToFile(AFilename, PublicKeyText);
     ISSigSaveTextToFile(AFilename, PublicKeyText);
 
 
-    Writeln('Public key file created: ', AFilename);
+    PrintFmtUnlessQuiet('Public key file created: "%s"', [AFilename]);
   finally
   finally
     Key.Free;
     Key.Free;
   end;
   end;
@@ -104,7 +127,7 @@ begin
     ISSigExportPrivateKeyText(Key, PrivateKeyText);
     ISSigExportPrivateKeyText(Key, PrivateKeyText);
     ISSigSaveTextToFile(Options.KeyFile, PrivateKeyText);
     ISSigSaveTextToFile(Options.KeyFile, PrivateKeyText);
 
 
-    Writeln('Private key file created: ', Options.KeyFile);
+    PrintFmtUnlessQuiet('Private key file created: "%s"', [Options.KeyFile]);
   finally
   finally
     Key.Free;
     Key.Free;
   end;
   end;
@@ -112,7 +135,7 @@ end;
 
 
 procedure SignSingleFile(const AKey: TECDSAKey; const AFilename: String);
 procedure SignSingleFile(const AKey: TECDSAKey; const AFilename: String);
 begin
 begin
-  Write(AFilename, ': ');
+  PrintFmtUnlessQuiet('%s: ', [AFilename], False);
 
 
   var FileSize: Int64;
   var FileSize: Int64;
   var FileHash: TSHA256Digest;
   var FileHash: TSHA256Digest;
@@ -140,7 +163,7 @@ begin
        ExistingFileHashValue) = vsrSuccess then begin
        ExistingFileHashValue) = vsrSuccess then begin
       if (FileSize = ExistingFileSizeValue) and
       if (FileSize = ExistingFileSizeValue) and
          SHA256DigestsEqual(FileHash, ExistingFileHashValue) then begin
          SHA256DigestsEqual(FileHash, ExistingFileHashValue) then begin
-        Writeln('signature unchanged');
+        PrintUnlessQuiet('signature unchanged');
         Exit;
         Exit;
       end;
       end;
     end;
     end;
@@ -148,7 +171,7 @@ begin
 
 
   const SigText = ISSigCreateSignatureText(AKey, FileSize, FileHash);
   const SigText = ISSigCreateSignatureText(AKey, FileSize, FileHash);
   ISSigSaveTextToFile(SigFilename, SigText);
   ISSigSaveTextToFile(SigFilename, SigText);
-  Writeln('signature written');
+  PrintUnlessQuiet('signature written');
 end;
 end;
 
 
 procedure CommandSign(const AFilenames: TStringList);
 procedure CommandSign(const AFilenames: TStringList);
@@ -167,16 +190,16 @@ end;
 function VerifySingleFile(const AKey: TECDSAKey; const AFilename: String): Boolean;
 function VerifySingleFile(const AKey: TECDSAKey; const AFilename: String): Boolean;
 begin
 begin
   Result := False;
   Result := False;
-  Write(AFilename, ': ');
+  PrintFmtUnlessQuiet('%s: ', [AFilename], False);
 
 
   if not NewFileExists(AFilename) then begin
   if not NewFileExists(AFilename) then begin
-    Writeln('MISSINGFILE (File does not exist)');
+    PrintUnlessQuiet('MISSINGFILE (File does not exist)');
     Exit;
     Exit;
   end;
   end;
 
 
   const SigFilename = AFilename + '.issig';
   const SigFilename = AFilename + '.issig';
   if not NewFileExists(SigFilename) then begin
   if not NewFileExists(SigFilename) then begin
-    Writeln('MISSINGSIGFILE (Signature file does not exist)');
+    PrintUnlessQuiet('MISSINGSIGFILE (Signature file does not exist)');
     Exit;
     Exit;
   end;
   end;
 
 
@@ -188,9 +211,9 @@ begin
   if VerifyResult <> vsrSuccess then begin
   if VerifyResult <> vsrSuccess then begin
     case VerifyResult of
     case VerifyResult of
       vsrMalformed, vsrBadSignature:
       vsrMalformed, vsrBadSignature:
-        Writeln('BADSIGFILE (Signature file is not valid)');
+        PrintUnlessQuiet('BADSIGFILE (Signature file is not valid)');
       vsrKeyNotFound:
       vsrKeyNotFound:
-        Writeln('UNKNOWNKEY (Incorrect key ID)');
+        PrintUnlessQuiet('UNKNOWNKEY (Incorrect key ID)');
     else
     else
       RaiseFatalError('Unknown verify result');
       RaiseFatalError('Unknown verify result');
     end;
     end;
@@ -200,19 +223,19 @@ begin
   const F = TFile.Create(AFilename, fdOpenExisting, faRead, fsRead);
   const F = TFile.Create(AFilename, fdOpenExisting, faRead, fsRead);
   try
   try
     if Int64(F.Size) <> ExpectedFileSize then begin
     if Int64(F.Size) <> ExpectedFileSize then begin
-      Writeln('WRONGSIZE (File size is incorrect)');
+      PrintUnlessQuiet('WRONGSIZE (File size is incorrect)');
       Exit;
       Exit;
     end;
     end;
     const ActualFileHash = CalcFileHash(F);
     const ActualFileHash = CalcFileHash(F);
     if not SHA256DigestsEqual(ActualFileHash, ExpectedFileHash) then begin
     if not SHA256DigestsEqual(ActualFileHash, ExpectedFileHash) then begin
-      Writeln('WRONGHASH (File hash is incorrect)');
+      PrintUnlessQuiet('WRONGHASH (File hash is incorrect)');
       Exit;
       Exit;
     end;
     end;
   finally
   finally
     F.Free;
     F.Free;
   end;
   end;
 
 
-  Writeln('OK');
+  PrintUnlessQuiet('OK');
   Result := True;
   Result := True;
 end;
 end;
 
 
@@ -244,6 +267,8 @@ begin
   Writeln(ErrOutput, 'or to generate a new private key:  issigtool [options] generate-private-key');
   Writeln(ErrOutput, 'or to generate a new private key:  issigtool [options] generate-private-key');
   Writeln(ErrOutput, 'Options:');
   Writeln(ErrOutput, 'Options:');
   Writeln(ErrOutput, '  --key-file=<filename> Specifies the private key filename (overrides ISSIGTOOL_KEY_FILE environment variable)');
   Writeln(ErrOutput, '  --key-file=<filename> Specifies the private key filename (overrides ISSIGTOOL_KEY_FILE environment variable)');
+  Writeln(ErrOutput, '  --quiet, -q           Suppresses status messages that are normally printed to standard output');
+  Writeln(ErrOutput, '  --help, -?            Prints this information');
   Writeln(ErrOutput, '');
   Writeln(ErrOutput, '');
 end;
 end;
 
 
@@ -254,15 +279,24 @@ begin
     for var I := 1 to NewParamCount do
     for var I := 1 to NewParamCount do
       ArgList.Add(NewParamStr(I));
       ArgList.Add(NewParamStr(I));
 
 
+    const InitialArgListCount = ArgList.Count;
     var J := 0;
     var J := 0;
     while J < ArgList.Count do begin
     while J < ArgList.Count do begin
       const S = ArgList[J];
       const S = ArgList[J];
-      if S.StartsWith('--key-file=') then begin
-        Options.KeyFile := S.Substring(Length('--key-file='));
+      if S.StartsWith('-') then begin
+        if (S = '--help') or (S = '-?') then begin
+          ShowUsage;
+          if InitialArgListCount <> 1 then
+            RaiseFatalErrorFmt('"%s" option cannot be combined with other arguments', [S]);
+          Exit;
+        end else if (S = '--quiet') or (S = '-q') then begin
+          Options.Quiet := True;
+        end else if S.StartsWith('--key-file=') then begin
+          Options.KeyFile := S.Substring(Length('--key-file='));
+        end else
+          RaiseFatalErrorFmt('Unknown option "%s".', [S]);
         ArgList.Delete(J);
         ArgList.Delete(J);
       end else begin
       end else begin
-        if S.StartsWith('-') then
-          RaiseFatalErrorFmt('Unknown option "%s"', [S]);
         if S = '' then
         if S = '' then
           RaiseFatalError('Empty arguments not allowed');
           RaiseFatalError('Empty arguments not allowed');
         Inc(J);
         Inc(J);