diff --git a/objects/applications/serverDemo/Makefile b/objects/applications/serverDemo/Makefile index 014d754..e17c27b 100644 --- a/objects/applications/serverDemo/Makefile +++ b/objects/applications/serverDemo/Makefile @@ -1,32 +1,17 @@ -# Sun-$Revision: 10.2 $ - -# Copyright 1992-2012 AUTHORS. -# See the LICENSE file for license information. - -# Make file for the foreign function (server) demo. -# An example application using dynamic linking. +ROOT=../../.. +CFLAGS=-DCOMPILER=GCC_COMPILER -DDYNLINK_SUPPORTED -DFAST_COMPILER -DGENERATE_DEBUGGING_AIDS=0 -DGLUE_CHECKSUM=0 -DHOST_ARCH=I386_ARCH -DNATIVE_ARCH=i386 -DQUARTZ_LIB -DSIC_COMPILER -DSPEND_TIME_FOR_DEBUGGING_BY_DEFAULT=0 -DTARGET_ARCH=I386_ARCH -DTARGET_IS_FOR_DEBUGGING=0 -DTARGET_IS_OPTIMIZED=1 -DTARGET_IS_PROFILED=0 -fno-threadsafe-statics -fvisibility=default -fkeep-inline-functions -fno-exceptions -fno-stack-protector -Wabi -Wreorder -Wreturn-type -Wswitch -Wcomment -Wformat -Wpointer-arith -Woverloaded-virtual -Wno-write-strings -Wparentheses -arch i386 -include $(ROOT)/incls/_precompiled.hh -Winvalid-pch -m32 -fpic +INCLUDE=-I$(ROOT)/vm/src/any/asm -I$(ROOT)/vm/src/any/fast_compiler -I$(ROOT)/vm/src/any/interpreter -I$(ROOT)/vm/src/any/lookup -I$(ROOT)/vm/src/any/memory -I$(ROOT)/vm/src/any/objects -I$(ROOT)/vm/src/any/os -I$(ROOT)/vm/src/any/parser -I$(ROOT)/vm/src/any/prims -I$(ROOT)/vm/src/any/runtime -I$(ROOT)/vm/src/any/sic -I$(ROOT)/vm/src/any/zone -I$(ROOT)/vm/src/i386/asm -I$(ROOT)/vm/src/i386/fast_compiler -I$(ROOT)/vm/src/i386/lookup -I$(ROOT)/vm/src/i386/memory -I$(ROOT)/vm/src/i386/prims -I$(ROOT)/vm/src/i386/runtime -I$(ROOT)/vm/src/i386/sic -I$(ROOT)/vm/src/i386/zone -I$(ROOT)/vm/src/unix/os -I$(ROOT)/vm/src/unix/prims -I$(ROOT)/vm/src/unix/runtime -I$(ROOT)/objects/glue -I/usr/local/opt/ncurses/include -I$(ROOT) -I$(ROOT)/incls +LFLAG= -dynamiclib -undefined suppress -flat_namespace -m32 +CXX=g++-4.2 -ROOT = ../../.. -FILES = Makefile -SELF_MODULES = server -C_MODULES = socks toself -include ${ROOT}/vm/templates/svr4.make -include ${ROOT}/vm/templates/optimized.make -include ${ROOT}/templates/sources.make +all: socks.dylib toself -OBJECT_FILES = socks.so toself +socks.o: socks.cc + $(CXX) $(CFLAGS) $(INCLUDE) -o $@ -c $? -NAME = socks -INCLUDES += -I${ROOT}/glueDefs +socks.dylib: socks.o + $(CXX) $(LFLAG) -o $@ $? -# Builds the dynamic library socks.so from socks.o using the default linker ld. -# Static constructors in the dynamic library is NOT working - -toself: toself.o - @echo Linking toself - @gcc -o toself toself.o $(EXTRA_LIBS) - -ifeq (${OS_VARIANT},SOLARIS) - EXTRA_LIBS = -lsocket -lnsl -endif +toself: toself.c + $(CXX) -o $@ $? diff --git a/objects/applications/serverDemo/server.self b/objects/applications/serverDemo/server.self index ff37e5a..d83fb1c 100644 --- a/objects/applications/serverDemo/server.self +++ b/objects/applications/serverDemo/server.self @@ -8,7 +8,7 @@ simple_accept_glue and shutdown_glue) to provide a server process that regular Unix processes can connect to to have arbitrary Self expressions evaluated. To try it out, do this: - 1. Make sure the path below for the socks.so file is correct. + 1. Make sure the path below for the applications/serverDemo/socks.dylib file is correct. 2. Read this file into Self, then type (at the Self prompt): [server start] fork 3. At the Unix prompt type: toself < server.self capitalize: stdio. @@ -18,15 +18,14 @@ (Restarting the server shortly after stopping it may cause an EADDRINUSE). Ole Agesen, Feb. 1992." - -traits applications _AddSlotsIfAbsent: ( | serverDemo* = () | ) -globals applications _AddSlotsIfAbsent: ( | serverDemo* = () | ) +traits _AddSlotsIfAbsent: ( | serverDemo* = () | ) +globals _AddSlotsIfAbsent: ( | serverDemo* = () | ) " This is how to get static initializer routines (no args, no return value) called whenever a file is loaded. Note: initializer names are dependant on what compiler generates the code, e.g. the g++ compiler puts the prefix __GLOBAL_$I$ on initializers. We need a better way to do this." -" foreignCodeDB at: '~/server/socks.so' PutInitializer: '__GLOBAL_$I$sild' " +" foreignCodeDB at: '~/server/applications/serverDemo/socks.dylib' PutInitializer: '__GLOBAL_$I$sild' " globals serverDemo _AddSlotsIfAbsent: ( | socksFct = () | ) @@ -49,7 +48,7 @@ traits socket _Define: ( | copyPort: portNumber = ( "Create socket, do bind, then listen." | sbl = foreignFct copyName: 'socket_bind_listen_glue' ArgCoercions: 'i ' ResultType: 'unknown' - Path: 'socks.so' + Path: 'applications/serverDemo/socks.dylib' . | sbl call: portNumber With: deadCopy IfFail: raiseError. ). @@ -57,10 +56,10 @@ ResultType: 'unknown' acceptIfFail: errBlk = ( "Return new unixFile object." | acc = foreignFct copyName: 'simple_accept_glue' ArgCoercions: ' ' ResultType: 'unknown' - Path: 'socks.so' + Path: 'applications/serverDemo/socks.dylib' . | acc call: self - With: (unixFile copyName: 'a socket') deadCopy + With: (os_file copyName: 'a socket') deadCopy IfFail: errBlk. ). @@ -92,7 +91,7 @@ server _Define: ( | of this object, with the stdio slot properly initialized." mother* = traits clonable. father* = lobby. - stdio <- unixFile copy. + stdio <- os_file copy. | ). @@ -116,7 +115,7 @@ server _Define: ( | continue: true. serverSocket: (socket copyPort: 1275). "HARDWIRED!" 'Server started.' printLine. - [continue] whileTrue: [| io <- unixFile. | + [continue] whileTrue: [| io <- os_file. | io: serverSocket acceptIfFail: [|:e| ('EINTR' isPrefixOf: e) || ('NOERR' isPrefixOf: e) ifFalse: [^ error: e]. @@ -151,7 +150,7 @@ traits unixFile _AddSlotsIfAbsent: ( | how: 0 = shut down receives, 1 = sends, 2 = both." | shut = foreignFct copyName: 'shutdown_glue' ArgCoercions: ' i' ResultType: 'unknown' - Path: 'socks.so' + Path: 'applications/serverDemo/socks.dylib' . | shut call: self With: how IfFail: raiseError. ). diff --git a/objects/applications/serverDemo/socks.cc b/objects/applications/serverDemo/socks.cc new file mode 100644 index 0000000..e5bb2c5 --- /dev/null +++ b/objects/applications/serverDemo/socks.cc @@ -0,0 +1,66 @@ +/* Sun-$Revision: 10.1 $ */ + +/* Copyright 1992-2012 AUTHORS. + See the LICENSE file for license information. */ + +# include +# include +# include +# include +# include + +# include "incls/_glueDefs.cpp.incl" + + +/* Why isn't this in a standard Unix header file? */ +extern "C" int shutdown(int socketFD, int how); + + +/* To test if static initializers are being called, include this. +struct fisk { + fisk() { printf("blob-blob\n"); } +} sild; +*/ + + +int socket_bind_listen(short port_no) { + int sock = socket(PF_INET, SOCK_STREAM, 0); + if (sock == -1) + return -1; /* Return error indication. */ + + struct sockaddr_in receiver; + receiver.sin_port = ntohs(port_no); + receiver.sin_family = AF_INET; + receiver.sin_addr.s_addr = INADDR_ANY; /* receive from anyone */ + /* the cast on the next line is to pacify the C++ compiler */ + if(bind(sock, (struct sockaddr *)&receiver, sizeof(receiver)) == -1) + return -1; /* Return error indication. */ + + if(listen(sock, 5) == -1) + return -1; /* Return error indication. */ + + return sock; +} + + +int simple_accept(int sock) { + struct sockaddr from; + socklen_t len = sizeof(from); + return accept(sock, &from, &len); +} + + + +extern char *UnixFile_seal; /* From unixPrims.h -- this is a hack! */ + +# define WHAT_GLUE FUNCTIONS + C_func_1(proxy_null,(int,UnixFile_seal), + socket_bind_listen, socket_bind_listen_glue,, short,) + + C_func_1( proxy_or_errno,(int,UnixFile_seal,-1), + simple_accept, simple_accept_glue,, + proxy_null,(int,UnixFile_seal)) + + C_func_2( int_or_errno,-1, shutdown, shutdown_glue,, + proxy_null,(int,UnixFile_seal), int,) +# undef WHAT_GLUE diff --git a/objects/applications/serverDemo/socks.so b/objects/applications/serverDemo/socks.so deleted file mode 100755 index fc6738e..0000000 Binary files a/objects/applications/serverDemo/socks.so and /dev/null differ diff --git a/objects/applications/serverDemo/toself b/objects/applications/serverDemo/toself deleted file mode 100755 index ebfc2cf..0000000 Binary files a/objects/applications/serverDemo/toself and /dev/null differ