chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / serial.c
index 5fff1bd5a9409a9f177e4d1e0eb66ba61b12cc68..2e67d3dd75e5f465876a37784baf64070dea991f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * ptty.c:
+ * serial.c:
  *
  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
  * All rights reserved.
@@ -10,6 +10,21 @@ static char rcsid[] = "$Id$";
 
 /*
  * $Log$
+ * 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 ***
  *
@@ -43,20 +58,23 @@ static char rcsid[] = "$Id$";
  */
 
 #include "project.h"
+#include <pwd.h>
+#include <dirent.h>
+#include <sys/stat.h>
 
 
 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;
@@ -66,13 +84,17 @@ ptty_close (TTY * _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;
 
+  t->blocked = serial_lock_check (t->lock);
+
+  if (t->blocked)
+    return 0;
+
   do
     {
 
@@ -97,7 +119,11 @@ static int
 ptty_write (TTY * _t, void *buf, int len)
 {
   int writ, done = 0;
-  PTTY *t = (PTTY *) _t;
+  Serial *t = (Serial *) _t;
+
+  t->blocked = serial_lock_check (t->lock);
+  if (t->blocked)
+    return 0;
 
   do
     {
@@ -119,58 +145,42 @@ ptty_write (TTY * _t, void *buf, int 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;
-
-  child = forkpty (&fd, name, &termios, &winsize);
-
-  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;
-
-      execv (path, argv);
-      _exit (-1);
-    }
+  fd = open (path, O_RDWR);
 
   set_nonblocking (fd);
 
-  t = (PTTY *) malloc (sizeof (PTTY));
+  t = (Serial *) malloc (sizeof (Serial));
+
+  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;
+  t->size.y = VT102_ROWS;
+  t->blocked = serial_lock_check (t->lock);
 
   return (TTY *) t;
 }