0

I've got a a program in which I want to prompt the user for some file and folder names. Ideally they just type half of it and then press tab to complete it based on files in the current working directory. However, when my program is running and I use scanf, fgets, fgetc, getchar, whatever, I press tab and it simply inserts a tab-character.

Whilst looking for a solution I learned about the GNU Readline library. Using readline() instead of fgets(), I have tab-completion again! But I'd really rather not use it because of all the unnecessary features and keybinds it adds which I don't need. (using readline, I can't use Ctrl-C to interrupt my program anymore, what's up with that?)

I thought tab-completion was a feature of the shell I used, the text I typed inside there was inaccessible to my program until after I pressed Enter. So what is Readline doing? Is it modifying the characters as I type them, or is it simply enabling a setting of the shell? How can I do this myself? Ideally without using the GNU Readline library.

Sander
  • 325
  • 2
  • 8
  • 1
    Your shell uses an internal or external library for that. There's no easy solution but to use a library. This is far from trivial to implement. – Cheatah Dec 18 '21 at 21:01
  • 1
    If you want to be able to change certain characters on the screen and hide input that the user typed, then this cannot be accomplished in pure ISO C. Instead, you require OS-specific functionality. On Linux, you may want to take a look at [ncurses](https://en.wikipedia.org/wiki/Ncurses). On Microsoft Windows, you can use the [Console API](https://learn.microsoft.com/en-us/windows/console/console-functions). – Andreas Wenzel Dec 18 '21 at 21:10
  • 1
    `I'd really rather not use it because of all the unnecessary features and keybinds it adds which I don't need` Readline is open-source and in C. If you ask "how can I add/implement tab-completion in my C command line tool?" then you can use readline source code - it's an example of just that. You seem to be asking about ANSI escape sequences and terminal RAW mode - both topics you should research. – KamilCuk Dec 18 '21 at 22:21
  • Readline *is* the library that bash uses to implement line-editing and tab completion. (Other shells also use it, or something similar, but it was basically written in conjunction with bash.) – rici Dec 18 '21 at 23:43
  • If your question is "how do I maintain the function of Ctl-C while I use the readline library?" You should ask that (much more focused) question. – rici Dec 18 '21 at 23:46

0 Answers0