From 069fe471ed8fe70c3d7ec528df98d12329551b6a Mon Sep 17 00:00:00 2001 From: vysheng Date: Sat, 21 Dec 2013 15:53:32 +0400 Subject: [PATCH] Added safe_quit query. Useful in pipe mode --- interface.c | 5 +++++ loop.c | 9 ++++++++- queries.c | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/interface.c b/interface.c index 664b976..86ad62b 100644 --- a/interface.c +++ b/interface.c @@ -49,6 +49,8 @@ char *default_prompt = "> "; int unread_messages; int msg_num_mode; +int safe_quit; + int in_readline; int readline_active; @@ -304,6 +306,7 @@ char *commands[] = { "status_offline", "contacts_search", "quit", + "safe_quit", "send_audio", "load_audio", "view_audio", @@ -1062,6 +1065,8 @@ void interpreter (char *line UU) { chat_mode_id = id; } else if (IS_WORD ("quit")) { exit (0); + } else if (IS_WORD ("safe_quit")) { + safe_quit = 1; } } #undef IS_WORD diff --git a/loop.c b/loop.c index 1dd73f8..f4b5fef 100644 --- a/loop.c +++ b/loop.c @@ -59,6 +59,8 @@ extern int binlog_enabled; extern int unknown_user_list_pos; extern int unknown_user_list[]; int register_mode; +extern int safe_quit; +extern int queries_num; int unread_messages; void got_it (char *line, int len); @@ -93,10 +95,15 @@ void net_loop (int flags, int (*is_end)(void)) { } } connections_poll_result (fds + cc, x - cc); + if (safe_quit && !queries_num) { + printf ("All done. Exit\n"); + rl_callback_handler_remove (); + exit (0); + } if (unknown_user_list_pos) { do_get_user_list_info_silent (unknown_user_list_pos, unknown_user_list); unknown_user_list_pos = 0; - } + } } } diff --git a/queries.c b/queries.c index 8ec160d..9067d1e 100644 --- a/queries.c +++ b/queries.c @@ -66,6 +66,8 @@ long long cur_downloaded_bytes; extern int binlog_enabled; extern int sync_from_start; +int queries_num; + void out_peer_id (peer_id_t id); #define QUERY_TIMEOUT 6.0 @@ -150,6 +152,7 @@ struct query *send_query (struct dc *DC, int ints, void *data, struct query_meth insert_event_timer (&q->ev); q->extra = extra; + queries_num ++; return q; } @@ -188,6 +191,7 @@ void query_error (long long id) { free (q->data); free (q); } + queries_num --; } #define MAX_PACKED_SIZE (1 << 24) @@ -255,6 +259,7 @@ void query_result (long long id UU) { in_ptr = end; in_end = eend; } + queries_num --; } #define event_timer_cmp(a,b) ((a)->timeout > (b)->timeout ? 1 : ((a)->timeout < (b)->timeout ? -1 : (memcmp (a, b, sizeof (struct event_timer)))))