-/*
- * ptty.c:
+/*
+ * serial.c:
*
* Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
* All rights reserved.
*
*/
-static char rcsid[] = "$Id$";
+static char rcsid[] = "$Id: serial.c,v 1.18 2011/02/06 16:51:22 james Exp $";
-/*
- * $Log$
+/*
+ * $Log: serial.c,v $
+ * Revision 1.18 2011/02/06 16:51:22 james
+ * *** empty log message ***
+ *
+ * Revision 1.17 2008/03/10 11:49:33 james
+ * *** empty log message ***
+ *
+ * Revision 1.16 2008/03/07 13:16:02 james
+ * *** empty log message ***
+ *
+ * Revision 1.15 2008/03/07 12:37:04 james
+ * *** empty log message ***
+ *
+ * Revision 1.14 2008/03/03 06:04:42 james
+ * *** empty log message ***
+ *
+ * Revision 1.13 2008/03/02 10:37:56 james
+ * *** empty log message ***
+ *
+ * Revision 1.12 2008/02/28 16:57:52 james
+ * *** empty log message ***
+ *
+ * Revision 1.11 2008/02/26 23:23:17 james
+ * *** empty log message ***
+ *
+ * Revision 1.10 2008/02/24 00:47:14 james
+ * *** empty log message ***
+ *
+ * Revision 1.9 2008/02/24 00:42:53 james
+ * *** empty log message ***
+ *
+ * Revision 1.8 2008/02/23 13:05:58 staffcvs
+ * *** empty log message ***
+ *
+ * Revision 1.7 2008/02/15 23:52:12 james
+ * *** empty log message ***
+ *
+ * Revision 1.6 2008/02/15 19:51:30 james
+ * *** empty log message ***
+ *
+ * Revision 1.5 2008/02/15 19:09:00 james
+ * *** empty log message ***
+ *
+ * Revision 1.4 2008/02/15 16:48:56 james
+ * *** empty log message ***
+ *
+ * Revision 1.3 2008/02/15 03:32:07 james
+ * *** empty log message ***
+ *
+ * Revision 1.2 2008/02/14 16:21:17 james
+ * *** empty log message ***
+ *
* Revision 1.1 2008/02/14 12:51:14 james
* *** empty log message ***
*
*/
#include "project.h"
+#include <pwd.h>
+#include <dirent.h>
+#include <sys/stat.h>
-typedef struct
-{
+typedef struct {
TTY_SIGNATURE;
+ Serial_lock *lock;
int fd;
- pid_t child;
-} PTTY;
+} Serial;
static void
-ptty_close (TTY * _t)
+serial_close (TTY * _t)
{
- PTTY *t = (PTTY *) _t;
+ Serial *t = (Serial *) _t;
if (!t)
return;
+ tcflush (t->fd, TCIOFLUSH);
close (t->fd);
free (t);
}
-
static int
-ptty_read (TTY * _t, void *buf, int len)
+serial_read (TTY * _t, void *buf, int len)
{
- PTTY *t = (PTTY *) _t;
+ Serial *t = (Serial *) _t;
int red, done = 0;
- do
- {
+ t->blocked = serial_lock_check (t->lock);
+
+ if (t->blocked)
+ return 0;
+
+ do {
- red = wrap_read (t->fd, buf, len);
- if (red < 0)
- return -1;
- if (!red)
- return done;
+ red = wrap_read (t->fd, buf, len);
+ if (red < 0)
+ return done ? done:-1;
+ if (!red)
+ return done;
- buf += red;
- len -= red;
- done += red;
- }
+ buf += red;
+ len -= red;
+ done += red;
+ }
while (len);
static int
-ptty_write (TTY * _t, void *buf, int len)
+serial_write (TTY * _t, void *buf, int len)
{
int writ, done = 0;
- PTTY *t = (PTTY *) _t;
+ Serial *t = (Serial *) _t;
- do
- {
+ t->blocked = serial_lock_check (t->lock);
+ if (t->blocked)
+ return 0;
- writ = wrap_write (t->fd, buf, len);
- if (writ < 0)
- return -1;
- if (!writ)
- sleep (1);
+ do {
- buf += writ;
- len -= writ;
- done += writ;
- }
+ writ = wrap_write (t->fd, buf, len);
+ if (writ < 0)
+ return -1;
+ if (!writ)
+ sleep (1);
+
+ buf += writ;
+ len -= writ;
+ done += writ;
+ }
while (len);
}
TTY *
-ptty_open (char *path, char *argv[])
+serial_open (char *path, int lock_mode)
{
- PTTY *t;
+ Serial *t;
pid_t child;
char name[1024];
struct winsize winsize = { 0 };
struct termios termios;
int fd;
- char *default_argv[] = { "-", (char *) 0 };
+ Serial_lock *l;
+ l = serial_lock_new (path, lock_mode);
+ if (!l)
+ return NULL;
- default_termios (&termios);
- winsize.ws_row = VT102_ROWS;
- winsize.ws_col = VT102_COLS;
+ fd = open (path, O_RDWR | O_NOCTTY | O_NONBLOCK);
- child = forkpty (&fd, name, &termios, &winsize);
+ set_nonblocking (fd);
- switch (child)
- {
- case -1: /*boo hiss */
- return NULL;
- case 0: /*waaah */
- setenv ("TERM", "vt102", 1);
- setenv ("LANG", "C", 1);
- if (!path)
- path = "/bin/sh";
- if (!argv)
- argv = default_argv;
+ if (tcgetattr (fd, &termios)) {
+ close (fd);
+ return NULL;
+ }
+ default_termios (&termios);
- execv (path, argv);
- _exit (-1);
- }
+ if (tcsetattr (fd, TCSANOW, &termios)) {
+ close (fd);
+ return NULL;
+ }
- set_nonblocking (fd);
+ t = (Serial *) xmalloc (sizeof (Serial));
- t = (PTTY *) malloc (sizeof (PTTY));
+ t->lock = l;
- strncpy (t->name, name, sizeof (t->name));
+ strncpy (t->name, path, sizeof (t->name));
t->name[sizeof (t->name) - 1] = 0;
- t->recv = ptty_read;
- t->xmit = ptty_write;
- t->close = ptty_close;
+ t->recv = serial_read;
+ t->xmit = serial_write;
+ t->close = serial_close;
t->fd = fd;
- t->child = child;
t->rfd = t->fd;
t->wfd = t->fd;
- t->size.x = winsize.ws_row;
- t->size.y = winsize.ws_col;
- t->blocked = 0;
+ t->size.x = VT102_COLS_80;
+ t->size.y = VT102_ROWS_24;
+ t->blocked = serial_lock_check (t->lock);
+ t->hanging_up = 0;
return (TTY *) t;
}