bookloupe/bookloupe.c
changeset 187 6ed7afd99ea9
parent 185 a6d93c9932ac
parent 186 4912234d80be
child 188 a4658f2df227
     1.1 --- a/bookloupe/bookloupe.c	Sun Sep 29 09:18:05 2013 +0100
     1.2 +++ b/bookloupe/bookloupe.c	Sun Oct 20 21:29:06 2013 +0100
     1.3 @@ -132,37 +132,99 @@
     1.4  gboolean pswit[SWITNO];  /* program switches */
     1.5  gchar *opt_charset;
     1.6  
     1.7 +gboolean typo_compat,paranoid_compat;
     1.8 +
     1.9  static GOptionEntry options[]={
    1.10      { "dp", 'd', 0, G_OPTION_ARG_NONE, pswit+DP_SWITCH,
    1.11        "Ignore DP-specific markup", NULL },
    1.12 -    { "noecho", 'e', 0, G_OPTION_ARG_NONE, pswit+ECHO_SWITCH,
    1.13 +    { "no-dp", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
    1.14 +      G_OPTION_ARG_NONE, pswit+DP_SWITCH,
    1.15 +      "Don't ignore DP-specific markup", NULL },
    1.16 +    { "echo", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, pswit+ECHO_SWITCH,
    1.17 +      "Echo queried line", NULL },
    1.18 +    { "no-echo", 'e', G_OPTION_FLAG_REVERSE,
    1.19 +      G_OPTION_ARG_NONE, pswit+ECHO_SWITCH,
    1.20        "Don't echo queried line", NULL },
    1.21      { "squote", 's', 0, G_OPTION_ARG_NONE, pswit+SQUOTE_SWITCH,
    1.22        "Check single quotes", NULL },
    1.23 -    { "typo", 't', 0, G_OPTION_ARG_NONE, pswit+TYPO_SWITCH,
    1.24 +    { "no-squote", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
    1.25 +      G_OPTION_ARG_NONE, pswit+SQUOTE_SWITCH,
    1.26 +      "Don't check single quotes", NULL },
    1.27 +    { "typo", 0, 0, G_OPTION_ARG_NONE, pswit+TYPO_SWITCH,
    1.28        "Check common typos", NULL },
    1.29 +    { "no-typo", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
    1.30 +      G_OPTION_ARG_NONE, pswit+TYPO_SWITCH,
    1.31 +      "Don't check common typos", NULL },
    1.32      { "qpara", 'p', 0, G_OPTION_ARG_NONE, pswit+QPARA_SWITCH,
    1.33        "Require closure of quotes on every paragraph", NULL },
    1.34 -    { "relaxed", 'x', 0, G_OPTION_ARG_NONE, pswit+PARANOID_SWITCH,
    1.35 +    { "no-qpara", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
    1.36 +      G_OPTION_ARG_NONE, pswit+QPARA_SWITCH,
    1.37 +      "Don't require closure of quotes on every paragraph", NULL },
    1.38 +    { "paranoid", 0, G_OPTION_FLAG_HIDDEN,
    1.39 +      G_OPTION_ARG_NONE, pswit+PARANOID_SWITCH,
    1.40 +      "Enable paranoid querying of everything", NULL },
    1.41 +    { "no-paranoid", 0, G_OPTION_FLAG_REVERSE,
    1.42 +      G_OPTION_ARG_NONE, pswit+PARANOID_SWITCH,
    1.43        "Disable paranoid querying of everything", NULL },
    1.44 -    { "line-end", 'l', 0, G_OPTION_ARG_NONE, pswit+LINE_END_SWITCH,
    1.45 -      "Disable line end checking", NULL },
    1.46 +    { "line-end", 0, G_OPTION_FLAG_HIDDEN,
    1.47 +      G_OPTION_ARG_NONE, pswit+LINE_END_SWITCH,
    1.48 +      "Enable line end checking", NULL },
    1.49 +    { "no-line-end", 'l', G_OPTION_FLAG_REVERSE,
    1.50 +      G_OPTION_ARG_NONE, pswit+LINE_END_SWITCH,
    1.51 +      "Diable line end checking", NULL },
    1.52      { "overview", 'o', 0, G_OPTION_ARG_NONE, pswit+OVERVIEW_SWITCH,
    1.53        "Overview: just show counts", NULL },
    1.54 +    { "no-overview", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
    1.55 +      G_OPTION_ARG_NONE, pswit+OVERVIEW_SWITCH,
    1.56 +      "Show individual warnings", NULL },
    1.57      { "stdout", 'y', 0, G_OPTION_ARG_NONE, pswit+STDOUT_SWITCH,
    1.58        "Output errors to stdout instead of stderr", NULL },
    1.59 +    { "no-stdout", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
    1.60 +      G_OPTION_ARG_NONE, pswit+STDOUT_SWITCH,
    1.61 +      "Output errors to stderr instead of stdout", NULL },
    1.62      { "header", 'h', 0, G_OPTION_ARG_NONE, pswit+HEADER_SWITCH,
    1.63        "Echo header fields", NULL },
    1.64 +    { "no-header", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
    1.65 +      G_OPTION_ARG_NONE, pswit+HEADER_SWITCH,
    1.66 +      "Don't echo header fields", NULL },
    1.67      { "markup", 'm', 0, G_OPTION_ARG_NONE, pswit+MARKUP_SWITCH,
    1.68        "Ignore markup in < >", NULL },
    1.69 +    { "no-markup", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
    1.70 +      G_OPTION_ARG_NONE, pswit+MARKUP_SWITCH,
    1.71 +      "No special handling for markup in < >", NULL },
    1.72      { "usertypo", 'u', 0, G_OPTION_ARG_NONE, pswit+USERTYPO_SWITCH,
    1.73        "Use file of user-defined typos", NULL },
    1.74 +    { "no-usertypo", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
    1.75 +      G_OPTION_ARG_NONE, pswit+USERTYPO_SWITCH,
    1.76 +      "Ignore file of user-defined typos", NULL },
    1.77 +    { "verbose", 'v', 0, G_OPTION_ARG_NONE, pswit+VERBOSE_SWITCH,
    1.78 +      "Verbose - list everything", NULL },
    1.79 +    { "no-verbose", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
    1.80 +      G_OPTION_ARG_NONE, pswit+VERBOSE_SWITCH,
    1.81 +      "Switch off verbose mode", NULL },
    1.82 +    { "charset", 0, 0, G_OPTION_ARG_STRING, &opt_charset,
    1.83 +      "Set of characters valid for this ebook", "NAME" },
    1.84 +    { NULL }
    1.85 +};
    1.86 +
    1.87 +/*
    1.88 + * Options relating to configuration which make no sense from inside
    1.89 + * a configuration file.
    1.90 + */
    1.91 +
    1.92 +static GOptionEntry config_options[]={
    1.93      { "web", 'w', 0, G_OPTION_ARG_NONE, pswit+WEB_SWITCH,
    1.94        "Defaults for use on www upload", NULL },
    1.95 -    { "verbose", 'v', 0, G_OPTION_ARG_NONE, pswit+VERBOSE_SWITCH,
    1.96 -      "Verbose - list everything", NULL },
    1.97 -    { "charset", 0, 0, G_OPTION_ARG_STRING, &opt_charset,
    1.98 -      "Set of characters valid for this ebook", "NAME" },
    1.99 +    { "dump-config", 0, 0, G_OPTION_ARG_NONE, pswit+DUMP_CONFIG_SWITCH,
   1.100 +      "Dump current config settings", NULL },
   1.101 +    { NULL }
   1.102 +};
   1.103 +
   1.104 +static GOptionEntry compatibility_options[]={
   1.105 +    { "toggle-typo", 't', 0, G_OPTION_ARG_NONE, &typo_compat,
   1.106 +      "Toggle checking for common typos", NULL },
   1.107 +    { "toggle-relaxed", 'x', 0, G_OPTION_ARG_NONE, &paranoid_compat,
   1.108 +      "Toggle both paranoid mode and common typos", NULL },
   1.109      { NULL }
   1.110  };
   1.111  
   1.112 @@ -206,6 +268,167 @@
   1.113  UINT saved_cp;
   1.114  #endif
   1.115  
   1.116 +GKeyFile *config;
   1.117 +
   1.118 +void config_file_update(GKeyFile *kf)
   1.119 +{
   1.120 +    int i;
   1.121 +    gboolean sw;
   1.122 +    for(i=0;options[i].long_name;i++)
   1.123 +    {
   1.124 +	if (g_str_has_prefix(options[i].long_name,"no-"))
   1.125 +	    continue;
   1.126 +	if (options[i].arg==G_OPTION_ARG_NONE)
   1.127 +	{
   1.128 +	    sw=*(gboolean *)options[i].arg_data;
   1.129 +	    if (options[i].flags&G_OPTION_FLAG_REVERSE)
   1.130 +		sw=!sw;
   1.131 +	    g_key_file_set_boolean(kf,"options",options[i].long_name,sw);
   1.132 +	}
   1.133 +	else
   1.134 +	    g_assert_not_reached();
   1.135 +    }
   1.136 +}
   1.137 +
   1.138 +void config_file_add_comments(GKeyFile *kf)
   1.139 +{
   1.140 +    int i;
   1.141 +    gchar *comment;
   1.142 +    g_key_file_set_comment(kf,NULL,NULL," Default configuration for bookloupe",
   1.143 +      NULL);
   1.144 +    for(i=0;options[i].long_name;i++)
   1.145 +    {
   1.146 +	if (g_str_has_prefix(options[i].long_name,"no-"))
   1.147 +	    continue;
   1.148 +	comment=g_strconcat(" ",options[i].description,NULL);
   1.149 +	g_key_file_set_comment(kf,"options",options[i].long_name,comment,NULL);
   1.150 +	g_free(comment);
   1.151 +    }
   1.152 +}
   1.153 +
   1.154 +void dump_config(void)
   1.155 +{
   1.156 +    gchar *s;
   1.157 +    if (config)
   1.158 +	config_file_update(config);
   1.159 +    else
   1.160 +    {
   1.161 +	config=g_key_file_new();
   1.162 +	config_file_update(config);
   1.163 +	config_file_add_comments(config);
   1.164 +    }
   1.165 +    s=g_key_file_to_data(config,NULL,NULL);
   1.166 +    if (s)
   1.167 +	g_print("%s",s);
   1.168 +    g_free(s);
   1.169 +}
   1.170 +
   1.171 +GKeyFile *read_config_file(gchar **full_path)
   1.172 +{
   1.173 +    int i;
   1.174 +    GError *err=NULL;
   1.175 +    gchar **search_dirs;
   1.176 +    gchar *path;
   1.177 +    const char *search_path;
   1.178 +    GKeyFile *kf;
   1.179 +    kf=g_key_file_new();
   1.180 +    search_path=g_getenv("BOOKLOUPE_CONFIG_PATH");
   1.181 +    if (search_path)
   1.182 +    {
   1.183 +#ifdef __WIN32__
   1.184 +	search_dirs=g_strsplit(search_path,";",0);
   1.185 +#else
   1.186 +	search_dirs=g_strsplit(search_path,":",0);
   1.187 +#endif
   1.188 +    }
   1.189 +    else
   1.190 +    {
   1.191 +	search_dirs=g_new(gchar *,4);
   1.192 +	search_dirs[0]=g_get_current_dir();
   1.193 +	search_dirs[1]=g_strdup(running_from);
   1.194 +	search_dirs[2]=g_strdup(g_get_user_config_dir());
   1.195 +	search_dirs[3]=NULL;
   1.196 +    }
   1.197 +    for(i=0;search_dirs[i];i++)
   1.198 +    {
   1.199 +	path=g_build_filename(search_dirs[i],"bookloupe.ini",NULL);
   1.200 +	if (g_key_file_load_from_file(kf,path,
   1.201 +	  G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS,&err))
   1.202 +	    break;
   1.203 +	if (!g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
   1.204 +	{
   1.205 +	    g_printerr("Bookloupe: Error reading %s\n",path);
   1.206 +	    g_printerr("%s\n",err->message);
   1.207 +	    exit(1);
   1.208 +	}
   1.209 +	g_clear_error(&err);
   1.210 +	g_free(path);
   1.211 +	path=NULL;
   1.212 +    }
   1.213 +    if (!search_dirs[i])
   1.214 +    {
   1.215 +	g_key_file_free(kf);
   1.216 +	kf=NULL;
   1.217 +    }
   1.218 +    g_strfreev(search_dirs);
   1.219 +    if (full_path && kf)
   1.220 +	*full_path=path;
   1.221 +    else
   1.222 +	g_free(path);
   1.223 +    return kf;
   1.224 +}
   1.225 +
   1.226 +void parse_config_file(void)
   1.227 +{
   1.228 +    int i,j;
   1.229 +    gchar *path;
   1.230 +    gchar **keys;
   1.231 +    gboolean sw;
   1.232 +    GError *err=NULL;
   1.233 +    config=read_config_file(&path);
   1.234 +    if (config)
   1.235 +	keys=g_key_file_get_keys(config,"options",NULL,NULL);
   1.236 +    else
   1.237 +	keys=NULL;
   1.238 +    if (keys)
   1.239 +    {
   1.240 +	for(i=0;keys[i];i++)
   1.241 +	{
   1.242 +	    for(j=0;options[j].long_name;j++)
   1.243 +	    {
   1.244 +		if (g_str_has_prefix(options[j].long_name,"no-"))
   1.245 +		    continue;
   1.246 +		else if (!strcmp(keys[i],options[j].long_name))
   1.247 +		{
   1.248 +		    if (options[j].arg==G_OPTION_ARG_NONE)
   1.249 +		    {
   1.250 +			sw=g_key_file_get_boolean(config,"options",keys[i],
   1.251 +			  &err);
   1.252 +			if (err)
   1.253 +			{
   1.254 +			    g_printerr("Bookloupe: %s: options.%s: %s\n",
   1.255 +			      path,keys[i],err->message);
   1.256 +			    g_clear_error(&err);
   1.257 +			}
   1.258 +			if (options[j].flags&G_OPTION_FLAG_REVERSE)
   1.259 +			    sw=!sw;
   1.260 +			*(gboolean *)options[j].arg_data=sw;
   1.261 +			break;
   1.262 +		    }
   1.263 +		    else
   1.264 +			g_assert_not_reached();
   1.265 +		}
   1.266 +	    }
   1.267 +	    if (!options[j].long_name)
   1.268 +		g_printerr("Bookloupe: %s: Unknown option \"%s\" ignored\n",
   1.269 +		  path,keys[i]);
   1.270 +	}
   1.271 +	g_strfreev(keys);
   1.272 +    }
   1.273 +    if (config)
   1.274 +	g_free(path);
   1.275 +}
   1.276 +
   1.277  gboolean set_charset(const char *name,GError **err)
   1.278  {
   1.279      /* The various UNICODE encodings all share the same character set. */
   1.280 @@ -253,27 +476,33 @@
   1.281  {
   1.282      GError *err=NULL;
   1.283      GOptionContext *context;
   1.284 +    GOptionGroup *compatibility;
   1.285      context=g_option_context_new(
   1.286 -      "file - looks for errors in Project Gutenberg(TM) etexts");
   1.287 +      "file - look for errors in Project Gutenberg(TM) etexts");
   1.288      g_option_context_add_main_entries(context,options,NULL);
   1.289 +    g_option_context_add_main_entries(context,config_options,NULL);
   1.290 +    compatibility=g_option_group_new("compatibility",
   1.291 +      "Options for Compatibility with Gutcheck:",
   1.292 +      "Show compatibility options",NULL,NULL);
   1.293 +    g_option_group_add_entries(compatibility,compatibility_options);
   1.294 +    g_option_context_add_group(context,compatibility);
   1.295 +    g_option_context_set_description(context,
   1.296 +      "For simplicity, only the switch options which reverse the\n"
   1.297 +      "default configuration are listed. In most cases, both vanilla\n"
   1.298 +      "and \"no-\" prefixed versions are available for use.");
   1.299      if (!g_option_context_parse(context,argc,argv,&err))
   1.300      {
   1.301  	g_printerr("Bookloupe: %s\n",err->message);
   1.302  	g_printerr("Use \"%s --help\" for help\n",(*argv)[0]);
   1.303  	exit(1);
   1.304      }
   1.305 -    /* Paranoid checking is turned OFF, not on, by its switch */
   1.306 -    pswit[PARANOID_SWITCH]=!pswit[PARANOID_SWITCH];
   1.307 -    if (pswit[PARANOID_SWITCH])
   1.308 -	/* if running in paranoid mode, typo checks default to enabled */
   1.309 +    if (typo_compat)
   1.310  	pswit[TYPO_SWITCH]=!pswit[TYPO_SWITCH];
   1.311 -    /* Line-end checking is turned OFF, not on, by its switch */
   1.312 -    pswit[LINE_END_SWITCH]=!pswit[LINE_END_SWITCH];
   1.313 -    /* Echoing is turned OFF, not on, by its switch */
   1.314 -    pswit[ECHO_SWITCH]=!pswit[ECHO_SWITCH];
   1.315 -    if (pswit[OVERVIEW_SWITCH])
   1.316 -	/* just print summary; don't echo */
   1.317 -	pswit[ECHO_SWITCH]=FALSE;
   1.318 +    if (paranoid_compat)
   1.319 +    {
   1.320 +	pswit[PARANOID_SWITCH]=!pswit[PARANOID_SWITCH];
   1.321 +	pswit[TYPO_SWITCH]=!pswit[TYPO_SWITCH];
   1.322 +    }
   1.323      /*
   1.324       * Web uploads - for the moment, this is really just a placeholder
   1.325       * until we decide what processing we really want to do on web uploads
   1.326 @@ -300,8 +529,16 @@
   1.327  	g_printerr("%s\n",err->message);
   1.328  	exit(1);
   1.329      }
   1.330 +    if (pswit[DUMP_CONFIG_SWITCH])
   1.331 +    {
   1.332 +	dump_config();
   1.333 +	exit(0);
   1.334 +    }
   1.335      g_free(opt_charset);
   1.336      opt_charset=NULL;
   1.337 +    if (pswit[OVERVIEW_SWITCH])
   1.338 +	/* just print summary; don't echo */
   1.339 +	pswit[ECHO_SWITCH]=FALSE;
   1.340      if (*argc<2)
   1.341      {
   1.342  	proghelp(context);
   1.343 @@ -448,6 +685,15 @@
   1.344      saved_cp=GetConsoleOutputCP();
   1.345  #endif
   1.346      running_from=g_path_get_dirname(argv[0]);
   1.347 +    /* Paranoid checking is turned OFF, not on, by its switch */
   1.348 +    pswit[PARANOID_SWITCH]=TRUE;
   1.349 +    /* if running in paranoid mode, typo checks default to enabled */
   1.350 +    pswit[TYPO_SWITCH]=TRUE;
   1.351 +    /* Line-end checking is turned OFF, not on, by its switch */
   1.352 +    pswit[LINE_END_SWITCH]=TRUE;
   1.353 +    /* Echoing is turned OFF, not on, by its switch */
   1.354 +    pswit[ECHO_SWITCH]=TRUE;
   1.355 +    parse_config_file();
   1.356      parse_options(&argc,&argv);
   1.357      if (pswit[USERTYPO_SWITCH])
   1.358  	read_user_scannos();
   1.359 @@ -489,6 +735,8 @@
   1.360      if (usertypo)
   1.361  	g_tree_unref(usertypo);
   1.362      set_charset(NULL,NULL);
   1.363 +    if (config)
   1.364 +	g_key_file_free(config);
   1.365      return 0;
   1.366  }
   1.367