tg/loop.c

76 lines
1.6 KiB
C
Raw Normal View History

2013-10-03 16:38:25 +04:00
#define READLINE_CALLBACKS
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
2013-10-03 20:09:06 +04:00
#include <errno.h>
2013-10-03 16:38:25 +04:00
#include "interface.h"
extern char *default_username;
extern char *auth_token;
void set_default_username (const char *s);
int main_loop (void) {
2013-10-03 20:09:06 +04:00
fd_set inp, outp;
struct timeval tv;
while (1) {
FD_ZERO (&inp);
FD_ZERO (&outp);
FD_SET (0, &inp);
tv.tv_sec = 1;
tv.tv_usec = 0;
2013-10-03 20:10:53 +04:00
2013-10-03 20:09:06 +04:00
int lfd = 0;
if (select (lfd + 1, &inp, &outp, NULL, &tv) < 0) {
if (errno == EINTR) {
2013-10-03 20:10:53 +04:00
/* resuming from interrupt, so not an error situation,
this generally happens when you suspend your
messenger with "C-z" and then "fg". This is allowed "
*/
rl_reset_line_state ();
rl_forced_update_display ();
continue;
}
perror ("select()");
break;
2013-10-03 20:09:06 +04:00
}
if (FD_ISSET (0, &inp)) {
2013-10-03 20:10:53 +04:00
rl_callback_read_char ();
2013-10-03 20:09:06 +04:00
}
}
return 0;
2013-10-03 16:38:25 +04:00
}
int loop (void) {
size_t size = 0;
char *user = default_username;
if (!user && !auth_token) {
printf ("Telephone number (with '+' sign): ");
if (getline (&user, &size, stdin) == -1) {
2013-10-03 20:10:53 +04:00
perror ("getline()");
exit (EXIT_FAILURE);
}
2013-10-03 16:38:25 +04:00
user[strlen (user) - 1] = '\0';
set_default_username (user);
}
fflush (stdin);
rl_callback_handler_install (get_default_prompt (), interpreter);
rl_attempted_completion_function = (CPPFunction *) complete_text;
rl_completion_entry_function = complete_none;
return main_loop ();
}