help-libtasn1
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] use stderr for status messages in asn1{Coding, Decoding, Parser}


From: Ivan Shmakov
Subject: [PATCH] use stderr for status messages in asn1{Coding, Decoding, Parser}
Date: Tue, 06 Nov 2012 01:25:59 +0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

        Please consider the patch MIME'd.

        This patch makes the GNU Libtasn1 command line tools more
        “filter-like.”  E. g., it makes the following uses possible:

$ asn1Parser -o /dev/stdout defs.asn.1 \
      | diff -u -- defs_tab.c - 
$ asn1Coding -o /dev/stdout defs.asn.1 assignments \
      | asn1Decoding defs.asn.1 /dev/stdin Defs.Foo 

        (The next step would be to use stdout for output by default,
        instead of, say, INPUT-FILE.out.  Then we may want to check if
        stdout is a TTY, though, just like gzip(1), etc. do.)

        To note is that this one replaces a few {,f}printf () calls with
        fputs () and putc () ones (when there's no actual /formatted/
        output, that is.)  Please mail me back if that may have any
        portability issues, and I'll drop these changes from the patch.

        Also, I don't know if it makes sense to rewrite this one:

$ nl -ba < src/asn1Parser.c 
   102          case 0:
   103            printf ("option %s", long_options[option_index].name);
   104            if (optarg)
   105              printf (" with arg %s", optarg);
   106            printf ("\n");
   107            break;
$ 

        as one (f)printf, e. g.:

        case 0:
          fprintf (stderr, "option %s%s%s\n",
                   long_options[option_index].name,
                   (optarg ? " with arg " : ""),
                   (optarg ? optarg : ""));
          break;

        I've left it split in the patch MIME'd.

$ git log -n 1 
Use stderr exclusively for error and status messages in CLI tools.

src/asn1Coding.c: Use stderr exclusively for error and status
messages; use fputs () and putc () instead of formatted output
whenever possible.
src/asn1Decoding.c: Likewise.
src/asn1Parser.c: Likewise.

-- 
FSF associate member #7257
diff --git a/src/asn1Coding.c b/src/asn1Coding.c
index 00057be..0a6a996 100644
--- a/src/asn1Coding.c
+++ b/src/asn1Coding.c
@@ -194,7 +194,7 @@ main (int argc, char *argv[])
   if (optind == argc || optind == argc - 1)
     {
       free (outputFileName);
-      fprintf (stderr, "asn1Coding: input files missing\n");
+      fputs ("asn1Coding: input files missing\n", stderr);
       usage (EXIT_FAILURE);
     }
 
@@ -210,18 +210,20 @@ main (int argc, char *argv[])
   switch (asn1_result)
     {
     case ASN1_SUCCESS:
-      printf ("Parse: done.\n");
+      fputs ("Parse: done.\n", stderr);
       break;
     case ASN1_FILE_NOT_FOUND:
-      printf ("asn1Coding: FILE %s NOT FOUND\n", inputFileAsnName);
+      fprintf (stderr, "asn1Coding: FILE %s NOT FOUND\n",
+               inputFileAsnName);
       break;
     case ASN1_SYNTAX_ERROR:
     case ASN1_IDENTIFIER_NOT_FOUND:
     case ASN1_NAME_TOO_LONG:
-      printf ("asn1Coding: %s\n", errorDescription);
+      fprintf (stderr, "asn1Coding: %s\n", errorDescription);
       break;
     default:
-      printf ("libtasn1 ERROR: %s\n", asn1_strerror (asn1_result));
+      fprintf (stderr, "libtasn1 ERROR: %s\n",
+               asn1_strerror (asn1_result));
     }
 
   if (asn1_result != ASN1_SUCCESS)
@@ -236,18 +238,19 @@ main (int argc, char *argv[])
 
   if (inputFile == NULL)
     {
-      printf ("asn1Coding: file '%s' not found\n", inputFileAssignmentName);
+      fprintf (stderr, "asn1Coding: file '%s' not found\n",
+               inputFileAssignmentName);
       free (inputFileAsnName);
       free (inputFileAssignmentName);
       exit (1);
     }
 
 
