console_read

int console_read(char *str, int cnt, int *newline)

Copies up to cnt bytes of received data to buffer pointed by str. Function tries to break the input into separate lines; once it encounters a newline character, it replaces that with end-of-string and returns.

Arguments

ArgumentsDescription
strBuffer where data is copied to.
cntMaximum number of characters to copy.
newlinePointer to an integer variable that is set to 1 when an newline character is received and set to 0 otherwise.

Returned values

Returns the number of characters copied. 0 if there was no data available, or if the first received character was ‘\n’.

Example

#define MAX_INPUT 128

static void
read_function(void *arg)
{
    char buf[MAX_INPUT];
    int rc; 
    int full_line;
    int off;

    off = 0;
    while (1) {
        rc = console_read(buf + off, MAX_INPUT - off, &full_line);
        if (rc <= 0 && !full_line) {
            continue;
        }
        off += rc;
        if (!full_line) {
            if (off == MAX_INPUT) {
                /*
                 * Full line, no newline yet. Reset the input buffer.
                 */
                off = 0;
            }
            continue;
        }
        /* Got full line - break out of the loop and process the input data */
        break;
    }
  
    /* Process the input line here */
     ....

    return;
}