Browse Source

* readline support for client

master
Alex 'AdUser' Z 8 years ago
parent
commit
6f44b474de
  1. 45
      src/client.c

45
src/client.c

@ -89,9 +89,35 @@ signal_handler(int signum) {
} }
} }
#ifdef WITH_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#else
char *
readline(const char *prompt) {
char line[INPUT_LINE_MAX];
while (1) {
line[0] = '\0';
fputs(prompt, stdout);
if (!fgets(line, sizeof(line) - 1, stdin)) {
if (feof(stdin)) {
fputc('\n', stdout);
} else {
fputs("read error\n", stdout);
}
break;
}
if (line[0] == '\n')
continue;
}
return strdup(line);
}
#endif
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
struct sigaction act; struct sigaction act;
char line[INPUT_LINE_MAX] = ""; char *line = NULL;
char opt = '\0'; char opt = '\0';
int ret; int ret;
@ -99,7 +125,7 @@ int main(int argc, char *argv[]) {
switch (opt) { switch (opt) {
case 'c': case 'c':
opts.mode = oneshot; opts.mode = oneshot;
strlcpy(line, optarg, sizeof(line)); line = strndup(optarg, INPUT_LINE_MAX);
break; break;
case 's': case 's':
strlcpy(opts.csocket_spath, optarg, sizeof(opts.csocket_spath)); strlcpy(opts.csocket_spath, optarg, sizeof(opts.csocket_spath));
@ -133,19 +159,10 @@ int main(int argc, char *argv[]) {
exit(ret); exit(ret);
} }
while (1) { while ((line = readline("f2b >> ")) != NULL) {
fputs("f2b >> ", stdout);
if (!fgets(line, sizeof(line) - 1, stdin)) {
if (feof(stdin)) {
fputc('\n', stdout);
} else {
fputs("read error\n", stdout);
}
break;
}
if (line[0] == '\n')
continue;
handle_cmd(line); handle_cmd(line);
free(line);
line = NULL;
} }
f2b_csocket_disconnect(opts.csocket, opts.csocket_cpath); f2b_csocket_disconnect(opts.csocket, opts.csocket_cpath);

Loading…
Cancel
Save