annotate src/settings.sml @ 1257:fd1a49b51db5

Fix C-mangling of datatype names
author Adam Chlipala <adamc@hcoop.net>
date Sun, 16 May 2010 15:34:24 -0400
parents e80582b927f2
children fc7ecf8883b1
rev   line source
adamc@1114 1 (* Copyright (c) 2008-2010, Adam Chlipala
adamc@764 2 * All rights reserved.
adamc@764 3 *
adamc@764 4 * Redistribution and use in source and binary forms, with or without
adamc@764 5 * modification, are permitted provided that the following conditions are met:
adamc@764 6 *
adamc@764 7 * - Redistributions of source code must retain the above copyright notice,
adamc@764 8 * this list of conditions and the following disclaimer.
adamc@764 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@764 10 * this list of conditions and the following disclaimer in the documentation
adamc@764 11 * and/or other materials provided with the distribution.
adamc@764 12 * - The names of contributors may not be used to endorse or promote products
adamc@764 13 * derived from this software without specific prior written permission.
adamc@764 14 *
adamc@764 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@764 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@764 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@764 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@764 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@764 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@764 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@764 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@764 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@764 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@764 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@764 26 *)
adamc@764 27
adamc@764 28 structure Settings :> SETTINGS = struct
adamc@764 29
adamc@764 30 val urlPrefix = ref "/"
adamc@764 31 val timeout = ref 0
adamc@764 32 val headers = ref ([] : string list)
adamc@766 33 val scripts = ref ([] : string list)
adamc@764 34
adamc@764 35 fun getUrlPrefix () = !urlPrefix
adamc@764 36 fun setUrlPrefix p =
adamc@764 37 urlPrefix := (if p = "" then
adamc@764 38 "/"
adamc@764 39 else if String.sub (p, size p - 1) <> #"/" then
adamc@764 40 p ^ "/"
adamc@764 41 else
adamc@764 42 p)
adamc@764 43
adamc@764 44 fun getTimeout () = !timeout
adamc@764 45 fun setTimeout n = timeout := n
adamc@764 46
adamc@764 47 fun getHeaders () = !headers
adamc@764 48 fun setHeaders ls = headers := ls
adamc@764 49
adamc@766 50 fun getScripts () = !scripts
adamc@766 51 fun setScripts ls = scripts := ls
adamc@766 52
adamc@765 53 type ffi = string * string
adamc@765 54
adamc@765 55 structure K = struct
adamc@765 56 type ord_key = ffi
adamc@765 57 fun compare ((m1, x1), (m2, x2)) =
adamc@765 58 Order.join (String.compare (m1, m2),
adamc@765 59 fn () => String.compare (x1, x2))
adamc@764 60 end
adamc@765 61
adamc@765 62 structure S = BinarySetFn(K)
adamc@765 63 structure M = BinaryMapFn(K)
adamc@765 64
adamc@765 65 fun basis x = S.addList (S.empty, map (fn x : string => ("Basis", x)) x)
adamc@765 66
adamc@765 67 val clientToServerBase = basis ["int",
adamc@765 68 "float",
adamc@765 69 "string",
adamc@765 70 "time",
adamc@765 71 "file",
adamc@765 72 "unit",
adamc@765 73 "option",
adamc@765 74 "list",
adamc@765 75 "bool"]
adamc@765 76 val clientToServer = ref clientToServerBase
adamc@765 77 fun setClientToServer ls = clientToServer := S.addList (clientToServerBase, ls)
adamc@765 78 fun mayClientToServer x = S.member (!clientToServer, x)
adamc@765 79
adamc@779 80 val effectfulBase = basis ["dml",
adamc@779 81 "nextval",
adamc@1073 82 "setval",
adamc@779 83 "set_cookie",
adamc@1050 84 "clear_cookie",
adamc@765 85 "new_channel",
adamc@1200 86 "send",
adamc@1200 87 "htmlifyInt_w",
adamc@1200 88 "htmlifyFloat_w",
adamc@1200 89 "htmlifyString_w",
adamc@1200 90 "htmlifyBool_w",
adamc@1200 91 "htmlifyTime_w",
adamc@1200 92 "attrifyInt_w",
adamc@1200 93 "attrifyFloat_w",
adamc@1200 94 "attrifyString_w",
adamc@1200 95 "attrifyChar_w",
adamc@1200 96 "urlifyInt_w",
adamc@1200 97 "urlifyFloat_w",
adamc@1200 98 "urlifyString_w",
adamc@1200 99 "urlifyBool_w",
adamc@1200 100 "urlifyChannel_w"]
adamc@765 101
adamc@765 102 val effectful = ref effectfulBase
adamc@765 103 fun setEffectful ls = effectful := S.addList (effectfulBase, ls)
adamc@765 104 fun isEffectful x = S.member (!effectful, x)
adamc@765 105
adamc@1171 106 val benignBase = basis ["get_cookie",
adamc@1171 107 "new_client_source",
adamc@1171 108 "get_client_source",
adamc@1171 109 "set_client_source",
adamc@1171 110 "current",
adamc@1171 111 "alert",
adamc@1171 112 "onError",
adamc@1171 113 "onFail",
adamc@1171 114 "onConnectFail",
adamc@1171 115 "onDisconnect",
adamc@1171 116 "onServerError",
adamc@1171 117 "kc",
adamc@1250 118 "debug",
adamc@1250 119 "rand"]
adamc@1171 120
adamc@1171 121 val benign = ref benignBase
adamc@1171 122 fun setBenignEffectful ls = benign := S.addList (benignBase, ls)
adamc@1171 123 fun isBenignEffectful x = S.member (!benign, x)
adamc@1171 124
adamc@765 125 val clientBase = basis ["get",
adamc@765 126 "set",
adamc@841 127 "current",
adamc@765 128 "alert",
adamc@765 129 "recv",
adamc@765 130 "sleep",
adamc@765 131 "spawn",
adamc@765 132 "onError",
adamc@765 133 "onFail",
adamc@765 134 "onConnectFail",
adamc@765 135 "onDisconnect",
adamc@895 136 "onServerError",
adamc@895 137 "kc"]
adamc@765 138 val client = ref clientBase
adamc@765 139 fun setClientOnly ls = client := S.addList (clientBase, ls)
adamc@765 140 fun isClientOnly x = S.member (!client, x)
adamc@765 141
adamc@765 142 val serverBase = basis ["requestHeader",
adamc@765 143 "query",
adamc@765 144 "dml",
adamc@765 145 "nextval",
adamc@1073 146 "setval",
adamc@765 147 "channel",
adamc@765 148 "send"]
adamc@765 149 val server = ref serverBase
adamc@765 150 fun setServerOnly ls = server := S.addList (serverBase, ls)
adamc@765 151 fun isServerOnly x = S.member (!server, x)
adamc@765 152
adamc@765 153 val basisM = foldl (fn ((k, v : string), m) => M.insert (m, ("Basis", k), v)) M.empty
adamc@765 154
adamc@765 155 val jsFuncsBase = basisM [("alert", "alert"),
adamc@765 156 ("get_client_source", "sg"),
adamc@841 157 ("current", "scur"),
adamc@765 158 ("htmlifyBool", "bs"),
adamc@765 159 ("htmlifyFloat", "ts"),
adamc@765 160 ("htmlifyInt", "ts"),
adamc@765 161 ("htmlifyString", "eh"),
adamc@765 162 ("new_client_source", "sc"),
adamc@765 163 ("set_client_source", "sv"),
adamc@838 164 ("stringToFloat", "pflo"),
adamc@838 165 ("stringToInt", "pio"),
adamc@765 166 ("stringToFloat_error", "pfl"),
adamc@765 167 ("stringToInt_error", "pi"),
adamc@765 168 ("urlifyInt", "ts"),
adamc@765 169 ("urlifyFloat", "ts"),
adamc@765 170 ("urlifyString", "uf"),
adamc@912 171 ("urlifyBool", "ub"),
adamc@765 172 ("recv", "rv"),
adamc@765 173 ("strcat", "cat"),
adamc@765 174 ("intToString", "ts"),
adamc@765 175 ("floatToString", "ts"),
adamc@821 176 ("charToString", "ts"),
adamc@765 177 ("onError", "onError"),
adamc@765 178 ("onFail", "onFail"),
adamc@765 179 ("onConnectFail", "onConnectFail"),
adamc@765 180 ("onDisconnect", "onDisconnect"),
adamc@798 181 ("onServerError", "onServerError"),
adamc@1108 182 ("attrifyString", "atr"),
adamc@798 183 ("attrifyInt", "ts"),
adamc@798 184 ("attrifyFloat", "ts"),
adamc@820 185 ("attrifyBool", "bs"),
adamc@821 186 ("boolToString", "ts"),
adamc@1057 187 ("str1", "id"),
adamc@821 188 ("strsub", "sub"),
adamc@828 189 ("strsuffix", "suf"),
adamc@829 190 ("strlen", "slen"),
adamc@829 191 ("strindex", "sidx"),
adamc@829 192 ("strchr", "schr"),
adamc@831 193 ("substring", "ssub"),
adamc@895 194 ("strcspn", "sspn"),
adamc@1061 195 ("kc", "kc"),
adamc@1061 196
adamc@1061 197 ("islower", "isLower"),
adamc@1061 198 ("isupper", "isUpper"),
adamc@1061 199 ("isalpha", "isAlpha"),
adamc@1061 200 ("isdigit", "isDigit"),
adamc@1061 201 ("isalnum", "isAlnum"),
adamc@1061 202 ("isblank", "isBlank"),
adamc@1061 203 ("isspace", "isSpace"),
adamc@1061 204 ("isxdigit", "isXdigit"),
adamc@1061 205 ("tolower", "toLower"),
adamc@1061 206 ("toupper", "toUpper")]
adamc@765 207 val jsFuncs = ref jsFuncsBase
adamc@765 208 fun setJsFuncs ls = jsFuncs := foldl (fn ((k, v), m) => M.insert (m, k, v)) jsFuncsBase ls
adamc@765 209 fun jsFunc x = M.find (!jsFuncs, x)
adamc@765 210
adamc@768 211 datatype pattern_kind = Exact | Prefix
adamc@768 212 datatype action = Allow | Deny
adamc@768 213 type rule = { action : action, kind : pattern_kind, pattern : string }
adamc@768 214
adamc@768 215 datatype path_kind = Any | Url | Table | Sequence | View | Relation | Cookie | Style
adamc@768 216 type rewrite = { pkind : path_kind, kind : pattern_kind, from : string, to : string }
adamc@768 217
adamc@768 218 val rewrites = ref ([] : rewrite list)
adamc@768 219
adamc@768 220 fun subsume (pk1, pk2) =
adamc@768 221 pk1 = pk2
adamc@768 222 orelse pk2 = Any
adamc@768 223 orelse pk2 = Relation andalso (pk1 = Table orelse pk1 = Sequence orelse pk1 = View)
adamc@768 224
adamc@768 225 fun setRewriteRules ls = rewrites := ls
adamc@768 226 fun rewrite pk s =
adamc@768 227 let
adamc@768 228 fun rew (ls : rewrite list) =
adamc@768 229 case ls of
adamc@768 230 [] => s
adamc@768 231 | rewr :: ls =>
adamc@768 232 let
adamc@768 233 fun match () =
adamc@768 234 case #kind rewr of
adamc@768 235 Exact => if #from rewr = s then
adamc@768 236 SOME (size s)
adamc@768 237 else
adamc@768 238 NONE
adamc@768 239 | Prefix => if String.isPrefix (#from rewr) s then
adamc@768 240 SOME (size (#from rewr))
adamc@768 241 else
adamc@768 242 NONE
adamc@768 243 in
adamc@768 244 if subsume (pk, #pkind rewr) then
adamc@768 245 case match () of
adamc@768 246 NONE => rew ls
adamc@768 247 | SOME suffixStart => #to rewr ^ String.extract (s, suffixStart, NONE)
adamc@768 248 else
adamc@768 249 rew ls
adamc@768 250 end
adamc@768 251 in
adamc@768 252 rew (!rewrites)
adamc@768 253 end
adamc@768 254
adamc@769 255 val url = ref ([] : rule list)
adamc@769 256 val mime = ref ([] : rule list)
adamc@769 257
adamc@769 258 fun setUrlRules ls = url := ls
adamc@769 259 fun setMimeRules ls = mime := ls
adamc@769 260
adamc@770 261 fun getUrlRules () = !url
adamc@770 262 fun getMimeRules () = !mime
adamc@770 263
adamc@769 264 fun check f rules s =
adamc@769 265 let
adamc@769 266 fun chk (ls : rule list) =
adamc@769 267 case ls of
adamc@769 268 [] => false
adamc@769 269 | rule :: ls =>
adamc@769 270 let
adamc@769 271 val matches =
adamc@769 272 case #kind rule of
adamc@769 273 Exact => #pattern rule = s
adamc@769 274 | Prefix => String.isPrefix (#pattern rule) s
adamc@769 275 in
adamc@769 276 if matches then
adamc@769 277 case #action rule of
adamc@769 278 Allow => true
adamc@769 279 | Deny => false
adamc@769 280 else
adamc@769 281 chk ls
adamc@769 282 end
adamc@769 283 in
adamc@769 284 f s andalso chk (!rules)
adamc@769 285 end
adamc@769 286
adamc@769 287 val checkUrl = check (fn _ => true) url
adamc@769 288 val checkMime = check
adamc@769 289 (CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"/" orelse ch = #"-" orelse ch = #"."))
adamc@769 290 mime
adamc@769 291
adamc@855 292
adamc@855 293 type protocol = {
adamc@855 294 name : string,
adamc@1096 295 compile : string,
adamc@1095 296 linkStatic : string,
adamc@1095 297 linkDynamic : string,
adamc@1164 298 persistent : bool,
adamc@1164 299 code : unit -> Print.PD.pp_desc
adamc@855 300 }
adamc@855 301 val protocols = ref ([] : protocol list)
adamc@855 302 fun addProtocol p = protocols := p :: !protocols
adamc@855 303 fun getProtocol s = List.find (fn p => #name p = s) (!protocols)
adamc@855 304
adamc@855 305 fun clibFile s = OS.Path.joinDirFile {dir = Config.libC,
adamc@855 306 file = s}
adamc@855 307
adamc@865 308 val curProto = ref {name = "",
adamc@1096 309 compile = "",
adamc@1095 310 linkStatic = "",
adamc@1095 311 linkDynamic = "",
adamc@1164 312 persistent = false,
adamc@1164 313 code = fn () => Print.box []}
adamc@856 314 fun setProtocol name =
adamc@856 315 case getProtocol name of
adamc@856 316 NONE => raise Fail ("Unknown protocol " ^ name)
adamc@856 317 | SOME p => curProto := p
adamc@855 318 fun currentProtocol () = !curProto
adamc@855 319
adamc@857 320 val debug = ref false
adamc@857 321 fun setDebug b = debug := b
adamc@857 322 fun getDebug () = !debug
adamc@857 323
adamc@867 324 datatype sql_type =
adamc@867 325 Int
adamc@867 326 | Float
adamc@867 327 | String
adamc@1011 328 | Char
adamc@867 329 | Bool
adamc@867 330 | Time
adamc@867 331 | Blob
adamc@867 332 | Channel
adamc@867 333 | Client
adamc@867 334 | Nullable of sql_type
adamc@867 335
adamc@873 336 fun p_sql_ctype t =
adamc@867 337 let
adamc@867 338 open Print.PD
adamc@867 339 open Print
adamc@867 340 in
adamc@867 341 case t of
adamc@870 342 Int => "uw_Basis_int"
adamc@870 343 | Float => "uw_Basis_float"
adamc@870 344 | String => "uw_Basis_string"
adamc@1011 345 | Char => "uw_Basis_char"
adamc@870 346 | Bool => "uw_Basis_bool"
adamc@870 347 | Time => "uw_Basis_time"
adamc@870 348 | Blob => "uw_Basis_blob"
adamc@870 349 | Channel => "uw_Basis_channel"
adamc@870 350 | Client => "uw_Basis_client"
adamc@870 351 | Nullable String => "uw_Basis_string"
adamc@873 352 | Nullable t => p_sql_ctype t ^ "*"
adamc@867 353 end
adamc@867 354
adamc@867 355 fun isBlob Blob = true
adamc@867 356 | isBlob (Nullable t) = isBlob t
adamc@867 357 | isBlob _ = false
adamc@867 358
adamc@870 359 fun isNotNull (Nullable _) = false
adamc@870 360 | isNotNull _ = true
adamc@870 361
adamc@866 362 type dbms = {
adamc@866 363 name : string,
adamc@866 364 header : string,
adamc@866 365 link : string,
adamc@873 366 p_sql_type : sql_type -> string,
adamc@870 367 init : {dbstring : string,
adamc@870 368 prepared : (string * int) list,
adamc@870 369 tables : (string * (string * sql_type) list) list,
adamc@872 370 views : (string * (string * sql_type) list) list,
adamc@870 371 sequences : string list} -> Print.PD.pp_desc,
adamc@873 372 query : {loc : ErrorMsg.span, cols : sql_type list,
adamc@880 373 doCols : ({loc : ErrorMsg.span, wontLeakStrings : bool, col : int, typ : sql_type} -> Print.PD.pp_desc)
adamc@867 374 -> Print.PD.pp_desc}
adamc@867 375 -> Print.PD.pp_desc,
adamc@867 376 queryPrepared : {loc : ErrorMsg.span, id : int, query : string,
adamc@873 377 inputs : sql_type list, cols : sql_type list,
adamc@880 378 doCols : ({loc : ErrorMsg.span, wontLeakStrings : bool, col : int,
adamc@880 379 typ : sql_type} -> Print.PD.pp_desc)
adamc@879 380 -> Print.PD.pp_desc,
adamc@879 381 nested : bool}
adamc@868 382 -> Print.PD.pp_desc,
adamc@868 383 dml : ErrorMsg.span -> Print.PD.pp_desc,
adamc@868 384 dmlPrepared : {loc : ErrorMsg.span, id : int, dml : string,
adamc@869 385 inputs : sql_type list} -> Print.PD.pp_desc,
adamc@878 386 nextval : {loc : ErrorMsg.span, seqName : string option, seqE : Print.PD.pp_desc} -> Print.PD.pp_desc,
adamc@874 387 nextvalPrepared : {loc : ErrorMsg.span, id : int, query : string} -> Print.PD.pp_desc,
adamc@1073 388 setval : {loc : ErrorMsg.span, seqE : Print.PD.pp_desc, count : Print.PD.pp_desc} -> Print.PD.pp_desc,
adamc@874 389 sqlifyString : string -> string,
adamc@874 390 p_cast : string * sql_type -> string,
adamc@874 391 p_blank : int * sql_type -> string,
adamc@877 392 supportsDeleteAs : bool,
adamc@886 393 supportsUpdateAs : bool,
adamc@877 394 createSequence : string -> string,
adamc@878 395 textKeysNeedLengths : bool,
adamc@879 396 supportsNextval : bool,
adamc@882 397 supportsNestedPrepared : bool,
adamc@890 398 sqlPrefix : string,
adamc@1014 399 supportsOctetLength : bool,
adamc@1014 400 trueString : string,
adamc@1196 401 falseString : string,
adamc@1196 402 onlyUnion : bool,
adamc@1196 403 nestedRelops : bool
adamc@866 404 }
adamc@866 405
adamc@866 406 val dbmses = ref ([] : dbms list)
adamc@866 407 val curDb = ref ({name = "",
adamc@866 408 header = "",
adamc@866 409 link = "",
adamc@873 410 p_sql_type = fn _ => "",
adamc@867 411 init = fn _ => Print.box [],
adamc@867 412 query = fn _ => Print.box [],
adamc@868 413 queryPrepared = fn _ => Print.box [],
adamc@868 414 dml = fn _ => Print.box [],
adamc@869 415 dmlPrepared = fn _ => Print.box [],
adamc@869 416 nextval = fn _ => Print.box [],
adamc@874 417 nextvalPrepared = fn _ => Print.box [],
adamc@1073 418 setval = fn _ => Print.box [],
adamc@874 419 sqlifyString = fn s => s,
adamc@874 420 p_cast = fn _ => "",
adamc@874 421 p_blank = fn _ => "",
adamc@877 422 supportsDeleteAs = false,
adamc@886 423 supportsUpdateAs = false,
adamc@877 424 createSequence = fn _ => "",
adamc@878 425 textKeysNeedLengths = false,
adamc@879 426 supportsNextval = false,
adamc@882 427 supportsNestedPrepared = false,
adamc@890 428 sqlPrefix = "",
adamc@1014 429 supportsOctetLength = false,
adamc@1014 430 trueString = "",
adamc@1196 431 falseString = "",
adamc@1196 432 onlyUnion = false,
adamc@1196 433 nestedRelops = false} : dbms)
adamc@866 434
adamc@866 435 fun addDbms v = dbmses := v :: !dbmses
adamc@866 436 fun setDbms s =
adamc@866 437 case List.find (fn db => #name db = s) (!dbmses) of
adamc@866 438 NONE => raise Fail ("Unknown DBMS " ^ s)
adamc@866 439 | SOME db => curDb := db
adamc@866 440 fun currentDbms () = !curDb
adamc@866 441
adamc@891 442 val dbstring = ref (NONE : string option)
adamc@891 443 fun setDbstring so = dbstring := so
adamc@891 444 fun getDbstring () = !dbstring
adamc@891 445
adamc@891 446 val exe = ref (NONE : string option)
adamc@891 447 fun setExe so = exe := so
adamc@891 448 fun getExe () = !exe
adamc@891 449
adamc@891 450 val sql = ref (NONE : string option)
adamc@891 451 fun setSql so = sql := so
adamc@891 452 fun getSql () = !sql
adamc@891 453
adamc@1016 454 val coreInline = ref 20
adamc@1016 455 fun setCoreInline n = coreInline := n
adamc@1016 456 fun getCoreInline () = !coreInline
adamc@1016 457
adamc@1016 458 val monoInline = ref 20
adamc@1016 459 fun setMonoInline n = monoInline := n
adamc@1016 460 fun getMonoInline () = !monoInline
adamc@1016 461
adamc@1095 462 val staticLinking = ref false
adamc@1095 463 fun setStaticLinking b = staticLinking := b
adamc@1095 464 fun getStaticLinking () = !staticLinking
adamc@1095 465
adamc@1114 466 val deadlines = ref false
adamc@1114 467 fun setDeadlines b = deadlines := b
adamc@1114 468 fun getDeadlines () = !deadlines
adamc@1114 469
adamc@1164 470 val sigFile = ref (NONE : string option)
adamc@1164 471 fun setSigFile v = sigFile := v
adamc@1164 472 fun getSigFile () = !sigFile
adamc@1164 473
adamc@1183 474 structure SS = BinarySetFn(struct
adamc@1183 475 type ord_key = string
adamc@1183 476 val compare = String.compare
adamc@1183 477 end)
adamc@1183 478
adamc@1183 479 val safeGet = ref SS.empty
adamc@1183 480 fun setSafeGets ls = safeGet := SS.addList (SS.empty, ls)
adamc@1183 481 fun isSafeGet x = SS.member (!safeGet, x)
adamc@1183 482
adamc@765 483 end