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