projects/pwm

changeset 13:cf81eb0c2d5a

Warn before quitting if there are unsaved changes

If there are unsaved changes emit a warning when the quit command is used. Only
quit if the quit command is used twice with no other command in between.
Add a new Quit command which immediatly quits pwm without a warning.
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Mon Aug 07 16:59:47 2017 +0200 (2017-08-07)
parents 8768fbd09bc5
children a01899a6e4bb
files cmd.c pwfile.c pwm.1.xml pwm.c pwm.h
line diff
     1.1 --- a/cmd.c	Thu Aug 03 10:22:07 2017 +0200
     1.2 +++ b/cmd.c	Mon Aug 07 16:59:47 2017 +0200
     1.3 @@ -172,6 +172,7 @@
     1.4      { "h", "help", "help", "Show help text", cmd_help },
     1.5      { "w", "write", "write", "Write the database", cmd_write },
     1.6      { "q", "quit", "quit", "Quit", cmd_quit },
     1.7 +    { "Q", "Quit", "Quit", "Quit without checking", cmd_quit },
     1.8      { 0 }
     1.9  };
    1.10  
    1.11 @@ -911,6 +912,12 @@
    1.12  		return (CMD_USAGE);
    1.13  	}
    1.14  
    1.15 +	if ((argv[0][0] == 'q') && ctx->unsaved_changes &&
    1.16 +	    (ctx->prev_cmd != NULL) && (strcmp(ctx->prev_cmd, "quit") != 0)) {
    1.17 +		printf("Warning: There are unsaved changes\n");
    1.18 +		return (CMD_OK);
    1.19 +	}
    1.20 +
    1.21  	return (CMD_QUIT);
    1.22  }
    1.23  
     2.1 --- a/pwfile.c	Thu Aug 03 10:22:07 2017 +0200
     2.2 +++ b/pwfile.c	Mon Aug 07 16:59:47 2017 +0200
     2.3 @@ -213,6 +213,8 @@
     2.4  
     2.5  	free(pws3_record_list);
     2.6  
     2.7 +	ctx->unsaved_changes = 0;
     2.8 +
     2.9  	return (0);
    2.10  }
    2.11  
    2.12 @@ -448,6 +450,8 @@
    2.13  	}
    2.14  	free(tmpfilename);
    2.15  
    2.16 +	ctx->unsaved_changes = !!retval;
    2.17 +
    2.18  	return (retval);
    2.19  }
    2.20  
    2.21 @@ -818,6 +822,8 @@
    2.22  	memcpy(entry->uuid, uuid, sizeof (entry->uuid));
    2.23  	RB_INSERT(record_id_tree, ctx->record_id_tree, entry);
    2.24  
    2.25 +	ctx->unsaved_changes = 1;
    2.26 +
    2.27  	return (0);
    2.28  }
    2.29  
    2.30 @@ -834,6 +840,8 @@
    2.31  
    2.32  	update_record(pws3_file_get_record(ctx->file, uuid), record);
    2.33  
    2.34 +	ctx->unsaved_changes = 1;
    2.35 +
    2.36  	return (0);
    2.37  }
    2.38  
    2.39 @@ -854,6 +862,8 @@
    2.40  	    &(struct record_id_entry){ .id = id });
    2.41  	free(RB_REMOVE(record_id_tree, ctx->record_id_tree, entry));
    2.42  
    2.43 +	ctx->unsaved_changes = 1;
    2.44 +
    2.45  	return (0);
    2.46  }
    2.47  
    2.48 @@ -886,6 +896,8 @@
    2.49  	}
    2.50  	pws3_file_insert_empty_group(ctx->file, empty_group_field);
    2.51  
    2.52 +	ctx->unsaved_changes = 1;
    2.53 +
    2.54  	return (0);
    2.55  }
    2.56  
    2.57 @@ -900,6 +912,8 @@
    2.58  	}
    2.59  	pws3_field_destroy(empty_group_field);
    2.60  
    2.61 +	ctx->unsaved_changes = 1;
    2.62 +
    2.63  	return (0);
    2.64  }
    2.65  
     3.1 --- a/pwm.1.xml	Thu Aug 03 10:22:07 2017 +0200
     3.2 +++ b/pwm.1.xml	Mon Aug 07 16:59:47 2017 +0200
     3.3 @@ -34,7 +34,7 @@
     3.4        <email>guido+pwm@berhoerster.name</email>
     3.5        <personblurb/>
     3.6      </author>
     3.7 -    <date>3 August, 2017</date>
     3.8 +    <date>6 August, 2017</date>
     3.9    </info>
    3.10    <refmeta>
    3.11      <refentrytitle>pwm</refentrytitle>
    3.12 @@ -505,7 +505,25 @@
    3.13                <command>q</command>
    3.14                <sbr/>
    3.15              </cmdsynopsis>
    3.16 -            <para>Quit <command>pwm</command>.</para>
    3.17 +            <para>Quit <command>pwm</command>.  If running in interactive mode
    3.18 +            and there are unsaved changes, <command>pwm</command> will not
    3.19 +            terminate but display a warning message. If the quit command is
    3.20 +            invoked twice consecutively, <command>pwm</command> will discard
    3.21 +            unsaved changes and terminate.</para>
    3.22 +          </listitem>
    3.23 +        </varlistentry>
    3.24 +        <varlistentry>
    3.25 +          <term>Quit and discard unsaved changes</term>
    3.26 +          <listitem>
    3.27 +            <cmdsynopsis>
    3.28 +              <command>Quit</command>
    3.29 +            </cmdsynopsis>
    3.30 +            <cmdsynopsis>
    3.31 +              <command>Q</command>
    3.32 +              <sbr/>
    3.33 +            </cmdsynopsis>
    3.34 +            <para>Quit <command>pwm</command> and discard any unsaved changes
    3.35 +            without a warning.</para>
    3.36            </listitem>
    3.37          </varlistentry>
    3.38        </variablelist>
     4.1 --- a/pwm.c	Thu Aug 03 10:22:07 2017 +0200
     4.2 +++ b/pwm.c	Mon Aug 07 16:59:47 2017 +0200
     4.3 @@ -1,5 +1,5 @@
     4.4  /*
     4.5 - * Copyright (C) 2016 Guido Berhoerster <guido+pwm@berhoerster.name>
     4.6 + * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
     4.7   *
     4.8   * Permission is hereby granted, free of charge, to any person obtaining
     4.9   * a copy of this software and associated documentation files (the
    4.10 @@ -69,6 +69,7 @@
    4.11  	int		i;
    4.12  
    4.13  	for (;;) {
    4.14 +		cmd = NULL;
    4.15  		if (fgets(buf, (int)sizeof (buf), stdin) == NULL) {
    4.16  			if (ferror(stdin)) {
    4.17  				/* error */
    4.18 @@ -146,6 +147,10 @@
    4.19  		}
    4.20  
    4.21  next:
    4.22 +		if (cmd != NULL) {
    4.23 +			ctx->prev_cmd = cmd->full_cmd;
    4.24 +		}
    4.25 +
    4.26  		for (i = 0; i < argc; i++) {
    4.27  			free(argv[i]);
    4.28  		}
     5.1 --- a/pwm.h	Thu Aug 03 10:22:07 2017 +0200
     5.2 +++ b/pwm.h	Mon Aug 07 16:59:47 2017 +0200
     5.3 @@ -1,5 +1,5 @@
     5.4  /*
     5.5 - * Copyright (C) 2016 Guido Berhoerster <guido+pwm@berhoerster.name>
     5.6 + * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
     5.7   *
     5.8   * Permission is hereby granted, free of charge, to any person obtaining
     5.9   * a copy of this software and associated documentation files (the
    5.10 @@ -27,9 +27,11 @@
    5.11  #include <pws.h>
    5.12  
    5.13  struct pwm_ctx {
    5.14 +	const char	*prev_cmd;
    5.15  	char		*filename;
    5.16  	struct pws3_file *file;
    5.17  	struct record_id_tree *record_id_tree;
    5.18 +	int		unsaved_changes;
    5.19  	unsigned int	next_id;
    5.20  	char		password[PWS3_MAX_PASSWORD_LEN + 1];
    5.21  };