+
+
+ if (conn
+#ifdef USE_SELECT
+ && FD_ISSET(cs_fileno(conn), &input)
+#endif
+ )
+ {
+ do
+ {
+ if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
+ {
+ perror("cs_get");
+ exit(1);
+ }
+ if (!res)
+ {
+ printf("Target closed connection.\n");
+ exit(1);
+ }
+ odr_reset(in); /* release APDU from last round */
+ record_last = 0;
+ odr_setbuf(in, netbuffer, res, 0);
+ if (!z_APDU(in, &apdu, 0, 0))
+ {
+ odr_perror(in, "Decoding incoming APDU");
+ fprintf(stderr, "[Near %d]\n", odr_offset(in));
+ fprintf(stderr, "Packet dump:\n---------\n");
+ odr_dumpBER(stderr, netbuffer, res);
+ fprintf(stderr, "---------\n");
+ if (apdu_file)
+ z_APDU(print, &apdu, 0, 0);
+ exit(1);
+ }
+ if (apdu_file && !z_APDU(print, &apdu, 0, 0))
+ {
+ odr_perror(print, "Failed to print incoming APDU");
+ odr_reset(print);
+ continue;
+ }
+ switch(apdu->which)
+ {
+ case Z_APDU_initResponse:
+ process_initResponse(apdu->u.initResponse);
+ break;
+ case Z_APDU_searchResponse:
+ process_searchResponse(apdu->u.searchResponse);
+ break;
+ case Z_APDU_scanResponse:
+ process_scanResponse(apdu->u.scanResponse);
+ break;
+ case Z_APDU_presentResponse:
+ print_refid (apdu->u.presentResponse->referenceId);
+ setno +=
+ *apdu->u.presentResponse->numberOfRecordsReturned;
+ if (apdu->u.presentResponse->records)
+ display_records(apdu->u.presentResponse->records);
+ else
+ printf("No records.\n");
+ printf ("nextResultSetPosition = %d\n",
+ *apdu->u.presentResponse->nextResultSetPosition);
+ break;
+ case Z_APDU_sortResponse:
+ process_sortResponse(apdu->u.sortResponse);
+ break;
+ case Z_APDU_extendedServicesResponse:
+ printf("Got extended services response\n");
+ process_ESResponse(apdu->u.extendedServicesResponse);
+ break;
+ case Z_APDU_close:
+ printf("Target has closed the association.\n");
+ process_close(apdu->u.close);
+ break;
+ case Z_APDU_resourceControlRequest:
+ process_resourceControlRequest
+ (apdu->u.resourceControlRequest);
+ break;
+ case Z_APDU_deleteResultSetResponse:
+ process_deleteResultSetResponse(apdu->u.
+ deleteResultSetResponse);
+ break;
+ default:
+ printf("Received unknown APDU type (%d).\n",
+ apdu->which);
+ exit(1);
+ }
+ }
+ while (conn && cs_more(conn));
+#if HAVE_GETTIMEOFDAY
+ gettimeofday (&tv_end, 0);
+#if 0
+ printf ("S/U S/U=%ld/%ld %ld/%ld",
+ (long) tv_start.tv_sec,
+ (long) tv_start.tv_usec,
+ (long) tv_end.tv_sec,
+ (long) tv_end.tv_usec);
+#endif
+ printf ("Elapsed: %.6f\n",
+ (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
+ ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
+#endif
+ }
+}
+
+
+static struct {
+ char *cmd;
+ int (*fun)(char *arg);
+ char *ad;
+ char *(*rl_completerfunction)(const char *text, int state);
+ int complete_filenames;
+} cmd[] = {
+ {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]",NULL,0},
+ {"quit", cmd_quit, "",NULL,0},
+ {"find", cmd_find, "<query>",NULL,0},
+ {"delete", cmd_delete, "<setname>",NULL,0},
+ {"base", cmd_base, "<base-name>",NULL,0},
+ {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]",NULL,0},
+ {"scan", cmd_scan, "<term>",NULL,0},
+ {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0},
+ {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0},
+ {"authentication", cmd_authentication, "<acctstring>",NULL,0},
+ {"lslb", cmd_lslb, "<largeSetLowerBound>",NULL,0},
+ {"ssub", cmd_ssub, "<smallSetUpperBound>",NULL,0},
+ {"mspn", cmd_mspn, "<mediumSetPresentNumber>",NULL,0},
+ {"status", cmd_status, "",NULL,0},
+ {"setnames", cmd_setnames, "",NULL,0},
+ {"cancel", cmd_cancel, "",NULL,0},
+ {"format", cmd_format, "<recordsyntax>",complete_format,0},
+ {"schema", cmd_schema, "<schema>",complete_schema,0},
+ {"elements", cmd_elements, "<elementSetName>",NULL,0},
+ {"close", cmd_close, "",NULL,0},
+ {"attributeset", cmd_attributeset, "<attrset>",complete_attributeset,0},
+ {"querytype", cmd_querytype, "<type>",complete_querytype,0},
+ {"refid", cmd_refid, "<id>",NULL,0},
+ {"itemorder", cmd_itemorder, "ill|item <itemno>",NULL,0},
+ {"update", cmd_update, "<item>",NULL,0},
+ {"packagename", cmd_packagename, "<packagename>",NULL,0},
+ {"proxy", cmd_proxy, "[('tcp'|'ssl')]<host>[':'<port>]",NULL,0},
+ {".", cmd_source, "<filename>",NULL,1},
+ {"!", cmd_subshell, "Subshell command",NULL,0},
+ {"set_apdufile", cmd_set_apdufile, "<filename>",NULL,0},
+ {"set_marcdump", cmd_set_marcdump," <filename>",NULL,0},
+ {"set_cclfields", cmd_set_cclfields,"<filename>",NULL,1},
+ {"register_oid",cmd_register_oid,"<name> <class> <oid>",NULL,0},
+ {"push_command",cmd_push_command,"<command>",command_generator,0},
+ /* Server Admin Functions */
+ {"adm-reindex", cmd_adm_reindex, "<database-name>",NULL,0},
+ {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>",NULL,0},
+ {"adm-create", cmd_adm_create, "",NULL,0},
+ {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>",NULL,0},
+ {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>",NULL,0},
+ {"adm-refresh", cmd_adm_refresh, "",NULL,0},
+ {"adm-commit", cmd_adm_commit, "",NULL,0},
+ {"adm-shutdown", cmd_adm_shutdown, "",NULL,0},
+ {"adm-startup", cmd_adm_startup, "",NULL,0},
+ {0,0,0,0,0}
+};
+
+void process_cmd_line(char* line)
+{
+ int i,res;
+ char word[32], arg[1024];
+
+#if HAVE_GETTIMEOFDAY
+ gettimeofday (&tv_start, 0);
+#endif
+
+ if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
+ {
+ strcpy(word, last_cmd);
+ *arg = '\0';
+ }
+ else if (res == 1)
+ *arg = 0;
+ strcpy(last_cmd, word);
+
+ /* removed tailing spaces from the arg command */
+ {
+ char* p;
+ char* lastnonspace=NULL;
+ p = arg;
+
+ for(;*p; ++p) {
+ if(!isspace(*p)) {
+ lastnonspace = p;
+ };
+ };
+ if(lastnonspace)
+ *(++lastnonspace) = 0;
+ };
+
+
+ for (i = 0; cmd[i].cmd; i++)
+ if (!strncmp(cmd[i].cmd, word, strlen(word)))
+ {
+ res = (*cmd[i].fun)(arg);
+ break;
+ }
+ if (!cmd[i].cmd) /* dump our help-screen */
+ {
+ printf("Unknown command: %s.\n", word);
+ printf("Currently recognized commands:\n");
+ for (i = 0; cmd[i].cmd; i++)
+ printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
+ return;
+ }
+
+ if(apdu_file) fflush(apdu_file);
+
+ if (res >= 2)
+ wait_and_handle_responce();
+
+ if(apdu_file) fflush(apdu_file);
+ if(marcdump) fflush(marcdump);
+}
+
+
+char *command_generator(const char *text, int state)
+{
+ static idx; // index is the last used the last time command_generator was called
+ char *command;
+ if (state==0) {
+ idx = 0;
+ }
+ for( ; cmd[idx].cmd; ++idx) {
+ if (!strncmp(cmd[idx].cmd,text,strlen(text))) {
+ ++idx; /* skip this entry on the next run */
+ return strdup(cmd[idx-1].cmd);
+ };
+ }
+ return NULL;
+}
+
+
+/*
+ This function only known how to complete on the first word
+*/
+char ** readline_completer(char *text, int start, int end) {
+#if HAVE_READLINE_READLINE_H
+
+ if(start == 0) {
+#ifdef RL_READLINE_VERSION
+ char** res=rl_completion_matches(text,
+ command_generator);
+#else
+ char** res=completion_matches(text,
+ (CPFunction*)command_generator);
+#endif
+ rl_attempted_completion_over = 1;
+ return res;
+ } else {
+ char arg[1024],word[32];
+ int i=0 ,res;
+ if ((res = sscanf(rl_line_buffer, "%31s %1023[^;]", word, arg)) <= 0) {
+ rl_attempted_completion_over = 1;
+ return NULL;
+ };
+
+ if(start != strlen(word) +1 ) {
+ rl_attempted_completion_over = 1;
+ return 0;
+ }
+ for (i = 0; cmd[i].cmd; i++) {
+ if (!strncmp(cmd[i].cmd, word, strlen(word))) {
+ break;
+ }
+ }
+
+
+ if(!cmd[i].complete_filenames)
+ rl_attempted_completion_over = 1;
+ if(cmd[i].rl_completerfunction) {
+#ifdef RL_READLINE_VERSION
+ char** res=
+ rl_completion_matches(text,
+ cmd[i].rl_completerfunction);
+#else
+ char** res=
+ completion_matches(text,
+ (CPFunction*)cmd[i].rl_completerfunction);
+#endif
+ rl_attempted_completion_over = 1;
+ return res;
+ } else {
+ rl_attempted_completion_over = 1;
+ return 0;
+ };
+ };
+#else
+ return 0;
+#endif
+};
+
+
+static void client(void)
+{
+