# HG changeset patch # User Sascha Wilde # Date 1396611255 -7200 # Node ID b832231640abe86cd17df967684314f38c1f6f60 # Parent f17226aa2e091f476a350d0e23aa1d6dec70d90e Read from file instead of stdin, if given. diff -r f17226aa2e09 -r b832231640ab cinst/mozilla.c --- 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: * R: @@ -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); } diff -r f17226aa2e09 -r b832231640ab common/errorcodes.h --- a/common/errorcodes.h Fri Apr 04 11:02:02 2014 +0000 +++ b/common/errorcodes.h Fri Apr 04 13:34:15 2014 +0200 @@ -34,6 +34,9 @@ /* Error: could not determine current users HOME */ #define ERR_MOZ_HOMELESS 0x0081 #define ERR_MOZ_INVALID_INPUT 0x0082 +/* Error: Called with wrong number of arguments */ +#define ERR_MOZ_WRONG_ARGC 0x0083 +#define ERR_MOZ_FAILED_TO_OPEN_INPUT 0x0084 /* Warning: Failed to read profile.ini */ #define WARN_MOZ_FAILED_TO_OPEN_INI 0x0091