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