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