1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2007 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 static void usage(const char *pn
)
34 "Usage: %s [-hv] [-E] [-s <socket>] [[-a] | [-p <password>]] [-S] <filename>\n"
35 " -E exit after a command failure\n"
39 " -S send the SAVE command after all others\n"
41 " -h this help text\n",
46 static void pwmd_show_error(int ret
, int error
)
48 if (ret
== PWMD_PERROR
)
49 fprintf(stderr
, "ERR %03i %s\n", error
, pwmd_strerror(error
));
50 else if (ret
== PWMD_ERROR
)
51 fprintf(stderr
, "ERR 000 %s\n", strerror(error
));
53 fprintf(stderr
, "ERR 000 Is gpg-agent running? Is GPG_AGENT_INFO set?\n");
56 int main(int argc
, char *argv
[])
60 char *password
= NULL
;
61 char *filename
= NULL
;
62 char *socketpath
= NULL
;
63 char command
[8196], *p
;
72 while ((opt
= getopt(argc
, argv
, "Ehvap:s:S")) != EOF
) {
81 socketpath
= strdup(optarg
);
84 password
= strdup(optarg
);
90 printf("%s (pwmc)\n%s\n", PACKAGE_STRING
, PACKAGE_BUGREPORT
);
98 if (use_agent
&& password
)
101 if (argc
- optind
!= 1)
104 filename
= argv
[optind
];
106 if ((pwm
= pwmd_connect(socketpath
, &error
)) == NULL
) {
110 memset(password
, 0, strlen(password
));
114 err(EXIT_FAILURE
, "pwmd_connect()");
118 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
, PWMD_OPTION_USEAGENT
, 1))
120 pwmd_show_error(ret
, error
);
126 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SETOPT
,
127 PWMD_OPTION_PASSWORD
, password
)) != PWMD_OK
) {
128 memset(password
, 0, strlen(password
));
130 pwmd_show_error(ret
, error
);
135 memset(password
, 0, strlen(password
));
139 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_OPEN
, filename
)) != PWMD_OK
) {
140 pwmd_show_error(ret
, error
);
145 while ((p
= fgets(command
, sizeof(command
), stdin
)) != NULL
) {
149 if (p
[len
- 1] != '\n') {
150 if ((t
= realloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
152 memset(buf
, 0, total
);
156 err(EXIT_FAILURE
, "realloc()");
160 memcpy(&buf
[total
], p
, len
);
167 if ((t
= realloc(buf
, (total
+ len
+ 1) * sizeof(char))) == NULL
) {
169 memset(buf
, 0, total
);
173 err(EXIT_FAILURE
, "realloc()");
177 memcpy(&buf
[total
], p
, len
);
183 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_COMMAND
,
184 (buf
) ? buf
: command
)) != PWMD_OK
) {
186 memset(buf
, 0, total
);
190 memset(&command
, 0, sizeof(command
));
191 pwmd_show_error(ret
, error
);
200 memset(buf
, 0, total
);
207 printf("%s\n", (char *)result
);
213 if ((ret
= pwmd_command(pwm
, &result
, &error
, PWMD_SAVE
)) != PWMD_OK
) {
214 memset(&command
, 0, sizeof(command
));
215 pwmd_show_error(ret
, error
);