Page 1 of 1
getchar()
Posted: Mon Jul 25, 2005 8:44 pm
by GLneo
hi all, ok, so far my kernel gets an IRQ1 and puts it on the screen(and stores it for later prossesing by kernel) but im starting extensive work on my std c lib and im going to need a getchar() for this but i dont now what to do? should i have a buffer for stord char's, then if a program needs one give it one(stack or que), or stop prog and wait for input then do all waiting progs get the char or just top one ???
Re:getchar()
Posted: Mon Jul 25, 2005 9:18 pm
by AR
You will need a buffer otherwise if the program isn't waiting for input then the input will be lost, in a GUI environment this may not be necessary as you may be able to take advantage of the Window Messages to immediately dispatch without the need to store it. (Naturally the message would only be dispatched to the active program via the Window Manager)
I believe the standard way (Solar knows all to do with the C library though) is that getchar() blocks if there is no input available, in implementation details you should probably have a FIFO queue as a buffer when the program isn't actively waiting for user input. In a console environment with multiple virtual consoles you may want a seperate buffer for each console, when a key is pressed it is stored in the active console's buffer. (getchar() internally draws from "stdin", stdin would represent the buffer for the console the program is executing in [stdin can be an actual file as well])
Re:getchar()
Posted: Tue Jul 26, 2005 12:16 am
by Solar
getchar() is defined to be equivalent to fgetc(stdin), on which
Dinkumware has to say:
The function reads the next character c (if present) from the input stream stream, advances the file-position indicator (if defined), and returns (int)(unsigned char)c. If the function sets either the end-of-file indicator or the error indicator, it returns EOF.
I'd happily quote directly from the standard, but I'm afraid I don't have it with me.
Re:getchar()
Posted: Tue Jul 26, 2005 9:16 am
by GLneo
thx, but in windows i do something like getchar for a yes or no menu but you have to type Y and hit [ENTER] does this function wait for a letter or enter or just give EOF if nothing in stdin???
Re:getchar()
Posted: Tue Jul 26, 2005 9:41 am
by Solar
Why don't you test it yourself?
Code: Select all
#include <stdio.h>
int main()
{
int c;
while ( ( c = getchar() ) != EOF )
{
putchar( c );
}
return 0;
}
Re:getchar()
Posted: Tue Jul 26, 2005 11:13 am
by GLneo
Re:getchar()
Posted: Tue Jul 26, 2005 11:29 am
by Solar
Only upon hitting the return key does what you entered become "stdin" - the shell window does buffer the input until then.
The program then loops over every char of the input, and prints it to the output, until there is no more input - which makes getchar() wait for the next char to be received. The program does
not terminate at this point, because there
has not been an EOF received so far.
That should happen when you type Ctrl-D first thing on a new line IIRC.
(I could have explained what would happen, but you know... teach a man to fish.
)
Re:getchar()
Posted: Tue Jul 26, 2005 11:31 am
by GLneo
your wright it makes more sence to see for your self
Re:getchar()
Posted: Tue Jul 26, 2005 5:01 pm
by Cjmovie
Actually, I beleive all windows does is (at getchar) take in ONE character to give. The thing is, if you don't have any getchar's and you type something, it immedietly buffers it. Then, say, you had:
{
char c = getchar();
}
and the user had pressed 'y' then enter, this would be true:
{
getchar() == 0X0D; //0X0D is 13, or enter scancode
}
So it buffers and DOES get that enter, and WAIT for it, but it is continually buffering, not just after you press enter. That way you could even retreive the 'enter' key press after the initial getchar().
Re:getchar()
Posted: Tue Jul 26, 2005 5:42 pm
by GLneo
all that that means is windows puts the trailing enter on the buffer(STDIN)
Re:getchar()
Posted: Tue Jul 26, 2005 8:35 pm
by GLneo
ok, my code is:
Code: Select all
char getchar()
{
char c;
c = curstdin[curbot];
if(curbot >= STDINSZ)
curbot = 0;
else
curbot++;
return c;
}
curstdin = my loop queue stdin buffer
curbot = bottom of queue
STDINSZ = size of stdin
the problem = it doesn't wait for key, it just loops though the queue, how do i get it to wait for valed data???, thx
Re:getchar()
Posted: Tue Jul 26, 2005 10:17 pm
by AR
Try something along the lines of:
Code: Select all
char getchar()
{
/* Wait until data is available */
while(curbot == curend)
__asm__ __volatile__ ("hlt");
char c = curstdin[curbot++];
if(curbot == STDINSZ) curbot = 0;
return c;
}
void Keyboard_IRQ()
{
/* Make sure we don't overrun unread data */
if((curend+1) == curbot)
{
Keyboard_GetKey(); /* Drop the input */
return;
}
curstdin[curend++] = Keyboard_GetKey();
if(curend == STDINSZ) curend = 0;
}
This is a theoretical implementation as always.
WARNING: It is only designed for these 2 functions, if you add more functions that modify "curend" then you will need locking primitives to make it threadsafe.