Lesson 10

From VistApedia
Jump to: navigation, search

Linux / Apache / GT.M / Web Application Lesson 10.

By Ben Irwin, 
Copied from: http://www.doggiedudes.com/fscc/list.htm
Tutorial Home: M Web Tutorials
Prev: Lesson 9
Next: Lesson 11

Placing the following htm file in your apache web page directory and calling it with a line simular to -

http://mandrake/libr10.htm

- will produce an input form with two text input boxes, one hidden input, and one submit button.

IMPORTANT NOTICE: ALL OF THE '<' CHARACTERS HAVE BEEN REPLACED BY A '{' CHARACTERS BECAUSE THE WIKI WAS NOT FORMATTING PROPERLY OTHERWISE. LIKEWISE, ALL THE '>' characters WERE CHANGED TO '}'.


libr10.htm
--------------------------------------------------------------------------
{head}
{title}
Lesson 10 Input Form
{/title}
{/head}
{body}
{form method=post Action="../cgi-bin/library10.cgi"}
{table}
{tr}{td}Full Name:{/td}{td}{input type=text name="fn" size="20"}{/td}{/tr}
{tr}{td}Random Characters:{/td}{td}{input type=text name="rc" size="20"}{/td}{/tr}
{tr}{td} &nbsp {/td}{td} &nbsp {input type=hidden name="hd" value="DANGER"}{/td}{/tr}
{tr}{td colspan=2 align=center}{input type=submit{/td}{/tr}
{/table}
{/form}
{/body}
{/html}
--------------------------------------------------------------------------


When the submit button is presses or the enter key is pressed the about form will call a cgi script as listed below in the cgi-bin directory. This cgi-script will read standard input into the variable XXXX, then setup gtm variables and call the mumps routine ZZLIB10.

library10.cgi
--------------------------------------------------------------------------
#!/bin/sh
read XXXX
export XXXX
cd /usr/local/gtm/VISTA
export gtm_dist=/usr/local/gtm
export gtmroutines="$gtm_dist/VISTA/uo($gtm_dist/VISTA/ur) $gtm_dist"
export gtmgbldir=/usr/local/gtm/VISTA/mumps.gld
export PATH=$PATH:$gtm_dist
mumps -run ZZLIB10
--------------------------------------------------------------------------


The following mumps routine will display the raw data read into the XXXX variable. It will also call a function called WEBIN to test are parse the input variables. The two inputs to the WEBIN function is the actual input line (XXXX) and a list of acceptable variables delimited by the "|", (formerly know as the broken vertical bar, but it isn't broken on most screens anymore).

As written the following routine will try to write the three input variables, fn, rc, and hd. However, because hd is not in variable list in the function call, that variable will be undefined and will print blank because of the use of $G(hd).

This routine and fuction are just a starting point to show how mumps can be used to parse a web input line.

ZZLIB10.m
--------------------------------------------------------------------------
ZZLIB10 ; ROUTINE TO CREATE A WEB PAGE USING THE ^ZZLIB3 GLOBAL.
       ;
       W "Content-type: text/html",!!
       W "{html}",!
       W "{head}",!
       W "{title}FORM VARIABLE PROCESSING.{/title}",!
       W "{/head}",!
       W "{body}",!
       ;
       S XXXX=$ZTRNLNM("XXXX")
       ;
       W "{pre}",!
       W XXXX,!!
       S CHK=$$WEBIN($ZTRNLNM("XXXX"),"fn|rc")
       I CHK=0 W "INVALID INPUT" G END
       ;
       W $G(fn),!
       W $G(rc),!
       W $G(hd),!
       ;
END    ; TAG USED TO QUIT ON INPUT ERROR
       ;
       W "{/pre}",!
       W "{/body}",!
       W "{/html}",!
       ;
       Q
       ;
WEBIN(WEBIN,VARS) ; Routine to Split the Input Line from a Web Form.
       ;
       ; Input: WEBIN - Web QUERY_STRING, POST, or GET variable.
       ;        VARS  - String of variables to set.  Can not include
       ;                XX, ERR, VAR, VARS, VARSA, VAL, WEBIN.
       ;
       ; Output: Web Form Variables
       ;         Function Value: 0 - Error
       ;                         1 - Successful
       ;
       N XX,VAR,VAL,VARSA,ERR
       ;
       ; Check for missing input variables.
       Q:$L(WEBIN)=0 0
       Q:$L(WEBIN,"=")<2 0
       Q:$L(VARS)=0 0
       ;
       ; Disable setting global variables and routine values.
       Q:$L(WEBIN,"^")>1 0
       Q:$L(WEBIN,"%5E")>1 0
       ;
       ; Set up acceptable output variables array.
       F XX=1:1:$L(VARS,"|") S:$P(VARS,"|",XX)'="" VARSA($P(VARS,"|",XX))=""
       ;
       ; Protect function variables.
       Q:$D(VARSA("XX"))=1 0
       Q:$D(VARSA("ERR"))=1 0
       Q:$D(VARSA("VAR"))=1 0
       Q:$D(VARSA("VARS"))=1 0
       Q:$D(VARSA("VARSA"))=1 0
       Q:$D(VARSA("VAL"))=1 0
       Q:$D(VARSA("WEBIN"))=1 0
       ;
       ; Translate the "+" to spaces.
       S WEBIN=$TR(WEBIN,"+"," ")
       ;
       ; Move through each Variable=Value pair.
       F XX=1:1:$L(WEBIN,"&") D
       .S VAR=$P($P(WEBIN,"&",XX),"=",1)
       .S VAL=$$PARSE($P($P(WEBIN,"&",XX),"=",2))
       .S:$D(VARSA(VAR))=1 @VAR=VAL
       ;
       Q 1
       ;
PARSE(PIN); Parse Input Line.  Assigning charicators to the ascii values.
       ;
       N YY,POUT,PIECE
       I $L(PIN,"%")<2 Q PIN
       S POUT=$P(PIN,"%",1)
       F YY=2:1:$L(PIN,"%") D
       .S PIECE=$P(PIN,"%",YY)
       .S POUT=POUT_$C($$FUNC^%HD($E(PIECE,1,2)))
       .S POUT=POUT_$E(PIECE,3,$L(PIECE))
       ;
       Q POUT


I used the input values shown below.

Ben Irwin
abc#$%345

And received the following results.

fn=Ben+Irwin&rc=abc%23%24%25345&hd=DANGER
Ben Irwin
abc#$%345
Tutorial Home: M Web Tutorials
Prev: Lesson 9
Next: Lesson 11