File: xlsock.c

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (189 lines) | stat: -rw-r--r-- 3,909 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "xlshlib.h"
#include <limits.h>
#include "sock.h"

#ifndef OPEN_MAX
#  define OPEN_MAX 64
#endif

static int sock[OPEN_MAX];
static int sock_inited = FALSE;

#define SOCK_MAX OPEN_MAX

static void cleanup(void)
{
  int i;
  for (i = 0; i < SOCK_MAX; i++)
    if (sock[i] != -1) {
      Sock_close(sock[i], NULL);
      sock[i] = -1;
    }
}

static LVAL enter_sock(int fd)
{
  if (fd == -1)
    return NIL;
  else {
    int i;
    for (i = 0; i < SOCK_MAX; i++)
      if (sock[i] == -1) {
        sock[i] = fd;
        return cvfixnum((FIXTYPE) fd);
      }
    Sock_close(fd, NULL);
    return NIL;
  }
}

static LVAL close_sock(int fd)
{
  int i;
  for (i = 0; i < SOCK_MAX; i++)
    if (sock[i] == fd) {
      sock[i] = -1;
      return Sock_close(fd, NULL) == -1 ? NIL : s_true;
    }
  return NIL;
}

static void check_init(void)
{
  if (! sock_inited) {
    int i;
    for (i = 0; i < SOCK_MAX; i++)
      sock[i] = -1;
    Sock_init();
    sock_inited = TRUE;
    atexit(cleanup);
  }
}

LVAL xsockopen()
{
  int port = getfixnum(xlgafixnum());
  xllastarg();
  check_init();
  return enter_sock(Sock_open(port, NULL));
}

LVAL xsocklisten()
{
  int sock = getfixnum(xlgafixnum());
  xllastarg();
  check_init();
  return enter_sock(Sock_listen(sock, NULL, 0, NULL));
}

LVAL xsockconnect()
{
  int port = getfixnum(xlgafixnum());
  char *serv = getstring(xlgastring());
  xllastarg();
  check_init();
  return enter_sock(Sock_connect(port, serv, NULL));
}

LVAL xsockclose()
{
  int sock = getfixnum(xlgafixnum());
  xllastarg();
  return close_sock(sock);
}

LVAL xsockread()
{
  ssize_t n;
  int port = getfixnum(xlgafixnum());
  LVAL buf = xlgastring();
  xllastarg();
  check_init();
  n = Sock_read(port, getstring(buf), getslength(buf), NULL);
  return n == -1 ? NIL : cvfixnum((FIXTYPE) n);
}

LVAL xsockwrite()
{
  ssize_t n;
  int port, start, end, len;
  LVAL buf;
  port = getfixnum(xlgafixnum());
  buf = xlgastring();
  start = getfixnum(xlgafixnum());
  end = getfixnum(xlgafixnum());
  xllastarg();
  check_init();
  len = getslength(buf);
  if (end > len)
    end = len;
  if (start < 0)
    start = 0;
  if (end < start)
    return NIL;
  n = Sock_write(port, getstring(buf) + start, end - start, NULL);
  return n == -1 ? NIL : cvfixnum((FIXTYPE) n);
}

#ifdef UNIX
/* Under X11 after a fork() the next call to XSync() hangs. I'll try
   to figure this out but for now if you want to use fork you can't
   use graphics -- just undefine DISPLAY before tarting xlisp.  I'm
   sure it isn't the fork as such but rather something like the
   attempt of both processes to wait on the display that is the
   problem. But I don't know exactly what it is. */
#ifdef XLISP_STAT
#include "xlgraph.h"
#endif /* XLISP_STAT */
#include <signal.h>
#include <sys/wait.h>
static void sig_child(int sig)
{
  int stat;
  while (waitpid(-1, &stat, WNOHANG) > 0);
}

static int sig_fork_inited = FALSE;

LVAL xsockfork()
{
  pid_t pid;
  xllastarg();
#ifdef XLISP_STAT
  if (StHasWindows())
    xlfail("can't fork under X11 (at least for now)");
#endif /* XLISP_STAT */
  if (! sig_fork_inited) {
    struct sigaction sa;
    sa.sa_handler = sig_child;
    sa.sa_flags = 0;
    sigaction(SIGCHLD, &sa, NULL);
    sig_fork_inited = TRUE;
  }
  pid = fork();
  return pid == -1 ? NIL : cvfixnum((FIXTYPE) pid);
}
#endif /* UNIX */

static FUNDEF myfuns[] = {
  { "SOCKETS::SOCK-OPEN", SUBR, xsockopen },
  { "SOCKETS::SOCK-LISTEN", SUBR, xsocklisten },
  { "SOCKETS::SOCK-CONNECT", SUBR, xsockconnect },
  { "SOCKETS::SOCK-CLOSE", SUBR, xsockclose },
  { "SOCKETS::BASE-SOCK-READ", SUBR, xsockread },
  { "SOCKETS::BASE-SOCK-WRITE", SUBR, xsockwrite },
#ifdef UNIX
  { "SOCKETS::FORK", SUBR, xsockfork },
#endif
  { NULL, 0, NULL }
};

static xlshlib_modinfo_t myinfo = {
  XLSHLIB_VERSION_INFO(0,1,0,1),
  myfuns,
  NULL,
  NULL,
  NULL
};

xlshlib_modinfo_t *xlsock__init() { return &myinfo; }