L4Re/departure

Annotated fsaccess/input.c

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