+ Z_External *r = (Z_External*) record;
+ oident *ent = oid_getentbyoid(r->direct_reference);
+
+ /*
+ * Tell the user what we got.
+ */
+ if (r->direct_reference)
+ {
+ printf("Record type: ");
+ if (ent)
+ printf("%s\n", ent->desc);
+ }
+ /* Check if this is a known, ASN.1 type tucked away in an octet string */
+ Z_ext_typeent *etype = z_ext_getentbyref(ent->value);
+ if (ent && (r->which == Z_External_octet || r->which == Z_External_single)
+ && (etype = z_ext_getentbyref(ent->value)))
+
+ {
+ void *rr;
+ /*
+ * Call the given decoder to process the record.
+ */
+ odr_setbuf(odr_decode(), (char*)record->u.octet_aligned->buf,
+ record->u.octet_aligned->len, 0);
+ if (!(*etype->fun)(odr_decode(), (char **)&rr, 0, 0))
+ {
+ odr_perror(odr_decode(), "Decoding constructed record.");
+ fprintf(stderr, "[Near %d]\n", odr_offset(odr_decode()));
+ fprintf(stderr, "Packet dump:\n---------\n");
+ odr_dumpBER(stderr, (char*)record->u.octet_aligned->buf,
+ record->u.octet_aligned->len);
+ fprintf(stderr, "---------\n");
+ }
+ if (etype->what == Z_External_sutrs)
+ {
+ Z_SUTRS *sutrs = (Z_SUTRS *) rr;
+ recv_textRecord ((int) VAL_SUTRS, (const char *) sutrs->buf,
+ (size_t) sutrs->len);
+ }
+ return;
+ }
+ if (r->which == Z_External_octet && record->u.octet_aligned->len)
+ {
+ recv_textRecord((int) ent->value,
+ (const char *) record->u.octet_aligned->buf,
+ (size_t) record->u.octet_aligned->len);
+ }
+ else if (ent && ent->value == VAL_SUTRS && r->which == Z_External_sutrs)
+ recv_textRecord((int) VAL_SUTRS, (const char *) r->u.sutrs->buf,
+ (size_t) r->u.sutrs->len);
+ else if (ent && ent->value == VAL_GRS1 && r->which == Z_External_grs1)
+ recv_genericRecord(r->u.grs1);
+ else
+ {
+ printf("Unknown record representation.\n");
+ if (!z_External(odr_print(), &r, 0, 0))
+ {
+ odr_perror(odr_print(), "Printing external");
+ odr_reset(odr_print());
+ }
+ }
+}
+
+void MyClient::recv_namePlusRecord (Z_NamePlusRecord *zpr, int offset)
+{
+ if (zpr->databaseName)
+ printf("[%s]", zpr->databaseName);
+ if (zpr->which == Z_NamePlusRecord_surrogateDiagnostic)
+ recv_diagrecs(&zpr->u.surrogateDiagnostic, 1);
+ else
+ recv_record(zpr->u.databaseRecord, offset, zpr->databaseName);
+}
+
+void MyClient::recv_records (Z_Records *records)
+{
+#ifdef ASN_COMPILED
+ Z_DiagRec dr, *dr_p = &dr;
+#endif
+ if (!records)
+ return;
+ int i;
+ switch (records->which)
+ {
+ case Z_Records_DBOSD:
+ for (i = 0; i < records->u.databaseOrSurDiagnostics->num_records; i++)
+ recv_namePlusRecord(records->u.databaseOrSurDiagnostics->
+ records[i], i + m_setOffset);
+ m_setOffset += records->u.databaseOrSurDiagnostics->num_records;
+ break;
+ case Z_Records_NSD:
+#ifdef ASN_COMPILED
+ dr.which = Z_DiagRec_defaultFormat;
+ dr.u.defaultFormat = records->u.nonSurrogateDiagnostic;
+ recv_diagrecs (&dr_p, 1);
+#else
+ recv_diagrecs (&records->u.nonSurrogateDiagnostic, 1);
+#endif
+ break;
+ case Z_Records_multipleNSD:
+ recv_diagrecs (records->u.multipleNonSurDiagnostics->diagRecs,
+ records->u.multipleNonSurDiagnostics->num_diagRecs);
+ break;
+ }
+}
+
+void MyClient::recv_searchResponse(Z_SearchResponse *searchResponse)
+{
+ printf ("Got SearchResponse. Status ");
+ if (!*searchResponse->searchStatus)
+ {
+ printf ("Fail\n");
+ return;
+ }
+ printf ("Ok\n");
+ printf ("Hits: %d\n", *searchResponse->resultCount);
+ recv_records (searchResponse->records);
+}
+
+void MyClient::recv_presentResponse(Z_PresentResponse *presentResponse)
+{
+ printf ("Got PresentResponse\n");
+ recv_records (presentResponse->records);
+}
+
+int MyClient::wait()
+{
+ set_lastReceived(0);
+ while (m_socketManager->processEvent() > 0)
+ {
+ if (get_lastReceived())
+ return 1;
+ }
+ return 0;
+}
+
+#define C_PROMPT "Z>"
+
+int MyClient::cmd_connect(char *host)
+{
+ client (host);
+ timeout (10);
+ wait ();
+ timeout (0);
+ return 1;
+}
+
+int MyClient::cmd_open(char *host)
+{
+ client (host);
+ timeout (10);
+ wait ();
+ timeout (0);
+ send_initRequest();
+ wait ();
+ return 1;
+}
+
+int MyClient::cmd_init(char *args)
+{
+ if (send_initRequest() >= 0)
+ wait();
+ else
+ close();
+ return 1;
+}
+
+int MyClient::cmd_quit(char *args)
+{
+ return 0;
+}
+
+int MyClient::cmd_close(char *args)
+{
+ close();
+ return 1;
+}
+
+int MyClient::cmd_find(char *args)
+{
+ Yaz_Z_Query query;
+
+ if (query.set_rpn(args) <= 0)
+ {
+ printf ("Bad RPN query\n");
+ return 1;
+ }
+ if (send_searchRequest(&query) >= 0)
+ wait();
+ else
+ printf ("Not connected\n");
+ return 1;
+}
+
+int MyClient::cmd_show(char *args)
+{
+ int start = m_setOffset, number = 1;
+
+ sscanf (args, "%d %d", &start, &number);
+ m_setOffset = start;
+ if (send_presentRequest(start, number) >= 0)
+ wait();
+ else
+ printf ("Not connected\n");
+ return 1;
+}
+
+int MyClient::cmd_cookie(char *args)
+{
+ set_cookie(*args ? args : 0);
+ return 1;
+}
+
+int MyClient::cmd_format(char *args)
+{
+ set_preferredRecordSyntax(args);
+ return 1;
+}
+
+int MyClient::cmd_proxy(char *args)
+{
+ set_proxy(args);
+ return 1;
+}
+
+int MyClient::processCommand(const char *commandLine)
+{
+ char cmdStr[1024], cmdArgs[1024];
+ cmdArgs[0] = '\0';
+ cmdStr[0] = '\0';
+ static struct {
+ char *cmd;
+ int (MyClient::*fun)(char *arg);
+ char *ad;
+ } cmd[] = {
+ {"open", &MyClient::cmd_open, "<host>[':'<port>][/<database>]"},
+ {"connect", &MyClient::cmd_connect, "<host>[':'<port>][/<database>]"},
+ {"quit", &MyClient::cmd_quit, ""},
+ {"close", &MyClient::cmd_close, ""},
+ {"find", &MyClient::cmd_find, "<query>"},
+ {"show", &MyClient::cmd_show, "[<start> [<number>]]"},
+ {"cookie", &MyClient::cmd_cookie, "<cookie>"},
+ {"init", &MyClient::cmd_init, ""},
+ {"format", &MyClient::cmd_format, "<record-syntax>"},
+ {"proxy", &MyClient::cmd_proxy, "<host>:[':'<port>]"},
+ {0,0,0}
+ };
+
+ if (sscanf(commandLine, "%s %[^;]", cmdStr, cmdArgs) < 1)
+ return 1;
+ int i;
+ for (i = 0; cmd[i].cmd; i++)
+ if (!strncmp(cmd[i].cmd, cmdStr, strlen(cmdStr)))
+ break;