# HG changeset patch # User Guido Berhoerster # Date 1502117987 -7200 # Node ID cf81eb0c2d5a2b3eb1cbe37923d67c48ad28adbd # Parent 8768fbd09bc50374fd3559eabd4d37a313c51d68 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. diff -r 8768fbd09bc5 -r cf81eb0c2d5a cmd.c --- 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); } diff -r 8768fbd09bc5 -r cf81eb0c2d5a pwfile.c --- 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); } diff -r 8768fbd09bc5 -r cf81eb0c2d5a pwm.1.xml --- 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 @@ guido+pwm@berhoerster.name - 3 August, 2017 + 6 August, 2017 pwm @@ -505,7 +505,25 @@ q - Quit pwm. + Quit pwm. If running in interactive mode + and there are unsaved changes, pwm will not + terminate but display a warning message. If the quit command is + invoked twice consecutively, pwm will discard + unsaved changes and terminate. + + + + Quit and discard unsaved changes + + + Quit + + + Q + + + Quit pwm and discard any unsaved changes + without a warning. diff -r 8768fbd09bc5 -r cf81eb0c2d5a pwm.c --- 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 + * Copyright (C) 2017 Guido Berhoerster * * 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]); } diff -r 8768fbd09bc5 -r cf81eb0c2d5a pwm.h --- 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 + * Copyright (C) 2017 Guido Berhoerster * * 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 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]; };