First work on Explain.
[ir-tcl-moved-to-github.git] / ir-tclp.h
1 /*
2  * IR toolkit for tcl/tk
3  * (c) Index Data 1995
4  * See the file LICENSE for details.
5  * Sebastian Hammer, Adam Dickmeiss
6  *
7  * $Log: ir-tclp.h,v $
8  * Revision 1.34  1996-08-16 15:07:47  adam
9  * First work on Explain.
10  *
11  * Revision 1.33  1996/07/03  13:31:13  adam
12  * The xmalloc/xfree functions from YAZ are used to manage memory.
13  *
14  * Revision 1.32  1996/03/20  13:54:05  adam
15  * The Tcl_File structure is only manipulated in the Tk-event interface
16  * in tkinit.c.
17  *
18  * Revision 1.31  1996/03/05  09:21:19  adam
19  * Bug fix: memory used by GRS records wasn't freed.
20  * Rewrote some of the error handling code - the connection is always
21  * closed before failback is called.
22  * If failback is defined the send APDU methods (init, search, ...) will
23  * return OK but invoke failback (as is the case if the write operation
24  * fails).
25  * Bug fix: ref_count in assoc object could grow if fraction of PDU was
26  * read.
27  *
28  * Revision 1.30  1996/02/29  15:30:23  adam
29  * Export of IrTcl functionality to extensions.
30  *
31  * Revision 1.29  1996/02/26  18:38:33  adam
32  * Work on export of set methods.
33  *
34  * Revision 1.28  1996/02/23  17:31:41  adam
35  * More functions made available to the wais tcl extension.
36  *
37  * Revision 1.27  1996/02/23  13:41:41  adam
38  * Work on public access to simple ir class system.
39  *
40  * Revision 1.26  1996/02/21  10:16:20  adam
41  * Simplified select handling. Only one function ir_tcl_select_set has
42  * to be externally defined.
43  *
44  * Revision 1.25  1996/02/05  17:58:04  adam
45  * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
46  *
47  * Revision 1.24  1996/01/29  11:35:27  adam
48  * Bug fix: cs_type member renamed to comstackType to avoid conflict with
49  * cs_type macro defined by YAZ.
50  *
51  * Revision 1.23  1996/01/19  16:22:40  adam
52  * New method: apduDump - returns information about last incoming APDU.
53  *
54  * Revision 1.22  1996/01/10  09:18:44  adam
55  * PDU specific callbacks implemented: initRespnse, searchResponse,
56  *  presentResponse and scanResponse.
57  * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
58  *
59  * Revision 1.21  1996/01/04  16:12:14  adam
60  * Setting PDUType renamed to eventType.
61  *
62  * Revision 1.20  1996/01/04  11:05:23  adam
63  * New setting: PDUType - returns type of last PDU returned from the target.
64  * Fixed a bug in configure/Makefile.
65  *
66  * Revision 1.19  1995/11/13  09:55:46  adam
67  * Multiple records at a position in a result-set with differnt
68  * element specs.
69  *
70  * Revision 1.18  1995/10/18  16:42:44  adam
71  * New settings: smallSetElementSetNames and mediumSetElementSetNames.
72  *
73  * Revision 1.17  1995/10/16  17:00:56  adam
74  * New setting: elementSetNames.
75  * Various client improvements. Medium presentation format looks better.
76  *
77  * Revision 1.16  1995/09/20  11:37:01  adam
78  * Configure searches for tk4.1 and tk7.5.
79  * Work on GRS.
80  *
81  * Revision 1.15  1995/08/29  15:30:15  adam
82  * Work on GRS records.
83  *
84  * Revision 1.14  1995/08/04  11:32:40  adam
85  * More work on output queue. Memory related routines moved
86  * to mem.c
87  *
88  * Revision 1.13  1995/08/03  13:23:00  adam
89  * Request queue.
90  *
91  * Revision 1.12  1995/07/28  10:28:38  adam
92  * First work on request queue.
93  *
94  * Revision 1.11  1995/06/20  08:07:35  adam
95  * New setting: failInfo.
96  * Working on better cancel mechanism.
97  *
98  * Revision 1.10  1995/06/16  12:28:20  adam
99  * Implemented preferredRecordSyntax.
100  * Minor changes in diagnostic handling.
101  * Record list deleted when connection closes.
102  *
103  * Revision 1.9  1995/06/14  15:08:01  adam
104  * Bug fix in cascade-target-list. Uses yaz-version.h.
105  *
106  * Revision 1.8  1995/06/14  13:37:18  adam
107  * Setting recordType implemented.
108  * Setting implementationVersion implemented.
109  * Settings implementationId / implementationName edited.
110  *
111  * Revision 1.7  1995/06/01  07:31:28  adam
112  * Rename of many typedefs -> IrTcl_...
113  *
114  * Revision 1.6  1995/05/31  08:36:40  adam
115  * Bug fix in client.tcl: didn't save options on clientrc.tcl.
116  * New method: referenceId. More work on scan.
117  *
118  * Revision 1.5  1995/05/29  08:44:25  adam
119  * Work on delete of objects.
120  *
121  * Revision 1.4  1995/05/26  11:44:10  adam
122  * Bugs fixed. More work on MARC utilities and queries. Test
123  * client is up-to-date again.
124  *
125  * Revision 1.3  1995/05/26  08:54:17  adam
126  * New MARC utilities. Uses prefix query.
127  *
128  * Revision 1.2  1995/05/24  14:10:23  adam
129  * Work on idAuthentication, protocolVersion and options.
130  *
131  * Revision 1.1  1995/05/23  15:34:49  adam
132  * Many new settings, userInformationField, smallSetUpperBound, etc.
133  * A number of settings are inherited when ir-set is executed.
134  * This version is incompatible with the graphical test client (client.tcl).
135  *
136  */
137
138 #ifndef IR_TCLP_H
139 #define IR_TCLP_H
140
141 #include <tcl.h>
142
143 #include <log.h>
144 #include <pquery.h>
145 #if CCL2RPN
146 #include <yaz-ccl.h>
147 #endif
148
149 #include <comstack.h>
150 #include <tcpip.h>
151
152 #if MOSI
153 #include <xmosi.h>
154 #endif
155
156 #include <yaz-version.h>
157 #include <odr.h>
158 #include <proto.h>
159 #include <oid.h>
160 #include <diagbib1.h>
161 #include <xmalloc.h>
162
163 #include "ir-tcl.h"
164
165 typedef struct {
166     char *name;
167     int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
168     char *desc;
169 } IrTcl_Method;
170
171 typedef struct {
172     void *obj;
173     IrTcl_Method *tab;
174 } IrTcl_Methods;
175
176 typedef struct {
177     char      **databaseNames;
178     int         num_databaseNames;
179     char       *queryType;
180     enum oid_value *preferredRecordSyntax;
181     int         replaceIndicator;
182     char       *referenceId;
183
184     char       *elementSetNames;
185     char       *smallSetElementSetNames;
186     char       *mediumSetElementSetNames;
187
188     int         smallSetUpperBound;
189     int         largeSetLowerBound;
190     int         mediumSetPresentNumber;
191 } IrTcl_SetCObj;
192     
193 typedef struct {
194     int         ref_count;
195
196     char       *comstackType;
197     int         protocol_type;
198     int         failInfo;
199     COMSTACK    cs_link;
200
201     int         state;
202
203     int         preferredMessageSize;
204     int         maximumRecordSize;
205     Odr_bitmask options;
206     Odr_bitmask protocolVersion;
207
208     char       *idAuthenticationOpen;
209     char       *idAuthenticationGroupId;
210     char       *idAuthenticationUserId;
211     char       *idAuthenticationPassword;
212
213     char       *implementationName;
214     char       *implementationId;
215     char       *implementationVersion;
216     int        initResult;
217     char       *targetImplementationName;
218     char       *targetImplementationId;
219     char       *targetImplementationVersion;
220     char       *userInformationField;
221
222     char       *hostname;
223     char       *eventType;
224    
225     char       *buf_in;
226     int         len_in;
227     ODR         odr_in;
228     ODR         odr_out;
229     ODR         odr_pr;
230
231     Tcl_Interp *interp;
232     char       *callback;
233     char       *failback;
234     char       *initResponse;
235
236     int        apduLen;
237     int        apduOffset;
238
239 #if CCL2RPN
240     CCL_bibset  bibset;
241 #endif
242     struct IrTcl_Request_ *request_queue;
243
244     IrTcl_SetCObj   set_inher;
245 } IrTcl_Obj;
246
247 typedef struct IrTcl_Request_ {
248     struct IrTcl_Request_ *next; 
249
250     char       *object_name;
251     
252     char       *buf_out;
253     int         len_out;
254
255     char       *callback;
256 } IrTcl_Request;
257
258 typedef struct {
259     int condition;
260     char *addinfo;
261 } IrTcl_Diagnostic;
262
263 struct GRS_Record_entry {
264     int tagType;
265     int tagWhich;
266     union {
267         int num;
268         char *str;
269     } tagVal;
270     int dataWhich;
271     union {
272         struct IrTcl_GRS_Record_ *sub;
273         char *str;
274         struct {
275             int len;
276             char *buf;
277         } octets;
278         int num;
279         int bool;
280     } tagData;
281 };
282
283 typedef struct IrTcl_GRS_Record_ {
284     int noTags;
285     struct GRS_Record_entry *entries;
286 } IrTcl_GRS_Record;
287
288 typedef struct IrTcl_RecordList_ {
289     int no;
290     char *elements;
291     int which;
292     union {
293         struct {
294             char *buf;
295             size_t size;
296             union {
297                 IrTcl_GRS_Record *grs1;
298             } u;
299             enum oid_value type;
300         } dbrec;
301         struct {
302             int num;
303             IrTcl_Diagnostic *list;
304         } surrogateDiagnostics;
305     } u;
306     struct IrTcl_RecordList_ *next;
307 } IrTcl_RecordList;
308
309 typedef struct IrTcl_SetObj_ {
310     IrTcl_Obj  *parent;
311     int         searchStatus;
312     int         presentStatus;
313     int         resultCount;
314     int         nextResultSetPosition;
315     int         start;
316     int         number;
317     int         numberOfRecordsReturned;
318     char       *setName;
319     char       *recordElements;
320     int         recordFlag;
321     int         which;
322     int         nonSurrogateDiagnosticNum;
323     char       *searchResponse;
324     char       *presentResponse;
325     IrTcl_Diagnostic *nonSurrogateDiagnosticList;
326     IrTcl_RecordList *record_list;
327     IrTcl_SetCObj set_inher;
328 } IrTcl_SetObj;
329
330 typedef struct IrTcl_ScanEntry_ {
331     int         which;
332     union {
333         struct {
334             char *buf;
335             int  globalOccurrences;
336         } term;
337         struct {
338             IrTcl_Diagnostic *list;
339             int num;
340         } diag;
341     } u;
342 } IrTcl_ScanEntry;
343
344 typedef struct IrTcl_ScanObj_ {
345     IrTcl_Obj   *parent;
346     int         stepSize;
347     int         numberOfTermsRequested;
348     int         preferredPositionInResponse;
349
350     int         scanStatus;
351     int         numberOfEntriesReturned;
352     int         positionOfTerm;
353
354     int         entries_flag;
355     int         which;
356
357     int         num_entries;
358     int         num_diagRecs;
359
360     char        *scanResponse;
361     IrTcl_ScanEntry *entries;
362     IrTcl_Diagnostic  *nonSurrogateDiagnosticList;
363     int         nonSurrogateDiagnosticNum;
364 } IrTcl_ScanObj;
365
366 struct ir_named_entry {
367     char *name;
368     int  pos;
369 };
370
371 int ir_tcl_get_marc (Tcl_Interp *interp, const char *buf,
372                      int argc, char **argv);
373 int ir_tcl_send_APDU (Tcl_Interp *interp, IrTcl_Obj *p, Z_APDU *apdu,
374                       const char *msg, const char *object_name);
375 int ir_tcl_send_q (IrTcl_Obj *p, IrTcl_Request *rq, const char *msg);
376 void ir_tcl_del_q (IrTcl_Obj *p);
377 int ir_tcl_strdup (Tcl_Interp *interp, char** p, const char *s);
378 int ir_tcl_strdel (Tcl_Interp *interp, char **p);
379
380 char *ir_tcl_fread_marc (FILE *inf, size_t *size);
381 void ir_tcl_grs_mk (Z_GenericRecord *r, IrTcl_GRS_Record **grs_record);
382 void ir_tcl_grs_del (IrTcl_GRS_Record **grs_record);
383 int ir_tcl_get_grs (Tcl_Interp *interp, IrTcl_GRS_Record *grs_record, 
384                      int argc, char **argv);
385
386 int ir_tcl_get_explain (Tcl_Interp *interp, Z_ExplainRecord *rec,
387                         int argc, char **argv);
388
389 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
390                    IrTcl_Methods *tab, int *ret);
391 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv);
392
393 typedef struct {
394     const char *name;
395     int (*ir_init)   (ClientData clientData, Tcl_Interp *interp,
396                       int argc, char **argv, ClientData *subData,
397                       ClientData parentData);
398     int (*ir_method) (ClientData clientData, Tcl_Interp *interp,
399                       int argc, char **argv);
400     void (*ir_delete)(ClientData clientData);
401 } IrTcl_Class;
402
403 extern IrTcl_Class ir_obj_class;
404 extern IrTcl_Class ir_set_obj_class;
405
406 void ir_select_add (int fd, void *obj);
407 void ir_select_add_write (int fd, void *obj);
408 void ir_select_remove (int fd, void *obj);
409 void ir_select_remove_write (int fd, void *obj);
410
411 int ir_tcl_eval (Tcl_Interp *interp, const char *command);
412 void ir_tcl_disconnect (IrTcl_Obj *p);
413
414 #define IR_TCL_FAIL_CONNECT      1
415 #define IR_TCL_FAIL_READ         2
416 #define IR_TCL_FAIL_WRITE        3
417 #define IR_TCL_FAIL_IN_APDU      4
418 #define IR_TCL_FAIL_UNKNOWN_APDU 5
419
420 #define IR_TCL_R_Idle            0
421 #define IR_TCL_R_Writing         1
422 #define IR_TCL_R_Waiting         2
423 #define IR_TCL_R_Reading         3
424 #define IR_TCL_R_Connecting      4
425 #endif