2 * Copyright (c) 1998-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Log: yaz-client.cpp,v $
7 * Revision 1.5 1999-04-09 11:46:57 adam
8 * Added object Yaz_Z_Assoc. Much more functional client.
10 * Revision 1.4 1999/03/23 14:17:57 adam
11 * More work on timeout handling. Work on yaz-client.
13 * Revision 1.3 1999/02/02 14:01:18 adam
14 * First WIN32 port of YAZ++.
16 * Revision 1.2 1999/01/28 13:08:42 adam
17 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
18 * yaz-socket-manager.cc.
20 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
21 * First implementation of YAZ++.
27 #include <yaz-ir-assoc.h>
28 #include <yaz-pdu-assoc.h>
29 #include <yaz-socket-manager.h>
32 #if HAVE_READLINE_READLINE_H
33 #include <readline/readline.h>
35 #if HAVE_READLINE_HISTORY_H
36 #include <readline/history.h>
40 class YAZ_EXPORT MyClient : public Yaz_IR_Assoc {
42 int m_interactive_flag;
43 char m_thisCommand[1024];
44 char m_lastCommand[1024];
45 Yaz_SocketManager *m_socketManager;
47 MyClient(IYaz_PDU_Observable *the_PDU_Observable,
48 Yaz_SocketManager *the_SocketManager);
49 IYaz_PDU_Observer *clone(IYaz_PDU_Observable *the_PDU_Observable);
50 int args(Yaz_SocketManager *socketManager, int argc, char **argv);
51 int interactive(Yaz_SocketManager *socketManager);
53 void recv_initResponse(Z_InitResponse *initResponse);
54 void recv_searchResponse(Z_SearchResponse *searchResponse);
55 int processCommand(const char *cmd);
56 const char *MyClient::getCommand();
57 int cmd_open(char *host);
58 int cmd_quit(char *args);
59 int cmd_close(char *args);
60 int cmd_find(char *args);
63 IYaz_PDU_Observer *MyClient::clone(IYaz_PDU_Observable *the_PDU_Observable)
65 return new MyClient(the_PDU_Observable, m_socketManager);
68 MyClient::MyClient(IYaz_PDU_Observable *the_PDU_Observable,
69 Yaz_SocketManager *the_socketManager) :
70 Yaz_IR_Assoc (the_PDU_Observable)
72 m_interactive_flag = 1;
73 m_thisCommand[0] = '\0';
74 m_lastCommand[0] = '\0';
75 m_socketManager = the_socketManager;
78 void usage(char *prog)
80 fprintf (stderr, "%s: [-v log] [-p proxy] [zurl]\n", prog);
84 void MyClient::recv_initResponse(Z_InitResponse *initResponse)
86 printf ("Got InitResponse. Status ");
87 if (*initResponse->result)
93 void MyClient::recv_searchResponse(Z_SearchResponse *searchResponse)
95 printf ("Got SearchResponse. Status ");
96 if (!*searchResponse->searchStatus)
102 printf ("Hits: %d\n", *searchResponse->resultCount);
108 while (m_socketManager->processEvent() > 0)
110 if (get_lastReceived())
116 #define C_PROMPT "Z>"
118 int MyClient::cmd_open(char *host)
121 if (send_initRequest() >= 0)
128 int MyClient::cmd_quit(char *args)
133 int MyClient::cmd_close(char *args)
139 int MyClient::cmd_find(char *args)
143 if (query.set_rpn(args) <= 0)
145 printf ("Bad RPN query\n");
148 if (send_searchRequest(&query) >= 0)
153 int MyClient::processCommand(const char *commandLine)
155 char cmdStr[1024], cmdArgs[1024];
160 int (MyClient::*fun)(char *arg);
163 {"open", &cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
164 {"quit", &cmd_quit, ""},
165 {"close", &cmd_close, ""},
166 {"find", &cmd_find, ""},
170 if (sscanf(commandLine, "%s %[^;]", cmdStr, cmdArgs) < 1)
173 for (i = 0; cmd[i].cmd; i++)
174 if (!strncmp(cmd[i].cmd, cmdStr, strlen(cmdStr)))
178 if (cmd[i].cmd) // Invoke command handler
179 res = (this->*cmd[i].fun)(cmdArgs);
180 else // Dump help screen
182 printf("Unknown command: %s.\n", cmdStr);
183 printf("Currently recognized commands:\n");
184 for (i = 0; cmd[i].cmd; i++)
185 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
190 const char *MyClient::getCommand()
192 #if HAVE_READLINE_READLINE_H
193 // Read using GNU readline
195 line_in=readline(C_PROMPT);
198 #if HAVE_READLINE_HISTORY_H
200 add_history(line_in);
202 strncpy(m_thisCommand,line_in, 1023);
203 m_thisCommand[1023] = '\0';
206 // Read using fgets(3)
209 if (!fgets(m_thisCommand, 1023, stdin))
212 // Remove trailing whitespace
213 char *cp = m_thisCommand + strlen(m_thisCommand);
214 while (cp != m_thisCommand && strchr("\t \n", cp[-1]))
218 // Remove leading spaces...
219 while (strchr ("\t \n", *cp))
221 // Save command if non-empty
223 strcpy (m_lastCommand, cp);
224 return m_lastCommand;
227 int MyClient::interactive(Yaz_SocketManager *socketManager)
230 if (!m_interactive_flag)
232 while ((cmd = getCommand()))
234 if (!processCommand(cmd))
240 int MyClient::args(Yaz_SocketManager *socketManager, int argc, char **argv)
245 char *prog = argv[0];
248 while ((ret = options("p:v:q", argv, argc, &arg)) != -2)
269 log_init_level (log_mask_str(arg));
272 m_interactive_flag = 0;
288 int main(int argc, char **argv)
290 Yaz_SocketManager mySocketManager;
291 Yaz_PDU_Assoc *some = new Yaz_PDU_Assoc(&mySocketManager, 0);
293 MyClient z(some, &mySocketManager);
295 if (z.args(&mySocketManager, argc, argv))
297 if (z.interactive(&mySocketManager))