From 586ccd4e874496014d82012f478c516cc002b8ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Sun, 13 Jul 2014 23:50:44 +0200 Subject: [PATCH] Put a prolog at the top of configuration files Later I'm going to put in there some more information about the format etc. --- src/common.c | 7 +++++-- src/kike.c | 33 +++++++++++++++++++++------------ src/zyklonb.c | 33 +++++++++++++++++++++------------ 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/common.c b/src/common.c index f35493f..d905929 100644 --- a/src/common.c +++ b/src/common.c @@ -1824,8 +1824,8 @@ read_config_file (struct str_map *config, struct error **e) } static char * -write_default_config (const char *filename, const struct config_item *table, - struct error **e) +write_default_config (const char *filename, const char *prolog, + const struct config_item *table, struct error **e) { struct str path, base; @@ -1861,6 +1861,9 @@ write_default_config (const char *filename, const struct config_item *table, goto error; } + if (prolog) + fputs (prolog, fp); + errno = 0; for (; table->key != NULL; table++) { diff --git a/src/kike.c b/src/kike.c index faea7b9..38e99ef 100644 --- a/src/kike.c +++ b/src/kike.c @@ -993,12 +993,30 @@ print_usage (const char *program_name) program_name); } +static void +call_write_default_config (const char *hint) +{ + static const char *prolog = + "# " PROGRAM_NAME " " PROGRAM_VERSION " configuration file\n" + "\n"; + + struct error *e = NULL; + char *filename = write_default_config (hint, prolog, g_config_table, &e); + if (!filename) + { + print_fatal ("%s", e->message); + error_free (e); + exit (EXIT_FAILURE); + } + print_status ("configuration written to `%s'", filename); + free (filename); +} + int main (int argc, char *argv[]) { const char *invocation_name = argv[0]; - struct error *e = NULL; static struct option opts[] = { { "debug", no_argument, NULL, 'd' }, @@ -1028,18 +1046,8 @@ main (int argc, char *argv[]) printf (PROGRAM_NAME " " PROGRAM_VERSION "\n"); exit (EXIT_SUCCESS); case 'w': - { - char *filename = write_default_config (optarg, g_config_table, &e); - if (!filename) - { - print_fatal ("%s", e->message); - error_free (e); - exit (EXIT_FAILURE); - } - print_status ("configuration written to `%s'", filename); - free (filename); + call_write_default_config (optarg); exit (EXIT_SUCCESS); - } default: print_fatal ("error in options"); exit (EXIT_FAILURE); @@ -1058,6 +1066,7 @@ main (int argc, char *argv[]) struct server_context ctx; server_context_init (&ctx); + struct error *e = NULL; if (!read_config_file (&ctx.config, &e)) { print_fatal ("error loading configuration: %s", e->message); diff --git a/src/zyklonb.c b/src/zyklonb.c index 3d8d388..1c7f6ae 100644 --- a/src/zyklonb.c +++ b/src/zyklonb.c @@ -1686,6 +1686,25 @@ print_usage (const char *program_name) program_name); } +static void +call_write_default_config (const char *hint) +{ + static const char *prolog = + "# " PROGRAM_NAME " " PROGRAM_VERSION " configuration file\n" + "\n"; + + struct error *e = NULL; + char *filename = write_default_config (hint, prolog, g_config_table, &e); + if (!filename) + { + print_fatal ("%s", e->message); + error_free (e); + exit (EXIT_FAILURE); + } + print_status ("configuration written to `%s'", filename); + free (filename); +} + int main (int argc, char *argv[]) { @@ -1693,7 +1712,6 @@ main (int argc, char *argv[]) str_vector_init (&g_original_argv); str_vector_add_vector (&g_original_argv, argv); - struct error *e = NULL; static struct option opts[] = { { "debug", no_argument, NULL, 'd' }, @@ -1723,18 +1741,8 @@ main (int argc, char *argv[]) printf (PROGRAM_NAME " " PROGRAM_VERSION "\n"); exit (EXIT_SUCCESS); case 'w': - { - char *filename = write_default_config (optarg, g_config_table, &e); - if (!filename) - { - print_fatal ("%s", e->message); - error_free (e); - exit (EXIT_FAILURE); - } - print_status ("configuration written to `%s'", filename); - free (filename); + call_write_default_config (optarg); exit (EXIT_SUCCESS); - } default: print_fatal ("error in options"); exit (EXIT_FAILURE); @@ -1753,6 +1761,7 @@ main (int argc, char *argv[]) struct bot_context ctx; bot_context_init (&ctx); + struct error *e = NULL; if (!read_config_file (&ctx.config, &e)) { print_fatal ("error loading configuration: %s", e->message);