ANSI color codes deleting printf - c

Iam developing a nanoShell, for a school work, and I have a problem with ANSI color codes.
So when I run the program and I write something, and press TAB, then if I backspace my tab I can also delet my previous printf, screenshot below.
I am wondering if exists on C another way to make my printf colored, or if I can block user from pressing TAB.
After the printf, Iam using fgets to read the args introduced, by user.
fgets(readpos, bufSize, stdin);
My printf is: printf("\033[1;31mnanoShell$ \033[0m");
Example:

Related

how to print with a while also using a scanf statement

I have an odd situation ive made a login system that has many printf and color statements
______________________________________________________________
example border_red(); printf (" $$ ");
_____________________________________________________________
I have multiple printf statements in the menu and in the middle of my code I have a scanf statment to scan the username. The menu gets cut off halfway through making just the top part of the menu appear.
WHAT THE MENU IS SUPPOST TO LOOK LIKE
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$ username:type here $
$ $
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
WHAT THE MENU LOOKS LIKE
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$ username: type here
i need the entire menu to show when you type in your username
QUESTION: How would i go about printing letters after a scanf statement
char username[50];
printf("#############################");
pritnf("# username:");
scanf(log, & username);
printf(# #');
printf("#############################");
how would i go about not having the scanf statement cut off my menu
Sorry if this is a simple question I may have googled it wrong but ive been at this for 1 hour now and this is the only thing I can turn to.
Both scanf and printf assume line-oriented input and output (think of a hardcopy terminal or teletype). They're just not designed to print a border, then scan from a location within that border1.
If you want to set up borders or place input fields in arbitrary places on screen, you'll have to look beyond the standard C library and use a third-party tool like ncurses.
There are tricks you can use - you can send ANSI terminal escape sequences to place the cursor at arbitrary locations on the screen, but it's ugly and a lot of work. You're better off using a library like ncurses.

Displaying text files

I'm trying to display the "Phrack" text files. The problem is that the screen doesn't clear before displaying the text file. And overwrites whatever is on the screen at the time. I've tried printf() declarations, like printf("^[[2J") and printf("^[[22;1H") and so forth. And various ncurses "clear screen" commands. None of which worked. Here's the line:
system("/usr/bin/stty -raw") | system("/usr/bin/cat /home/imp/phrack/1/P01-01") | system("/usr/bin/stty -cooked");
Thanks.
The line
printf("^[[2J")
and the tag c indicate that OP wants to write a program in C to clear the screen. The problem with that line is that there's no escape character. This would work:
printf("\033[H\033[2J"); fflush(stdout);
because it uses the escape character. I added the fflush to make it happen "now" rather than sometime later.
There are no uses of ncurses in the question.

Interactive input that doesn't appear on the in the window in C

I'm writing a program that needs to ask the user a yes or no question at the end. Going based off the example .exe my teacher provided us, the line is supposed to print out "Would you like to print an Amortization Table(Y/N)?Y" and it looks for one keystroke from the user. The Y is printed out following the question like I typed, as it is supposed to represent the default choice so if the user presses [y], [shift + y], or [enter] it goes to the function that does the amortization table and if the user presses anything else it goes to the next line When it gets the input from the user it processes the keystroke instantly as it is pressed (it does not need [enter] to process the input) without letting the keystroke appear on the command prompt. I have tried all the functions I can think of to do this (getc, getchar, getche) but everything I have tried ends up printing the user's input. Does anyone know what function he used or what trick he is doing to keep the keystroke from appearing in the command prompt? Thanks for your help in advance, I am obviously new to programming.
For the Windows platform, use the _getch() function from <conio.h> to read a keypress without buffering or echo.
Read the function description on MSDN

How do I accept Tab as input from STDIN to autofill text

How do I do autofill a text when Tab key is pressed after a period from STDIN using C?
Input would look like,
C:\>autofil.exe Hello
C:\>autofil.exe Hello. ( When I enter a period, it should autofil Hello after the period)
C:\>autofil.exe Hello.World *World is autofilled when period was entered.
Second requirement is if I am at
C:\>autofil.exe Hello.World (And when i press Tab key, `World` should print with other suggestions, as `Friend`, and if I press Tab again, print `Matt`, and scroll so on when tab is pressed... )
Well, that is my requirement. Tab complete to autofill text. Also, want to know how to read the Tab key from STDIN.
Thanks!
You could use Readline GNU library For Windows, is pretty easy to use, the other way around is to use PDcurses/Ncurses (are almost the same) and do it by hand (handling console behavior and such).
In case you use readline, to acomplish autocomplete is as simple as doing:
rl_bind_key('\t', rl_complete);
char *input = readline("C:\>");
In case you use Ncurses/PDcurses, you will have to do a little more work :)
First you save actual console parameters.
Then you set echo of the input off with noecho().
You will have to handle input and parse the arguments to call programs.
Also you will have to search in dirs (current and the ones on PATH variable) for things matching your current input.
Before ending your program set again the saved console parameters.
Ncurses How-To Is an easy right to the point guide.
Posix curses definition Contains functions and key definitions.

Why can't I read Ctrl+S in C?

I have this program in C that reads the input like this:
cod1 = getch ();
if (kbhit())
cod2 = getch ();
I can read every Ctrl+Char possible sequences, except for Ctrl+C, that closes the program - that is OK, and Ctrl+S, that simple is not catch. But I wanted to make Ctrl+S to be the save function in my program; how could I do that? Furthermore, is it possible to read Alt+Char characters? Because it reads it as a regular character, e.g., Alt+A is read with the same codes as A.
Your problem is that input probably gets eaten by terminal emulator.
For example Alt+<Whatever> is often reserved for menu shortcuts (e.g. Alt+F opens File menu). Matching characters are often hilighted once you hold Alt (F get's underscored in File).
Ctrl+S is reserved for Stops all output on screen (XOFF) (again your terminal emulator does that).
As for using Alt+<...> as shortcuts in your command line application. As far as I'm concerned holding Alt doesn't affect character received, it just sets flags which are hard to access in console. Even in GUI application (in Windows) it's quite tricky and you have to use function like GetAsyncState() to check whether alt was pressed.

Resources