Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/compiler/PervEnv/Sockets/pre-sock.sml
ViewVC logotype

Annotation of /sml/trunk/src/compiler/PervEnv/Sockets/pre-sock.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 113 - (view) (download)
Original Path: sml/branches/SMLNJ/src/compiler/PervEnv/Sockets/pre-sock.sml

1 : monnier 16 (* pre-sock.sml
2 :     *
3 :     * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
4 :     *
5 :     * These are some common type definitions used in the sockets library. This
6 :     * structure is called Sock, so that the signatures will compile.
7 :     *
8 :     *)
9 :    
10 :     structure PreSock =
11 :     struct
12 :    
13 :     (* the raw representation address data *)
14 :     type addr = Word8Vector.vector
15 :    
16 :     (* the raw representation of an address family *)
17 :     type af = CInterface.system_const
18 :    
19 :     (* the raw representation of a socket (a file descriptor for now) *)
20 :     type socket = int
21 :    
22 :     (* an internet address; this is here because it is abstract in the
23 :     * NetHostDB and IP structures.
24 :     *)
25 :     datatype in_addr = INADDR of addr
26 :    
27 :     (* an address family *)
28 :     datatype addr_family = AF of af
29 :    
30 :     (* socket types *)
31 :     datatype sock_type = SOCKTY of CInterface.system_const
32 :    
33 :     (* sockets are polymorphic; the instantiation of the type variables
34 :     * provides a way to distinguish between different kinds of sockets.
35 :     *)
36 :     datatype ('sock, 'af) sock = SOCK of socket
37 :     datatype 'af sock_addr = ADDR of addr
38 :    
39 :     (** Utility functions for parsing/unparsing network addresses **)
40 :     local
41 :     structure SysW = SysWord
42 :     structure SCvt = StringCvt
43 :     fun toW (getc, strm) = let
44 :     fun scan radix strm = (case (SysW.scan radix getc strm)
45 :     of NONE => NONE
46 :     | (SOME(w, strm)) => SOME(w, strm)
47 :     (* end case *))
48 :     in
49 :     case (getc strm)
50 :     of NONE => NONE
51 :     | (SOME(#"0", strm')) => (case (getc strm')
52 :     of NONE => SOME(0w0, strm')
53 :     | (SOME((#"x" | #"X"), strm'')) => scan SCvt.HEX strm''
54 :     | _ => scan SCvt.OCT strm
55 :     (* end case *))
56 :     | _ => scan SCvt.DEC strm
57 :     (* end case *)
58 :     end
59 :     (* check that the word is representable in the given number of bits; raise
60 :     * Overflow if not.
61 :     *)
62 :     fun chk (w, bits) =
63 :     if (SysW.>= (SysW.>>(0wxffffffff, Word.-(0w32, bits)), w))
64 :     then w
65 :     else raise General.Overflow
66 :     (* Scan a sequence of numbers separated by #"." *)
67 :     fun scan getc strm = (case toW (getc, strm)
68 :     of NONE => NONE
69 :     | SOME(w, strm') => scanRest getc ([w], strm')
70 :     (* end case *))
71 :     and scanRest getc (l, strm) = (case getc strm
72 :     of SOME(#".", strm') => (case toW (getc, strm')
73 :     of NONE => SOME(List.rev l, strm)
74 :     | SOME(w, strm'') => scanRest getc (w::l, strm'')
75 :     (* end case *))
76 :     | _ => SOME(List.rev l, strm)
77 :     (* end case *))
78 :     in
79 :     fun toWords getc strm = (case (scan getc strm)
80 :     of SOME([a, b, c, d], strm) =>
81 :     SOME([chk(a, 0w8), chk(b, 0w8), chk(c, 0w8), chk(d, 0w8)], strm)
82 :     | SOME([a, b, c], strm) =>
83 :     SOME([chk(a, 0w8), chk(b, 0w8), chk(c, 0w16)], strm)
84 :     | SOME([a, b], strm) =>
85 :     SOME([chk(a, 0w8), chk(b, 0w24)], strm)
86 :     | SOME([a], strm) =>
87 :     SOME([chk(a, 0w32)], strm)
88 :     | _ => NONE
89 :     (* end case *))
90 :     fun fromBytes (a, b, c, d) = let
91 :     val fmt = Word8.fmt StringCvt.DEC
92 :     in
93 :     concat [fmt a, ".", fmt b, ".", fmt c, ".", fmt d]
94 :     end
95 :     end
96 :    
97 :     end; (* PreSock *)
98 :    
99 :     (* We alias this structure to Socket so that the signature files will compile.
100 :     * We also need to keep the PreSock structure visible, so that structures
101 :     * compiled after the real Sock structure still have access to the representation
102 :     * types.
103 :     *)
104 :     structure Socket = PreSock;
105 :    
106 :    
107 :     (*
108 : monnier 113 * $Log$
109 : monnier 16 *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0