2 * IR toolkit for tcl/tk
5 * $Id: ir-tcl.c,v 1.3 1995-03-09 08:35:53 adam Exp $
27 int preferredMessageSize;
28 int maximumMessageSize;
30 Odr_bitmask protocolVersion;
31 char *idAuthentication;
32 char *implementationName;
33 char *implementationId;
48 int smallSetUpperBound;
49 int largeSetLowerBound;
50 int mediumSetPresentNumber;
53 int num_databaseNames;
62 int (*method) (void * obj, Tcl_Interp *interp, int argc, char **argv);
65 static int do_disconnect (void *obj,Tcl_Interp *interp, int argc, char **argv);
68 * get_parent_info: Returns information about parent object.
70 static int get_parent_info (Tcl_Interp *interp, const char *name,
71 Tcl_CmdInfo *parent_info)
73 char parent_name[128];
74 const char *csep = strrchr (name, '.');
79 interp->result = "missing .";
85 memcpy (parent_name, name, pos);
86 parent_name[pos] = '\0';
87 if (!Tcl_GetCommandInfo (interp, parent_name, parent_info))
93 * ir_method: Search for method in table and invoke method handler
95 int ir_method (void *obj, Tcl_Interp *interp, int argc, char **argv,
100 if (!strcmp (tab->name, argv[1]))
101 return (*tab->method)(obj, interp, argc, argv);
104 Tcl_AppendResult (interp, "unknown method: ", argv[1], NULL);
109 * ir_asc2bitmask: Ascii to ODR bitmask conversion
111 int ir_asc2bitmask (const char *asc, Odr_bitmask *ob)
113 const char *cp = asc + strlen(asc);
120 ODR_MASK_SET (ob, bitno);
127 * ir_strdup: Duplicate string
129 int ir_strdup (Tcl_Interp *interp, char** p, char *s)
131 *p = malloc (strlen(s)+1);
134 interp->result = "malloc fail";
141 /* ------------------------------------------------------- */
144 * do_init_request: init method on IR object
146 static int do_init_request (void *obj, Tcl_Interp *interp,
147 int argc, char **argv)
156 req.options = &p->options;
157 req.protocolVersion = &p->protocolVersion;
158 req.preferredMessageSize = &p->preferredMessageSize;
159 req.maximumRecordSize = &p->maximumMessageSize;
161 req.idAuthentication = p->idAuthentication;
162 req.implementationId = p->implementationId;
163 req.implementationName = p->implementationName;
164 req.implementationVersion = "0.1";
165 req.userInformationField = 0;
167 apdu.u.initRequest = &req;
168 apdu.which = Z_APDU_initRequest;
171 if (!z_APDU (p->odr_out, &apdup, 0))
173 interp->result = odr_errlist [odr_geterror (p->odr_out)];
174 odr_reset (p->odr_out);
177 sbuf = odr_getbuf (p->odr_out, &slen);
178 if (cs_put (p->cs_link, sbuf, slen) < 0)
180 interp->result = "cs_put failed in init";
183 printf("Sent initializeRequest (%d bytes).\n", slen);
188 * do_protocolVersion: Set protocol Version
190 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
191 int argc, char **argv)
194 ir_asc2bitmask (argv[2], &((IRObj *) obj)->protocolVersion);
199 * do_options: Set options
201 static int do_options (void *obj, Tcl_Interp *interp,
202 int argc, char **argv)
205 ir_asc2bitmask (argv[2], &((IRObj *) obj)->options);
210 * do_preferredMessageSize: Set preferred message size
212 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
213 int argc, char **argv)
217 if (Tcl_GetInt (interp, argv[2],
218 &((IRObj *)obj)->preferredMessageSize)==TCL_ERROR)
221 sprintf (interp->result, "%d", ((IRObj *)obj)->preferredMessageSize);
226 * do_maximumMessageSize: Set maximum message size
228 static int do_maximumMessageSize (void *obj, Tcl_Interp *interp,
229 int argc, char **argv)
233 if (Tcl_GetInt (interp, argv[2],
234 &((IRObj *)obj)->maximumMessageSize)==TCL_ERROR)
237 sprintf (interp->result, "%d", ((IRObj *)obj)->maximumMessageSize);
243 * do_implementationName: Set Implementation Name.
245 static int do_implementationName (void *obj, Tcl_Interp *interp,
246 int argc, char **argv)
250 free (((IRObj*)obj)->implementationName);
251 if (ir_strdup (interp, &((IRObj*) obj)->implementationName, argv[2])
255 Tcl_AppendResult (interp, ((IRObj*)obj)->implementationName,
261 * do_implementationId: Set Implementation Name.
263 static int do_implementationId (void *obj, Tcl_Interp *interp,
264 int argc, char **argv)
268 free (((IRObj*)obj)->implementationId);
269 if (ir_strdup (interp, &((IRObj*) obj)->implementationId, argv[2])
273 Tcl_AppendResult (interp, ((IRObj*)obj)->implementationId,
279 * do_idAuthentication: Set id Authentication
281 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
282 int argc, char **argv)
286 free (((IRObj*)obj)->idAuthentication);
287 if (ir_strdup (interp, &((IRObj*) obj)->idAuthentication, argv[2])
291 Tcl_AppendResult (interp, ((IRObj*)obj)->idAuthentication,
297 * do_connect: connect method on IR object
299 static int do_connect (void *obj, Tcl_Interp *interp,
300 int argc, char **argv)
307 interp->result = "missing hostname";
310 if (cs_type(p->cs_link) == tcpip_type)
312 addr = tcpip_strtoaddr (argv[2]);
315 interp->result = "tcpip_strtoaddr fail";
318 printf ("tcp/ip connect %s\n", argv[2]);
320 else if (cs_type (p->cs_link) == mosi_type)
322 addr = mosi_strtoaddr (argv[2]);
325 interp->result = "mosi_strtoaddr fail";
328 printf ("mosi connect %s\n", argv[2]);
330 if (cs_connect (p->cs_link, addr) < 0)
332 interp->result = "cs_connect fail";
333 do_disconnect (p, interp, argc, argv);
336 ir_select_add (cs_fileno (p->cs_link), p);
341 * do_disconnect: disconnect method on IR object
343 static int do_disconnect (void *obj, Tcl_Interp *interp,
344 int argc, char **argv)
348 ir_select_remove (cs_fileno (p->cs_link), p);
349 if (cs_type (p->cs_link) == tcpip_type)
351 cs_close (p->cs_link);
352 p->cs_link = cs_create (tcpip_type);
354 else if (cs_type (p->cs_link) == mosi_type)
356 cs_close (p->cs_link);
357 p->cs_link = cs_create (mosi_type);
361 interp->result = "unknown comstack type";
368 * do_comstack: comstack method on IR object
370 static int do_comstack (void *obj, Tcl_Interp *interp,
371 int argc, char **argv)
375 if (!strcmp (argv[2], "tcpip"))
376 ((IRObj *)obj)->cs_link = cs_create (tcpip_type);
377 else if (!strcmp (argv[2], "mosi"))
378 ((IRObj *)obj)->cs_link = cs_create (mosi_type);
381 interp->result = "wrong comstack type";
385 if (cs_type(((IRObj *)obj)->cs_link) == tcpip_type)
386 interp->result = "tcpip";
387 else if (cs_type(((IRObj *)obj)->cs_link) == mosi_type)
388 interp->result = "comstack";
393 * do_callback: add callback
395 static int do_callback (void *obj, Tcl_Interp *interp,
396 int argc, char **argv)
403 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
411 * do_databaseNames: specify database names
413 static int do_databaseNames (void *obj, Tcl_Interp *interp,
414 int argc, char **argv)
421 interp->result = "wrong # args";
424 if (p->databaseNames)
426 for (i=0; i<p->num_databaseNames; i++)
427 free (p->databaseNames[i]);
428 free (p->databaseNames);
430 p->num_databaseNames = argc - 2;
431 if (!(p->databaseNames = malloc (sizeof(*p->databaseNames) *
432 p->num_databaseNames)))
434 interp->result = "malloc fail";
437 for (i=0; i<p->num_databaseNames; i++)
439 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
447 * ir_obj_method: IR Object methods
449 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
450 int argc, char **argv)
452 static IRMethod tab[] = {
453 { "comstack", do_comstack },
454 { "connect", do_connect },
455 { "protocolVersion", do_protocolVersion },
456 { "options", do_options },
457 { "preferredMessageSize", do_preferredMessageSize },
458 { "maximumMessageSize", do_maximumMessageSize },
459 { "implementationName", do_implementationName },
460 { "implementationId", do_implementationId },
461 { "idAuthentication", do_idAuthentication },
462 { "init", do_init_request },
463 { "disconnect", do_disconnect },
464 { "callback", do_callback },
465 { "databaseNames", do_databaseNames},
470 interp->result = "wrong # args";
473 return ir_method (clientData, interp, argc, argv, tab);
477 * ir_obj_delete: IR Object disposal
479 static void ir_obj_delete (ClientData clientData)
481 free ( (void*) clientData);
485 * ir_obj_mk: IR Object creation
487 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
488 int argc, char **argv)
494 interp->result = "wrong # args";
497 obj = malloc (sizeof(*obj));
500 interp->result = "malloc fail";
503 obj->cs_link = cs_create (tcpip_type);
505 obj->maximumMessageSize = 10000;
506 obj->preferredMessageSize = 4096;
508 obj->idAuthentication = NULL;
510 if (ir_strdup (interp, &obj->implementationName, "TCL/TK on YAZ")
514 if (ir_strdup (interp, &obj->implementationId, "TCL/TK/YAZ")
518 obj->smallSetUpperBound = 0;
519 obj->largeSetLowerBound = 2;
520 obj->mediumSetPresentNumber = 0;
521 obj->replaceIndicator = 1;
522 obj->databaseNames = NULL;
523 obj->num_databaseNames = 0;
525 ODR_MASK_ZERO (&obj->protocolVersion);
526 ODR_MASK_SET (&obj->protocolVersion, 0);
527 ODR_MASK_SET (&obj->protocolVersion, 1);
529 ODR_MASK_ZERO (&obj->options);
530 ODR_MASK_SET (&obj->options, 0);
532 obj->odr_in = odr_createmem (ODR_DECODE);
533 obj->odr_out = odr_createmem (ODR_ENCODE);
534 obj->odr_pr = odr_createmem (ODR_PRINT);
536 obj->len_out = 10000;
537 obj->buf_out = malloc (obj->len_out);
540 interp->result = "malloc fail";
543 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out);
548 obj->callback = NULL;
550 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
551 (ClientData) obj, ir_obj_delete);
555 /* ------------------------------------------------------- */
557 * do_search: Do search request
559 static int do_search (void *o, Tcl_Interp *interp,
560 int argc, char **argv)
565 static Odr_oid bib1[] = {1, 2, 840, 10003, 3, 1, -1};
568 IRObj *p = obj->parent;
574 interp->result = "wrong # args";
577 if (!p->num_databaseNames)
579 interp->result = "no databaseNames";
582 apdu.which = Z_APDU_searchRequest;
583 apdu.u.searchRequest = &req;
587 req.smallSetUpperBound = &p->smallSetUpperBound;
588 req.largeSetLowerBound = &p->largeSetLowerBound;
589 req.mediumSetPresentNumber = &p->mediumSetPresentNumber;
590 req.replaceIndicator = &p->replaceIndicator;
591 req.resultSetName = "Default";
592 req.num_databaseNames = p->num_databaseNames;
593 req.databaseNames = p->databaseNames;
594 req.smallSetElementSetNames = 0;
595 req.mediumSetElementSetNames = 0;
596 req.preferredRecordSyntax = 0;
599 query.which = Z_Query_type_2;
600 query.u.type_2 = &ccl_query;
601 ccl_query.buf = argv[2];
602 ccl_query.len = strlen (argv[2]);
604 if (!z_APDU (p->odr_out, &apdup, 0))
606 interp->result = odr_errlist [odr_geterror (p->odr_out)];
607 odr_reset (p->odr_out);
610 sbuf = odr_getbuf (p->odr_out, &slen);
611 if (cs_put (p->cs_link, sbuf, slen) < 0)
613 interp->result = "cs_put failed in init";
620 * do_query: Set query for a Set Object
622 static int do_query (void *obj, Tcl_Interp *interp,
623 int argc, char **argv)
630 * ir_set_obj_method: IR Set Object methods
632 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
633 int argc, char **argv)
635 static IRMethod tab[] = {
636 { "query", do_query },
637 { "search", do_search },
643 interp->result = "wrong # args";
646 return ir_method (clientData, interp, argc, argv, tab);
650 * ir_set_obj_delete: IR Set Object disposal
652 static void ir_set_obj_delete (ClientData clientData)
654 free ( (void*) clientData);
658 * ir_set_obj_mk: IR Set Object creation
660 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
661 int argc, char **argv)
663 Tcl_CmdInfo parent_info;
668 interp->result = "wrong # args";
671 if (get_parent_info (interp, argv[1], &parent_info) == TCL_ERROR)
673 interp->result = "No parent";
676 obj = malloc (sizeof(*obj));
679 interp->result = "malloc fail";
682 obj->parent = (IRObj *) parent_info.clientData;
683 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
684 (ClientData) obj, ir_set_obj_delete);
688 /* ------------------------------------------------------- */
690 static void ir_searchResponse (void *obj, Z_SearchResponse *searchrs)
692 if (searchrs->searchStatus)
693 printf("Search was a success.\n");
695 printf("Search was a bloomin' failure.\n");
696 printf("Number of hits: %d, setno %d\n", *searchrs->resultCount, 1);
698 if (searchrs->records)
699 display_records(searchrs->records);
703 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
705 if (!*initrs->result)
706 printf("Connection rejected by target.\n");
708 printf("Connection accepted by target.\n");
709 if (initrs->implementationId)
710 printf("ID : %s\n", initrs->implementationId);
711 if (initrs->implementationName)
712 printf("Name : %s\n", initrs->implementationName);
713 if (initrs->implementationVersion)
714 printf("Version: %s\n", initrs->implementationVersion);
716 if (initrs->userInformationField)
718 printf("UserInformationfield:\n");
719 odr_external(&print, (Odr_external**)&initrs->
720 userInformationField, 0);
725 static void ir_presentResponse (void *obj, Z_PresentResponse *presrs)
727 printf("Received presentResponse.\n");
729 printf ("Got records\n");
731 printf("No records\n");
734 void ir_select_proc (ClientData clientData)
736 IRObj *p = clientData;
742 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) < 0)
744 printf ("cs_get failed\n");
747 odr_setbuf (p->odr_in, p->buf_in, r);
748 printf ("cs_get ok, got %d\n", r);
749 if (!z_APDU (p->odr_in, &apdu, 0))
751 printf ("%s\n", odr_errlist [odr_geterror (p->odr_in)]);
756 Tcl_Eval (p->interp, p->callback);
760 case Z_APDU_initResponse:
761 ir_initResponse (NULL, apdu->u.initResponse);
763 case Z_APDU_searchResponse:
764 ir_searchResponse (NULL, apdu->u.searchResponse);
766 case Z_APDU_presentResponse:
767 ir_presentResponse (NULL, apdu->u.presentResponse);
770 printf("Received unknown APDU type (%d).\n",
773 } while (cs_more (p->cs_link));
776 /* ------------------------------------------------------- */
779 * ir_tcl_init: Registration of TCL commands.
781 int ir_tcl_init (Tcl_Interp *interp)
783 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
784 (Tcl_CmdDeleteProc *) NULL);
785 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
786 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);