L4Re/departure

Annotated libe2access/host/input.c

635:18f77ccd5ea8
8 months ago Paul Boddie Updated dataspace size type usage.
paul@263 1
/*
paul@263 2
 * Elementary input utilities.
paul@263 3
 *
paul@263 4
 * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
paul@263 5
 *
paul@263 6
 * This program is free software; you can redistribute it and/or
paul@263 7
 * modify it under the terms of the GNU General Public License as
paul@263 8
 * published by the Free Software Foundation; either version 2 of
paul@263 9
 * the License, or (at your option) any later version.
paul@263 10
 *
paul@263 11
 * This program is distributed in the hope that it will be useful,
paul@263 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@263 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@263 14
 * GNU General Public License for more details.
paul@263 15
 *
paul@263 16
 * You should have received a copy of the GNU General Public License
paul@263 17
 * along with this program; if not, write to the Free Software
paul@263 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@263 19
 * Boston, MA  02110-1301, USA
paul@263 20
 */
paul@263 21
paul@263 22
#include <string.h>
paul@263 23
paul@263 24
#include "input.h"
paul@263 25
paul@263 26
paul@263 27
paul@263 28
/* Read a line from a file into the given buffer. */
paul@263 29
paul@263 30
char *read_line(FILE *fp, struct read_line_state *state)
paul@263 31
{
paul@263 32
    size_t nread;
paul@263 33
paul@263 34
    do
paul@263 35
    {
paul@263 36
        do
paul@263 37
        {
paul@263 38
            /* Search for a newline character in any available text. */
paul@263 39
paul@263 40
            if (state->end > state->start)
paul@263 41
            {
paul@263 42
                state->eolp = strchr(state->start, '\n');
paul@263 43
paul@263 44
                if (state->eolp != NULL)
paul@263 45
                {
paul@263 46
                    *(state->eolp) = '\0';
paul@263 47
                    return state->eolp;
paul@263 48
                }
paul@263 49
            }
paul@263 50
paul@263 51
            /* Obtain more text if necessary. */
paul@263 52
paul@263 53
            nread = fread(state->end, sizeof(char), state->remaining, fp);
paul@263 54
paul@263 55
            /* Handle end of file condition. */
paul@263 56
paul@263 57
            if (!nread)
paul@263 58
            {
paul@263 59
                if (state->end > state->start)
paul@263 60
                    return state->end;
paul@263 61
                else
paul@263 62
                    return NULL;
paul@263 63
            }
paul@263 64
paul@263 65
            /* Zero-terminate the string for searching. */
paul@263 66
paul@263 67
            *(state->end + nread) = '\0';
paul@263 68
paul@263 69
            /* Advance the end of string and subtract remaining space. */
paul@263 70
paul@263 71
            state->end += nread;
paul@263 72
            state->remaining -= nread;
paul@263 73
        }
paul@263 74
        while (state->remaining);
paul@263 75
paul@263 76
        /* Copy the remaining text to the start of the buffer. */
paul@263 77
paul@263 78
        if (state->start > state->buffer)
paul@263 79
        {
paul@263 80
            strcpy(state->buffer, state->start);
paul@263 81
paul@263 82
            state->end -= (state->start - state->buffer);
paul@263 83
            state->start = state->buffer;
paul@263 84
            state->remaining = state->buffer_size - 1 - (state->end - state->buffer);
paul@263 85
        }
paul@263 86
    }
paul@263 87
    while (state->remaining);
paul@263 88
paul@263 89
    return NULL;
paul@263 90
}
paul@263 91
paul@263 92
/* Parse the text in the given region, returning details of arguments. */
paul@263 93
paul@263 94
void parse_line(char *start, char *end, int *num_args, char *args[], const int max_args)
paul@263 95
{
paul@263 96
    *num_args = 0;
paul@263 97
paul@263 98
    while ((start != NULL) && (start < end) && (*num_args < max_args))
paul@263 99
    {
paul@263 100
        args[*num_args] = start;
paul@263 101
        (*num_args)++;
paul@263 102
paul@263 103
        /* NOTE: Only handling spaces as delimiters. */
paul@263 104
paul@263 105
        start = strchr(start, ' ');
paul@263 106
paul@263 107
        if (start != NULL)
paul@263 108
        {
paul@263 109
            *start = '\0';
paul@263 110
paul@263 111
            if (start < end)
paul@263 112
                start++;
paul@263 113
        }
paul@263 114
    }
paul@263 115
}
paul@263 116
paul@263 117
/* vim: tabstop=4 expandtab shiftwidth=4
paul@263 118
*/