# HG changeset patch # User Paul Boddie # Date 1645230892 -3600 # Node ID 9edfe5795697a1580331366832f936bde834fd30 # Parent 69bbf40245617b472cc4981de74405f456c46eb5 Moved input-related functions into a separate module. diff -r 69bbf4024561 -r 9edfe5795697 libe2access/host/Makefile --- a/libe2access/host/Makefile Sat Feb 19 00:44:46 2022 +0100 +++ b/libe2access/host/Makefile Sat Feb 19 01:34:52 2022 +0100 @@ -42,7 +42,7 @@ # Sources and objects. -E2ACCESS_SRC = e2access.c file.c +E2ACCESS_SRC = e2access.c file.c input.c E2ACCESS_OBJ = $(E2ACCESS_SRC:.c=.o) TEST_LISTING_SRC = test_listing.c diff -r 69bbf4024561 -r 9edfe5795697 libe2access/host/e2access.c --- a/libe2access/host/e2access.c Sat Feb 19 00:44:46 2022 +0100 +++ b/libe2access/host/e2access.c Sat Feb 19 01:34:52 2022 +0100 @@ -32,6 +32,7 @@ #include "file.h" #include "format.h" #include "image.h" +#include "input.h" #include "path.h" #include "utils.h" @@ -580,101 +581,6 @@ return _remove(fs, argc, argv, 0); } -/* Read a line from a file into the given buffer. */ - -struct read_line_state -{ - char *buffer, *start, *end, *eolp; - size_t remaining; -}; - -static char *read_line(FILE *fp, struct read_line_state *state) -{ - size_t nread; - - do - { - do - { - /* Search for a newline character in any available text. */ - - if (state->end > state->start) - { - state->eolp = strchr(state->start, '\n'); - - if (state->eolp != NULL) - { - *(state->eolp) = '\0'; - return state->eolp; - } - } - - /* Obtain more text if necessary. */ - - nread = fread(state->end, sizeof(char), state->remaining, fp); - - /* Handle end of file condition. */ - - if (!nread) - { - if (state->end > state->start) - return state->end; - else - return NULL; - } - - /* Zero-terminate the string for searching. */ - - *(state->end + nread) = '\0'; - - /* Advance the end of string and subtract remaining space. */ - - state->end += nread; - state->remaining -= nread; - } - while (state->remaining); - - /* Copy the remaining text to the start of the buffer. */ - - if (state->start > state->buffer) - { - strcpy(state->buffer, state->start); - - state->end -= (state->start - state->buffer); - state->start = state->buffer; - state->remaining = BUFSIZE - 1 - (state->end - state->buffer); - } - } - while (state->remaining); - - return NULL; -} - -/* Parse the text in the given region, returning details of arguments. */ - -static void parse_line(char *start, char *end, int *num_args, char *args[], const int max_args) -{ - *num_args = 0; - - while ((start != NULL) && (start < end) && (*num_args < max_args)) - { - args[*num_args] = start; - (*num_args)++; - - /* NOTE: Only handling spaces as delimiters. */ - - start = strchr(start, ' '); - - if (start != NULL) - { - *start = '\0'; - - if (start < end) - start++; - } - } -} - /* Read operations from a script file. */ enum op_results @@ -719,6 +625,7 @@ state.buffer = buffer; state.start = buffer; state.end = buffer; + state.buffer_size = BUFSIZE; state.remaining = BUFSIZE - 1; while (read_line(fp, &state) != NULL) diff -r 69bbf4024561 -r 9edfe5795697 libe2access/host/input.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libe2access/host/input.c Sat Feb 19 01:34:52 2022 +0100 @@ -0,0 +1,118 @@ +/* + * Elementary input utilities. + * + * Copyright (C) 2022 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include + +#include "input.h" + + + +/* Read a line from a file into the given buffer. */ + +char *read_line(FILE *fp, struct read_line_state *state) +{ + size_t nread; + + do + { + do + { + /* Search for a newline character in any available text. */ + + if (state->end > state->start) + { + state->eolp = strchr(state->start, '\n'); + + if (state->eolp != NULL) + { + *(state->eolp) = '\0'; + return state->eolp; + } + } + + /* Obtain more text if necessary. */ + + nread = fread(state->end, sizeof(char), state->remaining, fp); + + /* Handle end of file condition. */ + + if (!nread) + { + if (state->end > state->start) + return state->end; + else + return NULL; + } + + /* Zero-terminate the string for searching. */ + + *(state->end + nread) = '\0'; + + /* Advance the end of string and subtract remaining space. */ + + state->end += nread; + state->remaining -= nread; + } + while (state->remaining); + + /* Copy the remaining text to the start of the buffer. */ + + if (state->start > state->buffer) + { + strcpy(state->buffer, state->start); + + state->end -= (state->start - state->buffer); + state->start = state->buffer; + state->remaining = state->buffer_size - 1 - (state->end - state->buffer); + } + } + while (state->remaining); + + return NULL; +} + +/* Parse the text in the given region, returning details of arguments. */ + +void parse_line(char *start, char *end, int *num_args, char *args[], const int max_args) +{ + *num_args = 0; + + while ((start != NULL) && (start < end) && (*num_args < max_args)) + { + args[*num_args] = start; + (*num_args)++; + + /* NOTE: Only handling spaces as delimiters. */ + + start = strchr(start, ' '); + + if (start != NULL) + { + *start = '\0'; + + if (start < end) + start++; + } + } +} + +/* vim: tabstop=4 expandtab shiftwidth=4 +*/ diff -r 69bbf4024561 -r 9edfe5795697 libe2access/host/input.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libe2access/host/input.h Sat Feb 19 01:34:52 2022 +0100 @@ -0,0 +1,52 @@ +/* + * Elementary input utilities. + * + * Copyright (C) 2022 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#ifndef __INPUT_H__ + + + +#include + +struct read_line_state +{ + char *buffer, *start, *end, *eolp; + size_t buffer_size, remaining; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +void parse_line(char *start, char *end, int *num_args, char *args[], + const int max_args); + +char *read_line(FILE *fp, struct read_line_state *state); + +#ifdef __cplusplus +} +#endif + + + +#endif /* __INPUT_H__ */ + +/* vim: tabstop=4 expandtab shiftwidth=4 +*/