annotate cmd.c @ 11:85bce13237cf

Add filter expressions to list command Refactor field parsing so it can be used for parsing fields, field assignments and filter expressions.
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Mon, 31 Jul 2017 09:20:21 +0200
parents 17fb30016e64
children 8768fbd09bc5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1 /*
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
3 *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
4 * Permission is hereby granted, free of charge, to any person obtaining
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
5 * a copy of this software and associated documentation files (the
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
6 * "Software"), to deal in the Software without restriction, including
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
7 * without limitation the rights to use, copy, modify, merge, publish,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
8 * distribute, sublicense, and/or sell copies of the Software, and to
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
9 * permit persons to whom the Software is furnished to do so, subject to
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
10 * the following conditions:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
11 *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
12 * The above copyright notice and this permission notice shall be included
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
13 * in all copies or substantial portions of the Software.
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
14 *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
22 */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
23
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
24 #include "compat.h"
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
25
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
26 #ifdef HAVE_ERR_H
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
27 #include <err.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
28 #endif /* HAVE_ERR_H */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
29 #include <errno.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
30 #include <limits.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
31 #ifdef HAVE_READPASSPHRASE_H
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
32 #include <readpassphrase.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
33 #endif /* READPASSPHRASE_H */
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
34 #include <regex.h>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
35 #include <stdlib.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
36 #include <string.h>
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
37 #include <time.h>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
38 #include <unistd.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
39
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
40 #include "cmd.h"
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
41 #include "pwfile.h"
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
42 #include "util.h"
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
43
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
44 #define TIME_FORMAT "%Y-%m-%dT%TZ"
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
45 #define TIME_SIZE (4 + 1 + 2 + 1 + 2 + 1 + 8 + 1 + 1)
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
46
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
47 enum field_type {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
48 FIELD_UNKNOWN = -1,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
49 FIELD_GROUP,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
50 FIELD_TITLE,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
51 FIELD_USERNAME,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
52 FIELD_PASSWORD,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
53 FIELD_NOTES,
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
54 FIELD_URL,
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
55 FIELD_MTIME,
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
56 FIELD_CTIME
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
57 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
58
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
59 static enum cmd_return cmd_info(struct pwm_ctx *, int, char *[]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
60 static enum cmd_return cmd_list(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
61 static enum cmd_return cmd_create(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
62 static enum cmd_return cmd_modify(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
63 static enum cmd_return cmd_remove(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
64 static enum cmd_return cmd_show(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
65 static enum cmd_return cmd_pipe(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
66 static enum cmd_return cmd_creategroup(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
67 static enum cmd_return cmd_removegroup(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
68 static enum cmd_return cmd_changepassword(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
69 static enum cmd_return cmd_help(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
70 static enum cmd_return cmd_write(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
71 static enum cmd_return cmd_quit(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
72
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
73 static const char *field_names[] = {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
74 "group",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
75 "title",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
76 "username",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
77 "password",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
78 "notes",
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
79 "url",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
80 "ctime",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
81 "mtime"
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
82 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
83
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
84 static const char *field_labels[] = {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
85 "Group: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
86 "Title: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
87 "Username: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
88 "Password: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
89 "Notes: ",
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
90 "URL: ",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
91 "Created: ",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
92 "Modified: "
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
93 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
94
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
95 static struct cmd cmds[] = {
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
96 { "i", "info", "info", "Show metadata information about the current file",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
97 cmd_info },
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
98 { "ls", "list", "list [field~regex ...]", "List entries", cmd_list },
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
99 { "c", "create", "create field=value ...", "Create entry", cmd_create },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
100 { "m", "modify", "modify id field=value ...", "Modify entry", cmd_modify },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
101 { "rm", "remove", "remove id", "Delete entry", cmd_remove },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
102 { "s", "show", "show id field", "Show entry", cmd_show },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
103 { "p", "pipe", "pipe id field command", "Pipe entry to external command",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
104 cmd_pipe },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
105 { "cg", "creategroup", "creategroup name", "Create empty group",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
106 cmd_creategroup },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
107 { "rg", "removegroup", "removegroup name", "Delete empty group",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
108 cmd_removegroup },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
109 { "ch", "changepassword", "changepassword", "Change password",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
110 cmd_changepassword },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
111 { "h", "help", "help", "Show help text", cmd_help },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
112 { "w", "write", "write", "Write the database", cmd_write },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
113 { "q", "quit", "quit", "Quit", cmd_quit },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
114 { 0 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
115 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
116
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
117 static enum field_type
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
118 parse_field(char *field_arg, int sep, char **valuep)
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
119 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
120 int i;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
121 size_t field_name_len;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
122
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
123 for (i = 0; i < (int)COUNTOF(field_names); i++) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
124 field_name_len = strlen(field_names[i]);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
125 if ((strncmp(field_names[i], field_arg, field_name_len) == 0) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
126 (field_arg[field_name_len] == sep)) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
127 if (valuep != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
128 *valuep = field_arg + field_name_len + 1;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
129 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
130 return (i);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
131 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
132 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
133
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
134 return (FIELD_UNKNOWN);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
135 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
136
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
137 static int
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
138 parse_id(const char *arg, unsigned int *idp)
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
139 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
140 long x;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
141 char *p;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
142
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
143 errno = 0;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
144 x = strtol(arg, &p, 10);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
145 if ((errno != 0) || (*arg == '\0') || (*p != '\0') || (x > UINT_MAX) ||
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
146 (x <= 0)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
147 return (-1);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
148 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
149 *idp = (unsigned int)x;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
150
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
151 return (0);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
152 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
153
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
154 static enum cmd_return
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
155 cmd_info(struct pwm_ctx *ctx, int argc, char *argv[])
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
156 {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
157 struct metadata *metadata;
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
158 struct tm *tm;
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
159 char timebuf[TIME_SIZE];
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
160
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
161 if (argc != 1) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
162 return (CMD_USAGE);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
163 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
164
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
165 metadata = pwfile_get_metadata(ctx);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
166 printf("Format: 0x%04x\n", metadata->version);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
167 if (metadata->user != NULL) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
168 printf("User: %s\n", metadata->user);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
169 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
170 if (metadata->user != NULL) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
171 printf("Host: %s\n", metadata->host);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
172 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
173 if (metadata->user != NULL) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
174 printf("Application: %s\n", metadata->application);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
175 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
176 tm = gmtime(&metadata->timestamp);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
177 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
178 printf("Last Saved: %s\n", timebuf);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
179
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
180 pwfile_destroy_metadata(metadata);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
181
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
182 return (CMD_OK);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
183 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
184
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
185 static enum cmd_return
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
186 cmd_list(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
187 {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
188 int retval = CMD_ERR;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
189 int i;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
190 regex_t *group_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
191 regex_t *title_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
192 regex_t *username_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
193 regex_t *notes_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
194 regex_t *url_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
195 enum field_type type;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
196 char *value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
197 regex_t **repp;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
198 int errcode;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
199 char *errbuf;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
200 size_t errbuf_size;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
201 union list_item **list = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
202 size_t j;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
203 struct record *record;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
204
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
205 for (i = 1; i < argc; i++) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
206 type = parse_field(argv[i], '~', &value);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
207 if (type == FIELD_UNKNOWN) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
208 fprintf(stderr, "bad field name \"%s\"\n", argv[i]);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
209 goto out;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
210 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
211 if (value[0] == '\0') {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
212 /* skip empty expressions */
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
213 continue;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
214 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
215 switch (type) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
216 case FIELD_GROUP:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
217 repp = &group_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
218 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
219 case FIELD_TITLE:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
220 repp = &title_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
221 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
222 case FIELD_USERNAME:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
223 repp = &username_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
224 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
225 case FIELD_NOTES:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
226 repp = &notes_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
227 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
228 case FIELD_URL:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
229 repp = &url_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
230 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
231 default:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
232 fprintf(stderr, "bad field name \"%s\"\n", argv[i]);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
233 goto out;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
234 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
235
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
236 if (*repp == NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
237 *repp = xmalloc(sizeof (regex_t));
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
238 } else {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
239 regfree(*repp);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
240 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
241 errcode = regcomp(*repp, value, REG_EXTENDED | REG_NOSUB);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
242 if (errcode != 0) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
243 errbuf_size = regerror(errcode, *repp, "", 0);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
244 errbuf = xmalloc(errbuf_size);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
245 regerror(errcode, *repp, errbuf, errbuf_size);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
246 fprintf(stderr, "bad regular expression \"%s\"\n",
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
247 errbuf);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
248 free(errbuf);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
249
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
250 free(*repp);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
251 *repp = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
252
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
253 goto out;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
254 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
255 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
256
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
257 list = pwfile_create_list(ctx);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
258 for (j = 0; list[j] != NULL; j++) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
259 if (list[j]->any.type == ITEM_TYPE_GROUP) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
260 printf("[%s]\n", list[j]->group.group);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
261 } else {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
262 record = pwfile_get_record(ctx, list[j]->record.id);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
263 if (((group_re == NULL) || (regexec(group_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
264 record->group, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
265 ((title_re == NULL) || (regexec(title_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
266 record->title, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
267 ((username_re == NULL) || (regexec(username_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
268 record->username, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
269 ((notes_re == NULL) || (regexec(notes_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
270 record->notes, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
271 ((url_re == NULL) || (regexec(url_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
272 record->url, 0, NULL, 0) == 0))) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
273 printf("%4u %s\n", list[j]->record.id,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
274 (list[j]->record.title != NULL) ?
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
275 list[j]->record.title : "");
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
276 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
277 pwfile_destroy_record(record);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
278 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
279 }
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
280 retval = CMD_OK;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
281
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
282 out:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
283 if (group_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
284 regfree(group_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
285 free(group_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
286 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
287 if (title_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
288 regfree(title_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
289 free(title_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
290 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
291 if (username_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
292 regfree(username_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
293 free(username_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
294 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
295 if (notes_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
296 regfree(notes_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
297 free(notes_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
298 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
299 if (url_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
300 regfree(url_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
301 free(url_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
302 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
303
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
304 pwfile_destroy_list(list);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
305
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
306 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
307 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
308
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
309 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
310 cmd_create(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
311 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
312 int i;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
313 struct record record = { 0 };
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
314 enum field_type type;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
315 char *value;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
316
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
317 if (argc < 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
318 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
319 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
320
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
321 for (i = 1; i < argc; i++) {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
322 type = parse_field(argv[i], '=', &value);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
323 if (type == FIELD_UNKNOWN) {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
324 fprintf(stderr, "bad field assignment \"%s\"\n",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
325 argv[i]);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
326 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
327 if (value[0] == '\0') {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
328 /* skip empty assignments */
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
329 continue;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
330 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
331 switch (type) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
332 case FIELD_GROUP:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
333 record.group = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
334 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
335 case FIELD_TITLE:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
336 record.title = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
337 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
338 case FIELD_USERNAME:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
339 record.username = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
340 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
341 case FIELD_PASSWORD:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
342 record.password = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
343 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
344 case FIELD_NOTES:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
345 record.notes = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
346 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
347 case FIELD_URL:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
348 record.url = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
349 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
350 default:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
351 fprintf(stderr, "bad field name \"%s\"\n", argv[i]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
352 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
353 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
354 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
355
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
356 pwfile_create_record(ctx, &record);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
357
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
358 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
359 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
360
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
361 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
362 cmd_modify(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
363 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
364 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
365 int i;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
366 struct record record = { 0 };
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
367 enum field_type type;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
368 char *value;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
369
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
370 if (argc < 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
371 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
372 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
373
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
374 if (parse_id(argv[1], &id) != 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
375 fprintf(stderr, "invalid id %s\n", argv[1]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
376 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
377 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
378
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
379 for (i = 2; i < argc; i++) {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
380 type = parse_field(argv[i], '=', &value);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
381 if (type == FIELD_UNKNOWN) {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
382 fprintf(stderr, "bad field assignment \"%s\"\n",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
383 argv[i]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
384 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
385 }
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
386 if (value[0] == '\0') {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
387 /* skip empty assignments */
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
388 continue;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
389 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
390 switch (type) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
391 case FIELD_GROUP:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
392 record.group = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
393 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
394 case FIELD_TITLE:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
395 record.title = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
396 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
397 case FIELD_USERNAME:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
398 record.username = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
399 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
400 case FIELD_PASSWORD:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
401 record.password = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
402 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
403 case FIELD_NOTES:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
404 record.notes = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
405 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
406 case FIELD_URL:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
407 record.url = value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
408 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
409 default:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
410 fprintf(stderr, "bad field name \"%s\"\n", argv[i]);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
411 return (CMD_ERR);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
412 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
413 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
414
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
415 pwfile_modify_record(ctx, id, &record);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
416
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
417 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
418 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
419
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
420 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
421 cmd_remove(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
422 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
423 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
424
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
425 if (argc != 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
426 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
427 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
428
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
429 if (parse_id(argv[1], &id) != 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
430 fprintf(stderr, "invalid id %s\n", argv[1]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
431 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
432 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
433
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
434 if (pwfile_remove_record(ctx, id) != 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
435 fprintf(stderr, "failed to remove record %u\n", id);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
436 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
437 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
438
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
439 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
440 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
441
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
442 static int
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
443 print_field(const char *label, const char *value, int show_label, FILE *fp)
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
444 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
445 fprintf(fp, "%s%s\n", show_label ? label : "", (value != NULL) ?
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
446 value : "");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
447 if (ferror(fp)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
448 warn("fprintf");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
449 return (-1);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
450 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
451 return (0);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
452 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
453
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
454 static void
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
455 print_record(struct record *record, int fields[], int show_labels, FILE *fp)
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
456 {
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
457 struct tm *tm;
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
458 char timebuf[TIME_SIZE];
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
459
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
460 if (fields[FIELD_TITLE]) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
461 if (print_field(field_labels[FIELD_TITLE], record->title,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
462 show_labels, fp) != 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
463 return;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
464 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
465 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
466 if (fields[FIELD_GROUP]) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
467 if (print_field(field_labels[FIELD_GROUP], record->group,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
468 show_labels, fp)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
469 return;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
470 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
471 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
472 if (fields[FIELD_USERNAME]) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
473 if (print_field(field_labels[FIELD_USERNAME], record->username,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
474 show_labels, fp)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
475 return;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
476 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
477 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
478 if (fields[FIELD_PASSWORD]) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
479 if (print_field(field_labels[FIELD_PASSWORD], record->password,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
480 show_labels, fp)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
481 return;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
482 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
483 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
484 if (fields[FIELD_NOTES]) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
485 if (print_field(field_labels[FIELD_NOTES], record->notes,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
486 show_labels, fp)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
487 return;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
488 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
489 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
490 if (fields[FIELD_URL]) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
491 if (print_field(field_labels[FIELD_URL], record->url,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
492 show_labels, fp)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
493 return;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
494 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
495 }
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
496 if (fields[FIELD_CTIME]) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
497 tm = gmtime(&record->ctime);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
498 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
499 if (print_field(field_labels[FIELD_CTIME], timebuf,
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
500 show_labels, fp)) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
501 return;
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
502 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
503 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
504 if (fields[FIELD_MTIME]) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
505 tm = gmtime(&record->mtime);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
506 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
507 if (print_field(field_labels[FIELD_MTIME], timebuf,
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
508 show_labels, fp)) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
509 return;
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
510 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
511 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
512 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
513
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
514 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
515 cmd_show(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
516 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
517 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
518 struct record *record;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
519 int i;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
520 enum field_type type;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
521 int fields[COUNTOF(field_names)] = { 0 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
522
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
523 if (argc < 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
524 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
525 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
526
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
527 if (parse_id(argv[1], &id) != 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
528 fprintf(stderr, "invalid id %s\n", argv[1]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
529 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
530 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
531
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
532 for (i = 2; i < argc; i++) {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
533 type = parse_field(argv[i], '\0', NULL);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
534 if (type < 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
535 fprintf(stderr, "bad field name \"%s\"\n", argv[i]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
536 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
537 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
538 fields[type] = 1;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
539 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
540
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
541 record = pwfile_get_record(ctx, id);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
542 if (record == NULL) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
543 fprintf(stderr, "record %u does not exist\n", id);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
544 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
545 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
546 print_record(record, fields, 1, stdout);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
547 pwfile_destroy_record(record);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
548
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
549 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
550 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
551
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
552 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
553 cmd_pipe(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
554 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
555 enum cmd_return retval = CMD_ERR;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
556 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
557 struct record *record = NULL;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
558 enum field_type type;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
559 int fields[COUNTOF(field_names)] = { 0 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
560 FILE *fp = NULL;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
561
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
562 if (argc != 4) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
563 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
564 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
565
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
566 if (parse_id(argv[1], &id) != 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
567 fprintf(stderr, "invalid id %s\n", argv[1]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
568 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
569 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
570
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
571 type = parse_field(argv[2], '\0', NULL);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
572 if (type < 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
573 fprintf(stderr, "bad field name \"%s\"\n", argv[2]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
574 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
575 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
576 fields[type] = 1;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
577
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
578 fp = popen(argv[3], "w");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
579 if (fp == NULL) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
580 warn("popen");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
581 goto out;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
582 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
583
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
584 record = pwfile_get_record(ctx, id);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
585 if (record == NULL) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
586 fprintf(stderr, "record %u does not exist\n", id);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
587 goto out;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
588 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
589
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
590 print_record(record, fields, 0, fp);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
591
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
592 retval = CMD_OK;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
593
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
594 out:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
595 pwfile_destroy_record(record);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
596 if (fp != NULL) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
597 pclose(fp);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
598 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
599
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
600 return (retval);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
601 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
602
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
603 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
604 cmd_creategroup(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
605 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
606 if (argc != 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
607 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
608 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
609
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
610 if (pwfile_create_group(ctx, argv[1]) != 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
611 fprintf(stderr, "group \"%s\" already exists\n", argv[1]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
612 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
613 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
614
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
615 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
616 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
617
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
618 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
619 cmd_removegroup(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
620 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
621 if (argc != 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
622 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
623 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
624
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
625 if (pwfile_remove_group(ctx, argv[1]) != 0) {
4
b5c4267a7182 Fix check for successful deletion of a empty group
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
626 fprintf(stderr, "there is no empty group \"%s\"\n", argv[1]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
627 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
628 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
629
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
630 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
631 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
632
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
633 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
634 cmd_changepassword(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
635 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
636 size_t password_len;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
637 char password_buf[PWS3_MAX_PASSWORD_LEN + 1];
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
638 char confirm_buf[PWS3_MAX_PASSWORD_LEN + 1];
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
639
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
640 if (argc > 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
641 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
642 } else if (argc == 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
643 password_len = strlen(argv[1]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
644 if (password_len == 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
645 fprintf(stderr, "password must not be empty\n");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
646 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
647 } else if (password_len + 1 > sizeof (ctx->password)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
648 fprintf(stderr, "password too long\n");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
649 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
650 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
651 memcpy(ctx->password, argv[1], password_len + 1);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
652 } else {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
653 if (readpassphrase("Enter password: ", password_buf,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
654 sizeof (password_buf), RPP_ECHO_OFF | RPP_REQUIRE_TTY) ==
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
655 NULL) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
656 err(1, "readpassphrase");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
657 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
658 password_len = strlen(password_buf);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
659 if (password_len == 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
660 fprintf(stderr, "password must not be empty\n");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
661 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
662 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
663 if (readpassphrase("Confirm password: ", confirm_buf,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
664 sizeof (confirm_buf),
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
665 RPP_ECHO_OFF | RPP_REQUIRE_TTY) == NULL) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
666 err(1, "readpassphrase");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
667 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
668 if (strcmp(password_buf, confirm_buf) != 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
669 fprintf(stderr, "passwords do not match\n");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
670 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
671 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
672 memcpy(ctx->password, password_buf, password_len + 1);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
673 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
674
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
675 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
676 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
677
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
678 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
679 cmd_help(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
680 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
681 struct cmd *cmd;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
682
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
683 if (argc != 1) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
684 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
685 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
686
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
687 printf("Commands:\n");
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
688 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
689 printf("%-2s %-16s %s\n", cmd->abbrev_cmd, cmd->full_cmd,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
690 cmd->description);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
691 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
692
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
693 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
694 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
695
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
696 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
697 cmd_write(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
698 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
699 if (argc != 1) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
700 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
701 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
702
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
703 return ((pwfile_write_file(ctx) == 0) ? CMD_OK : CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
704 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
705
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
706 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
707 cmd_quit(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
708 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
709 if (argc != 1) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
710 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
711 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
712
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
713 return (CMD_QUIT);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
714 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
715
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
716 struct cmd *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
717 cmd_match(const char *cmd_name)
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
718 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
719 size_t i;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
720
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
721 for (i = 0; cmds[i].cmd_func != NULL; i++) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
722 if ((strcmp(cmds[i].abbrev_cmd, cmd_name) == 0) ||
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
723 (strcmp(cmds[i].full_cmd, cmd_name) == 0)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
724 return (&cmds[i]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
725 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
726 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
727
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
728 return (NULL);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
729 }