summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--usr.bin/cu/Makefile4
-rw-r--r--usr.bin/cu/command.c14
-rw-r--r--usr.bin/cu/cu.c23
-rw-r--r--usr.bin/cu/cu.h12
-rw-r--r--usr.bin/cu/error.c85
-rw-r--r--usr.bin/cu/input.c7
6 files changed, 117 insertions, 28 deletions
diff --git a/usr.bin/cu/Makefile b/usr.bin/cu/Makefile
index fe37a6d0dee..df93cbeb190 100644
--- a/usr.bin/cu/Makefile
+++ b/usr.bin/cu/Makefile
@@ -1,7 +1,7 @@
-# $OpenBSD: Makefile,v 1.1 2012/07/10 08:02:27 nicm Exp $
+# $OpenBSD: Makefile,v 1.2 2012/07/10 10:28:05 nicm Exp $
PROG= cu
-SRCS= cu.c command.c input.c
+SRCS= cu.c command.c input.c error.c
CDIAGFLAGS+= -Wall -W -Wno-unused-parameter
diff --git a/usr.bin/cu/command.c b/usr.bin/cu/command.c
index df115b24461..e48653bab04 100644
--- a/usr.bin/cu/command.c
+++ b/usr.bin/cu/command.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: command.c,v 1.5 2012/07/10 09:32:40 nicm Exp $ */
+/* $OpenBSD: command.c,v 1.6 2012/07/10 10:28:05 nicm Exp $ */
/*
* Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
@@ -21,7 +21,6 @@
#include <sys/wait.h>
#include <event.h>
-#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
@@ -51,7 +50,7 @@ pipe_command(void)
switch (pid = fork()) {
case -1:
- err(1, "fork");
+ cu_err(1, "fork");
case 0:
fd = open(_PATH_DEVNULL, O_RDWR);
if (fd < 0 || dup2(fd, STDIN_FILENO) == -1)
@@ -102,7 +101,7 @@ connect_command(void)
switch (pid = fork()) {
case -1:
- err(1, "fork");
+ cu_err(1, "fork");
case 0:
if (signal(SIGINT, SIG_DFL) == SIG_ERR)
_exit(1);
@@ -144,7 +143,7 @@ send_file(void)
expanded = tilde_expand(file);
f = fopen(expanded, "r");
if (f == NULL) {
- warn("%s", file);
+ cu_warn("%s", file);
return;
}
@@ -170,11 +169,12 @@ set_speed(void)
speed = strtonum(s, 0, UINT_MAX, &errstr);
if (errstr != NULL) {
- warnx("speed is %s: %s", errstr, s);
+ cu_warnx("speed is %s: %s", errstr, s);
return;
}
- set_line(speed);
+ if (set_line(speed) != 0)
+ cu_warn("tcsetattr");
}
void
diff --git a/usr.bin/cu/cu.c b/usr.bin/cu/cu.c
index ee87334b728..ad06b9ac273 100644
--- a/usr.bin/cu/cu.c
+++ b/usr.bin/cu/cu.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cu.c,v 1.3 2012/07/10 08:42:43 nicm Exp $ */
+/* $OpenBSD: cu.c,v 1.4 2012/07/10 10:28:05 nicm Exp $ */
/*
* Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
@@ -132,7 +132,7 @@ getopt:
err(1, "ioctl(TIOCEXCL)");
if (set_line(speed) != 0)
- exit(1);
+ err(1, "tcsetattr");
if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &saved_tio) != 0)
err(1, "tcgetattr");
@@ -146,7 +146,7 @@ getopt:
if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
err(1, "signal");
- set_termios();
+ set_termios(); /* after this use cu_err and friends */
/* stdin and stdout get separate events */
input_ev = bufferevent_new(STDIN_FILENO, stream_read, NULL,
@@ -201,17 +201,14 @@ set_termios(void)
tio.c_cc[VQUIT] = _POSIX_VDISABLE;
tio.c_cc[VSUSP] = _POSIX_VDISABLE;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio) != 0)
- err(1, "tcsetattr");
+ cu_err(1, "tcsetattr");
}
void
restore_termios(void)
{
- if (!isatty(STDIN_FILENO))
- return;
-
- if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tio) != 0)
- err(1, "tcsetattr");
+ if (isatty(STDIN_FILENO))
+ tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tio);
}
int
@@ -227,10 +224,8 @@ set_line(int speed)
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
cfsetspeed(&tio, speed);
- if (tcsetattr(line_fd, TCSAFLUSH, &tio) != 0) {
- warn("tcsetattr");
+ if (tcsetattr(line_fd, TCSAFLUSH, &tio) != 0)
return (-1);
- }
return (0);
}
@@ -343,12 +338,12 @@ tilde_expand(const char *filename1)
out = strdup(ret);
if (out == NULL)
- err(1, "strdup");
+ cu_err(1, "strdup");
return (out);
no_change:
out = strdup(filename1);
if (out == NULL)
- err(1, "strdup");
+ cu_err(1, "strdup");
return (out);
}
diff --git a/usr.bin/cu/cu.h b/usr.bin/cu/cu.h
index ef7f888a235..e51e6efe25f 100644
--- a/usr.bin/cu/cu.h
+++ b/usr.bin/cu/cu.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: cu.h,v 1.2 2012/07/10 08:16:27 nicm Exp $ */
+/* $OpenBSD: cu.h,v 1.3 2012/07/10 10:28:05 nicm Exp $ */
/*
* Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
@@ -35,4 +35,14 @@ char *tilde_expand(const char *);
/* input.c */
const char *get_input(const char *);
+/* error.c */
+void cu_warn(const char *, ...)
+ __attribute__ ((format (printf, 1, 2)));
+void cu_warnx(const char *, ...)
+ __attribute__ ((format (printf, 1, 2)));
+void cu_err(int, const char *, ...)
+ __attribute__ ((format (printf, 2, 3)));
+void cu_errx(int, const char *, ...)
+ __attribute__ ((format (printf, 2, 3)));
+
#endif
diff --git a/usr.bin/cu/error.c b/usr.bin/cu/error.c
new file mode 100644
index 00000000000..b7e9f52913d
--- /dev/null
+++ b/usr.bin/cu/error.c
@@ -0,0 +1,85 @@
+/* $OpenBSD: error.c,v 1.1 2012/07/10 10:28:05 nicm Exp $ */
+
+/*
+ * Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "cu.h"
+
+/*
+ * Once we've configured termios, we need to use \r\n to end lines, so use our
+ * own versions of warn/warnx/err/errx.
+ */
+
+extern char *__progname;
+
+void
+cu_err(int eval, const char *fmt, ...)
+{
+ va_list ap;
+
+ restore_termios();
+
+ va_start(ap, fmt);
+ verr(eval, fmt, ap);
+}
+
+void
+cu_errx(int eval, const char *fmt, ...)
+{
+ va_list ap;
+
+ restore_termios();
+
+ va_start(ap, fmt);
+ verrx(eval, fmt, ap);
+}
+
+void
+cu_warn(const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf(stderr, "%s: ", __progname);
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ fprintf(stderr, ": %s\r\n", strerror(errno));
+}
+
+void
+cu_warnx(const char *fmt, ...)
+{
+ va_list ap;
+
+ fprintf(stderr, "%s: ", __progname);
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ fprintf(stderr, "\r\n");
+}
diff --git a/usr.bin/cu/input.c b/usr.bin/cu/input.c
index 03f8599662d..c004108fc24 100644
--- a/usr.bin/cu/input.c
+++ b/usr.bin/cu/input.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: input.c,v 1.1 2012/07/10 08:02:27 nicm Exp $ */
+/* $OpenBSD: input.c,v 1.2 2012/07/10 10:28:05 nicm Exp $ */
/*
* Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
@@ -19,7 +19,6 @@
#include <sys/types.h>
#include <ctype.h>
-#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
@@ -57,7 +56,7 @@ get_input(const char *prompt)
act.sa_flags = 0;
act.sa_handler = input_signal;
if (sigaction(SIGINT, &act, &oact) != 0)
- err(1, "sigaction");
+ cu_err(1, "sigaction");
input_stop = 0;
restore_termios();
@@ -69,7 +68,7 @@ get_input(const char *prompt)
while (cp != s + sizeof(s) - 1) {
n = read(STDIN_FILENO, &c, 1);
if (n == -1 && errno != EINTR)
- err(1, "read");
+ cu_err(1, "read");
if (n != 1 || input_stop)
break;
if (c == '\n') {