L4Re/departure

libe2access/host/input.c

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