13 /* add abstraction at no cost, but may add more complexity as well? */
14 enum { FALSE
= 0, TRUE
= 1 };
17 #define COMPLETE_MODE 0
18 #define LINE_NUMBERS 1
20 /* preprocessor macros */
21 #define LENGTH(x) (sizeof(x) / sizeof(*x))
22 #define CONTROL(char) (char ^ 0x40)
23 #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
24 #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
27 * Fill the buffer apropriately with the lines and headers
30 fill_buffer(char *separator
)
32 /* fill buffer with string */
33 char string
[LINE_SIZE
];
35 Buffer
*buffer
= malloc(sizeof(Buffer
));
36 FILE *fp
= fopen("/dev/stdin", "r");
39 die("Can not open file for reading.");
42 /* read the file into a doubly linked list of lines */
43 while (fgets(string
, LINE_SIZE
, fp
)) {
45 last
= add_line(buffer
, buffer
->total
, string
, separator
, last
);
48 /* set the buffer stats */
49 buffer
->current
= buffer
->first
;
52 buffer
->empty
= malloc(sizeof(Line
));
53 buffer
->empty
->content
= "";
54 buffer
->empty
->comment
= "";
55 buffer
->empty
->number
= 0;
56 buffer
->empty
->matches
= 0;
57 buffer
->empty
->next
= buffer
->first
;
58 buffer
->empty
->prev
= buffer
->first
;
64 * Parse the line content to determine if it is a header and identify the
68 parse_line(char *s
, char *separator
)
70 Line
*line
= malloc(sizeof(Line
));
71 char *sep
= strstr(s
, separator
);
72 int pos
= !sep
? (int) strlen(s
) - 1 : (int) (sep
- s
);
74 /* strip trailing newline */
75 s
[strlen(s
) - 1] = '\0';
77 /* fill line->content */
78 line
->content
= malloc((pos
+ 1) * sizeof(char));
79 strncpy(line
->content
, s
, pos
);
81 /* fill line->comment */
83 line
->comment
= malloc((strlen(s
) - pos
) * sizeof(char));
84 strcpy(line
->comment
, s
+ pos
);
93 * Add a line to the end of the current buffer.
95 * This requires to create a new line with a link to the previous line
96 * and to NULL as the next line.
98 * The previous line's 'next' should be relinked to this new line.
100 * The header's last line have to point to this last line
103 add_line(Buffer
*buffer
, int number
, char *string
, char *separator
, Line
*prev
)
105 /* allocate new line */
106 Line
*line
= malloc(sizeof(Line
));
107 line
= parse_line(string
, separator
);
111 line
->number
= number
;
113 /* interlink with previous line if exists */
115 buffer
->first
= line
;
125 * Set buffer->candidates to an array of lines that match and update
126 * buffer->matching to number of matching candidates.
129 filter_lines(Buffer
*buffer
)
131 Line
* line
= buffer
->first
;
132 buffer
->matching
= 0;
135 line
->matches
= line_match_input(line
, buffer
->input
);
136 buffer
->matching
+= line
->matches
;
143 * Check if line matches and return TRUE if so
146 line_match_input(Line
*line
, char *input
)
149 if (!strncmp(input
, line
->content
, strlen(input
)))
152 if (strstr(line
->content
, input
))
160 * Replace tab as a multiple of 8 spaces in a line.
163 expand_tabs(char *line
)
166 char *converted
= malloc(sizeof(char) * (strlen(line
) * 8 + 1));
168 for (i
= 0, n
= 0; i
< strlen(line
); i
++, n
++) {
169 if (line
[i
] == '\t') {
173 for (; (n
) % 8 != 0; n
++) {
179 converted
[n
] = line
[i
];
189 * Print a line to stderr.
192 print_line(Line
*line
, int current
, int cols
)
196 char *content
= expand_tabs(line
->content
);
197 char *comment
= expand_tabs(line
->comment
);
199 /* clean the line in case it was not empty */
200 fputs("\033[K", stderr
);
202 /* line number if option set */
205 fputs("\033[1m", stderr
);
207 fputs("\033[1;30m", stderr
);
210 fprintf(stderr
, "%7d\033[0m ", line
->number
);
216 /* highlight current line */
218 fputs("\033[1;33m", stderr
);
221 /* print content without overflowing terminal width */
222 for (i
= 0; i
< strlen(content
) && n
< cols
; n
++, i
++) {
223 fputc(content
[i
], stderr
);
226 /* print spaces without overflowing terminal width */
227 for (i
= n
; i
<= 40 && n
< cols
; n
++, i
++) {
231 /* comments in grey */
232 fputs("\033[1;30m", stderr
);
234 /* print comment without overflowing terminal width */
235 for (i
= 0; i
< strlen(comment
) && n
< cols
; n
++, i
++) {
236 fputc(comment
[i
], stderr
);
239 fputs("\033[0m\n", stderr
);
246 * Print a header title.
254 * Print all the lines from an array of pointer to lines.
256 * The total number oflines printed shall not excess 'count'.
259 print_lines(Buffer
*buffer
, int count
, int offset
, int cols
)
261 Line
*line
= buffer
->current
;
265 /* seek back from current line to the first line to print */
266 while (line
&& i
< count
- offset
) {
267 i
= line
->matches
? i
+ 1 : i
;
270 line
= line
? line
: buffer
->first
;
272 /* print up to count lines that match the input */
273 while (line
&& j
< count
) {
275 print_line(line
, line
== buffer
->current
, cols
);
282 /* continue up to the end of the screen clearing it */
283 for (; j
< count
; j
++) {
284 fputs("\r\033[K\n", stderr
);
289 * Update the screen interface and print all candidates.
291 * This also has to clear the previous lines.
294 update_screen(Buffer
*buffer
, int count
, int offset
, int tty
)
297 ioctl(tty
, TIOCGWINSZ
, &w
);
300 print_lines(buffer
, count
, offset
, w
.ws_col
);
302 /* go up to the prompt position and update it */
303 fprintf(stderr
, "\033[%dA", count
+ 1);
304 print_prompt(buffer
, w
.ws_col
);
307 void clear_screen(int count
)
310 for (i
= 0; i
< count
+ 1; i
++) {
311 fputs("\r\033[K\n", stderr
);
314 fprintf(stderr
, "\033[%dA", count
+ 1);
318 * Set terminal to send one char at a time for interactive mode, and return the
319 * last terminal state.
322 terminal_set(int tty
)
324 struct termios termio_old
;
325 struct termios termio_new
;
327 /* set the terminal to send one key at a time. */
329 /* get the terminal's state */
330 if (tcgetattr(tty
, &termio_old
) < 0) {
331 die("Can not get terminal attributes with tcgetattr().");
334 /* create a new modified state by switching the binary flags */
335 termio_new
= termio_old
;
336 termio_new
.c_lflag
&= ~(ICANON
| ECHO
| IGNBRK
);
338 /* apply this state to current terminal now (TCSANOW) */
339 tcsetattr(tty
, TCSANOW
, &termio_new
);
345 * Listen for the user input and call the appropriate functions.
348 get_input(Buffer
*buffer
, int count
, int offset
, int tty
)
350 FILE *tty_fd
= fopen("/dev/tty", "r");
352 /* receive one character at a time from the terminal */
353 struct termios termio_old
= terminal_set(tty
);
355 /* get input char by char from the keyboard. */
356 while (do_key(fgetc(tty_fd
), buffer
)) {
357 update_screen(buffer
, count
, offset
, tty
);
360 /* resets the terminal to the previous state. */
361 tcsetattr(tty
, TCSANOW
, &termio_old
);
367 * Perform action associated with key
370 do_key(char key
, Buffer
*buffer
)
381 buffer
->input
[0] = '\0';
382 buffer
->current
= buffer
->first
;
383 filter_lines(buffer
);
387 length
= strlen(buffer
->input
) - 1;
389 for (i
= length
; i
>= 0 && isspace(buffer
->input
[i
]); i
--) {
390 buffer
->input
[i
] = '\0';
393 length
= strlen(buffer
->input
) - 1;
394 for (i
= length
; i
>= 0 && !isspace(buffer
->input
[i
]); i
--) {
395 buffer
->input
[i
] = '\0';
398 filter_lines(buffer
);
403 case CONTROL('H'): /* backspace */
404 buffer
->input
[strlen(buffer
->input
) - 1] = '\0';
405 filter_lines(buffer
);
407 if (!buffer
->current
->matches
) {
408 do_next_line(buffer
);
409 do_prev_line(buffer
);
414 do_next_line(buffer
);
418 do_prev_line(buffer
);
421 case CONTROL('I'): /* tab */
423 strcpy(buffer
->input
, buffer
->current
->content
);
424 filter_lines(buffer
);
426 do_next_line(buffer
);
431 case CONTROL('J'): /* enter */
432 fputs("\r\033[K", stderr
);
437 puts(buffer
->current
->content
);
443 length
= strlen(buffer
->input
);
444 buffer
->input
[length
] = key
;
445 buffer
->input
[length
+ 1] = '\0';
448 filter_lines(buffer
);
450 if (!buffer
->current
->matches
) {
451 do_next_line(buffer
);
452 do_prev_line(buffer
);
455 if (!buffer
->current
->matches
) {
456 buffer
->current
= buffer
->empty
;
464 * Set the current line to the next matching line, if any.
467 do_prev_line(Buffer
*buffer
)
469 Line
* line
= buffer
->current
;
475 buffer
->current
= line
;
483 * Set the current line to the next matching line, if any.
486 do_next_line(Buffer
*buffer
)
488 Line
* line
= buffer
->current
;
494 buffer
->current
= line
;
502 * Print the prompt, before the input, with the number of candidates that
506 print_prompt(Buffer
*buffer
, int cols
)
510 int matching
= buffer
->matching
;
511 int total
= buffer
->total
;
512 char *input
= expand_tabs(buffer
->input
);
513 char *suggest
= expand_tabs(buffer
->current
->content
);
515 /* for the '/' separator between the numbers */
518 /* count the number of digits */
519 for (i
= matching
; i
; i
/= 10, digits
++);
520 for (i
= total
; i
; i
/= 10, digits
++);
523 fputs("\r\033[K> ", stderr
);
526 /* input without overflowing terminal width */
527 for (i
= 0; i
< strlen(input
) && cols
> digits
; cols
--, i
++) {
528 fputc(input
[i
], stderr
);
531 /* save the cursor position at the end of the input */
532 fputs("\033[s", stderr
);
535 fputs("\033[1;30m", stderr
);
537 /* suggest without overflowing terminal width */
539 for (; i
< strlen(suggest
) && cols
> digits
; cols
--, i
++) {
540 fputc(suggest
[i
], stderr
);
544 /* go to the end of the line */
545 for (i
= 0; cols
> digits
; cols
--, i
++) {
549 /* total match and line count at the end of the line */
550 fprintf(stderr
, "%d/%d", matching
, total
);
552 /* restore cursor position at the end of the input */
553 fputs("\033[u", stderr
);
560 * Reset the terminal state and exit with error.
562 void die(const char *s
)
564 /* tcsetattr(STDIN_FILENO, TCSANOW, &termio_old); */
565 fprintf(stderr
, "%s\n", s
);
571 main(int argc
, char *argv
[])
574 Buffer
*buffer
= NULL
;
575 char *separator
= "separator";
578 int tty
= open("/dev/tty", O_RDWR
);
580 /* command line arguments */
581 for (i
= 0; i
<= argc
; i
++) {
582 if (argv
[i
][1] == '-') {
587 /* command line arguments */
588 buffer
= fill_buffer(separator
);
590 /* set the interface */
591 filter_lines(buffer
);
592 update_screen(buffer
, count
, offset
, tty
);
594 /* listen and interact to input */
595 get_input(buffer
, count
, offset
, tty
);