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