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