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