sockets.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* sockets.c Copyright (C) 1995 NAG Ltd */
  2. /*
  3. * Functions to support operations on sockets for AXIOM: basically just
  4. * interfaces to some of the functions in libspad.a (sockio-c.c). We
  5. * assume that all integers returned as Lisp objects can be represented
  6. * as CCL fixnums, i.e. do not lie in the range +/-(2^28 .. 2^32-1).
  7. *
  8. * Version 1.0 September 1995
  9. *
  10. * Author: Mike Dewar
  11. *
  12. */
  13. /* Signature: 1cca4b86 27-Jan-1997 */
  14. #include <stdarg.h>
  15. #include <sys/types.h>
  16. #include <string.h>
  17. #ifdef __WATCOMC__
  18. #include <dos.h>
  19. #endif
  20. #include "machine.h"
  21. #include "tags.h"
  22. #include "cslerror.h"
  23. #include "externs.h"
  24. #include "arith.h"
  25. #if defined(UNIX)
  26. #define RealSockets
  27. #endif
  28. #if defined(__linux__)
  29. void glnag(char * s, char * t)
  30. {
  31. }
  32. #endif
  33. #ifdef FLEX
  34. Lisp_Object Linit_lm(Lisp_Object nil, Lisp_Object dummy)
  35. {
  36. static int first_time =0;
  37. extern void glnag(char *prod_code, char *user_opts);
  38. extern void rlnag();
  39. /* If glnag fails it calls displmmsg and exits */
  40. if (first_time == 0) {
  41. #if defined(__alpha)
  42. glnag("AXDAU21NA","");
  43. #elif defined(__sparc) && defined(sparc)
  44. glnag("AXSU421NA","");
  45. #elif defined(__sparc)
  46. glnag("AXSOL21NA","");
  47. #elif defined(__hp9000s800)
  48. glnag("AXH9721NA","");
  49. #elif defined(__sgi) && defined(__mips) && __mips==1
  50. glnag("AXSG521NA","");
  51. #elif defined(__sgi) && defined(__mips) && __mips==3
  52. glnag("AXSG621NA","");
  53. #elif defined(_AIX)
  54. glnag("AXIB621NA","");
  55. #elif defined(__linux__)
  56. glnag("AXLUX21NA","");
  57. #elif defined(WINDOWS_NT)
  58. glnag("AXW3222NA","");
  59. #ifndef CWIN
  60. rlnag();
  61. #endif
  62. #endif
  63. first_time = 1;
  64. }
  65. return onevalue(fixnum_of_int(1));
  66. }
  67. #ifdef CWIN
  68. #undef exit
  69. #endif
  70. void displmmsg(int unlicensed, char *message)
  71. {
  72. /* term_printf only handles strings less than 256 characters long */
  73. if (strlen(message) > 255) {
  74. char *line;
  75. line = strtok(message,"\n");
  76. term_printf("%s\n",line);
  77. while ((line=strtok(NULL,"\n")) != NULL) term_printf("%s\n",line);
  78. }
  79. else
  80. term_printf("%s\n",message);
  81. #ifdef CWIN
  82. ensure_screen();
  83. #endif
  84. if (unlicensed > 0) {
  85. term_printf("+++ This copy of AXIOM is unlicensed ... exiting.\n");
  86. #ifdef CWIN
  87. ensure_screen();
  88. #endif
  89. sleep(10);
  90. my_exit(2);
  91. }
  92. }
  93. Lisp_Object MS_CDECL Lclose_lm(Lisp_Object nil, int32 nargs, ...)
  94. {
  95. #ifdef WINDOWS_NT
  96. extern void rlnag();
  97. argcheck(nargs,0,"close-lm");
  98. rlnag();
  99. #endif
  100. return nil;
  101. }
  102. #else
  103. Lisp_Object Linit_lm(Lisp_Object nil, Lisp_Object dummy)
  104. {
  105. return onevalue(fixnum_of_int(1));
  106. }
  107. #endif
  108. #ifdef RealSockets
  109. /* These are all in sockio-c.c */
  110. extern int32 open_server(char *server_name);
  111. extern int32 sock_get_int(int32 purpose);
  112. extern int32 sock_send_int(int32 purpose, int32 val);
  113. extern char *sock_get_string_buf(int32 purpose, char *buf, int32 buf_len);
  114. extern int32 sock_send_string_len(int32 purpose, char *str, int32 len);
  115. extern double sock_get_float(int32 purpose);
  116. extern int32 sock_send_float(int32 purpose, double num);
  117. extern int32 sock_send_wakeup(int32 purpose);
  118. extern int32 server_switch();
  119. extern int32 flush_stdout();
  120. extern int32 sock_send_signal(int purpose, int sig);
  121. extern int32 print_line(char *s);
  122. /* Utility function to test for CCL integers and turn them into C ints */
  123. int32 ccl2int(Lisp_Object i, char *fcn)
  124. { if (is_fixnum(i))
  125. return (int_of_fixnum(i));
  126. else if (is_numbers(i) && is_bignum(i) )
  127. return (thirty_two_bits(i));
  128. else
  129. return aerror(fcn);
  130. }
  131. Lisp_Object LopenServer(Lisp_Object nil, Lisp_Object server)
  132. {
  133. if (!(is_vector(server) && type_of_header(vechdr(server)) == TYPE_STRING) )
  134. return aerror("open_server");
  135. else
  136. return onevalue(fixnum_of_int(open_server(&celt(server,0))));
  137. }
  138. Lisp_Object LsockGetInt(Lisp_Object nil, Lisp_Object purpose)
  139. {
  140. return onevalue(fixnum_of_int(sock_get_int(ccl2int(purpose,"sock_get_int"))));
  141. }
  142. Lisp_Object LsockSendInt(Lisp_Object nil, Lisp_Object purpose, Lisp_Object val)
  143. {
  144. return onevalue(fixnum_of_int(
  145. sock_send_int(ccl2int(purpose,"sock_send_int"),
  146. ccl2int(val,"sock_send_int"))));
  147. }
  148. Lisp_Object MS_CDECL LsockGetStringBuf(Lisp_Object nil, int nargs, ...)
  149. {
  150. va_list args;
  151. Lisp_Object purpose, buf, buf_length;
  152. argcheck(nargs,3,"sock_get_string_buf");
  153. va_start(args,nargs);
  154. purpose = va_arg(args, Lisp_Object);
  155. buf = va_arg(args, Lisp_Object);
  156. buf_length = va_arg(args, Lisp_Object);
  157. va_end(args);
  158. if (!(is_vector(buf) && type_of_header(vechdr(buf)) == TYPE_STRING) )
  159. aerror("sock_get_string_buf");
  160. else
  161. sock_get_string_buf(ccl2int(purpose,"sock_get_string_buf"),
  162. &celt(buf,0),
  163. ccl2int(buf_length,"sock_get_string_buf") );
  164. /* AXIOM always ignores the return value */
  165. return buf;
  166. }
  167. Lisp_Object MS_CDECL LsockSendStringLen(Lisp_Object nil, int nargs, ...)
  168. {
  169. va_list args;
  170. Lisp_Object purpose, buf, buf_length;
  171. argcheck(nargs,3,"sock_send_string_len");
  172. va_start(args,nargs);
  173. purpose = va_arg(args, Lisp_Object);
  174. buf = va_arg(args, Lisp_Object);
  175. buf_length = va_arg(args, Lisp_Object);
  176. va_end(args);
  177. if (!(is_vector(buf) && type_of_header(vechdr(buf)) == TYPE_STRING) )
  178. return aerror("sock_send_string_len");
  179. else
  180. return onevalue(fixnum_of_int(
  181. sock_send_string_len(ccl2int(purpose,"sock_send_string_len"),
  182. &celt(buf,0),
  183. ccl2int(buf_length,"sock_send_string_len") )));
  184. }
  185. Lisp_Object LsockGetFloat(Lisp_Object nil, Lisp_Object purpose)
  186. {
  187. return onevalue(make_boxfloat(sock_get_float(
  188. ccl2int(purpose,"sock_get_float")), TYPE_DOUBLE_FLOAT) );
  189. }
  190. Lisp_Object LsockSendFloat(Lisp_Object nil, Lisp_Object purpose, Lisp_Object val)
  191. {
  192. if (!is_number(val)) aerror("sock_send_float");
  193. return onevalue(fixnum_of_int(sock_send_float(
  194. ccl2int(purpose,"sock_send_float"),
  195. float_of_number(val) )));
  196. }
  197. Lisp_Object LsockSendWakeup(Lisp_Object nil, Lisp_Object purpose)
  198. {
  199. return onevalue(fixnum_of_int(sock_send_wakeup(ccl2int(purpose,
  200. "sock_send_wakeup"))));
  201. }
  202. Lisp_Object MS_CDECL LserverSwitch(Lisp_Object nil, int nargs, ...)
  203. {
  204. argcheck(nargs, 0, "server_switch");
  205. return onevalue(fixnum_of_int(server_switch()));
  206. }
  207. Lisp_Object MS_CDECL LflushStdout(Lisp_Object nil, int nargs, ...)
  208. {
  209. argcheck(nargs, 0, "flush_stdout");
  210. return onevalue(fixnum_of_int(flush_stdout()));
  211. }
  212. Lisp_Object LsockSendSignal(Lisp_Object nil,Lisp_Object purpose,Lisp_Object sig)
  213. {
  214. return onevalue(fixnum_of_int(sock_send_signal(
  215. ccl2int(purpose,"sock_send_signal"),
  216. ccl2int(sig,"sock_send_signal"))));
  217. }
  218. Lisp_Object LprintLine(Lisp_Object nil, Lisp_Object buf)
  219. {
  220. if (!(is_vector(buf) && type_of_header(vechdr(buf)) == TYPE_STRING) )
  221. return aerror("print_line");
  222. else
  223. return onevalue(fixnum_of_int(print_line(&celt(buf,0))));
  224. }
  225. #endif /* UNIX */
  226. #if defined(__i386__) || defined(__i486__)|| defined(__i386)||defined(__i586)
  227. static signed char plus_infinity[8]= {0x0,0x0,0x0,0x0,0x0,0x0,0xf0,0x7f},
  228. minus_infinity[8]={0x0,0x0,0x0,0x0,0x0,0x0,0xf0,0xff},
  229. NANQ[8]= {0x0,0x0,0x0,0x0,0x0,0x0,0xf8,0xff};
  230. #elif defined(__alpha)
  231. static signed char plus_infinity[8]= {0x0,0x0,0x0,0x0,0x0,0x0,0xf0,0x7f},
  232. minus_infinity[8]={0x0,0x0,0x0,0x0,0x0,0x0,0xf0,0xff},
  233. NANQ[8]= {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f};
  234. #else
  235. static signed char plus_infinity[8]= {0x7f,0xf0,0x0,0x0,0x0,0x0,0x0,0x0},
  236. minus_infinity[8]={0xff,0xf0,0x0,0x0,0x0,0x0,0x0,0x0},
  237. NANQ[8]= {0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
  238. #endif
  239. Lisp_Object MS_CDECL LplusInfinity(Lisp_Object nil, int nargs, ...)
  240. {
  241. argcheck(nargs, 0, "plus_infinity");
  242. return onevalue(make_boxfloat(*(double *)plus_infinity,TYPE_DOUBLE_FLOAT));
  243. }
  244. Lisp_Object MS_CDECL LminusInfinity(Lisp_Object nil, int nargs, ...)
  245. {
  246. argcheck(nargs, 0, "minus_infinity");
  247. return onevalue(make_boxfloat(*(double *)minus_infinity,TYPE_DOUBLE_FLOAT));
  248. }
  249. Lisp_Object MS_CDECL Lnanq(Lisp_Object nil, int nargs, ...)
  250. {
  251. argcheck(nargs, 0, "NANQ");
  252. return onevalue(make_boxfloat(*(double *)NANQ,TYPE_DOUBLE_FLOAT));
  253. }
  254. Lisp_Object sock_error1(Lisp_Object nil, Lisp_Object arg)
  255. {
  256. return aerror0("Attempt to call socket function on non-unix platform");
  257. }
  258. Lisp_Object sock_error2(Lisp_Object nil, Lisp_Object arg1, Lisp_Object arg2)
  259. {
  260. return aerror0("Attempt to call socket function on non-unix platform");
  261. }
  262. Lisp_Object MS_CDECL sock_error0(Lisp_Object nil, int nargs, ...)
  263. {
  264. return aerror0("Attempt to call socket function on non-unix platform");
  265. }
  266. setup_type const socket_setup[] =
  267. {
  268. #ifdef FLEX
  269. {"init-lm", Linit_lm, too_many_1, wrong_no_1},
  270. {"close-lm", wrong_no_0a, wrong_no_0b, Lclose_lm},
  271. #else
  272. {"init-lm", Linit_lm, too_many_1, wrong_no_1},
  273. {"close-lm", sock_error1, sock_error2, sock_error0},
  274. #endif
  275. #ifdef RealSockets
  276. {"open_server", LopenServer, too_many_1, wrong_no_1},
  277. {"sock_get_int", LsockGetInt, too_many_1, wrong_no_1},
  278. {"sock_send_int", too_few_2, LsockSendInt, wrong_no_2},
  279. {"sock_get_string_buf", wrong_no_na, wrong_no_nb, LsockGetStringBuf},
  280. {"sock_send_string_len", wrong_no_na, wrong_no_nb, LsockSendStringLen},
  281. {"sock_get_float", LsockGetFloat, too_many_1, wrong_no_1},
  282. {"sock_send_float", too_few_2, LsockSendFloat, wrong_no_2},
  283. {"sock_send_wakeup", LsockSendWakeup, too_many_1, wrong_no_1},
  284. {"server_switch", wrong_no_0a, wrong_no_0b, LserverSwitch},
  285. {"flush_stdout", wrong_no_0a, wrong_no_0b, LflushStdout},
  286. {"sock_send_signal", too_few_2, LsockSendSignal, wrong_no_2},
  287. {"print_line", LprintLine, too_many_1, wrong_no_1},
  288. #else
  289. {"open_server", sock_error1, sock_error2, sock_error0},
  290. {"sock_get_int", sock_error1, sock_error2, sock_error0},
  291. {"sock_send_int", sock_error1, sock_error2, sock_error0},
  292. {"sock_get_string_buf", sock_error1, sock_error2, sock_error0},
  293. {"sock_send_string_len", sock_error1, sock_error2, sock_error0},
  294. {"sock_get_float", sock_error1, sock_error2, sock_error0},
  295. {"sock_send_float", sock_error1, sock_error2, sock_error0},
  296. {"sock_send_wakeup", sock_error1, sock_error2, sock_error0},
  297. {"server_switch", sock_error1, sock_error2, sock_error0},
  298. {"flush_stdout", sock_error1, sock_error2, sock_error0},
  299. {"sock_send_signal", sock_error1, sock_error2, sock_error0},
  300. {"print_line", sock_error1, sock_error2, sock_error0},
  301. #endif
  302. {"plus_infinity", wrong_no_0a, wrong_no_0b, LplusInfinity},
  303. {"minus_infinity", wrong_no_0a, wrong_no_0b, LminusInfinity},
  304. {"NANQ", wrong_no_0a, wrong_no_0b, Lnanq},
  305. {NULL, 0, 0, 0}
  306. };