Mercurial > projects > pwm
comparison cmd.c @ 10:17fb30016e64
Enable access to record and file metadata
Add info command to show file metadata.
Enable display of creation and modification dates of records.
author | Guido Berhoerster <guido+pwm@berhoerster.name> |
---|---|
date | Fri, 28 Jul 2017 15:53:57 +0200 |
parents | b5c4267a7182 |
children | 85bce13237cf |
comparison
equal
deleted
inserted
replaced
9:60c8ab006e55 | 10:17fb30016e64 |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2016 Guido Berhoerster <guido+pwm@berhoerster.name> | 2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name> |
3 * | 3 * |
4 * Permission is hereby granted, free of charge, to any person obtaining | 4 * Permission is hereby granted, free of charge, to any person obtaining |
5 * a copy of this software and associated documentation files (the | 5 * a copy of this software and associated documentation files (the |
6 * "Software"), to deal in the Software without restriction, including | 6 * "Software"), to deal in the Software without restriction, including |
7 * without limitation the rights to use, copy, modify, merge, publish, | 7 * without limitation the rights to use, copy, modify, merge, publish, |
31 #ifdef HAVE_READPASSPHRASE_H | 31 #ifdef HAVE_READPASSPHRASE_H |
32 #include <readpassphrase.h> | 32 #include <readpassphrase.h> |
33 #endif /* READPASSPHRASE_H */ | 33 #endif /* READPASSPHRASE_H */ |
34 #include <stdlib.h> | 34 #include <stdlib.h> |
35 #include <string.h> | 35 #include <string.h> |
36 #include <time.h> | |
36 #include <unistd.h> | 37 #include <unistd.h> |
37 | 38 |
38 #include "cmd.h" | 39 #include "cmd.h" |
39 #include "pwfile.h" | 40 #include "pwfile.h" |
40 #include "util.h" | 41 #include "util.h" |
42 | |
43 #define TIME_FORMAT "%Y-%m-%dT%TZ" | |
44 #define TIME_SIZE (4 + 1 + 2 + 1 + 2 + 1 + 8 + 1 + 1) | |
41 | 45 |
42 enum field_type { | 46 enum field_type { |
43 FIELD_UNKNOWN = -1, | 47 FIELD_UNKNOWN = -1, |
44 FIELD_GROUP, | 48 FIELD_GROUP, |
45 FIELD_TITLE, | 49 FIELD_TITLE, |
46 FIELD_USERNAME, | 50 FIELD_USERNAME, |
47 FIELD_PASSWORD, | 51 FIELD_PASSWORD, |
48 FIELD_NOTES, | 52 FIELD_NOTES, |
49 FIELD_URL | 53 FIELD_URL, |
54 FIELD_MTIME, | |
55 FIELD_CTIME | |
50 }; | 56 }; |
51 | 57 |
58 static enum cmd_return cmd_info(struct pwm_ctx *, int, char *[]); | |
52 static enum cmd_return cmd_list(struct pwm_ctx *, int, char *[]); | 59 static enum cmd_return cmd_list(struct pwm_ctx *, int, char *[]); |
53 static enum cmd_return cmd_create(struct pwm_ctx *, int, char *[]); | 60 static enum cmd_return cmd_create(struct pwm_ctx *, int, char *[]); |
54 static enum cmd_return cmd_modify(struct pwm_ctx *, int, char *[]); | 61 static enum cmd_return cmd_modify(struct pwm_ctx *, int, char *[]); |
55 static enum cmd_return cmd_remove(struct pwm_ctx *, int, char *[]); | 62 static enum cmd_return cmd_remove(struct pwm_ctx *, int, char *[]); |
56 static enum cmd_return cmd_show(struct pwm_ctx *, int, char *[]); | 63 static enum cmd_return cmd_show(struct pwm_ctx *, int, char *[]); |
66 "group", | 73 "group", |
67 "title", | 74 "title", |
68 "username", | 75 "username", |
69 "password", | 76 "password", |
70 "notes", | 77 "notes", |
71 "url" | 78 "url", |
79 "ctime", | |
80 "mtime" | |
72 }; | 81 }; |
73 | 82 |
74 static const char *field_labels[] = { | 83 static const char *field_labels[] = { |
75 "Group: ", | 84 "Group: ", |
76 "Title: ", | 85 "Title: ", |
77 "Username: ", | 86 "Username: ", |
78 "Password: ", | 87 "Password: ", |
79 "Notes: ", | 88 "Notes: ", |
80 "URL: " | 89 "URL: ", |
90 "Created: ", | |
91 "Modified: " | |
81 }; | 92 }; |
82 | 93 |
83 static struct cmd cmds[] = { | 94 static struct cmd cmds[] = { |
95 { "i", "info", "info", "Show metadata information about the current file", | |
96 cmd_info }, | |
84 { "ls", "list", "list", "List entries", cmd_list }, | 97 { "ls", "list", "list", "List entries", cmd_list }, |
85 { "c", "create", "create field=value ...", "Create entry", cmd_create }, | 98 { "c", "create", "create field=value ...", "Create entry", cmd_create }, |
86 { "m", "modify", "modify id field=value ...", "Modify entry", cmd_modify }, | 99 { "m", "modify", "modify id field=value ...", "Modify entry", cmd_modify }, |
87 { "rm", "remove", "remove id", "Delete entry", cmd_remove }, | 100 { "rm", "remove", "remove id", "Delete entry", cmd_remove }, |
88 { "s", "show", "show id field", "Show entry", cmd_show }, | 101 { "s", "show", "show id field", "Show entry", cmd_show }, |
148 break; | 161 break; |
149 case FIELD_URL: | 162 case FIELD_URL: |
150 record->url = value; | 163 record->url = value; |
151 break; | 164 break; |
152 default: | 165 default: |
153 break; | 166 return (FIELD_UNKNOWN); |
154 } | 167 } |
155 return (i); | 168 return (i); |
156 } | 169 } |
157 } | 170 } |
158 | 171 |
172 return (-1); | 185 return (-1); |
173 } | 186 } |
174 *idp = (unsigned int)x; | 187 *idp = (unsigned int)x; |
175 | 188 |
176 return (0); | 189 return (0); |
190 } | |
191 | |
192 static enum cmd_return | |
193 cmd_info(struct pwm_ctx *ctx, int argc, char *argv[]) | |
194 { | |
195 struct metadata *metadata; | |
196 struct tm *tm; | |
197 char timebuf[TIME_SIZE]; | |
198 | |
199 if (argc != 1) { | |
200 return (CMD_USAGE); | |
201 } | |
202 | |
203 metadata = pwfile_get_metadata(ctx); | |
204 printf("Format: 0x%04x\n", metadata->version); | |
205 if (metadata->user != NULL) { | |
206 printf("User: %s\n", metadata->user); | |
207 } | |
208 if (metadata->user != NULL) { | |
209 printf("Host: %s\n", metadata->host); | |
210 } | |
211 if (metadata->user != NULL) { | |
212 printf("Application: %s\n", metadata->application); | |
213 } | |
214 tm = gmtime(&metadata->timestamp); | |
215 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm); | |
216 printf("Last Saved: %s\n", timebuf); | |
217 | |
218 pwfile_destroy_metadata(metadata); | |
219 | |
220 return (CMD_OK); | |
177 } | 221 } |
178 | 222 |
179 static enum cmd_return | 223 static enum cmd_return |
180 cmd_list(struct pwm_ctx *ctx, int argc, char *argv[]) | 224 cmd_list(struct pwm_ctx *ctx, int argc, char *argv[]) |
181 { | 225 { |
288 } | 332 } |
289 | 333 |
290 static void | 334 static void |
291 print_record(struct record *record, int fields[], int show_labels, FILE *fp) | 335 print_record(struct record *record, int fields[], int show_labels, FILE *fp) |
292 { | 336 { |
337 struct tm *tm; | |
338 char timebuf[TIME_SIZE]; | |
339 | |
293 if (fields[FIELD_TITLE]) { | 340 if (fields[FIELD_TITLE]) { |
294 if (print_field(field_labels[FIELD_TITLE], record->title, | 341 if (print_field(field_labels[FIELD_TITLE], record->title, |
295 show_labels, fp) != 0) { | 342 show_labels, fp) != 0) { |
296 return; | 343 return; |
297 } | 344 } |
320 return; | 367 return; |
321 } | 368 } |
322 } | 369 } |
323 if (fields[FIELD_URL]) { | 370 if (fields[FIELD_URL]) { |
324 if (print_field(field_labels[FIELD_URL], record->url, | 371 if (print_field(field_labels[FIELD_URL], record->url, |
372 show_labels, fp)) { | |
373 return; | |
374 } | |
375 } | |
376 if (fields[FIELD_CTIME]) { | |
377 tm = gmtime(&record->ctime); | |
378 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm); | |
379 if (print_field(field_labels[FIELD_CTIME], timebuf, | |
380 show_labels, fp)) { | |
381 return; | |
382 } | |
383 } | |
384 if (fields[FIELD_MTIME]) { | |
385 tm = gmtime(&record->mtime); | |
386 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm); | |
387 if (print_field(field_labels[FIELD_MTIME], timebuf, | |
325 show_labels, fp)) { | 388 show_labels, fp)) { |
326 return; | 389 return; |
327 } | 390 } |
328 } | 391 } |
329 } | 392 } |