]> andersk Git - libfaim.git/blob - utils/faimtest/commands.c
baab3cb16d02d256c537a30308a666875df25e4b
[libfaim.git] / utils / faimtest / commands.c
1
2 #include "faimtest.h"
3 #include <readline/readline.h>
4 #include <readline/history.h>
5
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);
11
12 struct {
13   char *name;
14   Function *func;
15   char *doc;
16 } cmdlist[] = {
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 }
23 };
24
25 static char *stripwhite (char *string)
26 {
27   char *s, *t;
28
29   for (s = string; whitespace(*s); s++)
30     ;
31
32   if (*s == 0)
33     return (s);
34
35   t = s + strlen (s) - 1;
36   while (t > s && whitespace (*t))
37     t--;
38   *++t = '\0';
39
40   return s;
41 }
42
43 static char *cmdgenerator(char *text, int state)
44 {
45   static int list_index, len;
46   char *name;
47
48   if (!state) {
49     list_index = 0;
50     len = strlen (text);
51   }
52
53   while ((name = cmdlist[list_index].name)) {
54     list_index++;
55     if (strncmp (name, text, len) == 0)
56       return (strdup(name));
57   }
58
59   /* If no names matched, then return NULL. */
60   return (char *)NULL;
61 }
62
63 static char **cmdcomplete(char *text, int start, int end)
64 {
65   char **matches;
66
67   matches = (char **)NULL;
68
69   /* 
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
72    * directory. 
73    */
74   if (start == 0)
75     matches = completion_matches(text, cmdgenerator);
76
77   return matches;
78 }
79
80 static Function *cmdfind(char *name)
81 {
82   int i;
83
84   for (i = 0; cmdlist[i].name; i++)
85     if (strcmp (name, cmdlist[i].name) == 0)
86       return cmdlist[i].func;
87
88   return NULL;
89 }
90
91 static int cmdexec(char *line)
92 {
93   int i;
94   Function *cmd;
95   char *word;
96
97   /* Isolate the command word. */
98   i = 0;
99   while (line[i] && whitespace (line[i]))
100     i++;
101   word = line + i;
102
103   while (line[i] && !whitespace (line[i]))
104     i++;
105
106   if (line[i])
107     line[i++] = '\0';
108
109   if (!(cmd = cmdfind(word))) {
110     fprintf(stderr, "%s: invalid command\n", word);
111     return -1;
112   }
113   /* Get argument to command, if any. */
114   while (whitespace (line[i]))
115     i++;
116
117   word = line + i;
118
119   /* Call the function. */
120   return cmd(word);
121 }
122
123 static void fullline(void) 
124 {
125   char *stripped;
126
127   stripped = stripwhite(rl_line_buffer);
128
129   if (*stripped) {
130     add_history(stripped);
131     cmdexec(stripped);
132   }
133
134   return;
135 }
136
137 void cmd_init(void)
138 {
139
140   rl_attempted_completion_function = cmdcomplete;
141
142   printf("Welcome to faimtest.\n");
143
144   rl_callback_handler_install("faimtest>", fullline);
145
146   return;
147 }
148
149 void cmd_gotkey(void)
150 {
151
152   rl_callback_read_char();
153
154   return;
155 }
156
157 static int cmd_help(char *arg)
158 {
159   int i;
160
161   for (i = 0; cmdlist[i].name; i++)
162     printf("%16s\t\t%s\n", cmdlist[i].name, cmdlist[i].doc);
163
164   return 0;
165 }
166
167 static int cmd_quit(char *arg)
168 {
169   keepgoing = 0;
170
171   return 0;
172 }
173
174 static int cmd_login(char *arg)
175 {
176   char *sn = NULL, *passwd = NULL;
177
178   if (arg) {
179     sn = arg;
180     if ((passwd = index(sn, ' '))) {
181       *(passwd) = '\0';
182       passwd++;
183     }
184   }
185
186   if (login(sn, passwd) != 0)
187     printf("login failed\n");
188
189   return 0;
190 }
191
192 static int cmd_logout(char *arg)
193 {
194   logout();
195
196   return 0;
197 }
198
199 static int cmd_connlist(char *arg)
200 {
201   extern struct aim_session_t aimsess;
202   struct aim_conn_t *cur;
203
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);
207   }
208
209   return 0;
210 }
211
212 void cmd_uninit(void)
213 {
214
215   rl_callback_handler_remove();
216
217   return;
218 }
This page took 0.043237 seconds and 3 git commands to generate.