chiark / gitweb /
*** empty log message ***
authorjames <james>
Thu, 14 Feb 2008 12:14:50 +0000 (12:14 +0000)
committerjames <james>
Thu, 14 Feb 2008 12:14:50 +0000 (12:14 +0000)
src/ipc.c [new file with mode: 0644]
src/ipc.h [new file with mode: 0644]
src/log.c [new file with mode: 0644]
src/log.h [new file with mode: 0644]

diff --git a/src/ipc.c b/src/ipc.c
new file mode 100644 (file)
index 0000000..e42f6ff
--- /dev/null
+++ b/src/ipc.c
@@ -0,0 +1,152 @@
+/*
+ * ipc.c:
+ *
+ * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
+ * All rights reserved.
+ *
+ */
+
+static char rcsid[] = "$Id$";
+
+/*
+ * $Log$
+ * Revision 1.1  2008/02/14 12:17:42  james
+ * *** empty log message ***
+ *
+ */
+
+#include "project.h"
+
+IPC_Msg *
+ipc_check_for_message_in_slide (Slide * s)
+{
+  IPC_Msg *m;
+  if (SLIDE_BYTES (s) < sizeof (IPC_Msg_hdr))
+    return NULL;
+  m = (IPC_Msg *) SLIDE_RPTR (s);
+  if (SLIDE_BYTES (s) < m->hdr.size)
+    return NULL;
+  if (m->hdr.size < sizeof (IPC_Msg_hdr))
+    abort ();
+
+  return m;
+}
+
+void
+ipc_consume_message_in_slide (Slide * s)
+{
+  IPC_Msg *m = ipc_check_for_message_in_slide (s);
+  if (!m)
+    abort ();
+
+  slide_consume (s, m->hdr.size);
+}
+
+
+int
+ipc_msg_send (Socket * s, IPC_Msg * m)
+{
+  int len = m->hdr.size;
+  return (socket_write (s, m, len) == len) ? 0 : -1;
+}
+
+
+int
+ipc_msg_send_debug (Socket * s, char *msg)
+{
+  char buf[sizeof (IPC_Msg_hdr) + IPC_MAX_BUF];
+  IPC_Msg_debug *m;
+  int len;
+
+
+  m = (IPC_Msg_debug *) buf;
+  m->type = IPC_MSG_TYPE_DEBUG;
+  strncpy (m->msg, msg, IPC_MAX_BUF);
+  m->msg[IPC_MAX_BUF - 1] = 0;
+
+  m->size = sizeof (IPC_Msg_hdr) + strlen (m->msg) + 1;
+
+
+  return ipc_msg_send (s, (IPC_Msg *) m);
+}
+
+int
+ipc_msg_send_history (Socket * s, History_ent * l)
+{
+  IPC_Msg_history m;
+  int len;
+
+
+  m.type = IPC_MSG_TYPE_HISTORY;
+  m.history = *l;
+  m.size = sizeof (m);
+
+  return ipc_msg_send (s, (IPC_Msg *) & m);
+}
+
+int
+ipc_msg_send_vt102 (Socket * s, VT102 * v)
+{
+  IPC_Msg_VT102 m;
+  int len;
+
+
+  m.type = IPC_MSG_TYPE_VT102;
+  m.len = sizeof (VT102);
+  m.vt102 = *v;
+  m.size = sizeof (m);
+
+  return ipc_msg_send (s, (IPC_Msg *) & m);
+}
+
+
+int
+ipc_msg_send_key (Socket * s, int key)
+{
+  IPC_Msg_key m;
+
+  m.size = sizeof (m);
+  m.type = IPC_MSG_TYPE_KEY;
+  m.key = key;
+  return ipc_msg_send (s, (IPC_Msg *) & m);
+}
+
+
+
+int
+ipc_msg_send_term (Socket * s, void *buf, int len)
+{
+  char mbuf[IPC_MAX_BUF + sizeof (IPC_Msg_hdr)];
+
+  IPC_Msg_term *m = (IPC_Msg_term *) mbuf;
+
+  if (!len)
+    return 0;
+
+  m->size = len + sizeof (IPC_Msg_hdr);
+  m->type = IPC_MSG_TYPE_TERM;
+  m->len = len;
+  memcpy (m->term, buf, len);
+
+  return ipc_msg_send (s, (IPC_Msg *) & m);
+}
+
+
+int
+ipc_msg_send_status (Socket * s, char *buf)
+{
+  char mbuf[IPC_MAX_BUF + sizeof (IPC_Msg_hdr)];
+  IPC_Msg_status *m = (IPC_Msg_status *) mbuf;
+  int len;
+
+  if (!buf)
+    return 0;
+  len = strlen (buf) + 1;
+
+  m->size = len + sizeof (IPC_Msg_hdr);
+  m->type = IPC_MSG_TYPE_STATUS;
+  strncpy (m->status, buf, IPC_MAX_BUF - 1);
+  m->status[IPC_MAX_BUF - 1] = 0;
+
+  return ipc_msg_send (s, (IPC_Msg *) & m);
+}
diff --git a/src/ipc.h b/src/ipc.h
new file mode 100644 (file)
index 0000000..e8d224c
--- /dev/null
+++ b/src/ipc.h
@@ -0,0 +1,110 @@
+/*
+ * ipc.h:
+ *
+ * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
+ * All rights reserved.
+ *
+ */
+
+/*
+ * $Id$
+ */
+
+/*
+ * $Log$
+ * Revision 1.1  2008/02/14 12:17:42  james
+ * *** empty log message ***
+ *
+ */
+
+#ifndef __IPC_H__
+#define __IPC_H__
+
+#define IPC_MAX_BUF 1024
+
+#define IPC_MSG_TYPE_NOOP  0
+#define IPC_MSG_TYPE_DEBUG 1
+#define IPC_MSG_TYPE_HISTORY 2
+#define IPC_MSG_TYPE_VT102 3
+#define IPC_MSG_TYPE_KEY 4
+#define IPC_MSG_TYPE_TERM 5
+#define IPC_MSG_TYPE_STATUS 6
+
+typedef struct
+{
+  int32_t size;
+  int32_t type;
+  uint8_t payload[0];
+} IPC_Msg_hdr;
+
+
+typedef struct
+{
+  int32_t size;
+  int32_t type;
+} IPC_Msg_noop;
+
+
+typedef struct
+{
+  int32_t size;
+  int32_t type;
+  char msg[0];
+} IPC_Msg_debug;
+
+typedef struct 
+{
+  int32_t size;
+  int32_t type;
+  History_ent history;
+} IPC_Msg_history;
+
+typedef struct 
+{
+  int32_t size;
+  int32_t type;
+  int32_t len;
+  VT102 vt102;
+} IPC_Msg_VT102;
+
+
+typedef struct 
+{
+  int32_t size;
+  int32_t type;
+  int32_t key;
+} IPC_Msg_key;
+
+typedef struct 
+{
+  int32_t size;
+  int32_t type;
+  int32_t len;
+  uint8_t term[0];
+} IPC_Msg_term;
+
+
+typedef struct 
+{
+  int32_t size;
+  int32_t type;
+  char  status[0];
+} IPC_Msg_status;
+
+
+
+typedef union 
+{
+IPC_Msg_hdr hdr;
+IPC_Msg_noop noop;
+IPC_Msg_debug debug;
+IPC_Msg_history history;
+IPC_Msg_VT102 vt102;
+IPC_Msg_key key;
+IPC_Msg_term term;
+IPC_Msg_status status;
+} IPC_Msg;
+
+
+
+#endif /* __IPC_H__ */
diff --git a/src/log.c b/src/log.c
new file mode 100644 (file)
index 0000000..c5760d2
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,82 @@
+/*
+ * log.c:
+ *
+ * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
+ * All rights reserved.
+ *
+ */
+
+static char rcsid[] = "$Id$";
+
+/*
+ * $Log$
+ * Revision 1.1  2008/02/14 12:14:50  james
+ * *** empty log message ***
+ *
+ */
+
+#include "project.h"
+
+typedef struct
+{
+  LOG_SIGNATURE;
+  FILE *fp;
+} File_Log;
+
+static void
+flog_log (Log * _l, char *buf)
+{
+  File_Log *l = (File_Log *) _l;
+  struct timeval tv = { 0 };
+  struct tm *tm;
+  time_t t;
+  static const char *days[] = { "Sun", "Mon", "Tue",
+    "Wed", "Thu", "Fri", "Sat"
+  };
+  static const char *months[] = {
+    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
+    "Nov", "Dec"
+  };
+
+  if (!l->fp)
+    return;
+
+  gettimeofday (&tv, NULL);
+  t = tv.tv_sec;
+  tm = localtime (&t);
+
+  fprintf (l->fp, "%s %2d %02d:%02d:%02d.%06d ", months[tm->tm_mon],
+           tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
+
+  fputs (buf, l->fp);
+  fputc ('\n', l->fp);
+  fflush (l->fp);
+}
+
+static void
+flog_close (Log * _l)
+{
+  File_Log *l = (File_Log *) _l;
+  if (l->fp)
+    fclose (l->fp);
+  free (l);
+}
+
+Log *
+file_log_new (char *fn)
+{
+  File_Log *l;
+  FILE *f;
+
+  f = fopen (fn, "a+");
+  if (!f)
+    return NULL;
+
+  l = malloc (sizeof (File_Log));
+
+  l->log = flog_log;
+  l->close = flog_close;
+  l->fp = f;
+
+  return (Log *) l;
+}
diff --git a/src/log.h b/src/log.h
new file mode 100644 (file)
index 0000000..0b248df
--- /dev/null
+++ b/src/log.h
@@ -0,0 +1,32 @@
+/*
+ * log.h:
+ *
+ * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
+ * All rights reserved.
+ *
+ */
+
+/*
+ * $Id$
+ */
+
+/*
+ * $Log$
+ * Revision 1.1  2008/02/14 12:14:50  james
+ * *** empty log message ***
+ *
+ */
+
+#ifndef __LOG_H__
+#define __LOG_H__
+
+#define LOG_SIGNATURE \
+       void (*log)(struct Log_struct *,char *); \
+       void (*close)(struct Log_struct *)
+
+typedef struct Log_struct {
+       LOG_SIGNATURE;
+} Log;
+
+
+#endif /* __LOG_H__ */