chiark / gitweb /
Merge branch 'cvsimport'
[sympathy.git] / src / serial.c
index c8e2295190d42fa74955c9873bf50d413e298b08..dac5ff27af6248d797fce7ebdd446fda534e6ca8 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/* 
  * serial.c:
  *
  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
@@ -6,10 +6,58 @@
  *
  */
 
-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 ***
  *
@@ -50,143 +98,14 @@ static char rcsid[] = "$Id$";
 #include <dirent.h>
 #include <sys/stat.h>
 
-#define LOCK_ASCII
-#undef LOCK_BINARY
-
-#define NLOCKFILES     10
 
 typedef struct {
-char *lockfiles[NLOCKFILES];
-char *potential_lockfiles[NLOCKFILES];
-struct timeval last_content_check;
-} Serial_lock;
-
-typedef struct
-{
   TTY_SIGNATURE;
-  Serial_lock lock;
+  Serial_lock *lock;
   int fd;
 } Serial;
 
 
-
-static int
-chown_uucp(fd)
-int fd;
-{
-    static int uuid = -1, ugid;
-    struct passwd *pw;
-
-    if (uuid <0) {
-        if (pw = getpwnam("uucp")) {
-            uuid = pw->pw_uid;
-            ugid = pw->pw_gid;
-        } else {
-               return -1;
-       }
-    }
-    return fchown(fd, uuid, ugid);
-}
-
-int make_lockfile(char *name)
-{
-char buf[1024],tmpfn[1024];
-char *ptr;
-int fd;
-int i;
-
-strcpy(tmpfn,name);
-
-ptr=rindex(tmpfn,'/');
-if (!ptr) return -1;
-
-ptr++;
-
-ptr+=sprintf(ptr,"LTMP.%d",getpid());
-*ptr=0;
-
-i=sprintf(buf,"%10d\n",getpid());
-
-fd=open(tmpfn,O_WRONLY|O_CREAT|O_TRUNC,0444);
-if (fd<0) {
-       unlink(tmpfn);
-       return -1;
-}
-
-write(fd,buf,i);
-fchmod(fd,044);
-if( chown_uucp(fd)) {
-       close(fd);
-       unlink(tmpfn);
-       return -1;
-}
-
-close(fd);
-
-if (link(tmpfn,name)<0) {
-       unlink(tmpfn);
-       return -1;
-}
-
-unlink(tmpfn);
-return 0;
-}
-
-
-
-void construct_lock_file_name_by_name(char *ptr)
-{
-
-printf("lock by file %s\n",ptr);
-
-}
-void construct_lock_file_name_by_device(dev_t dev)
-{
-
-printf("lock by dev %x\n",dev);
-}
-
-
-#define DEV "/dev/"
-int construct_possible_lock_files(char *device)
-{
-int nl;
-struct dirent *de;
-DIR *d;
-struct stat dev_stat,ent_stat;
-char buf[1024];
-
-
-if (stat(device,&dev_stat)) return -1;
-if (!S_ISCHR(dev_stat.st_mode)) return -1;
-
-construct_lock_file_name_by_device(dev_stat.st_rdev);
-
-construct_lock_file_name_by_name(device);
-
-for (d=opendir(DEV);(de=readdir(d));)
-{
-strcpy(buf,DEV);
-strcat(buf,de->d_name);
-
-if (stat(buf,&ent_stat)) continue;
-if (!S_ISCHR(ent_stat.st_mode)) continue;
-if (ent_stat.st_rdev!=dev_stat.st_rdev) continue;
-
-construct_lock_file_name_by_name(buf);
-
-}
-closedir(d);
-
-}
-
-
-static void 
-serial_check_lock(Serial *t)
-{
-}
-
-
 static void
 serial_close (TTY * _t)
 {
@@ -195,34 +114,35 @@ serial_close (TTY * _t)
   if (!t)
     return;
 
+  tcflush (t->fd, TCIOFLUSH);
   close (t->fd);
   free (t);
 }
 
 
-
 static int
 serial_read (TTY * _t, void *buf, int len)
 {
   Serial *t = (Serial *) _t;
   int red, done = 0;
 
-  serial_check_lock(t);
-  if (t->blocked) return 0;
+  t->blocked = serial_lock_check (t->lock);
+
+  if (t->blocked)
+    return 0;
 
-  do
-    {
+  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);
 
 
@@ -231,27 +151,27 @@ serial_read (TTY * _t, void *buf, int len)
 
 
 static int
-ptty_write (TTY * _t, void *buf, int len)
+serial_write (TTY * _t, void *buf, int len)
 {
   int writ, done = 0;
   Serial *t = (Serial *) _t;
 
-  serial_check_lock(t);
-  if (t->blocked) return 0;
+  t->blocked = serial_lock_check (t->lock);
+  if (t->blocked)
+    return 0;
 
-  do
-    {
+  do {
 
-      writ = wrap_write (t->fd, buf, len);
-      if (writ < 0)
-        return -1;
-      if (!writ)
-        sleep (1);
+    writ = wrap_write (t->fd, buf, len);
+    if (writ < 0)
+      return -1;
+    if (!writ)
+      sleep (1);
 
-      buf += writ;
-      len -= writ;
-      done += writ;
-    }
+    buf += writ;
+    len -= writ;
+    done += writ;
+  }
   while (len);
 
 
@@ -259,7 +179,7 @@ ptty_write (TTY * _t, void *buf, int len)
 }
 
 TTY *
-serial_open (char *path)
+serial_open (char *path, int lock_mode)
 {
   Serial *t;
   pid_t child;
@@ -267,28 +187,46 @@ serial_open (char *path)
   struct winsize winsize = { 0 };
   struct termios termios;
   int fd;
+  Serial_lock *l;
 
+  l = serial_lock_new (path, lock_mode);
+  if (!l)
+    return NULL;
 
-  default_termios (&termios);
 
-  fd=open(path,O_RDWR);
+  fd = open (path, O_RDWR | O_NOCTTY | O_NONBLOCK);
 
   set_nonblocking (fd);
 
-  t = (Serial *) malloc (sizeof (Serial));
+
+  if (tcgetattr (fd, &termios)) {
+    close (fd);
+    return NULL;
+  }
+  default_termios (&termios);
+
+  if (tcsetattr (fd, TCSANOW, &termios)) {
+    close (fd);
+    return NULL;
+  }
+
+  t = (Serial *) xmalloc (sizeof (Serial));
+
+  t->lock = l;
 
   strncpy (t->name, path, sizeof (t->name));
   t->name[sizeof (t->name) - 1] = 0;
 
   t->recv = serial_read;
-  //t->xmit = serial_write;
+  t->xmit = serial_write;
   t->close = serial_close;
   t->fd = fd;
   t->rfd = t->fd;
   t->wfd = t->fd;
-  t->size.x = VT102_COLS;
-  t->size.y = VT102_ROWS;
-  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;
 }