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