1:- module(pwp, 2 [ pwp_files/2, % +FileIn, +FileOut 3 pwp_stream/3, % +StreamIn, +StreamOut, +Context 4 pwp_xml/3 % +DomIn, -DOMOut, +Context 5 ]). 6:- autoload(library(lists),[append/3]). 7:- autoload(library(readutil),[read_file_to_codes/3]). 8:- autoload(library(sgml),[load_xml_file/2]). 9:- autoload(library(sgml_write),[xml_write/3]). 10 11/** <module> Prolog Well-formed Pages 12 13PWP is an approach to server-side scripting using Prolog 14which is based on a simple key principle: 15 16 - The source form of a PWP should be WELL-FORMED XML 17 18Especially when generating XML rather than HTML, this is such an 19obvious thing to do. We have many kinds of XML checking tools. 20 21 - We can tell whether an XML document is WELL FORMED (all the 22 punctuation is right, all tags balance) using practically 23 any decent parser, including SWI Prolog's 'sgml'. 24 25 - If we can write a Document Type Description then we can check 26 that a document is VALID using tools like Open SP (formerly 27 James Clark's SP) or SWI Prolog's 'sgml'. This does not 28 guarantee that the output will be valid, but it does catch 29 a lot of errors very early. 30 31 - If we can write an XML Schema then we can check that a 32 document is schema-valid. (SWI Prolog's 'sgml' does not 33 yet come with a schema validator, but who knows what the 34 future holds?). 35 36 - Since an XML document is just a data structure, we can use 37 any checking tool that we can write in Prolog, IF the input 38 is well-formed so that we can load a template as a Prolog 39 data structure. 40 41Having decided that the input should be well formed, that means 42*|NO NEW SYNTAX|* 43 44None of the weird and horrible <% ... %> or whatever not-quite-XML 45stuff you see in other template systems, making checking so very hard 46(and therefore, making errors so distressingly common). 47 48That in turns means that PWP "markup" must be based on special 49elements or special attributes. The fact that an XML parser must 50allow undeclared attributes on any element even when validating, 51but must not allow undeclared elements, suggests doing this through 52attributes. In particular, one should be able to take an existing 53DTD, such as an XHTML DTD, and just use that without modification. 54So the design reduces to 55 56 - Allow dynamic data selection, insertion, and transformation 57 just using a small number of extra attributes. 58 59This description uses the following name space: 60 61 == 62 xmlns:pwp='http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl' 63 == 64 65The attributes are 66 67 - pwp:ask = Query 68 - pwp:use = Term 69 - pwp:how = text | xml 70 - pwp:tag = QName or '-' 71 - pwp:att = '' | 'one non-alphanumeric character' 72 73Here's what they mean. Each element is expanded in the context 74of a set of variable bindings. After expansion, if the tag is 75not mapped to '-', all attributes in the pwp: namespace are removed 76and the children elements are recursively expanded. 77 78 * pwp:ask = Query 79 80 * Query is a Prolog goal. For each solution of Query, the element 81 is further processed with the new variables of Query added to 82 the context. 83 84 * If Query is not a well formed Prolog goal, or if execution of 85 Query throws an exception, page transformation comes to a complete 86 halt and no page is generated. 87 88 * pwp:use = Term 89 * pwp:how = text | xml | text-file | xml-file | pwp-file 90 91 Term is a Prolog term; variables in Term are bound by the context. 92 An empty Term is regarded as a missing value for this attribute. 93 The Prolog variable CONTEXT refers to the entire context, a list 94 of Name = Value, where Name is a Prolog atom holding the name of the context variable and Value is an arbitrary Prolog term. 95 96 - If pwp:how is text, 97 The value of Term is used to define a sequence of characters. 98 99 - A number produces the same characters that write/1 would. 100 - An atom produces the same characters that write/1 would. 101 - A string produces the same characters that write/1 would. 102 - A list of character codes produces those characters. 103 104 - The following terms produce the same sequence of characters 105 that the corresponding goal would have sent to the current 106 output stream: 107 108 - write(Datum) 109 - writeq(Datum) 110 - write_canonical(Datum) 111 - print(Datum) 112 - print(Datum) 113 - format(Format) 114 - format(Format, Arguments) 115 116 - A singleton list [X] defines the characters that X defines. 117 - Any other term F(T1,...,Tn) defines the characters that T1 118 defines, followed by the characters that T2 defines, ..., 119 followed by the characters that Tn defines. 120 121 - If pwp:how is xml, 122 The value of Term must be an XML term as defined in the 123 SGML2PL documentation or a list of such terms. A single 124 term is taken as if it had been [Term]. The resulting 125 list of terms replaces the children of the current element 126 and will not be further processed. 127 128 - If pwp:how is text-file, 129 The value of Term is used to define a sequence of characters. 130 That sequence of characters is used as a file name. 131 The file is read as a sequence of characters, and that 132 sequence used as character data. 133 134 - If pwp:how is xml-file, 135 The value of Term is used to define a sequence of characters. 136 That sequence of characters is used as a file name. 137 The file is loaded as XML, and the sequence of XML items thus 138 obtained used. This means that PWP provides XML inclusion 139 without depending on the parser to support XInclude. 140 141 - If pwp:how is pwp-file, 142 Like xml-file, but PWP attributes are evaluated and processed. 143 The current context variables are passed to the PWP processor. 144 145 The default value for pwp:how is text. 146 147 * pwp:tag = QName or '-' 148 149 If pwp:tag is missing or the value is empty, the current element 150 appears in the output (after further processing) with its present 151 tag. If pwp:tag is a QName, the current element appears (...) 152 with that as its tag. That option is most useful in DTDs, where 153 an "authoring" DTD may use one tag and have it automatically mapped 154 to another tag in the output, e.g., <item> -> <li>. Finally, if 155 pwp:tag is '-', the children of the current element (either the 156 result of pwp:use or the transformed original children, whichever 157 applies) appear in the output but there is no element around them. 158 159 A missing or empty pwp:ask is just like pwp:ask = 'true'. 160 161 * pwp:att = '' | 'one non-alphanumeric character'. 162 163 Attributes in the pwp namespace are not affected by this attribute. 164 Such attributes are always stripped out and never substituted into. 165 166 If pwp:att is missing or empty, attributes of the current 167 element are copied over to the output unchanged. 168 169 If pwp:att = 'c' for some non-alphanumeric character c, 170 each attribute is examined for occurrences of c(...)c and c[...]c 171 which are as short as possible. 172 There is no one character which could be used every time, so you 173 have to explicitly choose a substitution marker which is safe 174 for the data you do not want to be altered. None of the pwp 175 attributes are inherited, least of all this one. 176 177 Text outside c(...)c groups is copied unchanged; text inside 178 a c(...)c group is parsed as a Prolog term and treated as if by 179 pwp:how = text. Text inside a c[...]c group is evaluated (in the 180 current context), and if it fails, the entire attribute will be 181 removed from the element. 182 183 184Examples: 185 186 1. *|A "Hello World" like example|* 187 188 == 189 <html 190 xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl" 191 pwp:ask = "ensure_loaded(msg), once(msg(Greeting))"> 192 <head> 193 <title pwp:use="Greeting"/> 194 </head> 195 <body> 196 <p><span pwp:use="Greeting" pwp:tag='-'/></p> 197 </body> 198 </html> 199 == 200 201 where msg.pl contains 202 203 == 204 msg('Hello, World!'). 205 == 206 207 This example illustrates an important point. Prolog Well-Formed 208 Pages provide *NO* way to physically incorporate Prolog *clauses* 209 into a page template. Prolog clauses must be put in separate 210 files which can be checked by a Prolog syntax checker, compiler, 211 cross-referencer, &c WITHOUT the Prolog tool in question needing 212 to know anything whatsoever about PWP. You load the files using 213 pwp:ask on the root element. 214 215 2. *|Binding some variables and using them|* 216 217 == 218 <html 219 xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl"> 220 <head><title>Example 2</title></head> 221 <body pwp:ask="Hello = 'Hello world', A = 20, B = 22"> 222 <h1 pwp:use="Hello"/> 223 <p>The answer is <span pwp:use="C" pwp:ask="C is A+B"/>.</p> 224 </body> 225 </html> 226 == 227 228 3. *|Making a table|* 229 We are given a Prolog database staff.pl defining 230 staff(NickName, FullName, Office, Phone, E_Mail_Address). 231 status(NickName, full_time | part_time). 232 We want to make a phone list of full time staff. 233 234 == 235 <html 236 xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl" 237 pwp:ask='ensure_loaded(staff)'> 238 <head> 239 <title>Phone list for Full-Time staff.</title> 240 </head> 241 <body> 242 <h1>Phone list for Full-Time staff.</h1> 243 <table 244 pwp:ask = "setof(FullName-Phone, 245 N^O^E^( 246 status(N, full_time), 247 staff(N, FullName, O, Phone, E) 248 ), 249 Staff_List)"> 250 <tr><th>Name</th><th>Phone</th></tr> 251 <tr pwp:ask="member(FullName-Phone, Staff_List)"> 252 <td pwp:use="FullName"/> 253 <td pwp:use="Phone"/> 254 </tr> 255 </table> 256 </body> 257 </html> 258 == 259 260 4. *|Substituting into an attribute|* 261 Same data base as before, but now we want to make a mailing list 262 page. 263 264 == 265 <html 266 xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl" 267 pwp:ask='ensure_loaded(staff)'> 268 <head> 269 <title>Phone list for Full-Time staff.</title> 270 </head> 271 <body> 272 <h1>Phone list for Full-Time staff.</h1> 273 <table 274 pwp:ask = "setof(FullName-E_Mail, 275 N^O^P^staff(N, FullName, O, P, E_Mail), 276 Staff_List)"> 277 <tr><th>Name</th><th>Address</th></tr> 278 <tr pwp:ask="member(FullName-E_Mail, Staff_List)"> 279 <td pwp:use="FullName"/> 280 <td><a pwp:use="E_Mail" 281 pwp:att='$' href="mailto:$(E_Mail)$"/></td> 282 </tr> 283 </table> 284 </body> 285 </html> 286 == 287 288 5. *|If-then-else effect|* 289 A page that displays the value of the 'SHELL' environment 290 variable if it has one, otherwise displays 'There is no 291 default shell.' 292 293 == 294 <html 295 xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl"> 296 <head><title>$SHELL</title></head> 297 <body> 298 <p pwp:ask="getenv('SHELL', Shell)" 299 >The default shell is <span pwp:tag="-" pwp:use="Shell"/>.</p> 300 <p pwp:ask="\+getenv('SHELL',_)">There is no default shell.</p> 301 </body> 302 </html> 303 == 304 305 There is one other criterion for a good server-side template 306 language: 307 308 It should be possible to compile templates so as to eliminate 309 most if not all interpretation overhead. 310 311 This particular notation satisfies that criterion with the 312 limitation that the conversion of a term to character data requires 313 run-time traversal of terms (because the terms are not known until 314 run time). 315 316## Security {#pwp-security} 317 318A PWP template is a program. The values of `pwp:ask`, `pwp:use` and the 319`c(...)c` and `c[...]c` parts of attribute values are read as Prolog 320terms and executed with full privileges, and the `text-file`, `xml-file` 321and `pwp-file` values of `pwp:how` read arbitrary files (`pwp-file` also 322executes the embedded queries). Templates must therefore be trusted in 323the same way as Prolog source code. Notably: 324 325 - Never process PWP documents that may be supplied or modified by 326 untrusted users, for example files in an upload directory that is 327 served by pwp_handler/2. 328 - Data that is substituted into a page is not interpreted as PWP, but 329 do not use request data to construct goals, format strings or file 330 names in templates. 331 332@author Richard O'Keefe 333@tbd Support compilation of PWP input files 334*/ 335 336:- meta_predicate 337 pwp_files(:, +), 338 pwp_stream(:, +, +), 339 pwp_xml(:, -, +). 340 341 342%% pwp_files(:In:atom, +Out:atom) is det. 343% 344% loads an Xml document from the file named In, 345% transforms it using the PWP attributes, and 346% writes the transformed version to the new file named Out. 347 348pwp_files(M:In, Out) :- 349 load_xml_file(In, Contents), 350 pwp_xml(M:Contents, Transformed, []), 351 !, 352 setup_call_cleanup(open(Out, write, Output), 353 xml_write(Output, Transformed, []), 354 close(Output)). 355 356 357%! pwp_stream(:Input:input_stream, +Output:output_stream, 358%! +Context:list) is det. 359% 360% Loads an Xml document from the given Input stream, transforms it 361% using the PWP attributes, and writes the transformed version to 362% the given Output stream. Context provides initial contextual 363% variables and is a list of Name=Value. 364 365pwp_stream(M:Input, Output, Context) :- 366 load_xml_file(stream(Input), Contents), 367 pwp_xml(M:Contents, Transformed, Context), 368 !, 369 xml_write(Output, Transformed, []). 370 371 372/* Recall that an XML term is one of 373 374 <atom> Character Data 375 sdata(...) SDATA (SGML only) 376 ndata(...) NDATA 377 pi(...) Processing instruction 378 379 element(Name, [Att...], [Child...]) 380 381 where Att is Attribute=Value and Child is an XML term. 382 383 We are only concerned with elements; all other XML terms are 384 left alone. I have given some thought to recognising 385 386 <?pwp ...Command...?> 387 388 processing instructions, executing the Command, and removing 389 the processing instructions, as a debugging tool. But this 390 is a proof-of-concept implementation; debugging features can 391 wait for The Real Thing. 392*/ 393 394 395 396%% pwp_xml(:In:list(xml), -Out:list(xml), +Context) 397% 398% maps down a list of XML items, acting specially on elements and 399% copying everything else unchanged, including white space. 400% The Context is a list of 'VariableName'=CurrentValue bindings. 401 402pwp_xml(M:In, Out, Context) :- 403 pwp_list(In, Out, M, Context). 404 405pwp_list([], [], _, _). 406pwp_list([element(Tag0,Atts0,Kids0)|Xs], Ys0, M, Context) :- 407 !, 408 pwp_attributes(Atts0, Ask, Use, How, Att, Tag1, Atts1), 409 ( nonvar(Tag1), Tag1 \== '' -> Tag2 = Tag1 410 ; Tag2 = Tag0 411 ), 412 ( nonvar(Ask), Ask \== '', Ask \== 'true' 413 -> atom_to_term(Ask, Query, Bindings), 414 pwp_unite(Bindings, Context, Context1), 415 findall(Xml, 416 ( M:Query, 417 pwp_element(Tag2, Atts1, Kids0, Use, How, Att, 418 M, Context1, Xml)), 419 NewContent) 420 ; /* Ask is missing, empty, or true */ 421 pwp_element(Tag2, Atts1, Kids0, Use, How, Att, 422 M, Context, NewContent) 423 ), 424 pwp_attach(NewContent, Ys0, Ys1), 425 pwp_list(Xs, Ys1, M, Context). 426pwp_list([X|Xs], [X|Ys], M, Context) :- 427 pwp_list(Xs, Ys, M, Context). 428 429 430%! pwp_attributes(+Atts0:list(=(atom,atom)), 431%! -Ask:atom, -Use:atom, -How:atom, -Att:atom, 432%! -Tag:atom, -Atts1:list(=(atom,atom))) 433% 434% Walks down a list of AttributeName=ItsValue pairs, stripping out 435% those whose AttributeName begins with the 'pwp:' prefix, and 436% copying the rest to Atts1. Along the way, Ask/Use/How/Att/Tag 437% are bound to the values of the 438% pwp:ask/pwp:use/pwp:how/pwp:att/pwp:tag attributes, if any. At 439% the end, any of these variables that are still unbound REMAIN 440% unbound; they are not bound to default values. 441 442pwp_attributes([], _, _, _, _, _, []). 443pwp_attributes([AV|AVs], Ask, Use, How, Att, Tag, New_Atts1) :- 444 AV = (Name=Value), 445 ( pwp_attr(Name, PWPName) 446 -> ( pwp_attr(PWPName, Value, Ask, Use, How, Att, Tag) 447 -> New_Atts1 = New_Atts2 448 ; New_Atts1 = New_Atts2 449 ) 450 ; New_Atts1 = [AV|New_Atts2] 451 ), 452 pwp_attributes(AVs, Ask, Use, How, Att, Tag, New_Atts2). 453 454 455pwp_attr(ask, Value, Value, _Use, _How, _Att, _Tag). 456pwp_attr(use, Value, _Ask, Value, _How, _Att, _Tag). 457pwp_attr(how, Value, _Ask, _Use, Value, _Att, _Tag). 458pwp_attr(att, Value, _Ask, _Use, _How, Value, _Tag). 459pwp_attr(tag, Value, _Ask, _Use, _How, _Att, Value). 460 461%! pwp_attr(+XMLAttr, -PWPLocal) is semidet. 462% 463% True if PWPLocal is the local name of a pwp:Local expression in 464% XML. This predicate deals with the three different XML 465% representations: the form is returned of XML namespace 466% processing is not enabled. The second if it is enabled and the 467% namespace is properly defined and the last if the namespace is 468% not defined. 469 470pwp_attr(Atom, PWP) :- 471 atom(Atom), 472 atom_concat('pwp:', PWP, Atom), 473 !. 474pwp_attr('http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl':PWP, PWP) :- !. 475pwp_attr('pwp':PWP, PWP) :- !. 476pwp_attr('xmlns:pwp', -). 477 478%% pwp_unite(+Bindings, +Context0, -Context:list(=(atom,any))) 479% 480% merges the new Bindings with the bindings in the outer Context0, 481% constructing a new list of VariableName=CurrentValue bindings in 482% Context1. This is only used when the CurrentValue parts of the 483% new Bindings are known to be distinct new variables, so the 484% Bindings cannot possibly conflict with any existing binding in 485% Context0. This is O(|Bindings|.|Context0|), which is not that 486% efficient, but since we do not expect there to be very many 487% variables it doesn't matter much. 488 489pwp_unite(Bindings, Context0, Context) :- 490 pwp_unite(Bindings, Context0, Context0, Context). 491 492 493pwp_unite([], _, Context, Context). 494pwp_unite([Binding|Bindings], Context0, Context1, Context) :- 495 memberchk(Binding, Context0), 496 !, 497 pwp_unite(Bindings, Context0, Context1, Context). 498pwp_unite(['CONTEXT'=Context0|Bindings], Context0, Context1, Context) :- 499 !, 500 pwp_unite(Bindings, Context0, Context1, Context). 501pwp_unite([Binding|Bindings], Context0, Context1, Context) :- 502 pwp_unite(Bindings, Context0, [Binding|Context1], Context). 503 504 505 506%% pwp_unite(+Bindings, +Context0: list(=(atom,any))) 507% 508% looks up the bindings in Bindings in the outer Context0. 509% This is only used for 'pwp:use' terms (and the related terms 510% in $(...)$ attribute value substitutions), so that we have 511% no interest in forming a new context. (If we did, we'd use 512% pwp_unite/3 instead.) This is only used when the CurrentValue 513% parts of the new Bindings are known to be distinct new variables, 514% so the Bindings cannot possibly conflict with any existing 515% binding in Context0. However, there _could_ be new variables 516% in Bindings, and that would cause problems. An XML term may 517% not contain variables, and a term we want to convert to a list 518% of character codes had better not contain variables either. 519% One approach would be to just bind such variables to something, 520% another is to throw some kind of exception. For the moment we 521% call functor/3 so as to get an instantiation error. 522 523pwp_unite([], _). 524pwp_unite([Binding|Bindings], Context) :- 525 memberchk(Binding, Context), 526 !, 527 pwp_unite(Bindings, Context). 528pwp_unite([_=Value|_], _) :- 529 functor(Value, _, _). 530 531%% pwp_attach(+Tree, ?Ys0: list(xml), ?Ys: list(xml)) 532% 533% is a combination of "flatten" and "append". 534% It unifies Ys0\Ys with the result of flattening Tree. 535 536pwp_attach([], Ys, Ys) :- !. 537pwp_attach([X|Xs], Ys0, Ys) :- 538 !, 539 pwp_attach(X, Ys0, Ys1), 540 pwp_attach(Xs, Ys1, Ys). 541pwp_attach(X, [X|Ys], Ys). 542 543 544 545pwp_element('-', _, Kids, Use, How, _, M, Context, Xml) :- 546 !, 547 pwp_use(Use, How, Kids, M, Context, Xml). 548pwp_element(Tag, Atts, [Value], Use, How, Magic, M, Context, 549 element(Tag,Atts1,Kids1)) :- 550 verbatim_element(Tag), nonvar(Magic), atomic(Value), 551 !, 552 %% Apply substition of c(..)c variables also to content of 553 %% <script> and <style> tags: 554 pwp_substitute([cdata=Value|Atts], Magic, Context, 555 [cdata=Value1|Atts1]), 556 pwp_use(Use, How, [Value1], M, Context, Kids1). 557pwp_element(Tag, Atts, Kids, Use, How, Magic, M, Context, 558 element(Tag,Atts1,Kids1)) :- 559 ( nonvar(Magic) 560 -> pwp_substitute(Atts, Magic, Context, Atts1) 561 ; Atts1 = Atts 562 ), 563 pwp_use(Use, How, Kids, M, Context, Kids1). 564 565pwp_use('', _, Kids, M, Context, Kids1) :- 566 !, 567 pwp_list(Kids, Kids1, M, Context). 568pwp_use(Use, How, _, M, Context, Kids1) :- 569 atom_to_term(Use, Term, Bindings), 570 pwp_unite(Bindings, Context), 571 pwp_how(How, Term, M, Context, Kids1). 572 573pwp_how('text', Term, _,_, [CData]) :- 574 !, 575 pwp_use_codes(Term, Codes, []), 576 atom_codes(CData, Codes). 577pwp_how('xml', Term, _,_, Kids1) :- 578 ( Term == [] -> Kids1 = Term 579 ; Term = [_|_] -> Kids1 = Term 580 ; Kids1 = [Term] 581 ). 582pwp_how('text-file', Term, _,_, [CData]) :- 583 pwp_use_codes(Term, Codes, []), 584 atom_codes(FileName, Codes), 585 read_file_to_codes(FileName, FileCodes, []), 586 atom_codes(CData, FileCodes). 587pwp_how('xml-file', Term, _,_, Kids1) :- 588 pwp_use_codes(Term, Codes, []), 589 atom_codes(FileName, Codes), 590 load_xml_file(FileName, Kids1). 591pwp_how('pwp-file', Term, M,Context, Kids1) :- 592 pwp_use_codes(Term, Codes, []), 593 atom_codes(FileName, Codes), 594 ( memberchk('SCRIPT_DIRECTORY'=ScriptDir,Context) -> true 595 ; ScriptDir='.' 596 ), 597 absolute_file_name(FileName, PathName, [relative_to(ScriptDir)]), 598 load_xml_file(PathName, Kids0), 599 pwp_xml(M:Kids0, Kids1, Context), 600 !. 601 602 603pwp_substitute([], _, _, []). 604pwp_substitute([AV|AVs], Magic, Context, NewAvs) :- 605 AV = (Name = Value), 606 ( sub_atom(Value, _, _, _, Magic) 607 -> char_code(Magic, C), 608 atom_codes(Value, Codes), 609 pwp_split(Codes, C, B0, T0, A0, Type), 610 !, 611 ( pwp_substitute(B0, T0, A0, C, Context, V, Type) 612 -> NewAvs = [AV1|Atts1], 613 atom_codes(New_Value, V), 614 AV1 = (Name = New_Value) 615 ; Type == existence-> 616 NewAvs = Atts1 617 ), 618 pwp_substitute(AVs, Magic, Context, Atts1) 619 ). 620pwp_substitute([AV|AVs], Magic, Context, [AV|Atts1]) :- 621 pwp_substitute(AVs, Magic, Context, Atts1). 622 623 624pwp_substitute(B0, T0, A0, C, Context, V0, Type) :- 625 append(B0, V1, V0), 626 atom_codes(Atom, T0), 627 atom_to_term(Atom, Term, Bindings), 628 pwp_unite(Bindings, Context, _), 629 ( Type == value 630 -> pwp_use_codes(Term, V1, V2) 631 ; catch(Term, _, fail), 632 V2 = V1 633 ), 634 ( pwp_split(A0, C, B1, T1, A1, T2) 635 -> pwp_substitute(B1, T1, A1, C, Context, V2, T2) 636 ; V2 = A0 637 ). 638 639 640pwp_split(Codes, C, Before, Text, After, Type) :- 641 append(Before, [C,C1|Rest], Codes), 642 ( C1 == 0'( 643 -> Type = value, 644 C2 = 0') 645 ; C1 == 0'[, 646 Type = existence, 647 C2 = 0'] 648 ), 649 append(Text, [C2,C|After], Rest), 650 !. 651 652pwp_use_codes(format(Format), S0, S) :- 653 !, 654 pwp_format(Format, [], S0, S). 655pwp_use_codes(format(Format,Args), S0, S) :- 656 !, 657 pwp_format(Format, Args, S0, S). 658pwp_use_codes(write_canonical(Datum), S0, S) :- 659 !, 660 pwp_format('~k', [Datum], S0, S). 661pwp_use_codes(print(Datum), S0, S) :- 662 !, 663 pwp_format('~p', [Datum], S0, S). 664pwp_use_codes(writeq(Datum), S0, S) :- 665 !, 666 pwp_format('~q', [Datum], S0, S). 667pwp_use_codes(write(Datum), S0, S) :- 668 !, 669 pwp_format('~w', [Datum], S0, S). 670pwp_use_codes(Atomic, S0, S) :- 671 atomic(Atomic), 672 !, 673 ( number(Atomic) -> number_codes(Atomic, Codes) 674 ; atom(Atomic) -> atom_codes(Atomic, Codes) 675 ; string(Atomic) -> string_codes(Atomic, Codes) 676 ; pwp_format('~w', [Atomic], S0, S) 677 ), 678 append(Codes, S, S0). 679pwp_use_codes([X|Xs], S0, S) :- 680 pwp_is_codes([X|Xs]), 681 !, 682 append([X|Xs], S, S0). 683pwp_use_codes([X|Xs], S0, S) :- 684 !, 685 pwp_use_codes(Xs, X, S0, S). 686pwp_use_codes(Compound, S0, S) :- 687 Compound =.. [_,X|Xs], 688 pwp_use_codes(Xs, X, S0, S). 689 690 691 692pwp_use_codes([], X, S0, S) :- 693 !, 694 pwp_use_codes(X, S0, S). 695pwp_use_codes([Y|Ys], X, S0, S) :- 696 pwp_use_codes(X, S0, S1), 697 pwp_use_codes(Ys, Y, S1, S). 698 699 700 701%% pwp_is_codes(+String: any) 702% 703% is true when String is a list of integers and each of those 704% integers is a possible Unicode value (in the range U+0000..U+10FFFF). 705% Back in the days of ISO Latin 1 we would have checked for 0..255, 706% and way back in the days of ASCII for 0..127. Yes, there are more 707% than a million possible characters in Unicode; currently about 708% 100 000 of them are in use. 709 710pwp_is_codes([]). 711pwp_is_codes([C|Cs]) :- 712 integer(C), C >= 0, C =< 0x10FFFF, 713 pwp_is_codes(Cs). 714 715pwp_format(Format, Arguments, S0, S) :- 716 format(codes(S0, S), Format, Arguments). 717 718 719verbatim_element(script). 720verbatim_element(style)