3 #include <readline/readline.h>
4 #include <readline/history.h>
6 static int cmd_help(char *arg);
7 static int cmd_quit(char *arg);
8 static int cmd_login(char *arg);
9 static int cmd_logout(char *arg);
10 static int cmd_connlist(char *arg);
17 { "help", cmd_help, "Help"},
18 { "quit", cmd_quit, "Quit"},
19 { "login", cmd_login, "Log into AIM"},
20 { "logout", cmd_login, "Log out of AIM"},
21 { "connlist", cmd_connlist, "List open connections"},
22 { (char *)NULL, (Function *)NULL, (char *)NULL }
25 static char *stripwhite (char *string)
29 for (s = string; whitespace(*s); s++)
35 t = s + strlen (s) - 1;
36 while (t > s && whitespace (*t))
43 static char *cmdgenerator(char *text, int state)
45 static int list_index, len;
53 while ((name = cmdlist[list_index].name)) {
55 if (strncmp (name, text, len) == 0)
56 return (strdup(name));
59 /* If no names matched, then return NULL. */
63 static char **cmdcomplete(char *text, int start, int end)
67 matches = (char **)NULL;
70 * If this word is at the start of the line, then it is a command
71 * to complete. Otherwise it is the name of a file in the current
75 matches = completion_matches(text, cmdgenerator);
80 static Function *cmdfind(char *name)
84 for (i = 0; cmdlist[i].name; i++)
85 if (strcmp (name, cmdlist[i].name) == 0)
86 return cmdlist[i].func;
91 static int cmdexec(char *line)
97 /* Isolate the command word. */
99 while (line[i] && whitespace (line[i]))
103 while (line[i] && !whitespace (line[i]))
109 if (!(cmd = cmdfind(word))) {
110 fprintf(stderr, "%s: invalid command\n", word);
113 /* Get argument to command, if any. */
114 while (whitespace (line[i]))
119 /* Call the function. */
123 static void fullline(void)
127 stripped = stripwhite(rl_line_buffer);
130 add_history(stripped);
140 rl_attempted_completion_function = cmdcomplete;
142 printf("Welcome to faimtest.\n");
144 rl_callback_handler_install("faimtest>", fullline);
149 void cmd_gotkey(void)
152 rl_callback_read_char();
157 static int cmd_help(char *arg)
161 for (i = 0; cmdlist[i].name; i++)
162 printf("%16s\t\t%s\n", cmdlist[i].name, cmdlist[i].doc);
167 static int cmd_quit(char *arg)
174 static int cmd_login(char *arg)
176 char *sn = NULL, *passwd = NULL;
180 if ((passwd = index(sn, ' '))) {
186 if (login(sn, passwd) != 0)
187 printf("login failed\n");
192 static int cmd_logout(char *arg)
199 static int cmd_connlist(char *arg)
201 extern struct aim_session_t aimsess;
202 struct aim_conn_t *cur;
204 printf("Open connections:\n");
205 for (cur = aimsess.connlist; cur; cur = cur->next) {
206 printf(" fd=%d type=0x%02x\n", cur->fd, cur->type);
212 void cmd_uninit(void)
215 rl_callback_handler_remove();