Merge branch 'refs/heads/t/para_play_fixes'
[paraslash.git] / interactive.c
1 /*
2  * Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file interactive.c Readline abstraction for interactive sessions. */
8
9 #include "para.h"
10
11 #include <regex.h>
12 #include <readline/readline.h>
13 #include <readline/history.h>
14 #include <sys/ioctl.h>
15 #include <signal.h>
16
17 #include "fd.h"
18 #include "buffer_tree.h"
19 #include "list.h"
20 #include "sched.h"
21 #include "interactive.h"
22 #include "string.h"
23 #include "error.h"
24
25 struct i9e_private {
26         struct i9e_client_info *ici;
27         FILE *stderr_stream;
28         int num_columns;
29         int num_key_bindings;
30         char empty_line[1000];
31         struct task *task;
32         struct btr_node *stdout_btrn;
33         bool last_write_was_status;
34         bool line_handler_running;
35         bool input_eof;
36         bool caught_sigint;
37         bool caught_sigterm;
38         Keymap standard_km;
39         Keymap bare_km;
40 };
41 static struct i9e_private i9e_private, *i9ep = &i9e_private;
42
43 /**
44  * Return the error state of the i9e task.
45  *
46  * This is mainly useful for other tasks to tell whether the i9e task is still
47  * running.
48  *
49  * \return A negative return value of zero means the i9e task terminated. Only
50  * in this case it is safe to call ie9_close().
51  */
52 int i9e_get_error(void)
53 {
54         return task_status(i9ep->task);
55 }
56
57 static bool is_prefix(const char *partial, const char *full, size_t len)
58 {
59         if (len == 0)
60                 len = strlen(partial);
61         return !strncmp(partial, full, len);
62 }
63
64 /*
65  * Generator function for command completion. STATE lets us know whether
66  * to start from scratch; without any state (i.e. STATE == 0), then we
67  * start at the top of the list.
68  */
69 static char *command_generator(const char *text, int state)
70 {
71         static int list_index, len;
72         const char *name;
73         struct i9e_client_info *ici = i9ep->ici;
74
75         rl_attempted_completion_over = 1; /* disable filename completion */
76         /*
77          * If this is a new word to complete, initialize now. This includes
78          * saving the length of TEXT for efficiency, and initializing the index
79          * variable to 0.
80          */
81         if (state == 0) {
82                 list_index = 0;
83                 len = strlen(text);
84         }
85         /* Return the next name which partially matches from the command list. */
86         while ((name = ici->completers[list_index].name)) {
87                 list_index++;
88                 if (is_prefix(text, name, len))
89                         return para_strdup(name);
90         }
91         return NULL; /* no names matched */
92 }
93
94 static void reset_completion_result(struct i9e_completion_result *cr)
95 {
96         cr->dont_append_space = false;
97         cr->filename_completion_desired = false;
98         cr->matches = NULL;
99 }
100
101 static void create_matches(struct i9e_completion_info *ci,
102                 struct i9e_completer *completers,
103                 struct i9e_completion_result *cr)
104 {
105         int i, ret;
106
107         reset_completion_result(cr);
108
109         ret = create_argv(ci->buffer, " ", &ci->argv);
110         if (ret < 0 || !ci->argv[0])
111                 return;
112
113         ci->argc = ret;
114         ci->word_num = compute_word_num(ci->buffer, " ", ci->point);
115         for (i = 0; completers[i].name; i++) {
116                 if (strcmp(completers[i].name, ci->argv[0]) != 0)
117                         continue;
118                 completers[i].completer(ci, cr);
119                 break;
120         }
121         PARA_DEBUG_LOG("current word: %d (%s)\n", ci->word_num,
122                 ci->argv[ci->word_num]);
123         if (cr->matches)
124                 for (i = 0; cr->matches[i]; i++)
125                         PARA_DEBUG_LOG("match %d: %s\n", i, cr->matches[i]);
126 }
127
128 static char *completion_generator(const char *word, int state)
129 {
130         static int list_index;
131         static char **argv, **matches;
132         struct i9e_completer *completers = i9ep->ici->completers;
133         struct i9e_completion_info ci = {
134                 .word = (char *)word,
135                 .point = rl_point,
136                 .buffer = rl_line_buffer,
137         };
138         struct i9e_completion_result cr = {.matches = NULL};
139
140         if (state != 0)
141                 goto out;
142         /* clean up previous matches and set defaults */
143         free(matches);
144         matches = NULL;
145         free_argv(argv);
146         argv = NULL;
147         list_index = 0;
148         rl_completion_append_character = ' ';
149         rl_completion_suppress_append = false;
150         rl_attempted_completion_over = true;
151
152         create_matches(&ci, completers, &cr);
153
154         matches = cr.matches;
155         argv = ci.argv;
156         rl_completion_suppress_append = cr.dont_append_space;
157         rl_attempted_completion_over = !cr.filename_completion_desired;
158 out:
159         if (!matches)
160                 return NULL;
161         return matches[list_index++];
162 }
163
164 /*
165  * Attempt to complete on the contents of TEXT. START and END bound the
166  * region of rl_line_buffer that contains the word to complete.  TEXT is
167  * the word to complete.  We can use the entire contents of rl_line_buffer
168  * in case we want to do some simple parsing. Return the array of matches,
169  * or NULL if there aren't any.
170  */
171 static char **i9e_completer(const char *text, int start, __a_unused int end)
172 {
173         struct i9e_client_info *ici = i9ep->ici;
174
175         if (!ici->completers)
176                 return NULL;
177         /* Complete on command names if this is the first word in the line. */
178         if (start == 0)
179                 return rl_completion_matches(text, command_generator);
180         return rl_completion_matches(text, completion_generator);
181 }
182
183 /**
184  * Prepare writing to stdout.
185  *
186  * \param producer The buffer tree node which produces output.
187  *
188  * The i9e subsystem maintains a buffer tree node which may be attached to
189  * another node which generates output (a "producer"). When attached, the i9e
190  * buffer tree node copies the buffers generated by the producer to stdout.
191  *
192  * This function attaches the i9e input queue to an output queue of \a
193  * producer.
194  *
195  * \return Standard.
196  */
197 void i9e_attach_to_stdout(struct btr_node *producer)
198 {
199         btr_remove_node(&i9ep->stdout_btrn);
200         i9ep->stdout_btrn = btr_new_node(&(struct btr_node_description)
201                 EMBRACE(.name = "interactive_stdout", .parent = producer));
202         rl_set_keymap(i9ep->bare_km);
203 }
204
205 static void wipe_bottom_line(void)
206 {
207         char x[] = "          ";
208         int n = i9ep->num_columns;
209
210         /*
211          * For reasons beyond my understanding, writing more than 68 characters
212          * here causes MacOS to mess up the terminal. Writing a line of spaces
213          * in smaller chunks works fine though. Weird.
214          */
215         fprintf(i9ep->stderr_stream, "\r");
216         while (n > 0) {
217                 if (n >= sizeof(x)) {
218                         fprintf(i9ep->stderr_stream, "%s", x);
219                         n -= sizeof(x);
220                         continue;
221                 }
222                 x[n] = '\0';
223                 fprintf(i9ep->stderr_stream, "%s", x);
224                 break;
225         }
226         fprintf(i9ep->stderr_stream, "\r");
227 }
228
229 #ifndef RL_FREE_KEYMAP_DECLARED
230 /**
231  * Free all storage associated with a keymap.
232  *
233  * This function is not declared in the readline headers although the symbol is
234  * exported and the function is documented in the readline info file. So we
235  * have to declare it here.
236  *
237  * \param keymap The keymap to deallocate.
238  */
239 void rl_free_keymap(Keymap keymap);
240 #endif
241
242 /**
243  * Reset the terminal and save the in-memory command line history.
244  *
245  * This should be called before the caller exits.
246  */
247 void i9e_close(void)
248 {
249         char *hf = i9ep->ici->history_file;
250
251         rl_free_keymap(i9ep->bare_km);
252         rl_callback_handler_remove();
253         if (hf)
254                 write_history(hf);
255         wipe_bottom_line();
256 }
257
258 static void clear_bottom_line(void)
259 {
260         int point;
261         char *text;
262
263         if (rl_point == 0 && rl_end == 0)
264                 return wipe_bottom_line();
265         /*
266          * We might have a multi-line input that needs to be wiped here, so the
267          * simple printf("\r<space>\r") is insufficient. To workaround this, we
268          * remove the whole line, redisplay and restore the killed text.
269          */
270         point = rl_point;
271         text = rl_copy_text(0, rl_end);
272         rl_kill_full_line(0, 0);
273         rl_redisplay();
274         wipe_bottom_line(); /* wipe out the prompt */
275         rl_insert_text(text);
276         free(text);
277         rl_point = point;
278 }
279
280 static bool input_available(void)
281 {
282         fd_set rfds;
283         struct timeval tv = {0, 0};
284         int ret;
285
286         FD_ZERO(&rfds);
287         FD_SET(i9ep->ici->fds[0], &rfds);
288         ret = para_select(1, &rfds, NULL, &tv);
289         return ret > 0;
290 }
291
292 static void i9e_line_handler(char *line)
293 {
294         int ret;
295         struct btr_node *dummy;
296
297         if (!line) {
298                 i9ep->input_eof = true;
299                 return;
300         }
301         if (!*line)
302                 goto free_line;
303         rl_set_prompt("");
304         dummy = btr_new_node(&(struct btr_node_description)
305                 EMBRACE(.name = "dummy line handler"));
306         i9e_attach_to_stdout(dummy);
307         ret = i9ep->ici->line_handler(line);
308         if (ret < 0)
309                 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
310         add_history(line);
311         btr_remove_node(&dummy);
312 free_line:
313         free(line);
314 }
315
316 static int i9e_post_select(__a_unused struct sched *s, __a_unused void *context)
317 {
318         int ret;
319         struct i9e_client_info *ici = i9ep->ici;
320         char *buf;
321         size_t sz, consumed = 0;
322
323         ret = -E_I9E_EOF;
324         if (i9ep->input_eof)
325                 goto rm_btrn;
326         ret = -E_I9E_TERM_RQ;
327         if (i9ep->caught_sigterm)
328                 goto rm_btrn;
329         ret = 0;
330         if (i9ep->caught_sigint)
331                 goto rm_btrn;
332         while (input_available())
333                 rl_callback_read_char();
334         if (!i9ep->stdout_btrn)
335                 goto out;
336         ret = btr_node_status(i9ep->stdout_btrn, 0, BTR_NT_LEAF);
337         if (ret < 0) {
338                 ret = 0;
339                 goto rm_btrn;
340         }
341         if (ret == 0)
342                 goto out;
343 again:
344         sz = btr_next_buffer(i9ep->stdout_btrn, &buf);
345         if (sz == 0)
346                 goto out;
347         if (i9ep->last_write_was_status)
348                 fprintf(i9ep->stderr_stream, "\n");
349         i9ep->last_write_was_status = false;
350         ret = xwrite(ici->fds[1], buf, sz);
351         if (ret < 0)
352                 goto rm_btrn;
353         btr_consume(i9ep->stdout_btrn, ret);
354         consumed += ret;
355         if (ret == sz && consumed < 10000)
356                 goto again;
357         goto out;
358 rm_btrn:
359         if (i9ep->stdout_btrn) {
360                 wipe_bottom_line();
361                 btr_remove_node(&i9ep->stdout_btrn);
362                 rl_set_keymap(i9ep->standard_km);
363                 rl_set_prompt(i9ep->ici->prompt);
364                 rl_redisplay();
365         }
366         if (ret < 0)
367                 wipe_bottom_line();
368 out:
369         i9ep->caught_sigint = false;
370         return ret;
371 }
372
373 static void i9e_pre_select(struct sched *s, __a_unused void *context)
374 {
375         int ret;
376
377         if (i9ep->input_eof || i9ep->caught_sigint || i9ep->caught_sigterm) {
378                 sched_min_delay(s);
379                 return;
380         }
381         if (i9ep->stdout_btrn) {
382                 ret = btr_node_status(i9ep->stdout_btrn, 0, BTR_NT_LEAF);
383                 if (ret < 0) {
384                         sched_min_delay(s);
385                         return;
386                 }
387                 if (ret > 0)
388                         para_fd_set(i9ep->ici->fds[1], &s->wfds, &s->max_fileno);
389         }
390         /*
391          * fd[0] might have been reset to blocking mode if our job was moved to
392          * the background due to CTRL-Z or SIGSTOP, so set the fd back to
393          * nonblocking mode.
394          */
395         ret = mark_fd_nonblocking(i9ep->ici->fds[0]);
396         if (ret < 0)
397                 PARA_WARNING_LOG("set to nonblock failed: (fd0 %d, %s)\n",
398                         i9ep->ici->fds[0], para_strerror(-ret));
399         para_fd_set(i9ep->ici->fds[0], &s->rfds, &s->max_fileno);
400 }
401
402 static void update_winsize(void)
403 {
404         struct winsize w;
405         int ret = ioctl(i9ep->ici->fds[2], TIOCGWINSZ, (char *)&w);
406
407         if (ret >= 0) {
408                 assert(w.ws_col < sizeof(i9ep->empty_line));
409                 i9ep->num_columns = w.ws_col;
410         } else
411                 i9ep->num_columns = 80;
412
413         memset(i9ep->empty_line, ' ', i9ep->num_columns);
414         i9ep->empty_line[i9ep->num_columns] = '\0';
415 }
416
417 /**
418  * Defined key sequences are mapped to keys starting with this offset. I.e.
419  * pressing the first defined key sequence yields the key number \p KEY_OFFSET.
420  */
421 static int dispatch_key(__a_unused int count, __a_unused int key)
422 {
423         int i, ret;
424
425         for (i = i9ep->num_key_bindings - 1; i >= 0; i--) {
426                 if (strcmp(rl_executing_keyseq, i9ep->ici->bound_keyseqs[i]))
427                         continue;
428                 ret = i9ep->ici->key_handler(i);
429                 return ret < 0? ret : 0;
430         }
431         assert(0);
432 }
433
434 /**
435  * Register the i9e task and initialize readline.
436  *
437  * \param ici The i9e configuration parameters set by the caller.
438  * \param s The scheduler instance to add the i9e task to.
439  *
440  * The caller must allocate and initialize the structure \a ici points to.
441  *
442  * \return Standard.
443  */
444 int i9e_open(struct i9e_client_info *ici, struct sched *s)
445 {
446         int ret;
447
448         if (!isatty(ici->fds[0]))
449                 return -E_I9E_SETUPTERM;
450         ret = mark_fd_nonblocking(ici->fds[0]);
451         if (ret < 0)
452                 return ret;
453         ret = mark_fd_nonblocking(ici->fds[1]);
454         if (ret < 0)
455                 return ret;
456         i9ep->task = task_register(&(struct task_info) {
457                 .name = "i9e",
458                 .pre_select = i9e_pre_select,
459                 .post_select = i9e_post_select,
460                 .context = i9ep,
461         }, s);
462
463         rl_readline_name = "para_i9e";
464         rl_basic_word_break_characters = " ";
465         rl_attempted_completion_function = i9e_completer;
466         i9ep->ici = ici;
467         i9ep->stderr_stream = fdopen(ici->fds[2], "w");
468         setvbuf(i9ep->stderr_stream, NULL, _IONBF, 0);
469
470         i9ep->standard_km = rl_get_keymap();
471         i9ep->bare_km = rl_make_bare_keymap();
472         if (ici->bound_keyseqs) {
473                 char *seq;
474                 int i;
475                 /* bind each key sequence to the our dispatcher */
476                 for (i = 0; (seq = ici->bound_keyseqs[i]); i++)
477                         rl_generic_bind(ISFUNC, seq, (char *)dispatch_key,
478                                 i9ep->bare_km);
479                 i9ep->num_key_bindings = i;
480         }
481         if (ici->history_file)
482                 read_history(ici->history_file);
483         update_winsize();
484         if (ici->producer) {
485                 rl_callback_handler_install("", i9e_line_handler);
486                 i9e_attach_to_stdout(ici->producer);
487                 rl_set_keymap(i9ep->bare_km);
488         } else
489                 rl_callback_handler_install(i9ep->ici->prompt, i9e_line_handler);
490         return 1;
491 }
492
493 static void reset_line_state(void)
494 {
495         if (i9ep->stdout_btrn)
496                 return;
497         rl_on_new_line();
498         rl_reset_line_state();
499         rl_forced_update_display();
500 }
501
502 /**
503  * The log function of the i9e subsystem.
504  *
505  * \param ll Severity log level.
506  * \param fmt Printf-like format string.
507  *
508  * This clears the bottom line of the terminal if necessary and writes the
509  * string given by \a fmt to fd[2], where fd[] is the array provided earlier in
510  * \ref i9e_open().
511  */
512 __printf_2_3 void i9e_log(int ll, const char* fmt,...)
513 {
514         va_list argp;
515
516         if (ll < i9ep->ici->loglevel)
517                 return;
518         clear_bottom_line();
519         va_start(argp, fmt);
520         vfprintf(i9ep->stderr_stream, fmt, argp);
521         va_end(argp);
522         reset_line_state();
523         i9ep->last_write_was_status = false;
524 }
525
526 /**
527  * Print the current status to stderr.
528  *
529  * \param buf The text to print.
530  * \param len The number of bytes in \a buf.
531  *
532  * This clears the bottom line, moves to the beginning of the line and prints
533  * the given text. If the length of this text exceeds the width of the
534  * terminal, the text is shortened by leaving out a part in the middle.
535  */
536 void ie9_print_status_bar(char *buf, unsigned len)
537 {
538         size_t x = i9ep->num_columns, y = (x - 4) / 2;
539
540         assert(x >= 6);
541         if (len > x) {
542                 buf[y] = '\0';
543                 fprintf(i9ep->stderr_stream, "\r%s", buf);
544                 fprintf(i9ep->stderr_stream, " .. ");
545                 fprintf(i9ep->stderr_stream, "%s", buf + len - y);
546         } else {
547                 char scratch[1000];
548
549                 y = x - len;
550                 scratch[0] = '\r';
551                 strcpy(scratch + 1, buf);
552                 memset(scratch + 1 + len, ' ', y);
553                 scratch[1 + len + y] = '\r';
554                 scratch[2 + len + y] = '\0';
555                 fprintf(i9ep->stderr_stream, "\r%s", scratch);
556         }
557         i9ep->last_write_was_status = true;
558 }
559
560 /**
561  * Tell i9e that the caller received a signal.
562  *
563  * \param sig_num The number of the signal received.
564  *
565  * Currently the function only cares about \p SIGINT, but this may change.
566  */
567 void i9e_signal_dispatch(int sig_num)
568 {
569         if (sig_num == SIGWINCH)
570                 return update_winsize();
571         if (sig_num == SIGINT) {
572                 fprintf(i9ep->stderr_stream, "\n");
573                 rl_replace_line ("", false /* clear_undo */);
574                 reset_line_state();
575                 i9ep->caught_sigint = true;
576         }
577         if (sig_num == SIGTERM)
578                 i9ep->caught_sigterm = true;
579 }
580
581 /**
582  * Wrapper for select(2) which does not restart on interrupts.
583  *
584  * \param n \sa \ref para_select().
585  * \param readfds \sa \ref para_select().
586  * \param writefds \sa \ref para_select().
587  * \param timeout_tv \sa \ref para_select().
588  *
589  * \return \sa \ref para_select().
590  *
591  * The only difference between this function and \ref para_select() is that
592  * \ref i9e_select() returns zero if the select call returned \p EINTR.
593  */
594 int i9e_select(int n, fd_set *readfds, fd_set *writefds,
595                 struct timeval *timeout_tv)
596 {
597         int ret = select(n, readfds, writefds, NULL, timeout_tv);
598
599         if (ret < 0) {
600                 if (errno == EINTR)
601                         ret = 0;
602                 else
603                         ret = -ERRNO_TO_PARA_ERROR(errno);
604         }
605         return ret;
606 }
607
608 /**
609  * Return the possible completions for a given word.
610  *
611  * \param word The word to complete.
612  * \param string_list All possible words in this context.
613  * \param result String list is returned here.
614  *
615  * This function never fails. If no completion was found, a string list of
616  * length zero is returned. In any case, the result must be freed by the caller
617  * using \ref free_argv().
618  *
619  * This function is independent of readline and may be called before
620  * i9e_open().
621  *
622  * \return The number of possible completions.
623  */
624 int i9e_extract_completions(const char *word, char **string_list,
625                 char ***result)
626 {
627         char **matches = para_malloc(sizeof(char *));
628         int match_count = 0, matches_len = 1;
629         char **p;
630         int len = strlen(word);
631
632         for (p = string_list; *p; p++) {
633                 if (!is_prefix(word, *p, len))
634                         continue;
635                 match_count++;
636                 if (match_count >= matches_len) {
637                         matches_len *= 2;
638                         matches = para_realloc(matches,
639                                 matches_len * sizeof(char *));
640                 }
641                 matches[match_count - 1] = para_strdup(*p);
642         }
643         matches[match_count] = NULL;
644         *result = matches;
645         return match_count;
646 }
647
648 /**
649  * Return the list of partially matching words.
650  *
651  * \param word The command to complete.
652  * \param completers The array containing all command names.
653  *
654  * This is similar to \ref i9e_extract_completions(), but completes on the
655  * command names in \a completers.
656  *
657  * \return See \ref i9e_extract_completions().
658  */
659 char **i9e_complete_commands(const char *word, struct i9e_completer *completers)
660 {
661         char **matches;
662         const char *cmd;
663         int i, match_count, len = strlen(word);
664
665         /*
666          * In contrast to completing against an arbitrary string list, here we
667          * know all possible completions and expect that there will not be many
668          * of them. So it should be OK to iterate twice over all commands which
669          * simplifies the code a bit.
670          */
671         for (i = 0, match_count = 0; (cmd = completers[i].name); i++) {
672                 if (is_prefix(word, cmd, len))
673                         match_count++;
674         }
675         matches = para_malloc((match_count + 1) * sizeof(*matches));
676         for (i = 0, match_count = 0; (cmd = completers[i].name); i++)
677                 if (is_prefix(word, cmd, len))
678                         matches[match_count++] = para_strdup(cmd);
679         matches[match_count] = NULL;
680         return matches;
681 }
682
683 /**
684  * Complete according to the given options.
685  *
686  * \param opts All available options.
687  * \param ci Information which was passed to the completer.
688  * \param cr Result pointer.
689  *
690  * This convenience helper can be used to complete an option. The array of all
691  * possible options is passed as the first argument. Flags, i.e. options
692  * without an argument, are expected to be listed as strings of type "-X" in \a
693  * opts while options which require an argument should be passed with a
694  * trailing "=" character like "-X=".
695  *
696  * If the word can be uniquely completed to a flag option, an additional space
697  * character is appended to the output. For non-flag options no space character
698  * is appended.
699  */
700 void i9e_complete_option(char **opts, struct i9e_completion_info *ci,
701                 struct i9e_completion_result *cr)
702 {
703         int num_matches;
704
705         num_matches = i9e_extract_completions(ci->word, opts, &cr->matches);
706         if (num_matches == 1) {
707                 char *opt = cr->matches[0];
708                 char c = opt[strlen(opt) - 1];
709                 if (c == '=')
710                         cr->dont_append_space = true;
711         }
712 }
713
714 /**
715  * Print possible completions to stdout.
716  *
717  * \param completers The array of completion functions.
718  *
719  * At the end of the output a line starting with "-o=", followed by the
720  * (possibly empty) list of completion options is printed. Currently, the only
721  * two completion options are "nospace" and "filenames". The former indicates
722  * that no space should be appended even for a unique match while the latter
723  * indicates that usual filename completion should be performed in addition to
724  * the previously printed options.
725  *
726  * \return Standard.
727  */
728 int i9e_print_completions(struct i9e_completer *completers)
729 {
730         struct i9e_completion_result cr;
731         struct i9e_completion_info ci;
732         char *buf;
733         const char *end, *p;
734         int i, n, ret;
735
736         reset_completion_result(&cr);
737         buf = getenv("COMP_POINT");
738         ci.point = buf? atoi(buf) : 0;
739         ci.buffer = para_strdup(getenv("COMP_LINE"));
740
741         ci.argc = create_argv(ci.buffer, " ", &ci.argv);
742         ci.word_num = compute_word_num(ci.buffer, " ", ci.point);
743
744         end = ci.buffer + ci.point;
745         for (p = end; p > ci.buffer && *p != ' '; p--)
746                 ; /* nothing */
747         if (*p == ' ')
748                 p++;
749
750         n = end - p + 1;
751         ci.word = para_malloc(n + 1);
752         strncpy(ci.word, p, n);
753         ci.word[n] = '\0';
754
755         PARA_DEBUG_LOG("line: %s, point: %d (%c), wordnum: %d, word: %s\n",
756                 ci.buffer, ci.point, ci.buffer[ci.point], ci.word_num, ci.word);
757         if (ci.word_num == 0)
758                 cr.matches = i9e_complete_commands(ci.word, completers);
759         else
760                 create_matches(&ci, completers, &cr);
761         ret = 0;
762         if (cr.matches && cr.matches[0]) {
763                 for (i = 0; cr.matches[i]; i++)
764                         printf("%s\n", cr.matches[i]);
765                 ret = 1;
766         }
767         printf("-o=");
768         if (cr.dont_append_space)
769                 printf("nospace");
770         if (cr.filename_completion_desired)
771                 printf(",filenames");
772         printf("\n");
773         free_argv(cr.matches);
774         free_argv(ci.argv);
775         free(ci.buffer);
776         free(ci.word);
777         return ret;
778 }