How to quited the git bash when Re-direct as output? - c

Today I have the problem about how to quit the git bash input.
The book wrote press Ctrl+D(UNIX) or Ctrl+Z(DOS) in the new line.
I try it but failed.
So I just ask the command to quit it.

Your program is written so that it quits when you enter '#' into the terminal. It cannot detect end of file because that would require a different condition. (You would have to test for EOF, and in addition declare the variable ch as int because that is the actual return value from getchar() and because EOF is usually defined as -1.)

Related

detect EOF in readline() C

I read commands for my program using readline() (I only can use readline()) and I need to exit the program if I enter ctrl-d (EOF). When the line is empty and I enter ctrl-d, EOF is detected and NULL is returned so I can exit the shell. But when the line is not empty, readline man contains that "it is treated as a new line", and I don´t see nothing happens.
I haven´t found more information about EOF in readline.

How to provide an input to my code in order to test it?

I am trying to learn the C language by following K&R2 reference book.
I wrote a program to count how many spaces, tabs and newlines are contained in a given input. (exercise 1-8 p.20)
How can I provide to this program input to test my code ?
#include<stdio.h>
main(){
int c,ne,nt,nf;
ne = 0;
nt = 0;
nf = 0;
while((c = getchar())!=EOF){
if (c == ' '){
ne++;
}if (c== '\t'){
nt++;
}if (c== '\n'){
nf++;
}
}
printf("Input contains %d spaces, %d tabs and %d newlines.",ne,nt,nf);
}
It depends on how you're running your program.
If you're running it from the command line, the program's standard input (which is where getchar reads from) is basically your keyboard, so you can just start typing.
If you're running under an IDE, it can create a new "terminal window" for your program to run in, and for you to type input in. At least for learning, such a terminal window ought to be the default, although unfortunately it seems that sometimes you have to take additional, nonobvious steps to set this up so that it will work.
When you're typing input on your keyboard, you also need a way of saying when you're done typing. You do this by typing a "control character". Under Unix, Linux, and MacOS it's control-D, and under Windows it's control-Z. (See also notes below.)
On a suitably powerful command line, you may also have various input redirection options available to you, such as reading input from a file by involving
./myprogram < filename
or taking some other program's output and "piping" it to your programs input by invoking something like
otherprogram | ./myprogram
A few more notes about control-D and control-Z:
On Unix, Linux, and MacOS, you either have to make sure you're typing the control-D at the beginning of a line (that is, you have to type Enter, then control-D), or if you're not at the beginning of a line, you have to hit control-D twice.
On Windows, you have to hit control-Z, then hit Enter.
(Computers can be so fussy sometimes! :-) )
Also, you should know that although typing control-D or control-Z indirectly results in an EOF value eventually squirting out as getchar's return value in your C program, you will not find that the macro EOF has a value of "control D" or "control Z". EOF is usually the value -1, and the process of turning a control character typed by you on the keyboard, into an EOF value returned by getchar in a C program, is actually a rather elaborate one.
[I've written several long writeups on that "rather elaborate process", but I can't seem to find any of them just now. If you're curious, check back in a couple days, and maybe I'll have found one, or written a new one.]

text auto complete using Tab in command line

I wonder how to implement a program in C that can automatically complete the command or text that you are entering in command line.For example, say your program is prompting user for a file name. Mostly one would use scanf() or else to do this. And then in the command line user would be prompt likeplease input the file name:_.
Let's say there is a shakespeare.txt in the same directory. And now I have entered shakes, and then I want the computer to auto-complete the shakespeare.txt for me, as it does for most programs when user hitting Tab. How to implement that?
Edit:to make it more clear, another example:
if you use grep in your command line, like grep -i "shakespeare" shakespeare.txt, before you complete shakespeare.txt yourself, if you simply use Tab, there would be some candidates show up.
How can I implement my program to make it possess this utilities when I try to prompt the user for input when using function like scanf()?
If you consider using an existing utility,
take a look at the GNU readline library which provides a pretty neat implementation of what you're looking for.
There some other helpful features like moving the cursor in your input, providing an input history and an input shell-like prompt.
This library's functionality works identical on different platforms.
As this example from Wikipedia shows, the key to indicate the completion can be set easily:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
int main()
{
char* input, shell_prompt[100];
// Configure readline to auto-complete paths when the tab key is hit.
rl_bind_key('\t', rl_complete);
for(;;) {
// Create prompt string from user name and current working directory.
snprintf(shell_prompt, sizeof(shell_prompt), "%s:%s $ ", getenv("USER"), getcwd(NULL, 1024));
// Display prompt and read input (NB: input must be freed after use)...
input = readline(shell_prompt);
// Check for EOF.
if (!input)
break;
// Add input to history.
add_history(input);
// Do stuff...
// Free input.
free(input);
}
return 0;
}
Because in C there is no portable solution for knowing what you have typed in a promt without pressing enter you need to read character by character.
In windows you can use _getch() function to read one character without the need of pressing enter. The function is defined in conio.h
In linux you can use getch() function.
Note: you may want to use getche() in order to echo the character back.
You will need to define a buffer where you will store character by character everything the user press, until the user hit enter or it press enter. When you find \t you will analyze the content from buffer and take a decision based on that.
The following code is a oversimplified example:
char c;
int i = 0;
do {
c = _getche();
if (c == '\t') {
// Autocomplete
// Print the rest of the word here
}
else {
buff[i++] = c;
}
} while (c != '\n' && c!='\r');
buff[i] = '\0'; // Let's keep the standard
It's not clear whether your asking for completion performed by
The program itself while it is running. Presumably you would be implementing some custom command line.
The operating system command line (e.g., bash) before you've started your program and you just want to auto-complete the arguments passed to it
Your grep example suggests your asking about shell completion. Here is one example.

getchar does not return even though buffering is turned off

I compile and run this simple program in console of Linux 3.12, GCC 4.8:
#include <stdio.h>
main() {
setvbuf(stdin, NULL, _IONBF, 0);
printf("%c\n", getchar());
}
Run the program and press any letter, the program does not exit unless I press enter key.
What is stopping getchar() from functioning? Is there a hidden buffer somewhere?
The problem is that your terminal is buffering the input. Until your terminal sends passes the input that it receives along, your program can't see it -- and by default, it doesn't pass it through until it sees a newline.
If you're running bash on Linux, running stty -icanon should change your terminal settings to pass all input through directly without requiring newlines.
The terminal is performing the buffering. A terminal doesn't actually write the line to the standard input of the foreground program until you hit enter.
To see the program do what you're expecting, you can just run echo 'a' | ./myprog. It will immediately exit and print the single character.

Terminal behaving strangely

When I run a C program the terminal prompt gets erased. The program just prints out a line from a file. This always happens when I run this program . Is there some problem with my bash?
FILE* f;
...open, read a line
printf("%s", line);
There might be a carriage return (\r) character in your file which causes prompt to get erased as cursor is positioned to start of line.
To remove the problem with the '\r' character that Bug Catcher suggested just run your file through dos2unix. Should fix the problem.

Resources