projects/pwm

changeset 28:e3db02d7f1f4

Add set command for setting or retrieving option values
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Wed Sep 27 19:44:05 2017 +0200 (2017-09-27)
parents 722a45b4028b
children 00d782cb45fa
files cmd.c pwm.1.xml pwm.c pwm.h
line diff
     1.1 --- a/cmd.c	Mon Sep 25 21:21:25 2017 +0200
     1.2 +++ b/cmd.c	Wed Sep 27 19:44:05 2017 +0200
     1.3 @@ -84,6 +84,13 @@
     1.4  	CHARCLASS_GRAPH
     1.5  };
     1.6  
     1.7 +enum option_type {
     1.8 +	OPTION_UNKNOWN = -1,
     1.9 +	OPTION_FILENAME,
    1.10 +	OPTION_PIPECOMMAND
    1.11 +};
    1.12 +
    1.13 +static enum cmd_return	cmd_set(struct pwm_ctx *, int, char *[]);
    1.14  static enum cmd_return	cmd_define(struct pwm_ctx *, int, char *[]);
    1.15  static enum cmd_return	cmd_status(struct pwm_ctx *, int, char *[]);
    1.16  static enum cmd_return	cmd_info(struct pwm_ctx *, int, char *[]);
    1.17 @@ -154,7 +161,15 @@
    1.18      CHARS_DIGIT CHARS_UPPER CHARS_LOWER CHARS_PUNCT
    1.19  };
    1.20  
    1.21 +static const char *optionv[] = {
    1.22 +    "filename",
    1.23 +    "pipecommand",
    1.24 +    NULL
    1.25 +};
    1.26 +
    1.27  static struct cmd cmds[] = {
    1.28 +    { "S", "set", "set [option=value]", "Set an option or show option values",
    1.29 +    cmd_set },
    1.30      { "D", "define", "define name=value", "Define a macro", cmd_define },
    1.31      { "t", "status", "status", "Redisplay an error message of the previous "
    1.32      "command and unsaved changes", cmd_status },
    1.33 @@ -221,6 +236,45 @@
    1.34  }
    1.35  
    1.36  static enum cmd_return
    1.37 +cmd_set(struct pwm_ctx *ctx, int argc, char *argv[])
    1.38 +{
    1.39 +	enum option_type type;
    1.40 +	char		*value;
    1.41 +
    1.42 +	if (argc == 1) {
    1.43 +		/* show options */
    1.44 +		if ((io_printf("%s: %s\n", optionv[OPTION_FILENAME],
    1.45 +		    ctx->filename) == IO_SIGNAL) ||
    1.46 +		    (io_printf("%s: %s\n", optionv[OPTION_PIPECOMMAND],
    1.47 +		    (ctx->pipecmd != NULL) ? ctx->pipecmd : "") == IO_SIGNAL)) {
    1.48 +			return (CMD_SIGNAL);
    1.49 +		}
    1.50 +
    1.51 +		return (CMD_OK);
    1.52 +	} else if (argc != 2) {
    1.53 +		return (CMD_USAGE);
    1.54 +	}
    1.55 +
    1.56 +	type = parse_arg(argv[1], optionv, '=', &value);
    1.57 +	switch (type) {
    1.58 +	case OPTION_FILENAME:
    1.59 +		free(ctx->filename);
    1.60 +		ctx->filename = (value[0] != '\0') ? xstrdup(value) :
    1.61 +		    xasprintf(&ctx->filename, "%s/pwm.psafe3", ctx->dirname);
    1.62 +		break;
    1.63 +	case OPTION_PIPECOMMAND:
    1.64 +		free(ctx->pipecmd);
    1.65 +		ctx->pipecmd = (value[0] != '\0') ? xstrdup(value) : NULL;
    1.66 +		break;
    1.67 +	default:
    1.68 +		pwm_err(ctx, "bad option name \"%s\"", argv[1]);
    1.69 +		return (CMD_ERR);
    1.70 +	}
    1.71 +
    1.72 +	return (CMD_OK);
    1.73 +}
    1.74 +
    1.75 +static enum cmd_return
    1.76  cmd_define(struct pwm_ctx *ctx, int argc, char *argv[])
    1.77  {
    1.78  	int		retval = CMD_ERR;
    1.79 @@ -998,9 +1052,12 @@
    1.80  	struct record	*record = NULL;
    1.81  	enum field_type	type;
    1.82  	int		fields[COUNTOF(field_namev) - 1] = { 0 };
    1.83 -	struct proc	proc = { 0 };
    1.84 +	char		*cmd;
    1.85 +	struct proc	proc = { 0 };	
    1.86  
    1.87 -	if (argc != 4) {
    1.88 +	/* if pipecommand is set, the last argument is optional */
    1.89 +	if (((ctx->pipecmd == NULL) && (argc != 4)) ||
    1.90 +	    ((ctx->pipecmd != NULL) && ((argc < 3) || (argc > 4)))) {
    1.91  		return (CMD_USAGE);
    1.92  	}
    1.93  
    1.94 @@ -1016,10 +1073,12 @@
    1.95  	}
    1.96  	fields[type] = 1;
    1.97  
    1.98 -	if (proc_open(&proc, argv[3], "w") != IO_OK) {
    1.99 +	cmd = (argc == 4) ? argv[3] : ctx->pipecmd;
   1.100 +	if (proc_open(&proc, cmd, "w") != IO_OK) {
   1.101  		goto out;
   1.102  	}
   1.103  
   1.104 +
   1.105  	record = pwfile_get_record(ctx, id);
   1.106  	if (record == NULL) {
   1.107  		pwm_err(ctx, "record %u does not exist", id);
     2.1 --- a/pwm.1.xml	Mon Sep 25 21:21:25 2017 +0200
     2.2 +++ b/pwm.1.xml	Wed Sep 27 19:44:05 2017 +0200
     2.3 @@ -34,7 +34,7 @@
     2.4        <email>guido+pwm@berhoerster.name</email>
     2.5        <personblurb/>
     2.6      </author>
     2.7 -    <date>25 September, 2017</date>
     2.8 +    <date>27 September, 2017</date>
     2.9    </info>
    2.10    <refmeta>
    2.11      <refentrytitle>pwm</refentrytitle>
    2.12 @@ -138,6 +138,29 @@
    2.13      in memory, any changes must be explicitly written back to the database
    2.14      using the write command.</para>
    2.15      <refsect2>
    2.16 +      <title>Configuration Variables</title>
    2.17 +      <para><command>pwm</command> can be configured through configuration
    2.18 +      variables using the <command>set</command> command. The following
    2.19 +      configuration variables are supported:</para>
    2.20 +      <variablelist>
    2.21 +        <varlistentry>
    2.22 +          <term>filename</term>
    2.23 +          <listitem>
    2.24 +            <para>Specifies the password database file if no other file is
    2.25 +            given as a command line argument.</para>
    2.26 +          </listitem>
    2.27 +        </varlistentry>
    2.28 +        <varlistentry>
    2.29 +          <term>pipecommand</term>
    2.30 +          <listitem>
    2.31 +            <para>Specifies a default command which is invoked by the
    2.32 +            <command>pipe</command> command if not other command is
    2.33 +            explicitly specified as a argument.</para>
    2.34 +          </listitem>
    2.35 +        </varlistentry>
    2.36 +      </variablelist>
    2.37 +    </refsect2>
    2.38 +    <refsect2>
    2.39        <title>IDs</title>
    2.40        <para>Database entries are referred to by an ID value which is a
    2.41        positive integer value that is guaranteed to be unqiue during the run
    2.42 @@ -374,7 +397,8 @@
    2.43              argument, thus special care should be applied to quoting command.
    2.44              See the <citerefentry><refentrytitle>sh</refentrytitle>
    2.45              <manvolnum>1</manvolnum></citerefentry> manual page for
    2.46 -            details.</para>
    2.47 +            details. The <replaceable>command</replaceable> argument is
    2.48 +            optional if the pipecommand configuration variable is set.</para>
    2.49            </listitem>
    2.50          </varlistentry>
    2.51          <varlistentry>
    2.52 @@ -510,6 +534,28 @@
    2.53            </listitem>
    2.54          </varlistentry>
    2.55          <varlistentry>
    2.56 +          <term>Set configuration variable or display their values</term>
    2.57 +          <listitem>
    2.58 +            <cmdsynopsis>
    2.59 +              <command>set</command>
    2.60 +              <arg choice="opt">
    2.61 +                <replaceable>variable</replaceable>=<replaceable>value</replaceable>
    2.62 +              </arg>
    2.63 +            </cmdsynopsis>
    2.64 +            <cmdsynopsis>
    2.65 +              <command>S</command>
    2.66 +              <arg choice="plain">
    2.67 +                <replaceable>variable</replaceable>=<replaceable>value</replaceable>
    2.68 +              </arg>
    2.69 +              <sbr/>
    2.70 +            </cmdsynopsis>
    2.71 +            <para>If <replaceable>variable</replaceable> is specified, set the
    2.72 +            configuration variable <replaceable>variable</replaceable>
    2.73 +            to <replaceable>value</replaceable>. If no arguments are specified,
    2.74 +            display the current values of all configuration variables.</para>
    2.75 +          </listitem>
    2.76 +        </varlistentry>
    2.77 +        <varlistentry>
    2.78            <term>Display help text</term>
    2.79            <listitem>
    2.80              <cmdsynopsis>
     3.1 --- a/pwm.c	Mon Sep 25 21:21:25 2017 +0200
     3.2 +++ b/pwm.c	Wed Sep 27 19:44:05 2017 +0200
     3.3 @@ -454,6 +454,7 @@
     3.4  	free(ctx.filename);
     3.5  	free(ctx.dirname);
     3.6  	free(ctx.errmsg);
     3.7 +	free(ctx.pipecmd);
     3.8  
     3.9  	exit(status);
    3.10  }
     4.1 --- a/pwm.h	Mon Sep 25 21:21:25 2017 +0200
     4.2 +++ b/pwm.h	Wed Sep 27 19:44:05 2017 +0200
     4.3 @@ -46,6 +46,7 @@
     4.4  struct pwm_ctx {
     4.5  	int		is_interactive;
     4.6  	int		is_readonly;
     4.7 +	char		*pipecmd;
     4.8  	const char	*prev_cmd;
     4.9  	char		*errmsg;
    4.10  	char		*dirname;