Skip to content

Commit

Permalink
renovation
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Feb 6, 2012
1 parent 4ae7373 commit 0635495
Show file tree
Hide file tree
Showing 137 changed files with 5,796 additions and 377 deletions.
6 changes: 4 additions & 2 deletions blogs.tex
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ \subsection{ocaml blogs}
\href{http://llvm.org/docs/tutorial}{llvm}\\
\href{http://blog.incubaid.com}{incubaid}\\
\href{http://phonology.cogsci.udel.edu/~heinz/software/}{heniz}\\
\href{http://askra.de/software/memcheck/}{memcheck}

\href{http://askra.de/software/memcheck/}{memcheck} \\
\href{http://danmey.org/}{danmey}\\
\href{http://www.oiwa.jp/~yutaka/caml/index-en.html}{yutaka}\\
\href{http://www.st.cs.uni-saarland.de/~lindig/}{Lindig}\\
%%% Local Variables:
%%% mode: LaTex
%%% TeX-master: "master"
Expand Down
88 changes: 88 additions & 0 deletions books/code/unix/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
### Makefile ---

## Author: [email protected]
## Version: $Id: Makefile,v 0.0 2012/02/04 16:02:18 bobzhang1988 Exp $
## Keywords:
## X-URL:

## make -p -f /dev/null
## for predefining rules

## OCAMLLIBPATH := $(shell ocamlc -where)
## call shell command
### Makefile ends here

### OUT_OBJS=$(addprefix $(OUT_DIR)/,$(OBJS))
### $(OB) $(basename $@).inferred.mli
### cp $(LIB)/$(basename $@).inferred.mli $@

## call built-in Makefile command
# define a suffix rule for .c -> .o
# A good reference
# .c$(LIB)/*.o :
# test -d $(LIB) || mkdir $(LIB)
# $(CC) $(CFLAGS) -c $< -o $(LIB)/$@



.SUFFIXES:
# clear out all suffixes

## C Part
CC = gcc
OCAMLLIBPATH := $(shell ocamlc -where)
CFLAGS = -g -Wall -I $(OCAMLLIBPATH)
LD = $(CC)
LDFLAGS =
RM = rm
LIB := _build
SRCS :=


# list only those we use
.SUFFIXES: .o .c

.c.o :
$(CC) $(CFLAGS) -c $<


## OCAML PART
OCAML = ocaml
.SUFFIXES: .mli .ml .cmi .byte .native .cmo

OB := ocamlbuild
.ml.mli:
rm -f $@
$(OB) $(basename $@).inferred.mli
cp $(LIB)/$(basename $@).inferred.mli $@
%.native:
$(OB) $@
%.byte:
$(OB) $@
%.cmo:
$(OB) $@
.mli.cmi:
$(OB) $@
.ml.cmi:
$(OB) $@

## Common PART
clean :
-$(RM) -rf $(EXE) $(OBJS) $(LIB)/$(EXE) $(LIB)/$(OBJS) \
*.o *.cmo $(LBI)/*.o $(LIB)/*.cmo
$(OB) -clean

EXE :=
OBJS := ${SRCS:.c=.o}

all : $(EXE)
$(EXE) : $(OBJS)
$(LD) -o $@ $(OBJS)


# _build/single_write.o: single_write.o
# test -d $(LIB) || mkdir $(LIB)
# cp single_write.o $(LIB)
# # tag single_write.o precious
# write.cma: _build/single_write.o write.cmo
# cd $(LIB); ocamlc -custom -a -o single_write.o write.cmo
8 changes: 8 additions & 0 deletions books/code/unix/_tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
true : use_unix
true : pkg_batteries
<single_write.o> : precious
<util.{ml,native,byte}> : thread
<thread_*.{ml,native,byte}> : thread
# <minishell.ml> : pp(camlp4o -parser pa_mikmatch_pcre.cma)
# <minishell.{cmo,byte}> : pkg_pcre, pkg_mikmatch_pcre

53 changes: 53 additions & 0 deletions books/code/unix/client.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
open BatPervasives
open Printf
open Util



let client () = Unix.( begin
if Array.length Sys.argv < 3 then begin
prerr_endline "Usage: client <host> <port>";
exit 2
end ;
let server_name = Sys.argv.(1)
and port_number = int_of_string Sys.argv.(2) in
let server_addr =
try
(gethostbyname server_name).h_addr_list.(0)
with
Not_found -> begin
prerr_endline ^$ server_name ^ ": Host not found";
exit 2 ;
end in
let sock = socket PF_INET SOCK_STREAM 0 in
connect sock (ADDR_INET (server_addr, port_number));
match fork() with
| 0 -> begin
retransmit stdin sock;
shutdown sock SHUTDOWN_SEND;
exit 0 ;
end
|_ -> begin
retransmit sock stdout;
close stdout;
wait ();
end
end)

let _ = Unix.handle_unix_error client ()
















64 changes: 64 additions & 0 deletions books/code/unix/compose.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
open BatPervasives
open Printf
open Util

let compose () = Unix.(
let len = Array.length Sys.argv - 1 in begin
(* prerr_string ^$ sprintf "len %d\n" len ; *)
for i = 1 to len - 1 do
let fd_in, fd_out = pipe () in
match fork () with
| 0 -> begin
dup2 fd_out ^$ stdout;
close fd_out;
close fd_in;
execv "/bin/sh" [|"/bin/sh"; "-c"; Sys.argv.(i)|];
end
| pid -> begin
dup2 fd_in ^$ stdin; (** inherited later *)
close fd_out;
close fd_in;
end
done;
match fork () with
| 0 -> begin
execv "/bin/sh" [| "/bin/sh";"-c";Sys.argv.(len) |];
end
| pid -> begin
close stdout;
let rec wait_for_children retcode =
try begin
match wait () with
| (_, WEXITED n) -> wait_for_children (retcode lor n)
| (_,_) -> wait_for_children 127
end
with Unix_error(ECHILD,_,_) -> retcode in begin
(** close stdout; you can not close stdout here, since
your previous fork may be very slow, and does not
inherit it yet, and dup2 a closed stdout will emit an
error*)
exit (wait_for_children 0);
end
end
end
)

let _ = Unix.handle_unix_error compose ()


















36 changes: 36 additions & 0 deletions books/code/unix/fork_search.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

open BatPervasives

exception Found

let simple_find f arg s len =
try begin
for i = s to s + len - 1 do
if f arg.(i)
then raise Found
done ;
false;
end
with Found -> true

(** parent communicate child via exit code child process signal by the
exit code, and the parent process to check it out
*)
let fork_search f arg = Unix.(
let n = Array.length arg in
match fork () with
| 0 -> (** child *)
exit(
if simple_find f arg 0 (n/2)
then 0
else 1
)
| pid -> begin
let ret = simple_find f arg (n/2) ((n+1)/2) in
match wait () with
| _, WEXITED i -> ret || i == 0
| _, _ -> failwith "fork_search";
end
)


75 changes: 75 additions & 0 deletions books/code/unix/geturl.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
open BatPervasives
open Printf
open Util


let default_port = "80"

let host_regexp = {
regexp = Str.regexp "\\([^/:]*\\)\\(:\\([0-9]+\\)\\)?";
fields = [1,None; 3, Some default_port];
}

let url_regexp = {
regexp = Str.regexp "http://\\([^/:]*\\(:[0-9]+\\)?\\)\\(/.*\\)";
fields = [1,None; 3 , None];
}

let parse_host host = match regexp_match host_regexp host with
| Some (host::port::_) -> host, int_of_string port
| _ -> error host "Ill format host"

(**
parse_url "http://www.google.com:190/";;
- : (string * int) * string = (("www.google.com", 190), "/")
*)
let parse_url url = match regexp_match url_regexp url with
| Some (host::path::_) -> parse_host host, path
| _ -> error url "Ill formed url"

let send_get url sock = Unix.(
let s = sprintf "GET %s\r\n" url in
prerr_endline s ;
ignore ^$ write sock s 0 ^$ String.length s
)

(**
it first try proxy,
if there exists a proxy
then it sends the url to the proxy
else it sends the root to the url host name
*)
let get_url proxy url fdout = Unix.(
let (hostname, port), path = match proxy with
| None -> parse_url url
| Some host -> parse_host host,url in
let hostaddr =
try inet_addr_of_string hostname
with Failure _ ->
try (gethostbyname hostname).h_addr_list.(0)
with Not_found -> error hostname "Host not found" in
let sock = socket PF_INET SOCK_STREAM 0 in
finally begin fun _ -> close sock end begin fun _ ->
connect sock (ADDR_INET (hostaddr,port));
send_get path sock;
retransmit sock fdout;
end ()
)

let geturl () : unit =
let len = Array.length Sys.argv in
if len < 2
then error "geturl: Usage:" ^$ Sys.argv.(0) ^ " [ proxy [:<port>] ] <url>"
else
let proxy,url =
if len > 2
then Some Sys.argv.(1), Sys.argv.(2)
else None, Sys.argv.(1)
in get_url proxy url Unix.stdout

(*
when your module has side effects, other module who invoke your library
will be polluted as well, so next time be careful!!!
let _ = Unix.handle_unix_error (handle_error geturl) ()
*)

34 changes: 34 additions & 0 deletions books/code/unix/grepi.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
open BatPervasives

let grep () = Unix.(
execvp "grep"
(Array.append
[|"grep"; "-i"|]
(Array.sub Sys.argv 1 (Array.length Sys.argv - 1))
)

)

let _ =
Unix.handle_unix_error grep ()





















Loading

0 comments on commit 0635495

Please sign in to comment.