annotate src/compiler.sml @ 793:3e5d1c6ae30c

Stop potential buffer overrun of path_copy
author Adam Chlipala <adamc@hcoop.net>
date Tue, 12 May 2009 20:15:11 -0400
parents d20d6afc1206
children dc3fc3f3b834
rev   line source
adamc@764 1 (* Copyright (c) 2008-2009, Adam Chlipala
adamc@1 2 * All rights reserved.
adamc@1 3 *
adamc@1 4 * Redistribution and use in source and binary forms, with or without
adamc@1 5 * modification, are permitted provided that the following conditions are met:
adamc@1 6 *
adamc@1 7 * - Redistributions of source code must retain the above copyright notice,
adamc@1 8 * this list of conditions and the following disclaimer.
adamc@1 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@1 10 * this list of conditions and the following disclaimer in the documentation
adamc@1 11 * and/or other materials provided with the distribution.
adamc@1 12 * - The names of contributors may not be used to endorse or promote products
adamc@1 13 * derived from this software without specific prior written permission.
adamc@1 14 *
adamc@1 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@1 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@1 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@1 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@1 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@1 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@1 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@1 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@1 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@1 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@1 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@1 26 *)
adamc@1 27
adamc@1 28 structure Compiler :> COMPILER = struct
adamc@1 29
adamc@244 30 structure UrwebLrVals = UrwebLrValsFn(structure Token = LrParser.Token)
adamc@244 31 structure Lex = UrwebLexFn(structure Tokens = UrwebLrVals.Tokens)
adamc@244 32 structure UrwebP = Join(structure ParserData = UrwebLrVals.ParserData
adamc@1 33 structure Lex = Lex
adamc@1 34 structure LrParser = LrParser)
adamc@1 35
adamc@270 36 type job = {
adamc@385 37 prefix : string,
adamc@270 38 database : string option,
adamc@274 39 sources : string list,
adamc@274 40 exe : string,
adamc@274 41 sql : string option,
adamc@502 42 debug : bool,
adamc@673 43 profile : bool,
adamc@764 44 timeout : int,
adamc@764 45 ffi : string list,
adamc@764 46 link : string list,
adamc@765 47 headers : string list,
adamc@766 48 scripts : string list,
adamc@765 49 clientToServer : Settings.ffi list,
adamc@765 50 effectful : Settings.ffi list,
adamc@765 51 clientOnly : Settings.ffi list,
adamc@765 52 serverOnly : Settings.ffi list,
adamc@768 53 jsFuncs : (Settings.ffi * string) list,
adamc@769 54 rewrites : Settings.rewrite list,
adamc@769 55 filterUrl : Settings.rule list,
adamc@769 56 filterMime : Settings.rule list
adamc@270 57 }
adamc@201 58
adamc@201 59 type ('src, 'dst) phase = {
adamc@201 60 func : 'src -> 'dst,
adamc@201 61 print : 'dst -> Print.PD.pp_desc
adamc@201 62 }
adamc@201 63
adamc@201 64 type pmap = (string * Time.time) list
adamc@201 65
adamc@201 66 type ('src, 'dst) transform = {
adamc@201 67 func : 'src -> 'dst option,
adamc@201 68 print : 'dst -> Print.PD.pp_desc,
adamc@201 69 time : 'src * pmap -> 'dst option * pmap
adamc@201 70 }
adamc@201 71
adamc@201 72 fun transform (ph : ('src, 'dst) phase) name = {
adamc@201 73 func = fn input => let
adamc@201 74 val v = #func ph input
adamc@201 75 in
adamc@201 76 if ErrorMsg.anyErrors () then
adamc@201 77 NONE
adamc@201 78 else
adamc@201 79 SOME v
adamc@201 80 end,
adamc@201 81 print = #print ph,
adamc@201 82 time = fn (input, pmap) => let
adamc@201 83 val befor = Time.now ()
adamc@201 84 val v = #func ph input
adamc@201 85 val elapsed = Time.- (Time.now (), befor)
adamc@201 86 in
adamc@201 87 (if ErrorMsg.anyErrors () then
adamc@201 88 NONE
adamc@201 89 else
adamc@201 90 SOME v,
adamc@201 91 (name, elapsed) :: pmap)
adamc@201 92 end
adamc@201 93 }
adamc@201 94
adamc@270 95 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
adamc@201 96 func = fn input => case #func tr1 input of
adamc@201 97 NONE => NONE
adamc@201 98 | SOME v => #func tr2 v,
adamc@201 99 print = #print tr2,
adamc@201 100 time = fn (input, pmap) => let
adamc@201 101 val (ro, pmap) = #time tr1 (input, pmap)
adamc@201 102 in
adamc@201 103 case ro of
adamc@201 104 NONE => (NONE, pmap)
adamc@201 105 | SOME v => #time tr2 (v, pmap)
adamc@201 106 end
adamc@201 107 }
adamc@201 108
adamc@346 109 fun check (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@346 110 ignore (#func tr x))
adamc@346 111
adamc@280 112 fun run (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@280 113 #func tr x)
adamc@201 114
adamc@201 115 fun runPrint (tr : ('src, 'dst) transform) input =
adamc@280 116 (ErrorMsg.resetErrors ();
adamc@280 117 case #func tr input of
adamc@280 118 NONE => print "Failure\n"
adamc@280 119 | SOME v =>
adamc@280 120 (print "Success\n";
adamc@280 121 Print.print (#print tr v);
adamc@280 122 print "\n"))
adamc@201 123
adamc@201 124 fun time (tr : ('src, 'dst) transform) input =
adamc@55 125 let
adamc@201 126 val (_, pmap) = #time tr (input, [])
adamc@201 127 in
adamc@201 128 app (fn (name, time) =>
adamc@201 129 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 130 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 131 print "\n"
adamc@201 132 end
adamc@55 133
adamc@201 134 fun timePrint (tr : ('src, 'dst) transform) input =
adamc@201 135 let
adamc@201 136 val (ro, pmap) = #time tr (input, [])
adamc@55 137 in
adamc@201 138 app (fn (name, time) =>
adamc@201 139 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 140 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 141 print "\n";
adamc@201 142 case ro of
adamc@201 143 NONE => print "Failure\n"
adamc@201 144 | SOME v =>
adamc@201 145 (print "Success\n";
adamc@201 146 Print.print (#print tr v);
adamc@201 147 print "\n")
adamc@55 148 end
adamc@55 149
adamc@244 150 val parseUrs =
adamc@201 151 {func = fn filename => let
adamc@201 152 val fname = OS.FileSys.tmpName ()
adamc@201 153 val outf = TextIO.openOut fname
adamc@201 154 val () = TextIO.output (outf, "sig\n")
adamc@201 155 val inf = TextIO.openIn filename
adamc@201 156 fun loop () =
adamc@201 157 case TextIO.inputLine inf of
adamc@201 158 NONE => ()
adamc@201 159 | SOME line => (TextIO.output (outf, line);
adamc@201 160 loop ())
adamc@201 161 val () = loop ()
adamc@201 162 val () = TextIO.closeIn inf
adamc@201 163 val () = TextIO.closeOut outf
adamc@201 164
adamc@201 165 val () = (ErrorMsg.resetErrors ();
adamc@201 166 ErrorMsg.resetPositioning filename;
adamc@201 167 Lex.UserDeclarations.initialize ())
adamc@201 168 val file = TextIO.openIn fname
adamc@201 169 fun get _ = TextIO.input file
adamc@201 170 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 171 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 172 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 173 in
adamc@201 174 TextIO.closeIn file;
adamc@201 175 case absyn of
adamc@201 176 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => sgis
adamc@201 177 | _ => (ErrorMsg.errorAt {file = filename,
adamc@201 178 first = {line = 0,
adamc@201 179 char = 0},
adamc@201 180 last = {line = 0,
adamc@201 181 char = 0}} "Not a signature";
adamc@201 182 [])
adamc@201 183 end
adamc@201 184 handle LrParser.ParseError => [],
adamc@201 185 print = Print.p_list_sep Print.PD.newline SourcePrint.p_sgn_item}
adamc@55 186
adamc@1 187 (* The main parsing routine *)
adamc@244 188 val parseUr = {
adamc@201 189 func = fn filename =>
adamc@201 190 let
adamc@201 191 val () = (ErrorMsg.resetErrors ();
adamc@201 192 ErrorMsg.resetPositioning filename;
adamc@201 193 Lex.UserDeclarations.initialize ())
adamc@201 194 val file = TextIO.openIn filename
adamc@201 195 fun get _ = TextIO.input file
adamc@201 196 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 197 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 198 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 199 in
adamc@201 200 TextIO.closeIn file;
adamc@201 201 case absyn of
adamc@201 202 [(Source.DSgn ("?", _), _)] =>
adamc@201 203 (ErrorMsg.errorAt {file = filename,
adamc@201 204 first = {line = 0,
adamc@201 205 char = 0},
adamc@201 206 last = {line = 0,
adamc@201 207 char = 0}} "File starts with 'sig'";
adamc@201 208 [])
adamc@201 209 | _ => absyn
adamc@201 210 end
adamc@201 211 handle LrParser.ParseError => [],
adamc@201 212 print = SourcePrint.p_file}
adamc@56 213
adamc@768 214 fun p_job ({prefix, database, exe, sql, sources, debug, profile,
adamc@768 215 timeout, ffi, link, headers, scripts,
adamc@768 216 clientToServer, effectful, clientOnly, serverOnly, jsFuncs, ...} : job) =
adamc@270 217 let
adamc@270 218 open Print.PD
adamc@270 219 open Print
adamc@765 220
adamc@765 221 fun p_ffi name = p_list_sep (box []) (fn (m, s) =>
adamc@765 222 box [string name, space, string m, string ".", string s, newline])
adamc@270 223 in
adamc@274 224 box [if debug then
adamc@274 225 box [string "DEBUG", newline]
adamc@274 226 else
adamc@274 227 box [],
adamc@502 228 if profile then
adamc@502 229 box [string "PROFILE", newline]
adamc@502 230 else
adamc@502 231 box [],
adamc@274 232 case database of
adamc@270 233 NONE => string "No database."
adamc@270 234 | SOME db => string ("Database: " ^ db),
adamc@270 235 newline,
adamc@274 236 string "Exe: ",
adamc@274 237 string exe,
adamc@274 238 newline,
adamc@274 239 case sql of
adamc@274 240 NONE => string "No SQL file."
adamc@274 241 | SOME sql => string ("SQL fle: " ^ sql),
adamc@673 242 newline,
adamc@673 243 string "Timeout: ",
adamc@673 244 string (Int.toString timeout),
adamc@673 245 newline,
adamc@764 246 p_list_sep (box []) (fn s => box [string "Ffi", space, string s, newline]) ffi,
adamc@764 247 p_list_sep (box []) (fn s => box [string "Header", space, string s, newline]) headers,
adamc@766 248 p_list_sep (box []) (fn s => box [string "Script", space, string s, newline]) scripts,
adamc@764 249 p_list_sep (box []) (fn s => box [string "Link", space, string s, newline]) link,
adamc@765 250 p_ffi "ClientToServer" clientToServer,
adamc@765 251 p_ffi "Effectful" effectful,
adamc@765 252 p_ffi "ClientOnly" clientOnly,
adamc@765 253 p_ffi "ServerOnly" serverOnly,
adamc@765 254 p_list_sep (box []) (fn ((m, s), s') =>
adamc@765 255 box [string "JsFunc", space, string m, string ".", string s,
adamc@765 256 space, string "=", space, string s', newline]) jsFuncs,
adamc@270 257 string "Sources:",
adamc@270 258 p_list string sources,
adamc@270 259 newline]
adamc@270 260 end
adamc@270 261
adamc@270 262 fun trim s =
adamc@270 263 let
adamc@270 264 val (_, s) = Substring.splitl Char.isSpace s
adamc@270 265 val (s, _) = Substring.splitr Char.isSpace s
adamc@270 266 in
adamc@270 267 s
adamc@270 268 end
adamc@270 269
adamc@767 270 fun parseUrp' filename =
adamc@767 271 let
adamc@767 272 val dir = OS.Path.dir filename
adamc@767 273 val inf = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
adamc@767 274
adamc@767 275 fun relify fname =
adamc@767 276 OS.Path.concat (dir, fname)
adamc@767 277 handle OS.Path.Path => fname
adamc@767 278
adamc@767 279 val absDir = OS.Path.mkAbsolute {path = dir, relativeTo = OS.FileSys.getDir ()}
adamc@767 280
adamc@767 281 fun relifyA fname = OS.Path.mkAbsolute {path = fname, relativeTo = absDir}
adamc@767 282
adamc@767 283 fun readSources acc =
adamc@767 284 case TextIO.inputLine inf of
adamc@767 285 NONE => rev acc
adamc@767 286 | SOME line =>
adamc@767 287 let
adamc@767 288 val acc = if CharVector.all Char.isSpace line then
adamc@767 289 acc
adamc@767 290 else
adamc@767 291 let
adamc@767 292 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
adamc@767 293 (String.explode line))
adamc@767 294 val fname = relify fname
adamc@767 295 in
adamc@767 296 fname :: acc
adamc@767 297 end
adamc@767 298 in
adamc@767 299 readSources acc
adamc@767 300 end
adamc@767 301
adamc@767 302 val prefix = ref NONE
adamc@767 303 val database = ref NONE
adamc@767 304 val exe = ref NONE
adamc@767 305 val sql = ref NONE
adamc@767 306 val debug = ref false
adamc@767 307 val profile = ref false
adamc@767 308 val timeout = ref NONE
adamc@767 309 val ffi = ref []
adamc@767 310 val link = ref []
adamc@767 311 val headers = ref []
adamc@767 312 val scripts = ref []
adamc@767 313 val clientToServer = ref []
adamc@767 314 val effectful = ref []
adamc@767 315 val clientOnly = ref []
adamc@767 316 val serverOnly = ref []
adamc@767 317 val jsFuncs = ref []
adamc@768 318 val rewrites = ref []
adamc@769 319 val url = ref []
adamc@769 320 val mime = ref []
adamc@767 321 val libs = ref []
adamc@767 322
adamc@767 323 fun finish sources =
adamc@767 324 let
adamc@767 325 val job = {
adamc@767 326 prefix = Option.getOpt (!prefix, "/"),
adamc@767 327 database = !database,
adamc@767 328 exe = Option.getOpt (!exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
adamc@767 329 ext = SOME "exe"}),
adamc@767 330 sql = !sql,
adamc@767 331 debug = !debug,
adamc@767 332 profile = !profile,
adamc@767 333 timeout = Option.getOpt (!timeout, 60),
adamc@767 334 ffi = rev (!ffi),
adamc@767 335 link = rev (!link),
adamc@767 336 headers = rev (!headers),
adamc@767 337 scripts = rev (!scripts),
adamc@767 338 clientToServer = rev (!clientToServer),
adamc@767 339 effectful = rev (!effectful),
adamc@767 340 clientOnly = rev (!clientOnly),
adamc@767 341 serverOnly = rev (!serverOnly),
adamc@767 342 jsFuncs = rev (!jsFuncs),
adamc@768 343 rewrites = rev (!rewrites),
adamc@769 344 filterUrl = rev (!url),
adamc@769 345 filterMime = rev (!mime),
adamc@767 346 sources = sources
adamc@767 347 }
adamc@767 348
adamc@767 349 fun mergeO f (old, new) =
adamc@767 350 case (old, new) of
adamc@767 351 (NONE, _) => new
adamc@767 352 | (_, NONE) => old
adamc@767 353 | (SOME v1, SOME v2) => SOME (f (v1, v2))
adamc@767 354
adamc@767 355 fun same desc = mergeO (fn (x : string, y) =>
adamc@767 356 (if x = y then
adamc@767 357 ()
adamc@767 358 else
adamc@767 359 ErrorMsg.error ("Multiple "
adamc@767 360 ^ desc ^ " values that don't agree");
adamc@767 361 x))
adamc@767 362
adamc@767 363 fun merge (old : job, new : job) = {
adamc@767 364 prefix = #prefix old,
adamc@767 365 database = #database old,
adamc@767 366 exe = #exe old,
adamc@767 367 sql = #sql old,
adamc@767 368 debug = #debug old orelse #debug new,
adamc@767 369 profile = #profile old orelse #profile new,
adamc@767 370 timeout = #timeout old,
adamc@767 371 ffi = #ffi old @ #ffi new,
adamc@767 372 link = #link old @ #link new,
adamc@767 373 headers = #headers old @ #headers new,
adamc@767 374 scripts = #scripts old @ #scripts new,
adamc@767 375 clientToServer = #clientToServer old @ #clientToServer new,
adamc@767 376 effectful = #effectful old @ #effectful new,
adamc@767 377 clientOnly = #clientOnly old @ #clientOnly new,
adamc@767 378 serverOnly = #serverOnly old @ #serverOnly new,
adamc@767 379 jsFuncs = #jsFuncs old @ #jsFuncs new,
adamc@768 380 rewrites = #rewrites old @ #rewrites new,
adamc@769 381 filterUrl = #filterUrl old @ #filterUrl new,
adamc@769 382 filterMime = #filterMime old @ #filterMime new,
adamc@792 383 sources = #sources new @ #sources old
adamc@767 384 }
adamc@767 385 in
adamc@767 386 foldr (fn (fname, job) => merge (job, parseUrp' fname)) job (!libs)
adamc@767 387 end
adamc@767 388
adamc@768 389 fun parsePkind s =
adamc@768 390 case s of
adamc@768 391 "all" => Settings.Any
adamc@768 392 | "url" => Settings.Url
adamc@768 393 | "table" => Settings.Table
adamc@768 394 | "sequence" => Settings.Sequence
adamc@768 395 | "view" => Settings.View
adamc@768 396 | "relation" => Settings.Relation
adamc@768 397 | "cookie" => Settings.Cookie
adamc@768 398 | "style" => Settings.Style
adamc@768 399 | _ => (ErrorMsg.error "Bad path kind spec";
adamc@768 400 Settings.Any)
adamc@768 401
adamc@768 402 fun parseFrom s =
adamc@768 403 if size s > 1 andalso String.sub (s, size s - 2) = #"/" andalso String.sub (s, size s - 1) = #"*" then
adamc@768 404 (Settings.Prefix, String.substring (s, 0, size s - 1))
adamc@768 405 else
adamc@768 406 (Settings.Exact, s)
adamc@768 407
adamc@769 408 fun parseFkind s =
adamc@769 409 case s of
adamc@769 410 "url" => url
adamc@769 411 | "mime" => mime
adamc@769 412 | _ => (ErrorMsg.error "Bad filter kind";
adamc@769 413 url)
adamc@769 414
adamc@769 415 fun parsePattern s =
adamc@769 416 if size s > 0 andalso String.sub (s, size s - 1) = #"*" then
adamc@769 417 (Settings.Prefix, String.substring (s, 0, size s - 1))
adamc@769 418 else
adamc@769 419 (Settings.Exact, s)
adamc@769 420
adamc@767 421 fun read () =
adamc@767 422 case TextIO.inputLine inf of
adamc@767 423 NONE => finish []
adamc@767 424 | SOME "\n" => finish (readSources [])
adamc@767 425 | SOME line =>
adamc@767 426 let
adamc@767 427 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adamc@767 428 val cmd = Substring.string (trim cmd)
adamc@767 429 val arg = Substring.string (trim arg)
adamc@767 430
adamc@767 431 fun ffiS () =
adamc@767 432 case String.fields (fn ch => ch = #".") arg of
adamc@767 433 [m, x] => (m, x)
adamc@767 434 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func");
adamc@767 435 ("", ""))
adamc@767 436
adamc@767 437 fun ffiM () =
adamc@767 438 case String.fields (fn ch => ch = #"=") arg of
adamc@767 439 [f, s] =>
adamc@767 440 (case String.fields (fn ch => ch = #".") f of
adamc@767 441 [m, x] => ((m, x), s)
adamc@767 442 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adamc@767 443 (("", ""), "")))
adamc@767 444 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adamc@767 445 (("", ""), ""))
adamc@767 446 in
adamc@767 447 case cmd of
adamc@767 448 "prefix" =>
adamc@767 449 (case !prefix of
adamc@767 450 NONE => ()
adamc@767 451 | SOME _ => ErrorMsg.error "Duplicate 'prefix' directive";
adamc@767 452 prefix := SOME arg)
adamc@767 453 | "database" =>
adamc@767 454 (case !database of
adamc@767 455 NONE => ()
adamc@767 456 | SOME _ => ErrorMsg.error "Duplicate 'database' directive";
adamc@767 457 database := SOME arg)
adamc@767 458 | "exe" =>
adamc@767 459 (case !exe of
adamc@767 460 NONE => ()
adamc@767 461 | SOME _ => ErrorMsg.error "Duplicate 'exe' directive";
adamc@767 462 exe := SOME (relify arg))
adamc@767 463 | "sql" =>
adamc@767 464 (case !sql of
adamc@767 465 NONE => ()
adamc@767 466 | SOME _ => ErrorMsg.error "Duplicate 'sql' directive";
adamc@767 467 sql := SOME (relify arg))
adamc@767 468 | "debug" => debug := true
adamc@767 469 | "profile" => profile := true
adamc@767 470 | "timeout" =>
adamc@767 471 (case !timeout of
adamc@767 472 NONE => ()
adamc@767 473 | SOME _ => ErrorMsg.error "Duplicate 'timeout' directive";
adamc@767 474 timeout := SOME (valOf (Int.fromString arg)))
adamc@767 475 | "ffi" => ffi := relify arg :: !ffi
adamc@767 476 | "link" => link := relifyA arg :: !link
adamc@767 477 | "include" => headers := relifyA arg :: !headers
adamc@767 478 | "script" => scripts := arg :: !scripts
adamc@767 479 | "clientToServer" => clientToServer := ffiS () :: !clientToServer
adamc@767 480 | "effectful" => effectful := ffiS () :: !effectful
adamc@767 481 | "clientOnly" => clientOnly := ffiS () :: !clientOnly
adamc@767 482 | "serverOnly" => serverOnly := ffiS () :: !serverOnly
adamc@767 483 | "jsFunc" => jsFuncs := ffiM () :: !jsFuncs
adamc@768 484 | "rewrite" =>
adamc@768 485 let
adamc@768 486 fun doit (pkind, from, to) =
adamc@768 487 let
adamc@768 488 val pkind = parsePkind pkind
adamc@768 489 val (kind, from) = parseFrom from
adamc@768 490 in
adamc@768 491 rewrites := {pkind = pkind, kind = kind, from = from, to = to} :: !rewrites
adamc@768 492 end
adamc@768 493 in
adamc@768 494 case String.tokens Char.isSpace arg of
adamc@768 495 [pkind, from, to] => doit (pkind, from, to)
adamc@768 496 | [pkind, from] => doit (pkind, from, "")
adamc@768 497 | _ => ErrorMsg.error "Bad 'rewrite' syntax"
adamc@768 498 end
adamc@769 499 | "allow" =>
adamc@769 500 (case String.tokens Char.isSpace arg of
adamc@769 501 [fkind, pattern] =>
adamc@769 502 let
adamc@769 503 val fkind = parseFkind fkind
adamc@769 504 val (kind, pattern) = parsePattern pattern
adamc@769 505 in
adamc@769 506 fkind := {action = Settings.Allow, kind = kind, pattern = pattern} :: !fkind
adamc@769 507 end
adamc@769 508 | _ => ErrorMsg.error "Bad 'allow' syntax")
adamc@769 509 | "deny" =>
adamc@769 510 (case String.tokens Char.isSpace arg of
adamc@769 511 [fkind, pattern] =>
adamc@769 512 let
adamc@769 513 val fkind = parseFkind fkind
adamc@769 514 val (kind, pattern) = parsePattern pattern
adamc@769 515 in
adamc@769 516 fkind := {action = Settings.Deny, kind = kind, pattern = pattern} :: !fkind
adamc@769 517 end
adamc@769 518 | _ => ErrorMsg.error "Bad 'deny' syntax")
adamc@767 519 | "library" => libs := relify arg :: !libs
adamc@767 520 | _ => ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adamc@767 521 read ()
adamc@767 522 end
adamc@767 523
adamc@767 524 val job = read ()
adamc@767 525 in
adamc@767 526 TextIO.closeIn inf;
adamc@767 527 Settings.setUrlPrefix (#prefix job);
adamc@767 528 Settings.setTimeout (#timeout job);
adamc@767 529 Settings.setHeaders (#headers job);
adamc@767 530 Settings.setScripts (#scripts job);
adamc@767 531 Settings.setClientToServer (#clientToServer job);
adamc@767 532 Settings.setEffectful (#effectful job);
adamc@767 533 Settings.setClientOnly (#clientOnly job);
adamc@767 534 Settings.setServerOnly (#serverOnly job);
adamc@767 535 Settings.setJsFuncs (#jsFuncs job);
adamc@768 536 Settings.setRewriteRules (#rewrites job);
adamc@769 537 Settings.setUrlRules (#filterUrl job);
adamc@769 538 Settings.setMimeRules (#filterMime job);
adamc@767 539 job
adamc@767 540 end
adamc@767 541
adamc@270 542 val parseUrp = {
adamc@767 543 func = parseUrp',
adamc@270 544 print = p_job
adamc@270 545 }
adamc@270 546
adamc@270 547 val toParseJob = transform parseUrp "parseJob"
adamc@270 548
adamc@56 549 fun capitalize "" = ""
adamc@56 550 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@56 551
adamc@201 552 val parse = {
adamc@764 553 func = fn {database, sources = fnames, ffi, ...} : job =>
adamc@201 554 let
adamc@201 555 fun nameOf fname = capitalize (OS.Path.file fname)
adamc@109 556
adamc@764 557 fun parseFfi fname =
adamc@764 558 let
adamc@764 559 val mname = nameOf fname
adamc@764 560 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@764 561
adamc@764 562 val loc = {file = urs,
adamc@764 563 first = ErrorMsg.dummyPos,
adamc@764 564 last = ErrorMsg.dummyPos}
adamc@764 565
adamc@764 566 val sgn = (Source.SgnConst (#func parseUrs urs), loc)
adamc@764 567 in
adamc@764 568 (Source.DFfiStr (mname, sgn), loc)
adamc@764 569 end
adamc@764 570
adamc@201 571 fun parseOne fname =
adamc@201 572 let
adamc@201 573 val mname = nameOf fname
adamc@244 574 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 575 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 576
adamc@201 577 val sgnO =
adamc@244 578 if Posix.FileSys.access (urs, []) then
adamc@244 579 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 580 {file = urs,
adamc@201 581 first = ErrorMsg.dummyPos,
adamc@201 582 last = ErrorMsg.dummyPos})
adamc@201 583 else
adamc@201 584 NONE
adamc@56 585
adamc@244 586 val loc = {file = ur,
adamc@201 587 first = ErrorMsg.dummyPos,
adamc@201 588 last = ErrorMsg.dummyPos}
adamc@56 589
adamc@244 590 val ds = #func parseUr ur
adamc@201 591 in
adamc@201 592 (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@201 593 end
adamc@56 594
adamc@764 595 val dsFfi = map parseFfi ffi
adamc@201 596 val ds = map parseOne fnames
adamc@201 597 in
adamc@201 598 let
adamc@201 599 val final = nameOf (List.last fnames)
adamc@271 600
adamc@764 601 val ds = dsFfi @ ds
adamc@764 602 @ [(Source.DExport (Source.StrVar final, ErrorMsg.dummySpan), ErrorMsg.dummySpan)]
adamc@201 603 in
adamc@271 604 case database of
adamc@271 605 NONE => ds
adamc@271 606 | SOME s => (Source.DDatabase s, ErrorMsg.dummySpan) :: ds
adamc@201 607 end handle Empty => ds
adamc@201 608 end,
adamc@201 609 print = SourcePrint.p_file
adamc@201 610 }
adamc@56 611
adamc@270 612 val toParse = transform parse "parse" o toParseJob
adamc@38 613
adamc@378 614 fun libFile s = OS.Path.joinDirFile {dir = Config.libUr,
adamc@378 615 file = s}
adamc@378 616 fun clibFile s = OS.Path.joinDirFile {dir = Config.libC,
adamc@378 617 file = s}
adamc@378 618
adamc@201 619 val elaborate = {
adamc@201 620 func = fn file => let
adamc@378 621 val basis = #func parseUrs (libFile "basis.urs")
adamc@378 622 val topSgn = #func parseUrs (libFile "top.urs")
adamc@378 623 val topStr = #func parseUr (libFile "top.ur")
adamc@201 624 in
adamc@325 625 Elaborate.elabFile basis topStr topSgn ElabEnv.empty file
adamc@201 626 end,
adamc@201 627 print = ElabPrint.p_file ElabEnv.empty
adamc@201 628 }
adamc@5 629
adamc@270 630 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 631
adamc@448 632 val unnest = {
adamc@448 633 func = Unnest.unnest,
adamc@448 634 print = ElabPrint.p_file ElabEnv.empty
adamc@448 635 }
adamc@448 636
adamc@448 637 val toUnnest = transform unnest "unnest" o toElaborate
adamc@448 638
adamc@313 639 val termination = {
adamc@313 640 func = (fn file => (Termination.check file; file)),
adamc@313 641 print = ElabPrint.p_file ElabEnv.empty
adamc@313 642 }
adamc@313 643
adamc@448 644 val toTermination = transform termination "termination" o toUnnest
adamc@313 645
adamc@201 646 val explify = {
adamc@201 647 func = Explify.explify,
adamc@201 648 print = ExplPrint.p_file ExplEnv.empty
adamc@201 649 }
adamc@201 650
adamc@625 651 val toExplify = transform explify "explify" o toUnnest
adamc@201 652
adamc@201 653 val corify = {
adamc@201 654 func = Corify.corify,
adamc@201 655 print = CorePrint.p_file CoreEnv.empty
adamc@201 656 }
adamc@201 657
adamc@270 658 val toCorify = transform corify "corify" o toExplify
adamc@201 659
adamc@482 660 (*val reduce_local = {
adamc@482 661 func = ReduceLocal.reduce,
adamc@482 662 print = CorePrint.p_file CoreEnv.empty
adamc@482 663 }
adamc@482 664
adamc@482 665 val toReduce_local = transform reduce_local "reduce_local" o toCorify*)
adamc@482 666
adamc@443 667 val especialize = {
adamc@443 668 func = ESpecialize.specialize,
adamc@443 669 print = CorePrint.p_file CoreEnv.empty
adamc@443 670 }
adamc@443 671
adamc@443 672 val toEspecialize = transform especialize "especialize" o toCorify
adamc@443 673
adamc@454 674 val core_untangle = {
adamc@454 675 func = CoreUntangle.untangle,
adamc@454 676 print = CorePrint.p_file CoreEnv.empty
adamc@454 677 }
adamc@454 678
adamc@454 679 val toCore_untangle = transform core_untangle "core_untangle" o toEspecialize
adamc@454 680
adamc@202 681 val shake = {
adamc@202 682 func = Shake.shake,
adamc@202 683 print = CorePrint.p_file CoreEnv.empty
adamc@202 684 }
adamc@39 685
adamc@454 686 val toShake1 = transform shake "shake1" o toCore_untangle
adamc@110 687
adamc@607 688 val rpcify = {
adamc@607 689 func = Rpcify.frob,
adamc@607 690 print = CorePrint.p_file CoreEnv.empty
adamc@607 691 }
adamc@607 692
adamc@607 693 val toRpcify = transform rpcify "rpcify" o toShake1
adamc@607 694
adamc@642 695 val toCore_untangle2 = transform core_untangle "core_untangle2" o toRpcify
adamc@642 696 val toShake2 = transform shake "shake2" o toCore_untangle2
adamc@642 697
adamc@202 698 val tag = {
adamc@202 699 func = Tag.tag,
adamc@202 700 print = CorePrint.p_file CoreEnv.empty
adamc@202 701 }
adamc@193 702
adamc@642 703 val toTag = transform tag "tag" o toCore_untangle2
adamc@20 704
adamc@202 705 val reduce = {
adamc@202 706 func = Reduce.reduce,
adamc@202 707 print = CorePrint.p_file CoreEnv.empty
adamc@202 708 }
adamc@25 709
adamc@692 710 val toReduce = transform reduce "reduce" o toTag
adamc@23 711
adamc@315 712 val unpoly = {
adamc@315 713 func = Unpoly.unpoly,
adamc@315 714 print = CorePrint.p_file CoreEnv.empty
adamc@315 715 }
adamc@315 716
adamc@315 717 val toUnpoly = transform unpoly "unpoly" o toReduce
adamc@315 718
adamc@202 719 val specialize = {
adamc@202 720 func = Specialize.specialize,
adamc@202 721 print = CorePrint.p_file CoreEnv.empty
adamc@202 722 }
adamc@132 723
adamc@315 724 val toSpecialize = transform specialize "specialize" o toUnpoly
adamc@131 725
adamc@642 726 val toShake3 = transform shake "shake3" o toSpecialize
adamc@133 727
adamc@692 728 val marshalcheck = {
adamc@692 729 func = (fn file => (MarshalCheck.check file; file)),
adamc@692 730 print = CorePrint.p_file CoreEnv.empty
adamc@692 731 }
adamc@692 732
adamc@692 733 val toMarshalcheck = transform marshalcheck "marshalcheck" o toShake3
adamc@692 734
adamc@732 735 val effectize = {
adamc@732 736 func = Effective.effectize,
adamc@732 737 print = CorePrint.p_file CoreEnv.empty
adamc@732 738 }
adamc@732 739
adamc@732 740 val toEffectize = transform effectize "effectize" o toMarshalcheck
adamc@732 741
adamc@202 742 val monoize = {
adamc@202 743 func = Monoize.monoize CoreEnv.empty,
adamc@202 744 print = MonoPrint.p_file MonoEnv.empty
adamc@202 745 }
adamc@134 746
adamc@732 747 val toMonoize = transform monoize "monoize" o toEffectize
adamc@96 748
adamc@202 749 val mono_opt = {
adamc@202 750 func = MonoOpt.optimize,
adamc@202 751 print = MonoPrint.p_file MonoEnv.empty
adamc@202 752 }
adamc@29 753
adamc@270 754 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 755
adamc@202 756 val untangle = {
adamc@202 757 func = Untangle.untangle,
adamc@202 758 print = MonoPrint.p_file MonoEnv.empty
adamc@202 759 }
adamc@1 760
adamc@270 761 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 762
adamc@202 763 val mono_reduce = {
adamc@202 764 func = MonoReduce.reduce,
adamc@202 765 print = MonoPrint.p_file MonoEnv.empty
adamc@202 766 }
adamc@16 767
adamc@270 768 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 769
adamc@202 770 val mono_shake = {
adamc@202 771 func = MonoShake.shake,
adamc@202 772 print = MonoPrint.p_file MonoEnv.empty
adamc@202 773 }
adamc@110 774
adamc@270 775 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 776
adamc@572 777 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@572 778
adamc@567 779 val jscomp = {
adamc@567 780 func = JsComp.process,
adamc@567 781 print = MonoPrint.p_file MonoEnv.empty
adamc@567 782 }
adamc@567 783
adamc@572 784 val toJscomp = transform jscomp "jscomp" o toMono_opt2
adamc@567 785
adamc@572 786 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
adamc@20 787
adamc@506 788 val fuse = {
adamc@506 789 func = Fuse.fuse,
adamc@506 790 print = MonoPrint.p_file MonoEnv.empty
adamc@506 791 }
adamc@506 792
adamc@572 793 val toFuse = transform fuse "fuse" o toMono_opt3
adamc@506 794
adamc@506 795 val toUntangle2 = transform untangle "untangle2" o toFuse
adamc@506 796
adamc@601 797 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
adamc@601 798 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
adamc@506 799
adamc@377 800 val pathcheck = {
adamc@377 801 func = (fn file => (PathCheck.check file; file)),
adamc@377 802 print = MonoPrint.p_file MonoEnv.empty
adamc@377 803 }
adamc@377 804
adamc@506 805 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake2
adamc@377 806
adamc@202 807 val cjrize = {
adamc@202 808 func = Cjrize.cjrize,
adamc@202 809 print = CjrPrint.p_file CjrEnv.empty
adamc@202 810 }
adamc@23 811
adamc@377 812 val toCjrize = transform cjrize "cjrize" o toPathcheck
adamc@29 813
adamc@643 814 val scriptcheck = {
adamc@643 815 func = ScriptCheck.classify,
adamc@643 816 print = CjrPrint.p_file CjrEnv.empty
adamc@643 817 }
adamc@643 818
adamc@643 819 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
adamc@643 820
adamc@282 821 val prepare = {
adamc@282 822 func = Prepare.prepare,
adamc@282 823 print = CjrPrint.p_file CjrEnv.empty
adamc@282 824 }
adamc@282 825
adamc@643 826 val toPrepare = transform prepare "prepare" o toScriptcheck
adamc@282 827
adamc@274 828 val sqlify = {
adamc@274 829 func = Cjrize.cjrize,
adamc@274 830 print = CjrPrint.p_sql CjrEnv.empty
adamc@274 831 }
adamc@274 832
adamc@274 833 val toSqlify = transform sqlify "sqlify" o toMono_opt2
adamc@274 834
adamc@764 835 fun compileC {cname, oname, ename, libs, profile, debug, link = link'} =
adamc@183 836 let
adamc@378 837 val urweb_o = clibFile "urweb.o"
adamc@378 838 val driver_o = clibFile "driver.o"
adamc@378 839
adamc@435 840 val compile = "gcc " ^ Config.gccArgs ^ " -Wstrict-prototypes -Werror -O3 -I include -c " ^ cname ^ " -o " ^ oname
adamc@734 841 val link = "gcc -Werror -O3 -lm -lmhash -pthread " ^ libs ^ " " ^ urweb_o ^ " " ^ oname ^ " " ^ driver_o ^ " -o " ^ ename
adamc@502 842
adamc@502 843 val (compile, link) =
adamc@502 844 if profile then
adamc@502 845 (compile ^ " -pg", link ^ " -pg")
adamc@502 846 else
adamc@502 847 (compile, link)
adamc@742 848
adamc@742 849 val (compile, link) =
adamc@742 850 if debug then
adamc@742 851 (compile ^ " -g", link ^ " -g")
adamc@742 852 else
adamc@742 853 (compile, link)
adamc@764 854
adamc@764 855 val link = foldl (fn (s, link) => link ^ " " ^ s) link link'
adamc@183 856 in
adamc@183 857 if not (OS.Process.isSuccess (OS.Process.system compile)) then
adamc@183 858 print "C compilation failed\n"
adamc@183 859 else if not (OS.Process.isSuccess (OS.Process.system link)) then
adamc@186 860 print "C linking failed\n"
adamc@183 861 else
adamc@673 862 ()
adamc@183 863 end
adamc@183 864
adamc@202 865 fun compile job =
adamc@282 866 case run toPrepare job of
adamc@244 867 NONE => print "Ur compilation failed\n"
adamc@29 868 | SOME file =>
adamc@202 869 let
adamc@274 870 val job = valOf (run (transform parseUrp "parseUrp") job)
adamc@102 871
adamc@274 872 val (cname, oname, cleanup) =
adamc@274 873 if #debug job then
adamc@457 874 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
adamc@274 875 else
adamc@274 876 let
adamc@274 877 val dir = OS.FileSys.tmpName ()
adamc@403 878 val () = if OS.FileSys.access (dir, []) then
adamc@403 879 OS.FileSys.remove dir
adamc@403 880 else
adamc@403 881 ()
adamc@457 882 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
adamc@457 883 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
adamc@274 884 in
adamc@274 885 OS.FileSys.mkDir dir;
adamc@274 886 (cname, oname,
adamc@274 887 fn () => (OS.FileSys.remove cname;
adamc@274 888 OS.FileSys.remove oname;
adamc@473 889 OS.FileSys.rmDir dir)
adamc@473 890 handle OS.SysErr _ => OS.FileSys.rmDir dir)
adamc@274 891 end
adamc@274 892 val ename = #exe job
adamc@202 893 in
adamc@274 894 let
adamc@274 895 val outf = TextIO.openOut cname
adamc@274 896 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@432 897
adamc@432 898 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
adamc@432 899 val libs =
adamc@432 900 if hasDb then
adamc@432 901 "-lpq"
adamc@432 902 else
adamc@432 903 ""
adamc@274 904 in
adamc@274 905 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@415 906 TextIO.output1 (outf, #"\n");
adamc@274 907 TextIO.closeOut outf;
adamc@102 908
adamc@274 909 case #sql job of
adamc@274 910 NONE => ()
adamc@274 911 | SOME sql =>
adamc@274 912 let
adamc@274 913 val outf = TextIO.openOut sql
adamc@274 914 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@274 915 in
adamc@274 916 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
adamc@274 917 TextIO.closeOut outf
adamc@274 918 end;
adamc@274 919
adamc@742 920 compileC {cname = cname, oname = oname, ename = ename, libs = libs,
adamc@764 921 profile = #profile job, debug = #debug job, link = #link job};
adamc@274 922
adamc@274 923 cleanup ()
adamc@274 924 end
adamc@274 925 handle ex => (((cleanup ()) handle _ => ()); raise ex)
adamc@202 926 end
adamc@29 927
adamc@1 928 end