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