Mercurial > 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, 07 Aug 2017 16:59:47 +0200 |
parents | 8768fbd09bc5 |
children | a01899a6e4bb |
files | cmd.c pwfile.c pwm.1.xml pwm.c pwm.h |
diffstat | 5 files changed, 50 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/cmd.c Thu Aug 03 10:22:07 2017 +0200 +++ b/cmd.c Mon Aug 07 16:59:47 2017 +0200 @@ -172,6 +172,7 @@ { "h", "help", "help", "Show help text", cmd_help }, { "w", "write", "write", "Write the database", cmd_write }, { "q", "quit", "quit", "Quit", cmd_quit }, + { "Q", "Quit", "Quit", "Quit without checking", cmd_quit }, { 0 } }; @@ -911,6 +912,12 @@ return (CMD_USAGE); } + if ((argv[0][0] == 'q') && ctx->unsaved_changes && + (ctx->prev_cmd != NULL) && (strcmp(ctx->prev_cmd, "quit") != 0)) { + printf("Warning: There are unsaved changes\n"); + return (CMD_OK); + } + return (CMD_QUIT); }
--- a/pwfile.c Thu Aug 03 10:22:07 2017 +0200 +++ b/pwfile.c Mon Aug 07 16:59:47 2017 +0200 @@ -213,6 +213,8 @@ free(pws3_record_list); + ctx->unsaved_changes = 0; + return (0); } @@ -448,6 +450,8 @@ } free(tmpfilename); + ctx->unsaved_changes = !!retval; + return (retval); } @@ -818,6 +822,8 @@ memcpy(entry->uuid, uuid, sizeof (entry->uuid)); RB_INSERT(record_id_tree, ctx->record_id_tree, entry); + ctx->unsaved_changes = 1; + return (0); } @@ -834,6 +840,8 @@ update_record(pws3_file_get_record(ctx->file, uuid), record); + ctx->unsaved_changes = 1; + return (0); } @@ -854,6 +862,8 @@ &(struct record_id_entry){ .id = id }); free(RB_REMOVE(record_id_tree, ctx->record_id_tree, entry)); + ctx->unsaved_changes = 1; + return (0); } @@ -886,6 +896,8 @@ } pws3_file_insert_empty_group(ctx->file, empty_group_field); + ctx->unsaved_changes = 1; + return (0); } @@ -900,6 +912,8 @@ } pws3_field_destroy(empty_group_field); + ctx->unsaved_changes = 1; + return (0); }
--- a/pwm.1.xml Thu Aug 03 10:22:07 2017 +0200 +++ b/pwm.1.xml Mon Aug 07 16:59:47 2017 +0200 @@ -34,7 +34,7 @@ <email>guido+pwm@berhoerster.name</email> <personblurb/> </author> - <date>3 August, 2017</date> + <date>6 August, 2017</date> </info> <refmeta> <refentrytitle>pwm</refentrytitle> @@ -505,7 +505,25 @@ <command>q</command> <sbr/> </cmdsynopsis> - <para>Quit <command>pwm</command>.</para> + <para>Quit <command>pwm</command>. If running in interactive mode + and there are unsaved changes, <command>pwm</command> will not + terminate but display a warning message. If the quit command is + invoked twice consecutively, <command>pwm</command> will discard + unsaved changes and terminate.</para> + </listitem> + </varlistentry> + <varlistentry> + <term>Quit and discard unsaved changes</term> + <listitem> + <cmdsynopsis> + <command>Quit</command> + </cmdsynopsis> + <cmdsynopsis> + <command>Q</command> + <sbr/> + </cmdsynopsis> + <para>Quit <command>pwm</command> and discard any unsaved changes + without a warning.</para> </listitem> </varlistentry> </variablelist>
--- a/pwm.c Thu Aug 03 10:22:07 2017 +0200 +++ b/pwm.c Mon Aug 07 16:59:47 2017 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Guido Berhoerster <guido+pwm@berhoerster.name> + * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name> * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -69,6 +69,7 @@ int i; for (;;) { + cmd = NULL; if (fgets(buf, (int)sizeof (buf), stdin) == NULL) { if (ferror(stdin)) { /* error */ @@ -146,6 +147,10 @@ } next: + if (cmd != NULL) { + ctx->prev_cmd = cmd->full_cmd; + } + for (i = 0; i < argc; i++) { free(argv[i]); }
--- a/pwm.h Thu Aug 03 10:22:07 2017 +0200 +++ b/pwm.h Mon Aug 07 16:59:47 2017 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Guido Berhoerster <guido+pwm@berhoerster.name> + * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name> * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -27,9 +27,11 @@ #include <pws.h> struct pwm_ctx { + const char *prev_cmd; char *filename; struct pws3_file *file; struct record_id_tree *record_id_tree; + int unsaved_changes; unsigned int next_id; char password[PWS3_MAX_PASSWORD_LEN + 1]; };