-  printf ("\n");
+  putc ('\n', stderr);
 
   while (readAssignment (inputFile, varName, value) == ASSIGNMENT_SUCCESS)
     {
-      printf ("var=%s, value=%s\n", varName, value);
+      fprintf (stderr, "var=%s, value=%s\n", varName, value);
       if (structure == NULL)
        {
          asn1_result = asn1_create_element (definitions, value, &structure);
@@ -257,7 +260,8 @@ main (int argc, char *argv[])
 
       if (asn1_result != ASN1_SUCCESS)
        {
-         printf ("libtasn1 ERROR: %s\n", asn1_strerror (asn1_result));
+         fprintf (stderr, "libtasn1 ERROR: %s\n",
+                  asn1_strerror (asn1_result));
 
          asn1_delete_structure (&definitions);
          asn1_delete_structure (&structure);
@@ -271,8 +275,8 @@ main (int argc, char *argv[])
     }
   fclose (inputFile);
 
-  printf ("\n");
-  asn1_print_structure (stdout, structure, "", ASN1_PRINT_NAME_TYPE_VALUE);
+  putc ('\n', stderr);
+  asn1_print_structure (stderr, structure, "", ASN1_PRINT_NAME_TYPE_VALUE);
 
   der_len = 0;
   asn1_result = asn1_der_coding (structure, "", der, &der_len,
@@ -283,10 +287,10 @@ main (int argc, char *argv[])
       asn1_result = asn1_der_coding (structure, "", der, &der_len,
                                     errorDescription);
     }
-  printf ("\nCoding: %s\n\n", asn1_strerror (asn1_result));
+  fprintf (stderr, "\nCoding: %s\n\n", asn1_strerror (asn1_result));
   if (asn1_result != ASN1_SUCCESS)
     {
-      printf ("asn1Coding: %s\n", errorDescription);
+      fprintf (stderr, "asn1Coding: %s\n", errorDescription);
 
       free (der);
 
@@ -300,10 +304,10 @@ main (int argc, char *argv[])
     }
 
   /* Print the 'Certificate1' DER encoding */
-  printf ("-----------------\nNumber of bytes=%i\n", der_len);
+  fprintf (stderr, "-----------------\nNumber of bytes=%i\n", der_len);
   for (k = 0; k < der_len; k++)
-    printf ("%02x ", der[k]);
-  printf ("\n-----------------\n");
+    fprintf (stderr, "%02x ", der[k]);
+  fputs ("\n-----------------\n", stderr);
 
   asn1_delete_structure (&definitions);
   asn1_delete_structure (&structure);
@@ -313,14 +317,15 @@ main (int argc, char *argv[])
       if (outputFileName == NULL)
        createFileName (inputFileAssignmentName, &outputFileName);
 
-      printf ("\nOutputFile=%s\n", outputFileName);
+      fprintf (stderr, "\nOutputFile=%s\n", outputFileName);
 
       outputFile = fopen (outputFileName, "w");
 
       if (outputFile == NULL)
        {
-         printf ("asn1Coding: output file '%s' not available\n",
-                 outputFileName);
+         fprintf (stderr,
+                  "asn1Coding: output file '%s' not available\n",
+                  outputFileName);
          free (der);
          free (inputFileAsnName);
          free (inputFileAssignmentName);
@@ -331,7 +336,7 @@ main (int argc, char *argv[])
       for (k = 0; k < der_len; k++)
        fprintf (outputFile, "%c", der[k]);
       fclose (outputFile);
-      printf ("\nWriting: done.\n");
+      fputs ("\nWriting: done.\n", stderr);
     }
 
   free (der);
diff --git a/src/asn1Decoding.c b/src/asn1Decoding.c
index ee09e18..00156c5 100644
--- a/src/asn1Decoding.c
+++ b/src/asn1Decoding.c
@@ -144,18 +144,20 @@ main (int argc, char *argv[])
   switch (asn1_result)
     {
     case ASN1_SUCCESS:
-      printf ("Parse: done.\n");
+      fprintf (stderr, "Parse: done.\n");
       break;
     case ASN1_FILE_NOT_FOUND:
-      printf ("asn1Decoding: FILE %s NOT FOUND\n", inputFileAsnName);
+      fprintf (stderr, "asn1Decoding: FILE %s NOT FOUND\n",
+               inputFileAsnName);
       break;
     case ASN1_SYNTAX_ERROR:
     case ASN1_IDENTIFIER_NOT_FOUND:
     case ASN1_NAME_TOO_LONG:
-      printf ("asn1Decoding: %s\n", errorDescription);
+      fprintf (stderr, "asn1Decoding: %s\n", errorDescription);
       break;
     default:
-      printf ("libtasn1 ERROR: %s\n", asn1_strerror (asn1_result));
+      fprintf (stderr, "libtasn1 ERROR: %s\n",
+               asn1_strerror (asn1_result));
     }
 
   if (asn1_result != ASN1_SUCCESS)
@@ -175,7 +177,8 @@ main (int argc, char *argv[])
 
   if (der == NULL)
     {
-      printf ("asn1Decoding: could not read '%s'\n", inputFileDerName);
+      fprintf (stderr, "asn1Decoding: could not read '%s'\n",
+               inputFileDerName);
       asn1_delete_structure (&definitions);
 
       free (inputFileAsnName);
@@ -238,7 +241,8 @@ char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
 
   if (asn1_result != ASN1_SUCCESS)
     {
-      printf ("Structure creation: %s\n", asn1_strerror (asn1_result));
+      fprintf (stderr, "Structure creation: %s\n",
+               asn1_strerror (asn1_result));
       asn1_delete_structure (&structure);
       return asn1_result;
     }
@@ -246,17 +250,18 @@ char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
   asn1_result =
     asn1_der_decoding (&structure, der, der_len, errorDescription);
 
-  if (!benchmark) printf ("\nDecoding: %s\n", asn1_strerror (asn1_result));
+  if (!benchmark)
+    fprintf (stderr, "\nDecoding: %s\n", asn1_strerror (asn1_result));
   if (asn1_result != ASN1_SUCCESS)
     {
-      printf ("asn1Decoding: %s\n", errorDescription);
+      fprintf (stderr, "asn1Decoding: %s\n", errorDescription);
       asn1_delete_structure (&structure);
       return asn1_result;
     }
 
   if (!benchmark)
     {
-      printf ("\nDECODING RESULT:\n");
+      fprintf (stderr, "\nDECODING RESULT:\n");
       asn1_print_structure (stdout, structure, "", ASN1_PRINT_NAME_TYPE_VALUE);
     }
   asn1_delete_structure (&structure);
diff --git a/src/asn1Parser.c b/src/asn1Parser.c
index 430ac7f..359a6c5 100644
--- a/src/asn1Parser.c
+++ b/src/asn1Parser.c
@@ -100,10 +100,11 @@ main (int argc, char *argv[])
       switch (option_result)
        {
        case 0:
-         printf ("option %s", long_options[option_index].name);
+         fprintf (stderr, "option %s",
+                  long_options[option_index].name);
          if (optarg)
-           printf (" with arg %s", optarg);
-         printf ("\n");
+           fprintf (stderr, " with arg %s", optarg);
+         putc ("\n", stderr);
          break;
        case 'h':               /* HELP */
          free (outputFileName);
@@ -170,18 +171,20 @@ main (int argc, char *argv[])
   switch (parse_result)
     {
     case ASN1_SUCCESS:
-      printf ("Done.\n");
+      fputs ("Done.\n", stderr);
       break;
     case ASN1_FILE_NOT_FOUND:
-      printf ("asn1Parser: FILE %s NOT FOUND\n", inputFileName);
+      fprintf (stderr, "asn1Parser: FILE %s NOT FOUND\n",
+               inputFileName);
       break;
     case ASN1_SYNTAX_ERROR:
     case ASN1_IDENTIFIER_NOT_FOUND:
     case ASN1_NAME_TOO_LONG:
-      printf ("asn1Parser: %s\n", errorDescription);
+      fprintf (stderr, "asn1Parser: %s\n", errorDescription);
       break;
     default:
-      printf ("libtasn1 ERROR: %s\n", asn1_strerror (parse_result));
+      fprintf (stderr, "libtasn1 ERROR: %s\n",
+               asn1_strerror (parse_result));
     }
 
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]