% Test interface to HTML forms for NU-Prolog % Adapted from C code from Andrew Davison (http://www.cs.mu.oz.au/~ad), % who got it from elsewhere no doubt. % % Note: The Web tends to be 8-bit aware and supports character sets such % as ISO Latin-1. NU-Prolog is not really 8-bit aware. Internally % strings are implemented as lists of integers (plenty of bits) but various % string-hendling builtins have their own idea of what lists of integers % look like valid strings. Eg, format (with ~s), and putl give error % messages and write (and close relatives) will print a list of integers % when given 8-bit strings. put for single chars works ok, as does printf % with %s. Of course, even if your Prolog program handles 8-bit % characters correctly, the chances are that if the output goes anywhere % except /dev/null something else will break. Hopefully NU-Prolog will % be made fully 8-bit aware some time soon. % top level main(_) :- html_form_interface. % Outputs the required "Content-type:" line + blank line, % checks that the apropriate environment variables are set % correctly, reads chars from standard input, forms list of % name-value pairs, fixes escape sequences etc and passes list % to html_form/1, which is expected to write a HTML page to stdout. % % The argument to html_form/1 is a list of Name-Value pairs. % The Names are atoms which have *not* had escape sequences % processed. They are the names of the form fields, so you % had better not use any funny characters in the names! The % Values are strings which have had escape sequences processed. % I have no idea how non-ascii characters entered into a form % turn out. html_form_interface :- putl("Content-type: text/html\n\n"), (getenv('REQUEST_METHOD', "POST") -> true ; putl("This script should be referenced with a METHOD of POST.\n"), putl("If you don't understand this, read "), putl("forms overview.\n"), exit(1) ), (getenv('CONTENT_TYPE', "application/x-www-form-urlencoded") -> true ; putl("This script can only be used to decode form results.\n"), exit(1) ), getenv('CONTENT_LENGTH', CLS), intToString(CL, CLS), read_all(CL, Input), % read whole input inp_to_nvs(Input, NEVs), % convert to name-escaped_value pairs expand_esc_plus_nv(NEVs, NVs), html_form(NVs). % Given list of Name-Value pairs from form, % output new HTML page html_form(NVs) :- putl("\n"), putl("
\n"), putl("%s =\n", [N]), putl(V), putl("