Handle quitting a bit better

And add some documentation.
This commit is contained in:
Přemysl Eric Janouch 2015-04-08 02:24:07 +02:00
parent ec54630fef
commit f0d60bb689
1 changed files with 24 additions and 0 deletions

View File

@ -85,12 +85,22 @@ struct app_context;
struct backend_iface struct backend_iface
{ {
/// Prepare the backend for RPC calls
void (*init) (struct app_context *ctx, void (*init) (struct app_context *ctx,
const char *endpoint, struct http_parser_url *url); const char *endpoint, struct http_parser_url *url);
/// Add an HTTP header to send with requests
void (*add_header) (struct app_context *ctx, const char *header); void (*add_header) (struct app_context *ctx, const char *header);
/// Make an RPC call
bool (*make_call) (struct app_context *ctx, bool (*make_call) (struct app_context *ctx,
const char *request, bool expect_content, const char *request, bool expect_content,
struct str *buf, struct error **e); struct str *buf, struct error **e);
/// Do everything necessary to deal with ev_break(EVBREAK_ALL)
void (*on_quit) (struct app_context *ctx);
/// Free any resources
void (*destroy) (struct app_context *ctx); void (*destroy) (struct app_context *ctx);
}; };
@ -1402,6 +1412,16 @@ backend_ws_make_call (struct app_context *ctx,
return true; return true;
} }
static void
backend_ws_on_quit (struct app_context *ctx)
{
struct ws_context *self = &ctx->ws;
if (self->waiting_for_event && !self->e)
error_set (&self->e, "aborted by user");
// We also have to be careful not to change the ev_break status
}
static void static void
backend_ws_destroy (struct app_context *ctx) backend_ws_destroy (struct app_context *ctx)
{ {
@ -1432,6 +1452,7 @@ static struct backend_iface g_backend_ws =
.init = backend_ws_init, .init = backend_ws_init,
.add_header = backend_ws_add_header, .add_header = backend_ws_add_header,
.make_call = backend_ws_make_call, .make_call = backend_ws_make_call,
.on_quit = backend_ws_on_quit,
.destroy = backend_ws_destroy, .destroy = backend_ws_destroy,
}; };
@ -1861,6 +1882,9 @@ on_readline_input (char *line)
if (!line) if (!line)
{ {
if (g_ctx.backend->on_quit)
g_ctx.backend->on_quit (&g_ctx);
ev_break (EV_DEFAULT_ EVBREAK_ALL); ev_break (EV_DEFAULT_ EVBREAK_ALL);
// We must do this here, or the prompt gets printed twice. *shrug* // We must do this here, or the prompt gets printed twice. *shrug*