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