projects/pwm

view io.c @ 26:5bdea77d0c1d

Add pwm-clip utility for setting the X11 CLIPBOARD selection
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Thu Sep 21 09:45:59 2017 +0200 (2017-09-21)
parents 5c6155c8e9b6
children 131c35592054
line source
1 /*
2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
24 #include "compat.h"
26 #ifdef HAVE_ERR_H
27 #include <err.h>
28 #endif /* HAVE_ERR_H */
29 #include <errno.h>
30 #include <setjmp.h>
31 #include <signal.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "io.h"
39 #include "util.h"
40 #include "pwm.h"
42 static sigjmp_buf signal_env;
44 static void
45 signal_handler(int signal_no)
46 {
47 siglongjmp(signal_env, signal_no);
48 }
50 void
51 io_trim_nl(char *s)
52 {
53 size_t len;
55 len = strlen(s);
56 if ((len > 0) && (s[len - 1] == '\n')) {
57 s[len - 1] = '\0';
58 }
59 }
61 int
62 io_gl_complete_nothing(WordCompletion *cpl, void *data, const char *line,
63 int word_end)
64 {
65 return (0);
66 }
68 enum io_status
69 io_get_char(const char *prompt, int *cp)
70 {
71 enum io_status retval = IO_OK;
72 GetLine *gl = NULL;
74 gl = new_GetLine(16, 0);
75 if (gl== NULL) {
76 err(1, "new_GetLine");
77 }
78 gl_catch_blocked(gl);
80 /* prompt with echo off */
81 gl_echo_mode(gl, 0);
82 if ((*cp = gl_query_char(gl, prompt, '\0')) == EOF) {
83 switch (gl_return_status(gl)) {
84 case GLR_SIGNAL:
85 retval = IO_SIGNAL;
86 break;
87 case GLR_ERROR:
88 errx(1, "gl_get_line: %s",
89 gl_error_message(gl, NULL, 0));
90 default:
91 errx(1, "unknown error in gl_get_line");
92 }
93 }
95 /* erase prompt */
96 if (io_printf("\r%*s\r", (int)strlen(prompt), "") == IO_SIGNAL) {
97 retval = IO_SIGNAL;
98 }
100 del_GetLine(gl);
102 return (retval);
103 }
105 enum io_status
106 io_get_line(GetLine *gl, const char *prompt, int with_history,
107 const char *start_line, int start_pos, size_t buf_size, char *buf)
108 {
109 enum io_status retval = IO_OK;
110 GetLine *gl_private = NULL;
111 GlHistoryState state;
112 char *line;
114 if (gl == NULL) {
115 gl = gl_private = new_GetLine(buf_size - 1, 0);
116 if (gl_private == NULL) {
117 err(1, "new_GetLine");
118 }
119 gl_catch_blocked(gl_private);
120 }
122 gl_state_of_history(gl, &state);
123 gl_toggle_history(gl, with_history);
125 line = gl_get_line(gl, prompt, start_line, start_pos);
126 if (line == NULL) {
127 switch (gl_return_status(gl)) {
128 case GLR_BLOCKED:
129 break;
130 case GLR_SIGNAL:
131 retval = IO_SIGNAL;
132 goto out;
133 case GLR_EOF:
134 retval = IO_EOF;
135 goto out;
136 case GLR_ERROR:
137 errx(1, "gl_get_line: %s",
138 gl_error_message(gl, NULL, 0));
139 default:
140 errx(1, "unknown error in gl_get_line");
141 }
142 }
144 if (snprintf(buf, buf_size, "%s", line) >= (int)buf_size) {
145 retval = IO_TRUNCATED;
146 }
148 out:
149 if (gl != NULL) {
150 gl_toggle_history(gl, state.enabled);
151 }
152 del_GetLine(gl_private);
154 return (retval);
155 }
157 enum io_status
158 io_get_password(const char *prompt, const char *confirm_prompt,
159 size_t buf_size, char *buf)
160 {
161 enum io_status retval = IO_OK;
162 GetLine *gl = NULL;
163 size_t len;
164 char *password_buf = NULL;
165 char *confirm_buf = NULL;
167 gl = new_GetLine(buf_size - 1, 0);
168 if (gl == NULL) {
169 err(1, "new_GetLine");
170 }
171 /* disable default filename completion */
172 gl_customize_completion(gl, NULL, io_gl_complete_nothing);
173 gl_echo_mode(gl, 0);
175 password_buf = xmalloc(buf_size);
177 if (io_get_line(gl, prompt, 0, NULL, 0, buf_size, password_buf) ==
178 IO_SIGNAL) {
179 retval = IO_SIGNAL;
180 goto out;
181 }
182 len = strlen(password_buf);
183 /* strip trailing newline */
184 if ((len > 0) && (password_buf[len - 1] == '\n')) {
185 password_buf[--len] = '\0';
186 }
187 if (len == 0) {
188 retval = IO_PASSWORD_EMPTY;
189 goto out;
190 }
192 if (confirm_prompt != NULL) {
193 if (io_printf("\n") == IO_SIGNAL) {
194 retval = IO_SIGNAL;
195 goto out;
196 }
198 /* confirm new password */
199 confirm_buf = xmalloc(buf_size);
200 if (io_get_line(gl, confirm_prompt, 0, NULL, 0,
201 buf_size, confirm_buf) == IO_SIGNAL) {
202 retval = IO_SIGNAL;
203 goto out;
204 }
205 len = strlen(confirm_buf);
206 /* strip trailing newline */
207 if ((len > 0) && (confirm_buf[len - 1] == '\n')) {
208 confirm_buf[--len] = '\0';
209 }
210 if (strcmp(password_buf, confirm_buf) != 0) {
211 retval = IO_PASSWORD_MISMATCH;
212 goto out;
213 }
214 }
216 strcpy(buf, password_buf);
218 out:
219 if (io_printf("\n") == IO_SIGNAL) {
220 retval = IO_SIGNAL;
221 goto out;
222 }
223 free(password_buf);
224 free(confirm_buf);
225 del_GetLine(gl);
227 return (retval);
228 }
230 enum io_status
231 io_dputs(int fd, const char *s)
232 {
233 struct sigaction action;
234 struct sigaction oaction;
235 int signal_no = 0;
236 const char *p = s;
237 size_t remaining;
238 ssize_t n;
240 /* install signal handlers */
241 action.sa_handler = signal_handler;
242 action.sa_flags = 0;
243 sigemptyset(&action.sa_mask);
244 sigaddset(&action.sa_mask, SIGINT);
245 sigaddset(&action.sa_mask, SIGTERM);
246 sigaddset(&action.sa_mask, SIGHUP);
247 sigaddset(&action.sa_mask, SIGQUIT);
248 if ((sigaction(SIGINT, &action, &oaction) != 0) ||
249 (sigaction(SIGTERM, &action, &oaction) != 0) ||
250 (sigaction(SIGHUP, &action, &oaction) != 0) ||
251 (sigaction(SIGQUIT, &action, &oaction) != 0)) {
252 err(1, "sigaction");
253 }
255 if ((signal_no = sigsetjmp(signal_env, 1)) != 0) {
256 /* signal received, signal mask has been restored */
257 goto out;
258 }
260 remaining = strlen(s);
261 while (remaining > 0) {
262 pwm_unblock_signals();
263 n = write(fd, p, remaining);
264 if ((n < (int)remaining) && (errno != EINTR)) {
265 err(1, "write");
266 }
267 pwm_block_signals();
268 remaining -= MAX(n, 0);
269 p += MAX(n, 0);
270 }
272 out:
273 /* restore signal handlers */
274 if ((sigaction(SIGINT, &oaction, NULL) != 0) ||
275 (sigaction(SIGTERM, &oaction, NULL) != 0) ||
276 (sigaction(SIGHUP, &oaction, NULL) != 0) ||
277 (sigaction(SIGQUIT, &oaction, NULL) != 0)) {
278 err(1, "sigaction");
279 }
281 return ((signal_no == 0) ? IO_OK : IO_SIGNAL);
282 }
284 enum io_status
285 io_vdprintf(int fd, const char *fmt, va_list args)
286 {
287 enum io_status retval;
288 char *buf;
290 xvasprintf(&buf, fmt, args);
291 retval = io_dputs(fd, buf);
292 free(buf);
294 return (retval);
295 }
297 enum io_status
298 io_dprintf(int fd, const char *fmt, ...)
299 {
300 enum io_status retval;
301 va_list args;
303 va_start(args, fmt);
304 retval = io_vdprintf(fd, fmt, args);
305 va_end(args);
307 return (retval);
308 }
310 enum io_status
311 io_printf(const char *fmt, ...)
312 {
313 enum io_status retval;
314 va_list args;
316 va_start(args, fmt);
317 retval = io_vdprintf(STDOUT_FILENO, fmt, args);
318 va_end(args);
320 return (retval);
321 }