1.1 --- a/libe2access/host/Makefile Sat Feb 19 00:44:46 2022 +0100
1.2 +++ b/libe2access/host/Makefile Sat Feb 19 01:34:52 2022 +0100
1.3 @@ -42,7 +42,7 @@
1.4
1.5 # Sources and objects.
1.6
1.7 -E2ACCESS_SRC = e2access.c file.c
1.8 +E2ACCESS_SRC = e2access.c file.c input.c
1.9 E2ACCESS_OBJ = $(E2ACCESS_SRC:.c=.o)
1.10
1.11 TEST_LISTING_SRC = test_listing.c
2.1 --- a/libe2access/host/e2access.c Sat Feb 19 00:44:46 2022 +0100
2.2 +++ b/libe2access/host/e2access.c Sat Feb 19 01:34:52 2022 +0100
2.3 @@ -32,6 +32,7 @@
2.4 #include "file.h"
2.5 #include "format.h"
2.6 #include "image.h"
2.7 +#include "input.h"
2.8 #include "path.h"
2.9 #include "utils.h"
2.10
2.11 @@ -580,101 +581,6 @@
2.12 return _remove(fs, argc, argv, 0);
2.13 }
2.14
2.15 -/* Read a line from a file into the given buffer. */
2.16 -
2.17 -struct read_line_state
2.18 -{
2.19 - char *buffer, *start, *end, *eolp;
2.20 - size_t remaining;
2.21 -};
2.22 -
2.23 -static char *read_line(FILE *fp, struct read_line_state *state)
2.24 -{
2.25 - size_t nread;
2.26 -
2.27 - do
2.28 - {
2.29 - do
2.30 - {
2.31 - /* Search for a newline character in any available text. */
2.32 -
2.33 - if (state->end > state->start)
2.34 - {
2.35 - state->eolp = strchr(state->start, '\n');
2.36 -
2.37 - if (state->eolp != NULL)
2.38 - {
2.39 - *(state->eolp) = '\0';
2.40 - return state->eolp;
2.41 - }
2.42 - }
2.43 -
2.44 - /* Obtain more text if necessary. */
2.45 -
2.46 - nread = fread(state->end, sizeof(char), state->remaining, fp);
2.47 -
2.48 - /* Handle end of file condition. */
2.49 -
2.50 - if (!nread)
2.51 - {
2.52 - if (state->end > state->start)
2.53 - return state->end;
2.54 - else
2.55 - return NULL;
2.56 - }
2.57 -
2.58 - /* Zero-terminate the string for searching. */
2.59 -
2.60 - *(state->end + nread) = '\0';
2.61 -
2.62 - /* Advance the end of string and subtract remaining space. */
2.63 -
2.64 - state->end += nread;
2.65 - state->remaining -= nread;
2.66 - }
2.67 - while (state->remaining);
2.68 -
2.69 - /* Copy the remaining text to the start of the buffer. */
2.70 -
2.71 - if (state->start > state->buffer)
2.72 - {
2.73 - strcpy(state->buffer, state->start);
2.74 -
2.75 - state->end -= (state->start - state->buffer);
2.76 - state->start = state->buffer;
2.77 - state->remaining = BUFSIZE - 1 - (state->end - state->buffer);
2.78 - }
2.79 - }
2.80 - while (state->remaining);
2.81 -
2.82 - return NULL;
2.83 -}
2.84 -
2.85 -/* Parse the text in the given region, returning details of arguments. */
2.86 -
2.87 -static void parse_line(char *start, char *end, int *num_args, char *args[], const int max_args)
2.88 -{
2.89 - *num_args = 0;
2.90 -
2.91 - while ((start != NULL) && (start < end) && (*num_args < max_args))
2.92 - {
2.93 - args[*num_args] = start;
2.94 - (*num_args)++;
2.95 -
2.96 - /* NOTE: Only handling spaces as delimiters. */
2.97 -
2.98 - start = strchr(start, ' ');
2.99 -
2.100 - if (start != NULL)
2.101 - {
2.102 - *start = '\0';
2.103 -
2.104 - if (start < end)
2.105 - start++;
2.106 - }
2.107 - }
2.108 -}
2.109 -
2.110 /* Read operations from a script file. */
2.111
2.112 enum op_results
2.113 @@ -719,6 +625,7 @@
2.114 state.buffer = buffer;
2.115 state.start = buffer;
2.116 state.end = buffer;
2.117 + state.buffer_size = BUFSIZE;
2.118 state.remaining = BUFSIZE - 1;
2.119
2.120 while (read_line(fp, &state) != NULL)
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/libe2access/host/input.c Sat Feb 19 01:34:52 2022 +0100
3.3 @@ -0,0 +1,118 @@
3.4 +/*
3.5 + * Elementary input utilities.
3.6 + *
3.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
3.8 + *
3.9 + * This program is free software; you can redistribute it and/or
3.10 + * modify it under the terms of the GNU General Public License as
3.11 + * published by the Free Software Foundation; either version 2 of
3.12 + * the License, or (at your option) any later version.
3.13 + *
3.14 + * This program is distributed in the hope that it will be useful,
3.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.17 + * GNU General Public License for more details.
3.18 + *
3.19 + * You should have received a copy of the GNU General Public License
3.20 + * along with this program; if not, write to the Free Software
3.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
3.22 + * Boston, MA 02110-1301, USA
3.23 + */
3.24 +
3.25 +#include <string.h>
3.26 +
3.27 +#include "input.h"
3.28 +
3.29 +
3.30 +
3.31 +/* Read a line from a file into the given buffer. */
3.32 +
3.33 +char *read_line(FILE *fp, struct read_line_state *state)
3.34 +{
3.35 + size_t nread;
3.36 +
3.37 + do
3.38 + {
3.39 + do
3.40 + {
3.41 + /* Search for a newline character in any available text. */
3.42 +
3.43 + if (state->end > state->start)
3.44 + {
3.45 + state->eolp = strchr(state->start, '\n');
3.46 +
3.47 + if (state->eolp != NULL)
3.48 + {
3.49 + *(state->eolp) = '\0';
3.50 + return state->eolp;
3.51 + }
3.52 + }
3.53 +
3.54 + /* Obtain more text if necessary. */
3.55 +
3.56 + nread = fread(state->end, sizeof(char), state->remaining, fp);
3.57 +
3.58 + /* Handle end of file condition. */
3.59 +
3.60 + if (!nread)
3.61 + {
3.62 + if (state->end > state->start)
3.63 + return state->end;
3.64 + else
3.65 + return NULL;
3.66 + }
3.67 +
3.68 + /* Zero-terminate the string for searching. */
3.69 +
3.70 + *(state->end + nread) = '\0';
3.71 +
3.72 + /* Advance the end of string and subtract remaining space. */
3.73 +
3.74 + state->end += nread;
3.75 + state->remaining -= nread;
3.76 + }
3.77 + while (state->remaining);
3.78 +
3.79 + /* Copy the remaining text to the start of the buffer. */
3.80 +
3.81 + if (state->start > state->buffer)
3.82 + {
3.83 + strcpy(state->buffer, state->start);
3.84 +
3.85 + state->end -= (state->start - state->buffer);
3.86 + state->start = state->buffer;
3.87 + state->remaining = state->buffer_size - 1 - (state->end - state->buffer);
3.88 + }
3.89 + }
3.90 + while (state->remaining);
3.91 +
3.92 + return NULL;
3.93 +}
3.94 +
3.95 +/* Parse the text in the given region, returning details of arguments. */
3.96 +
3.97 +void parse_line(char *start, char *end, int *num_args, char *args[], const int max_args)
3.98 +{
3.99 + *num_args = 0;
3.100 +
3.101 + while ((start != NULL) && (start < end) && (*num_args < max_args))
3.102 + {
3.103 + args[*num_args] = start;
3.104 + (*num_args)++;
3.105 +
3.106 + /* NOTE: Only handling spaces as delimiters. */
3.107 +
3.108 + start = strchr(start, ' ');
3.109 +
3.110 + if (start != NULL)
3.111 + {
3.112 + *start = '\0';
3.113 +
3.114 + if (start < end)
3.115 + start++;
3.116 + }
3.117 + }
3.118 +}
3.119 +
3.120 +/* vim: tabstop=4 expandtab shiftwidth=4
3.121 +*/
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/libe2access/host/input.h Sat Feb 19 01:34:52 2022 +0100
4.3 @@ -0,0 +1,52 @@
4.4 +/*
4.5 + * Elementary input utilities.
4.6 + *
4.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
4.8 + *
4.9 + * This program is free software; you can redistribute it and/or
4.10 + * modify it under the terms of the GNU General Public License as
4.11 + * published by the Free Software Foundation; either version 2 of
4.12 + * the License, or (at your option) any later version.
4.13 + *
4.14 + * This program is distributed in the hope that it will be useful,
4.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.17 + * GNU General Public License for more details.
4.18 + *
4.19 + * You should have received a copy of the GNU General Public License
4.20 + * along with this program; if not, write to the Free Software
4.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
4.22 + * Boston, MA 02110-1301, USA
4.23 + */
4.24 +
4.25 +#ifndef __INPUT_H__
4.26 +
4.27 +
4.28 +
4.29 +#include <stdio.h>
4.30 +
4.31 +struct read_line_state
4.32 +{
4.33 + char *buffer, *start, *end, *eolp;
4.34 + size_t buffer_size, remaining;
4.35 +};
4.36 +
4.37 +#ifdef __cplusplus
4.38 +extern "C" {
4.39 +#endif
4.40 +
4.41 +void parse_line(char *start, char *end, int *num_args, char *args[],
4.42 + const int max_args);
4.43 +
4.44 +char *read_line(FILE *fp, struct read_line_state *state);
4.45 +
4.46 +#ifdef __cplusplus
4.47 +}
4.48 +#endif
4.49 +
4.50 +
4.51 +
4.52 +#endif /* __INPUT_H__ */
4.53 +
4.54 +/* vim: tabstop=4 expandtab shiftwidth=4
4.55 +*/