annotate cmd.c @ 27:722a45b4028b

Add define command for defining macros Macros are parsed when they are defined with the D command and can subsequently be used as arguments for other commands. Handle out of memory errors directly in tok.c.
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Mon, 25 Sep 2017 21:21:25 +0200
parents 1b89066d992c
children e3db02d7f1f4
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
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
26 #include <ctype.h>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
27 #ifdef HAVE_ERR_H
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
28 #include <err.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
29 #endif /* HAVE_ERR_H */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
30 #include <errno.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
31 #include <limits.h>
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
32 #include <regex.h>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
33 #include <stdlib.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
34 #include <string.h>
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
35 #include <time.h>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
36 #include <unistd.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
37
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
38 #include "cmd.h"
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
39 #include "io.h"
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
40 #include "macro.h"
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
41 #include "pager.h"
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
42 #include "proc.h"
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
43 #include "pw.h"
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
44 #include "pwfile.h"
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
45 #include "tok.h"
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
46 #include "util.h"
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
47
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
48 #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
49 #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
50
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
51 #define CHARS_DIGIT "0123456789"
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
52 #define CHARS_UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
53 #define CHARS_LOWER "abcdefghijklmnopqrstuvwxyz"
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
54 #define CHARS_PUNCT "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
55
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
56 enum field_type {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
57 FIELD_UNKNOWN = -1,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
58 FIELD_GROUP,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
59 FIELD_TITLE,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
60 FIELD_USERNAME,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
61 FIELD_PASSWORD,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
62 FIELD_NOTES,
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
63 FIELD_URL,
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
64 FIELD_MTIME,
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
65 FIELD_CTIME
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
66 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
67
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
68 enum cmd_generatepassword_arg_type {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
69 CMD_GP_ARG_UNKNOWN = -1,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
70 CMD_GP_ARG_LEN,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
71 CMD_GP_ARG_CHARS,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
72 CMD_GP_ARG_CHARCLASS
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
73 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
74
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
75 enum charclass_type {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
76 CHARCLASS_UNKNOWN = -1,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
77 CHARCLASS_DIGIT,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
78 CHARCLASS_UPPER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
79 CHARCLASS_LOWER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
80 CHARCLASS_PUNCT,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
81 CHARCLASS_ALPHA,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
82 CHARCLASS_ALNUM,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
83 CHARCLASS_XDIGIT,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
84 CHARCLASS_GRAPH
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
85 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
86
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
87 static enum cmd_return cmd_define(struct pwm_ctx *, int, char *[]);
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
88 static enum cmd_return cmd_status(struct pwm_ctx *, int, char *[]);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
89 static enum cmd_return cmd_info(struct pwm_ctx *, int, char *[]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
90 static enum cmd_return cmd_list(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
91 static enum cmd_return cmd_create(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
92 static enum cmd_return cmd_modify(struct pwm_ctx *, int, char *[]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
93 static enum cmd_return cmd_generatepassword(struct pwm_ctx *, int, char *[]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
94 static enum cmd_return cmd_remove(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
95 static enum cmd_return cmd_show(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
96 static enum cmd_return cmd_pipe(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
97 static enum cmd_return cmd_creategroup(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
98 static enum cmd_return cmd_removegroup(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
99 static enum cmd_return cmd_changepassword(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
100 static enum cmd_return cmd_help(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
101 static enum cmd_return cmd_write(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
102 static enum cmd_return cmd_quit(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
103
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
104 static const char *field_namev[] = {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
105 "group",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
106 "title",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
107 "username",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
108 "password",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
109 "notes",
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
110 "url",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
111 "ctime",
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
112 "mtime",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
113 NULL
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
114 };
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 static const char *field_labels[] = {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
117 "Group: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
118 "Title: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
119 "Username: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
120 "Password: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
121 "Notes: ",
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
122 "URL: ",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
123 "Created: ",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
124 "Modified: "
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
125 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
126
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
127 static const char *cmd_generatepassword_argv[] = {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
128 "len",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
129 "char",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
130 "charclass",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
131 NULL
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
132 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
133
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
134 static const char *charclass_namev[] = {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
135 "digit",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
136 "upper",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
137 "lower",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
138 "punct",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
139 "alpha",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
140 "alnum",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
141 "xdigit",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
142 "graph",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
143 NULL
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
144 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
145
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
146 static const char *charclass_values[] = {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
147 CHARS_DIGIT,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
148 CHARS_UPPER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
149 CHARS_LOWER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
150 CHARS_PUNCT,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
151 CHARS_UPPER CHARS_LOWER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
152 CHARS_DIGIT CHARS_UPPER CHARS_LOWER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
153 CHARS_DIGIT "abcdef",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
154 CHARS_DIGIT CHARS_UPPER CHARS_LOWER CHARS_PUNCT
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
155 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
156
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
157 static struct cmd cmds[] = {
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
158 { "D", "define", "define name=value", "Define a macro", cmd_define },
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
159 { "t", "status", "status", "Redisplay an error message of the previous "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
160 "command and unsaved changes", cmd_status },
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
161 { "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
162 cmd_info },
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
163 { "ls", "list", "list [field~regex ...]", "List entries", cmd_list },
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
164 { "c", "create", "create field=value ...", "Create entry", cmd_create },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
165 { "m", "modify", "modify id field=value ...", "Modify entry", cmd_modify },
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
166 { "gp", "generatepassword", "generatepassword [id] [len=n] [chars=n:chars] "
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
167 "[charclass=n:class] ...", "Randomly generate a password",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
168 cmd_generatepassword },
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
169 { "rm", "remove", "remove id", "Delete entry", cmd_remove },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
170 { "s", "show", "show id field", "Show entry", cmd_show },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
171 { "p", "pipe", "pipe id field command", "Pipe entry to external command",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
172 cmd_pipe },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
173 { "cg", "creategroup", "creategroup name", "Create empty group",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
174 cmd_creategroup },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
175 { "rg", "removegroup", "removegroup name", "Delete empty group",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
176 cmd_removegroup },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
177 { "ch", "changepassword", "changepassword", "Change password",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
178 cmd_changepassword },
14
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
179 { "h", "help", "help [command]", "Show help text", cmd_help },
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
180 { "w", "write", "write", "Write the database", cmd_write },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
181 { "q", "quit", "quit", "Quit", cmd_quit },
13
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
182 { "Q", "Quit", "Quit", "Quit without checking", cmd_quit },
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
183 { 0 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
184 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
185
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
186 static int
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
187 parse_arg(char *arg, const char *namev[], int sep, char **valuep)
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
188 {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
189 size_t i;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
190 size_t name_len;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
191
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
192 for (i = 0; namev[i] != NULL; i++) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
193 name_len = strlen(namev[i]);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
194 if ((strncmp(namev[i], arg, name_len) == 0) &&
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
195 (arg[name_len] == sep)) {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
196 if (valuep != NULL) {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
197 *valuep = arg + name_len + 1;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
198 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
199 return (i);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
200 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
201 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
202
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
203 return (-1);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
204 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
205
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
206 static int
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
207 parse_id(const char *arg, unsigned int *idp)
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
208 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
209 long x;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
210 char *p;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
211
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
212 errno = 0;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
213 x = strtol(arg, &p, 10);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
214 if ((errno != 0) || (*arg == '\0') || (*p != '\0') || (x > UINT_MAX) ||
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
215 (x <= 0)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
216 return (-1);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
217 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
218 *idp = (unsigned int)x;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
219
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
220 return (0);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
221 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
222
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
223 static enum cmd_return
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
224 cmd_define(struct pwm_ctx *ctx, int argc, char *argv[])
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
225 {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
226 int retval = CMD_ERR;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
227 const char *value;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
228 char *name = NULL;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
229 size_t tokenc = 0;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
230 union tok **tokenv = NULL;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
231 struct macro_entry *macro_entry;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
232
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
233 if (argc != 2) {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
234 return (CMD_USAGE);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
235 }
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
236
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
237 /* split into name and value */
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
238 value = strchr(argv[1], '=');
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
239 if (value == NULL) {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
240 pwm_err(ctx, "bad macro definition \"%s\"", argv[1]);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
241 goto out;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
242 }
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
243 xasprintf(&name, "%.*s", value - argv[1], argv[1]);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
244 value++;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
245
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
246 /* tokenize macro value */
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
247 switch (tok_tokenize(value, &tokenc, &tokenv)) {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
248 case TOK_ERR_UNTERMINATED_QUOTE:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
249 pwm_err(ctx, "unterminated quote in macro");
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
250 goto out;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
251 case TOK_ERR_TRAILING_BACKSLASH:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
252 pwm_err(ctx, "trailing backslash in macro");
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
253 goto out;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
254 case TOK_ERR_INVALID_MACRO_NAME:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
255 pwm_err(ctx, "invalid macro name referenced in macro");
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
256 goto out;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
257 }
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
258
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
259 /* parse macro definition */
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
260 switch (macro_parse(name, tokenc, tokenv, ctx->macro_head,
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
261 &macro_entry)) {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
262 case MACRO_ERR_INVALID_NAME:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
263 pwm_err(ctx, "invalid macro name \"%s\"", name);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
264 goto out;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
265 case MACRO_ERR_UNDEFINED_MACRO:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
266 pwm_err(ctx, "macro definition references undefined macro");
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
267 goto out;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
268 case MACRO_ERR_RECURSIVE:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
269 pwm_err(ctx, "macro definition must not be recursive");
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
270 goto out;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
271 }
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
272
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
273 macro_add(ctx->macro_head, macro_entry);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
274 retval = CMD_OK;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
275
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
276 out:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
277 tok_free(tokenv);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
278 free(name);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
279
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
280 return (retval);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
281 }
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
282
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 23
diff changeset
283 static enum cmd_return
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
284 cmd_status(struct pwm_ctx *ctx, int argc, char *argv[])
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
285 {
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
286 if (argc != 1) {
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
287 return (CMD_USAGE);
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
288 }
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
289
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
290 if (ctx->errmsg != NULL) {
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
291 if (io_printf("%s\n", ctx->errmsg) == IO_SIGNAL) {
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
292 return (CMD_SIGNAL);
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
293 }
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
294 }
23
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
295 if (ctx->is_readonly) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
296 if (io_printf("Read-only mode\n") == IO_SIGNAL) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
297 return (CMD_SIGNAL);
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
298 }
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
299 } else {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
300 if (io_printf("There are%sunsaved changes\n",
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
301 ctx->unsaved_changes ? " " : " no ") == IO_SIGNAL) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
302 return (CMD_SIGNAL);
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
303 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
304 }
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
305
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
306 return (CMD_STATUS);
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
307 }
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
308
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
309 static enum cmd_return
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
310 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
311 {
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
312 enum cmd_return retval;
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
313 struct metadata *metadata;
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
314 struct pager *pager;
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
315 struct tm *tm;
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
316 char timebuf[TIME_SIZE];
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
317
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
318 if (argc != 1) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
319 return (CMD_USAGE);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
320 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
321
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
322 metadata = pwfile_get_metadata(ctx);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
323
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
324 pager = pager_create(STDOUT_FILENO);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
325 pager_printf(pager, "Format: 0x%04x\n", metadata->version);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
326 if (metadata->user != NULL) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
327 pager_printf(pager, "User: %s\n", metadata->user);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
328 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
329 if (metadata->user != NULL) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
330 pager_printf(pager, "Host: %s\n", metadata->host);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
331 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
332 if (metadata->user != NULL) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
333 pager_printf(pager, "Application: %s\n", metadata->application);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
334 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
335 tm = gmtime(&metadata->timestamp);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
336 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
337 pager_printf(pager, "Last Saved: %s\n", timebuf);
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
338 retval = (pager_show(pager) != IO_SIGNAL) ? CMD_OK : CMD_SIGNAL;
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
339 pager_destroy(pager);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
340
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
341 pwfile_destroy_metadata(metadata);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
342
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
343 return (retval);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
344 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
345
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
346 static enum cmd_return
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
347 cmd_list(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
348 {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
349 int retval = CMD_ERR;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
350 int i;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
351 regex_t *group_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
352 regex_t *title_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
353 regex_t *username_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
354 regex_t *notes_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
355 regex_t *url_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
356 enum field_type type;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
357 char *value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
358 regex_t **repp;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
359 int errcode;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
360 char *errbuf;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
361 size_t errbuf_size;
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
362 struct pager *pager = NULL;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
363 union list_item **list = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
364 size_t j;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
365 struct record *record;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
366
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
367 for (i = 1; i < argc; i++) {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
368 type = parse_arg(argv[i], field_namev, '~', &value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
369 if (type == FIELD_UNKNOWN) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
370 pwm_err(ctx, "bad field name \"%s\"", argv[i]);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
371 goto out;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
372 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
373 if (value[0] == '\0') {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
374 /* skip empty expressions */
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
375 continue;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
376 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
377 switch (type) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
378 case FIELD_GROUP:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
379 repp = &group_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
380 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
381 case FIELD_TITLE:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
382 repp = &title_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
383 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
384 case FIELD_USERNAME:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
385 repp = &username_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
386 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
387 case FIELD_NOTES:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
388 repp = &notes_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
389 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
390 case FIELD_URL:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
391 repp = &url_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
392 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
393 default:
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
394 pwm_err(ctx, "bad field name \"%s\"", argv[i]);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
395 goto out;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
396 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
397
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
398 if (*repp == NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
399 *repp = xmalloc(sizeof (regex_t));
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
400 } else {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
401 regfree(*repp);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
402 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
403 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
404 if (errcode != 0) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
405 errbuf_size = regerror(errcode, *repp, "", 0);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
406 errbuf = xmalloc(errbuf_size);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
407 regerror(errcode, *repp, errbuf, errbuf_size);
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
408 pwm_err(ctx, "bad regular expression \"%s\"", errbuf);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
409 free(errbuf);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
410
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
411 free(*repp);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
412 *repp = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
413
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
414 goto out;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
415 }
0
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
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
418 pager = pager_create(STDOUT_FILENO);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
419 list = pwfile_create_list(ctx);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
420 for (j = 0; list[j] != NULL; j++) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
421 if (list[j]->any.type == ITEM_TYPE_GROUP) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
422 pager_printf(pager, "[%s]\n", list[j]->group.group);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
423 } else {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
424 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
425 if (((group_re == NULL) || (regexec(group_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
426 record->group, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
427 ((title_re == NULL) || (regexec(title_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
428 record->title, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
429 ((username_re == NULL) || (regexec(username_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
430 record->username, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
431 ((notes_re == NULL) || (regexec(notes_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
432 record->notes, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
433 ((url_re == NULL) || (regexec(url_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
434 record->url, 0, NULL, 0) == 0))) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
435 pager_printf(pager, "%4u %s\n",
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
436 list[j]->record.id,
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
437 (list[j]->record.title != NULL) ?
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
438 list[j]->record.title : "");
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
439 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
440 pwfile_destroy_record(record);
0
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 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
443 retval = (pager_show(pager) != IO_SIGNAL) ? CMD_OK : CMD_SIGNAL;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
444
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
445 out:
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
446 pager_destroy(pager);
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
447
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
448 if (group_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
449 regfree(group_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
450 free(group_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
451 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
452 if (title_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
453 regfree(title_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
454 free(title_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
455 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
456 if (username_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
457 regfree(username_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
458 free(username_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
459 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
460 if (notes_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
461 regfree(notes_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
462 free(notes_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
463 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
464 if (url_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
465 regfree(url_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
466 free(url_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
467 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
468
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
469 pwfile_destroy_list(list);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
470
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
471
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
472 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
473 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
474
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
475 static int
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
476 read_record_fields(struct pwm_ctx *ctx, struct record *record)
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
477 {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
478 char group_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
479 char title_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
480 char username_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
481 char password_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
482 char notes_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
483 char url_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
484
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
485 if (io_get_line(NULL, "Group: ", 0, record->group, -1,
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
486 sizeof (group_buf), group_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
487 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
488 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
489 io_trim_nl(group_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
490
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
491 if (io_get_line(NULL, "Title: ", 0, record->title, -1,
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
492 sizeof (title_buf), title_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
493 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
494 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
495 io_trim_nl(title_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
496
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
497 if (io_get_line(NULL, "Username: ", 0, record->username, -1,
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
498 sizeof (username_buf), username_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
499 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
500 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
501 io_trim_nl(username_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
502
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
503 for (;;) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
504 switch (io_get_password("Password: ", "Confirm Password: ",
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
505 sizeof (password_buf), password_buf)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
506 case IO_OK: /* FALLTHROUGH */
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
507 case IO_PASSWORD_EMPTY:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
508 goto password_done;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
509 case IO_SIGNAL:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
510 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
511 case IO_PASSWORD_MISMATCH:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
512 pwm_err(ctx, "passwords do not match");
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
513 continue;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
514 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
515 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
516
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
517 password_done:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
518 if (io_get_line(NULL, "Notes: ", 0, record->notes, -1,
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
519 sizeof (notes_buf), notes_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
520 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
521 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
522 io_trim_nl(notes_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
523
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
524 if (io_get_line(NULL, "URL: ", 0, record->url, -1, sizeof (url_buf),
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
525 url_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
526 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
527 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
528 io_trim_nl(url_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
529
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
530 free(record->group);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
531 record->group = (group_buf[0] != '\0') ? xstrdup(group_buf) : NULL;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
532 free(record->title);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
533 record->title = (title_buf[0] != '\0') ? xstrdup(title_buf) : NULL;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
534 free(record->username);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
535 record->username = (username_buf[0] != '\0') ? xstrdup(username_buf) :
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
536 NULL;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
537 /*
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
538 * the current password cannot be edited, keep the current password if
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
539 * the user pressed return or ^D instead of deleting it like other
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
540 * fields
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
541 */
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
542 if (password_buf[0] != '\0') {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
543 free(record->password);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
544 record->password = xstrdup(password_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
545 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
546 free(record->notes);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
547 record->notes = (notes_buf[0] != '\0') ? xstrdup(notes_buf) : NULL;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
548 free(record->url);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
549 record->url = (url_buf[0] != '\0') ? xstrdup(url_buf) : NULL;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
550
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
551 return (CMD_OK);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
552 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
553
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
554 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
555 cmd_create(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
556 {
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
557 enum cmd_return retval = CMD_ERR;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
558 int i;
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
559 struct record *record = NULL;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
560 enum field_type type;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
561 char *value;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
562
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
563 if (!ctx->is_interactive && (argc < 2)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
564 retval = CMD_USAGE;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
565 goto out;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
566 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
567
23
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
568 if (ctx->is_readonly) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
569 pwm_err(ctx, "cannot create new entries in read-only mode");
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
570 goto out;
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
571 }
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
572
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
573 record = pwfile_create_record();
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
574
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
575 for (i = 1; i < argc; i++) {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
576 type = parse_arg(argv[i], field_namev, '=', &value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
577 if (type == FIELD_UNKNOWN) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
578 pwm_err(ctx, "bad field assignment \"%s\"", argv[i]);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
579 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
580 if (value[0] == '\0') {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
581 /* skip empty assignments */
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
582 continue;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
583 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
584 switch (type) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
585 case FIELD_GROUP:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
586 free(record->group);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
587 record->group = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
588 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
589 case FIELD_TITLE:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
590 free(record->title);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
591 record->title = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
592 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
593 case FIELD_USERNAME:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
594 free(record->username);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
595 record->username = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
596 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
597 case FIELD_PASSWORD:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
598 free(record->password);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
599 record->password = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
600 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
601 case FIELD_NOTES:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
602 free(record->notes);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
603 record->notes = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
604 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
605 case FIELD_URL:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
606 free(record->url);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
607 record->url = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
608 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
609 default:
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
610 pwm_err(ctx, "bad field name \"%s\"", argv[i]);
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
611 goto out;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
612 }
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
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
615 if (ctx->is_interactive && (argc < 2)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
616 if (read_record_fields(ctx, record) != 0) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
617 goto out;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
618 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
619 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
620
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
621 pwfile_create_pws_record(ctx, record);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
622 retval = CMD_OK;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
623
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
624 out:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
625 pwfile_destroy_record(record);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
626
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
627 return (retval);
0
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 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
631 cmd_modify(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
632 {
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
633 int retval = CMD_ERR;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
634 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
635 int i;
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
636 struct record *record = NULL;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
637 enum field_type type;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
638 char *value;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
639
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
640 if (!ctx->is_interactive && (argc < 2)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
641 retval = CMD_USAGE;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
642 goto out;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
643 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
644
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
645 if (parse_id(argv[1], &id) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
646 pwm_err(ctx, "invalid id %s", argv[1]);
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
647 goto out;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
648 }
23
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
649
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
650 if (ctx->is_readonly) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
651 pwm_err(ctx, "cannot modify entries in read-only mode");
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
652 goto out;
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
653 }
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
654
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
655 record = pwfile_get_record(ctx, id);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
656
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
657 for (i = 2; i < argc; i++) {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
658 type = parse_arg(argv[i], field_namev, '=', &value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
659 if (type == FIELD_UNKNOWN) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
660 pwm_err(ctx, "bad field assignment \"%s\"", argv[i]);
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
661 goto out;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
662 }
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
663 if (value[0] == '\0') {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
664 /* skip empty assignments */
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
665 continue;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
666 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
667 switch (type) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
668 case FIELD_GROUP:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
669 free(record->group);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
670 record->group = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
671 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
672 case FIELD_TITLE:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
673 free(record->title);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
674 record->title = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
675 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
676 case FIELD_USERNAME:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
677 free(record->username);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
678 record->username = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
679 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
680 case FIELD_PASSWORD:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
681 free(record->password);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
682 record->password = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
683 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
684 case FIELD_NOTES:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
685 free(record->notes);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
686 record->notes = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
687 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
688 case FIELD_URL:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
689 free(record->url);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
690 record->url = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
691 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
692 default:
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
693 pwm_err(ctx, "bad field name \"%s\"", argv[i]);
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
694 goto out;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
695 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
696 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
697
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
698 if (ctx->is_interactive && (argc < 3)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
699 if (read_record_fields(ctx, record) != 0) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
700 goto out;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
701 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
702 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
703
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
704 pwfile_modify_pws_record(ctx, id, record);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
705 retval = CMD_OK;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
706
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
707 out:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
708 pwfile_destroy_record(record);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
709
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
710 return (retval);
0
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 static enum cmd_return
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
714 cmd_generatepassword(struct pwm_ctx *ctx, int argc, char *argv[])
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
715 {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
716 enum cmd_return retval = CMD_ERR;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
717 unsigned int id = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
718 int i = 1;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
719 char *value = NULL;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
720 long x;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
721 char *p;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
722 size_t password_len = 16;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
723 size_t chars_min;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
724 const char *chars;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
725 struct pw_char_group *char_groupv = NULL;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
726 size_t char_groupv_len = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
727 int charclass;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
728 size_t j;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
729 char password[PWS3_MAX_PASSWORD_LEN + 1] = { 0 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
730
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
731 /* check if first argument is an id */
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
732 if ((argc > 1) && (parse_id(argv[1], &id) == 0)) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
733 i++;
23
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
734 if (ctx->is_readonly) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
735 pwm_err(ctx, "cannot modify entries in read-only mode");
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
736 goto out;
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
737 }
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
738 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
739
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
740 for (; i < argc; i++) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
741 switch (parse_arg(argv[i], cmd_generatepassword_argv, '=',
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
742 &value)) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
743 case CMD_GP_ARG_LEN:
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
744 errno = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
745 x = strtol(value, &p, 10);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
746 if ((errno != 0) || (*value == '\0') || (*p != '\0') ||
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
747 (x > PWS3_MAX_PASSWORD_LEN) || (x <= 0)) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
748 pwm_err(ctx, "invalid password length \"%s\"",
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
749 argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
750 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
751 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
752 password_len = x;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
753 break;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
754 case CMD_GP_ARG_CHARS:
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
755 errno = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
756 x = strtol(value, &p, 10);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
757 if ((errno != 0) || (*value == '\0') || (*p != ':') ||
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
758 (x < 0) || (x > PWS3_MAX_PASSWORD_LEN)) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
759 pwm_err(ctx, "invalid minimum number of "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
760 "characters \"%s\"", argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
761 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
762 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
763 chars_min = x;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
764
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
765 chars = ++p;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
766 while (*p != '\0') {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
767 if (!isascii(*p) || !isprint(*p)) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
768 pwm_err(ctx, "invalid character in "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
769 "character group \"%s\"", argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
770 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
771 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
772 p++;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
773 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
774
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
775 char_groupv = xrealloc(char_groupv,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
776 sizeof (struct pw_char_group) * (char_groupv_len +
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
777 1));
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
778 char_groupv[char_groupv_len].chars = chars;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
779 char_groupv[char_groupv_len].chars_min = chars_min;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
780 char_groupv_len++;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
781 break;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
782 case CMD_GP_ARG_CHARCLASS:
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
783 errno = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
784 x = strtol(value, &p, 10);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
785 if ((errno != 0) || (*value == '\0') || (*p != ':') ||
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
786 (x < 0) || (x > PWS3_MAX_PASSWORD_LEN)) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
787 pwm_err(ctx, "invalid minimum number of "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
788 "characters \"%s\"", argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
789 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
790 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
791 chars_min = x;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
792
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
793 charclass = parse_arg(++p, charclass_namev, '\0', NULL);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
794 if (charclass < 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
795 pwm_err(ctx, "unknown character class \"%s\"",
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
796 argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
797 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
798 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
799 chars = charclass_values[charclass];
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
800 char_groupv = xrealloc(char_groupv,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
801 sizeof (struct pw_char_group) * (char_groupv_len +
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
802 1));
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
803 char_groupv[char_groupv_len].chars = chars;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
804 char_groupv[char_groupv_len].chars_min = chars_min;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
805 char_groupv_len++;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
806 break;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
807 default:
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
808 pwm_err(ctx, "invalid argument \"%s\"", argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
809 retval = CMD_USAGE;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
810 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
811 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
812 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
813
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
814 for (j = 0; j < char_groupv_len; j++) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
815 if (char_groupv[j].chars_min > password_len) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
816 pwm_err(ctx, "invalid minimum number of characters "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
817 "\"%zu:%s\"", char_groupv[j].chars_min,
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
818 char_groupv[j].chars);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
819 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
820 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
821 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
822
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
823 if (char_groupv_len == 0) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
824 /* use defaults */
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
825 char_groupv = xmalloc(sizeof (struct pw_char_group));
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
826 char_groupv[0].chars = charclass_values[CHARCLASS_GRAPH];
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
827 char_groupv[0].chars_min = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
828 char_groupv_len++;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
829 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
830
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
831 if (pw_genrandom(char_groupv, char_groupv_len, password,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
832 password_len) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
833 pwm_err(ctx, "failed to generate password that meets the given "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
834 "constraints");
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
835 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
836 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
837
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
838 if (id != 0) {
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
839 if (pwfile_modify_pws_record(ctx, id,
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
840 &(struct record){ .password = password }) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
841 pwm_err(ctx, "record %u does not exist", id);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
842 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
843 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
844 retval = CMD_OK;
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
845 } else {
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
846 retval = io_printf("%s\n", password);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
847 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
848
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
849 out:
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
850 free(char_groupv);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
851
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
852 return (retval);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
853 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
854
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
855 static enum cmd_return
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
856 cmd_remove(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
857 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
858 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
859
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
860 if (argc != 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
861 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
862 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
863
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
864 if (parse_id(argv[1], &id) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
865 pwm_err(ctx, "invalid id %s", argv[1]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
866 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
867 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
868
23
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
869 if (ctx->is_readonly) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
870 pwm_err(ctx, "cannot remove entries in read-only mode");
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
871 return (CMD_ERR);
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
872 }
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
873
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
874 if (pwfile_remove_pws_record(ctx, id) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
875 pwm_err(ctx, "failed to remove record %u", id);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
876 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
877 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
878
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
879 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
880 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
881
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
882 static int
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
883 print_record(struct record *record, int fields[], int show_labels, int fd)
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
884 {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
885 struct pager *pager;
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
886 struct tm *tm;
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
887 char timebuf[TIME_SIZE];
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
888 int retval;
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
889
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
890 pager = pager_create(fd);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
891 if (fields[FIELD_TITLE]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
892 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
893 field_labels[FIELD_TITLE] : "", (record->title != NULL) ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
894 record->title : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
895 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
896 if (fields[FIELD_GROUP]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
897 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
898 field_labels[FIELD_GROUP] : "", (record->group != NULL) ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
899 record->group : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
900 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
901 if (fields[FIELD_USERNAME]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
902 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
903 field_labels[FIELD_USERNAME] : "",
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
904 (record->username != NULL) ? record->username : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
905 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
906 if (fields[FIELD_PASSWORD]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
907 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
908 field_labels[FIELD_PASSWORD] : "",
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
909 (record->password != NULL) ? record->password : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
910 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
911 if (fields[FIELD_NOTES]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
912 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
913 field_labels[FIELD_NOTES] : "", (record->notes != NULL) ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
914 record->notes : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
915 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
916 if (fields[FIELD_URL]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
917 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
918 field_labels[FIELD_URL] : "", (record->url != NULL) ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
919 record->url : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
920 }
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
921 if (fields[FIELD_CTIME]) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
922 tm = gmtime(&record->ctime);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
923 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
924 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
925 field_labels[FIELD_CTIME] : "", timebuf);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
926 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
927 if (fields[FIELD_MTIME]) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
928 tm = gmtime(&record->mtime);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
929 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
930 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
931 field_labels[FIELD_MTIME] : "", timebuf);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
932 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
933 retval = pager_show(pager);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
934 pager_destroy(pager);
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
935
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
936 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
937 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
938
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
939 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
940 cmd_show(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
941 {
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
942 enum cmd_return retval;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
943 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
944 struct record *record;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
945 int i;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
946 enum field_type type;
15
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
947 int fields[COUNTOF(field_namev) - 1] = {
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
948 [FIELD_GROUP] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
949 [FIELD_TITLE] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
950 [FIELD_USERNAME] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
951 [FIELD_PASSWORD] = 0,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
952 [FIELD_NOTES] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
953 [FIELD_URL] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
954 [FIELD_MTIME] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
955 [FIELD_CTIME] = 1
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
956 };
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
957
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
958 if (argc < 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
959 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
960 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
961
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
962 if (parse_id(argv[1], &id) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
963 pwm_err(ctx, "invalid id %s", argv[1]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
964 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
965 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
966
15
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
967 if (argc > 2) {
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
968 /* show only explicitly given field names */
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
969 memset(fields, 0, sizeof (fields));
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
970 }
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
971
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
972 for (i = 2; i < argc; i++) {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
973 type = parse_arg(argv[i], field_namev, '\0', NULL);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
974 if (type < 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
975 pwm_err(ctx, "bad field name \"%s\"", argv[i]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
976 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
977 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
978 fields[type] = 1;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
979 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
980
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
981 record = pwfile_get_record(ctx, id);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
982 if (record == NULL) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
983 pwm_err(ctx, "record %u does not exist", id);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
984 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
985 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
986 retval = (print_record(record, fields, 1, STDOUT_FILENO) != IO_SIGNAL) ?
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
987 CMD_OK : CMD_SIGNAL;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
988 pwfile_destroy_record(record);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
989
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
990 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
991 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
992
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
993 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
994 cmd_pipe(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
995 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
996 enum cmd_return retval = CMD_ERR;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
997 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
998 struct record *record = NULL;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
999 enum field_type type;
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
1000 int fields[COUNTOF(field_namev) - 1] = { 0 };
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1001 struct proc proc = { 0 };
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1002
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1003 if (argc != 4) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1004 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1005 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1006
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1007 if (parse_id(argv[1], &id) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
1008 pwm_err(ctx, "invalid id %s", argv[1]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1009 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1010 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1011
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
1012 type = parse_arg(argv[2], field_namev, '\0', NULL);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1013 if (type < 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
1014 pwm_err(ctx, "bad field name \"%s\"", argv[2]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1015 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1016 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1017 fields[type] = 1;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1018
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1019 if (proc_open(&proc, argv[3], "w") != IO_OK) {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1020 goto out;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1021 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1022
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1023 record = pwfile_get_record(ctx, id);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1024 if (record == NULL) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
1025 pwm_err(ctx, "record %u does not exist", id);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1026 goto out;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1027 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1028
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1029 retval = (print_record(record, fields, 0, proc.fd) != IO_SIGNAL) ?
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1030 CMD_OK : CMD_SIGNAL;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1031
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1032 out:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1033 pwfile_destroy_record(record);
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1034 if (proc.pid != 0) {
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1035 if (proc_close(&proc) == IO_SIGNAL) {
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1036 retval = CMD_SIGNAL;
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1037 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1038 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1039
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1040 return (retval);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1041 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1042
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1043 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1044 cmd_creategroup(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1045 {
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1046 char group_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1047
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1048 if (!ctx->is_interactive && (argc != 2)) {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1049 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1050 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1051
23
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1052 if (ctx->is_readonly) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1053 pwm_err(ctx, "cannot create groups in read-only mode");
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1054 return (CMD_ERR);
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1055 }
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1056
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1057 if (ctx->is_interactive && (argc != 2)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1058 if (io_get_line(NULL, "Group: ", 0, NULL, 0,
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1059 sizeof (group_buf), group_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1060 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1061 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1062 io_trim_nl(group_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1063 } else {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1064 strcpy(group_buf, argv[1]);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1065 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1066
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1067 if (group_buf[0] != '\0') {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1068 if (pwfile_create_group(ctx, group_buf) != 0) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1069 pwm_err(ctx, "group \"%s\" already exists", group_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1070 return (CMD_ERR);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
1071 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1072 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1073
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1074 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1075 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1076
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1077 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1078 cmd_removegroup(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1079 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1080 if (argc != 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1081 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1082 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1083
23
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1084 if (ctx->is_readonly) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1085 pwm_err(ctx, "cannot remove groups in read-only mode");
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1086 return (CMD_ERR);
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1087 }
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1088
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1089 if (pwfile_remove_group(ctx, argv[1]) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
1090 pwm_err(ctx, "empty group \"%s\" does not exist", argv[1]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1091 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1092 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1093
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1094 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1095 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1096
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1097 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1098 cmd_changepassword(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1099 {
18
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1100 size_t len;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1101
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1102 if (argc > 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1103 return (CMD_USAGE);
23
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1104 }
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1105
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1106 if (ctx->is_readonly) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1107 pwm_err(ctx, "cannot modify entries in read-only mode");
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1108 return (CMD_ERR);
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1109 }
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1110
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1111 if (argc == 2) {
18
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1112 len = strlen(argv[1]);
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1113 if (len == 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
1114 pwm_err(ctx, "password must not be empty");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1115 return (CMD_ERR);
18
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1116 } else if (len + 1 > sizeof (ctx->password)) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
1117 pwm_err(ctx, "password too long");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1118 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1119 }
18
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1120 memcpy(ctx->password, argv[1], len + 1);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1121 } else {
18
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1122 if (pwm_read_password(ctx, 1) != 0) {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1123 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1124 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1125 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1126
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1127 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1128 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1129
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1130 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1131 cmd_help(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1132 {
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1133 enum cmd_return retval = CMD_OK;
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
1134 struct pager *pager;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1135 struct cmd *cmd;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1136
14
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1137 if (argc > 2) {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1138 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1139 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1140
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1141 pager = pager_create(STDOUT_FILENO);
14
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1142 if (argc == 2) {
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1143 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1144 if ((strcmp(argv[1], cmd->abbrev_cmd) == 0) ||
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1145 (strcmp(argv[1], cmd->full_cmd) == 0)) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
1146 pager_printf(pager, "%s\n", cmd->usage);
14
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1147 break;
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1148 }
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1149 }
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1150 } else {
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1151 printf("Commands:\n");
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1152 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
1153 pager_printf(pager, "%-2s %-16s %s\n", cmd->abbrev_cmd,
14
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1154 cmd->full_cmd, cmd->description);
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1155 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1156 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1157 retval = (pager_show(pager) != IO_SIGNAL) ? CMD_OK : CMD_SIGNAL;
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
1158 pager_destroy(pager);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1159
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1160 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1161 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1162
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1163 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1164 cmd_write(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1165 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1166 if (argc != 1) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1167 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1168 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1169
23
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1170 if (ctx->is_readonly) {
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1171 pwm_err(ctx, "cannot write changes in read-only mode");
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1172 return (CMD_ERR);
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1173 }
1b89066d992c Add read-only mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 22
diff changeset
1174
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1175 return ((pwfile_write_file(ctx) == 0) ? CMD_OK : CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1176 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1177
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1178 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1179 cmd_quit(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1180 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1181 if (argc != 1) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1182 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1183 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1184
13
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1185 if ((argv[0][0] == 'q') && ctx->unsaved_changes &&
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1186 (ctx->prev_cmd != NULL) && (strcmp(ctx->prev_cmd, "quit") != 0)) {
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1187 printf("Warning: There are unsaved changes\n");
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1188 return (CMD_OK);
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1189 }
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1190
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1191 return (CMD_QUIT);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1192 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1193
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1194 struct cmd *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1195 cmd_match(const char *cmd_name)
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1196 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1197 size_t i;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1198
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1199 for (i = 0; cmds[i].cmd_func != NULL; i++) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1200 if ((strcmp(cmds[i].abbrev_cmd, cmd_name) == 0) ||
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1201 (strcmp(cmds[i].full_cmd, cmd_name) == 0)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1202 return (&cmds[i]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1203 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1204 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1205
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1206 return (NULL);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1207 }