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