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