getchar()

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
GLneo

getchar()

Post 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 ???
AR

Re:getchar()

Post 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])
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:getchar()

Post 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. ;)
Every good solution is obvious once you've found it.
GLneo

Re:getchar()

Post 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???
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:getchar()

Post 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;
}
Every good solution is obvious once you've found it.
GLneo

Re:getchar()

Post by GLneo »

:o :o :o i type a sentence and it prints the entier sectence not just a char is that right???
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:getchar()

Post 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. :D )
Every good solution is obvious once you've found it.
GLneo

Re:getchar()

Post by GLneo »

your wright it makes more sence to see for your self ;)
Cjmovie

Re:getchar()

Post 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().
GLneo

Re:getchar()

Post by GLneo »

all that that means is windows puts the trailing enter on the buffer(STDIN)
GLneo

Re:getchar()

Post 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
AR

Re:getchar()

Post 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.
Post Reply