2 openisis - an open implementation of the CDS/ISIS database
3 Version 0.8.x (patchlevel see file Version)
4 Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 see README for more information
24 $Id: tspr.c,v 1.7 2003/06/06 10:35:38 mawag Exp $
26 must be binary in order to use with #! lines
30 #include <stdlib.h> /* getenv */
31 #include <unistd.h> /* read/write */
34 #include "openisistcl.h"
36 #define CL_MAX 512*1024
39 static const char **opv;
42 static int urldec ( OpenIsisRec **preq, const char *q, int l )
44 OpenIsisRec *req = *preq;
45 const char *e = q + (0 <= l ? l : (l = strlen(q)));
47 while ( q < e && *q ) {
50 /* assume numeric tags */
51 len = openIsisA2il( q, e-q, &tag );
55 int cmplen = strlen(opv[i]);
58 if ( !memcmp( opv[i], q, cmplen ) && '=' == q[cmplen] ) {
66 if ( '=' != *q || 0 >= tag ) {
67 openIsisSMsg( OPENISIS_LOG_ERROR, "bad param %d '%.32s'", tag, q );
71 OPENISIS_RADD( req, tag, 0, e-q, !0 );
74 p = v = (char*)req->field[req->len-1].val;
77 case '+': *p = ' '; continue;
78 default: *p = *q; continue;
79 case '%': /* dehex 2 */
82 if ( '0' <= *q && *q <= '9' )
84 else if ( 'A' <= *q && *q <= 'F' )
86 else if ( 'a' <= *q && *q <= 'f' )
89 return 2; /* carp bad hex */
92 if ( '0' <= *q && *q <= '9' )
94 else if ( 'A' <= *q && *q <= 'F' )
96 else if ( 'a' <= *q && *q <= 'f' )
107 req->used += req->field[req->len-1].len = p - v;
118 extern int Oratcl_Init ( Tcl_Interp *ip );
121 extern int Pgtcl_Init ( Tcl_Interp *ip );
125 static int appinit ( Tcl_Interp *interp )
127 if ( TCL_OK != Tcl_Init( interp )
128 /* done in new session
129 || TCL_OK != OpenisisTcl_Init( interp )
132 || TCL_OK != Oratcl_Init( interp )
135 || TCL_OK != Pgtcl_Init( interp )
142 static char prelude[] = "Content-Type: text/html\r\n\r\n";
143 static char code[] = "package require openIsis 0.8\nopenIsis::Tspr $tsp";
145 int main ( int argc, const char **argv )
147 OpenIsisRec *req = 0;
158 /* Tcl_Main( argc, argv, appinit ); */
159 Tcl_FindExecutable(argv[0]); /* adds ../lib to auto_path */
160 interp = Tcl_CreateInterp();
162 if ( (q = getenv("SCRIPT_NAME")) )
163 OPENISIS_RADDS( req, -10, q, !0 );
164 if ( (q = getenv("PATH_INFO")) )
165 OPENISIS_RADDS( req, -11, q, !0 );
166 if ( (q = getenv("REMOTE_USER")) )
167 OPENISIS_RADDS( req, -12, q, !0 );
168 /* URL-decode env(QUERY_STRING) into request record */
169 if ( (q = getenv("QUERY_STRING")) && (ret = urldec( &req, q, -1 )) )
171 if ( (q = getenv("CONTENT_LENGTH")) && *q ) {
173 int l = openIsisA2i( q, -1 );
176 if ( !(q = getenv("CONTENT_TYPE"))
177 || strcmp("application/x-www-form-urlencoded", q)
180 if ( l != read( 0, buf, l ) )
183 if ( (ret = urldec( &req, buf, l )) )
186 sid = openIsisTclNewSession( interp );
187 rid = openIsisTclCreateRecCmd( sid, "request", 0, 0 );
189 Tcl_SetVar( interp, "tsp", (char*)argv[argc-1], 0 );
190 write( 1, prelude, sizeof(prelude)-1 );
191 if ( openIsisTclEval( sid, 1, &rid, &req, code ) ) {
192 const char *errorInfo
193 = Tcl_GetVar( interp, "errorInfo", TCL_GLOBAL_ONLY );
194 openIsisSMsg( OPENISIS_LOG_ERROR, "error in tsp\n%s",
195 errorInfo ? errorInfo : "-" );