diff cinst/mozilla.c @ 315:b832231640ab

Read from file instead of stdin, if given.
author Sascha Wilde <wilde@intevation.de>
date Fri, 04 Apr 2014 13:34:15 +0200
parents 4033c16beb04
children 46fd11699646
line wrap: on
line diff
--- a/cinst/mozilla.c	Fri Apr 04 11:02:02 2014 +0000
+++ b/cinst/mozilla.c	Fri Apr 04 13:34:15 2014 +0200
@@ -2,7 +2,8 @@
  * @file
  * @brief Mozilla installation process
  *
- * Reads from stdin a list of instructions in the form:
+ * Reads from a file given on command line or stdin a list of
+ * instructions in the form:
  *
  * I:<base64 DER econded certificate>
  * R:<base64 DER econded certificate>
@@ -555,14 +556,15 @@
  * @param[inout] remove_list list of SECItems with certifiactes to remove
  */
 static void
-parse_commands (seciteml_t **install_list, seciteml_t **remove_list)
+parse_commands (FILE *stream,
+                seciteml_t **install_list, seciteml_t **remove_list)
 {
   char inpl[LINEBUFLEN];
   size_t inpllen;
   bool parserr = true;
   SECItem secitem;
 
-  while ( fgets(inpl, LINEBUFLEN, stdin) != NULL )
+  while ( fgets(inpl, LINEBUFLEN, stream) != NULL )
     {
       inpllen = strnlen(inpl, LINEBUFLEN);
       /* Validate input line:
@@ -609,18 +611,41 @@
 
 
 int
-main ()
+main (int argc, char **argv)
 {
   char **dbdirs;
   seciteml_t *certs_to_remove = NULL;
   seciteml_t *certs_to_add = NULL;
+  FILE *input_stream;
+
+  switch (argc)
+    {
+    case 1:
+      DEBUGPRINTF("Opening STDIN for input..\n.");
+      input_stream = stdin;
+      break;
+    case 2:
+      DEBUGPRINTF("Opening %s for input...\n", argv[1]);
+      if ((input_stream = fopen(argv[1], "r")) == NULL)
+        {
+          DEBUGPRINTF("FATAL: Could not open %s for reading!\n",
+                      argv[1]);
+          return_code = ERR_MOZ_FAILED_TO_OPEN_INPUT;
+          goto exit;
+        }
+      break;
+    default:
+      DEBUGPRINTF("FATAL: Wrong number of arguments!\n");
+      return_code = ERR_MOZ_WRONG_ARGC;
+      goto exit;
+    }
 
   dbdirs =
     get_all_nssdb_dirs();
 
   if (dbdirs != NULL)
     {
-      parse_commands(&certs_to_add, &certs_to_remove);
+      parse_commands(input_stream, &certs_to_add, &certs_to_remove);
 
 #ifdef DEBUGOUTPUT
       DEBUGPRINTF("OLD List of installed certs:\n");
@@ -642,5 +667,9 @@
 
       strv_free(dbdirs);
     }
+
+ fclose(input_stream);
+
+ exit:
   exit(return_code);
 }

http://wald.intevation.org/projects/trustbridge/