paul@525 | 1 | /* |
paul@525 | 2 | * Show lines from a file. |
paul@525 | 3 | * |
paul@615 | 4 | * Copyright (C) 2022, 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@525 | 5 | * |
paul@525 | 6 | * This program is free software; you can redistribute it and/or |
paul@525 | 7 | * modify it under the terms of the GNU General Public License as |
paul@525 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@525 | 9 | * the License, or (at your option) any later version. |
paul@525 | 10 | * |
paul@525 | 11 | * This program is distributed in the hope that it will be useful, |
paul@525 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@525 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@525 | 14 | * GNU General Public License for more details. |
paul@525 | 15 | * |
paul@525 | 16 | * You should have received a copy of the GNU General Public License |
paul@525 | 17 | * along with this program; if not, write to the Free Software |
paul@525 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@525 | 19 | * Boston, MA 02110-1301, USA |
paul@525 | 20 | */ |
paul@525 | 21 | |
paul@525 | 22 | #include <l4/sys/err.h> |
paul@525 | 23 | |
paul@525 | 24 | #include <stdio.h> |
paul@525 | 25 | #include <stdlib.h> |
paul@525 | 26 | #include <string.h> |
paul@525 | 27 | |
paul@615 | 28 | /* NOTE: For inclusion in the C library: stream acquisition and access. */ |
paul@615 | 29 | |
paul@525 | 30 | #include <fsclient/client.h> |
paul@525 | 31 | #include <systypes/env.h> |
paul@525 | 32 | #include <systypes/fcntl.h> |
paul@525 | 33 | |
paul@615 | 34 | static file_t *input, *output; |
paul@615 | 35 | static char output_buffer[256]; |
paul@615 | 36 | |
paul@615 | 37 | #define _fprintf(file, s, ...) \ |
paul@615 | 38 | ( \ |
paul@615 | 39 | sprintf(output_buffer, s, ##__VA_ARGS__), \ |
paul@615 | 40 | client_write(file, output_buffer, strlen(output_buffer)) \ |
paul@615 | 41 | ) |
paul@615 | 42 | |
paul@525 | 43 | |
paul@525 | 44 | |
paul@525 | 45 | /* Read a line from the file. */ |
paul@525 | 46 | |
paul@525 | 47 | static int readline(file_t *file, int *lineno, char **start, char **end) |
paul@525 | 48 | { |
paul@525 | 49 | static char buf[256]; |
paul@525 | 50 | static offset_t current = 0, limit = 0; |
paul@615 | 51 | static int next_lineno = 1; |
paul@615 | 52 | char *newline; |
paul@615 | 53 | |
paul@615 | 54 | /* Advance to the next line if previously found. */ |
paul@525 | 55 | |
paul@615 | 56 | if (next_lineno > *lineno) |
paul@615 | 57 | *lineno = next_lineno; |
paul@615 | 58 | |
paul@615 | 59 | /* Obtain file content. */ |
paul@615 | 60 | |
paul@615 | 61 | if (!limit) |
paul@525 | 62 | { |
paul@615 | 63 | limit = client_read(file, buf, 256); |
paul@525 | 64 | |
paul@525 | 65 | if (!limit) |
paul@525 | 66 | { |
paul@615 | 67 | *start = NULL; |
paul@615 | 68 | *end = NULL; |
paul@615 | 69 | return 0; |
paul@615 | 70 | } |
paul@615 | 71 | } |
paul@525 | 72 | |
paul@615 | 73 | /* Find newline. */ |
paul@525 | 74 | |
paul@615 | 75 | newline = (char *) memchr(buf + current, (int) '\n', limit - current); |
paul@615 | 76 | *start = (buf + current); |
paul@615 | 77 | |
paul@615 | 78 | /* Return final line fragment, potentially leaving more available text. */ |
paul@525 | 79 | |
paul@615 | 80 | if (newline != NULL) |
paul@615 | 81 | { |
paul@615 | 82 | /* Advance the line number when reading again. */ |
paul@615 | 83 | |
paul@615 | 84 | next_lineno = (*lineno) + 1; |
paul@615 | 85 | |
paul@615 | 86 | /* Define the end of the line and the start of the next line. */ |
paul@525 | 87 | |
paul@615 | 88 | *end = newline + 1; |
paul@615 | 89 | current = *end - buf; |
paul@525 | 90 | |
paul@615 | 91 | if (current >= limit) |
paul@615 | 92 | { |
paul@615 | 93 | current = 0; |
paul@615 | 94 | limit = 0; |
paul@525 | 95 | } |
paul@525 | 96 | |
paul@615 | 97 | return 1; |
paul@615 | 98 | } |
paul@525 | 99 | |
paul@615 | 100 | /* Or return mid-line fragment reaching the limit of the available text. */ |
paul@525 | 101 | |
paul@615 | 102 | else |
paul@615 | 103 | { |
paul@615 | 104 | *end = buf + limit; |
paul@615 | 105 | current = 0; |
paul@615 | 106 | limit = 0; |
paul@525 | 107 | |
paul@615 | 108 | return 1; |
paul@525 | 109 | } |
paul@525 | 110 | } |
paul@525 | 111 | |
paul@525 | 112 | |
paul@525 | 113 | |
paul@525 | 114 | int main(int argc, char *argv[]) |
paul@525 | 115 | { |
paul@525 | 116 | file_t *file; |
paul@615 | 117 | int lineno, startline, numlines; |
paul@525 | 118 | char *start, *end; |
paul@525 | 119 | |
paul@615 | 120 | /* NOTE: For inclusion in the C library: stream acquisition and access. */ |
paul@615 | 121 | |
paul@615 | 122 | input = client_get_stream(ENV_INPUT_STREAM_NAME, O_RDONLY); |
paul@615 | 123 | output = client_get_stream(ENV_OUTPUT_STREAM_NAME, O_WRONLY); |
paul@615 | 124 | |
paul@525 | 125 | if (argc < 4) |
paul@525 | 126 | return 1; |
paul@525 | 127 | |
paul@615 | 128 | if (!strcmp(argv[1], "-")) |
paul@615 | 129 | file = input; |
paul@615 | 130 | else |
paul@615 | 131 | file = client_open(argv[1], O_RDONLY); |
paul@525 | 132 | |
paul@525 | 133 | if (!client_opened(file)) |
paul@525 | 134 | { |
paul@525 | 135 | if (file != NULL) |
paul@615 | 136 | _fprintf(output, "Error: %s\n", l4sys_errtostr(file->error)); |
paul@525 | 137 | else |
paul@615 | 138 | _fprintf(output, "Could not open file.\n"); |
paul@525 | 139 | |
paul@615 | 140 | client_flush(output); |
paul@615 | 141 | return 1; |
paul@525 | 142 | } |
paul@525 | 143 | |
paul@525 | 144 | startline = atoi(argv[2]); |
paul@525 | 145 | numlines = atoi(argv[3]); |
paul@525 | 146 | |
paul@615 | 147 | lineno = 1; |
paul@615 | 148 | |
paul@615 | 149 | while (lineno < startline + numlines) |
paul@560 | 150 | { |
paul@628 | 151 | /* Read a line, updating the current line number. */ |
paul@628 | 152 | |
paul@615 | 153 | if (!readline(file, &lineno, &start, &end)) |
paul@560 | 154 | { |
paul@628 | 155 | /* If the last line was passed, an empty line is an acceptable end-of-file |
paul@628 | 156 | condition. */ |
paul@628 | 157 | |
paul@628 | 158 | if (lineno >= startline + numlines) |
paul@628 | 159 | break; |
paul@628 | 160 | |
paul@628 | 161 | /* Otherwise, indicate that end-of-file occurred. */ |
paul@628 | 162 | |
paul@615 | 163 | _fprintf(output, "EOF error at line %d.\n", lineno); |
paul@615 | 164 | client_flush(output); |
paul@560 | 165 | return 1; |
paul@560 | 166 | } |
paul@615 | 167 | |
paul@628 | 168 | /* Emit line content while within the desired range of lines. */ |
paul@628 | 169 | |
paul@615 | 170 | if ((lineno >= startline) && (lineno < startline + numlines)) |
paul@615 | 171 | client_write(output, start, end - start); |
paul@560 | 172 | } |
paul@525 | 173 | |
paul@615 | 174 | client_close(file); |
paul@525 | 175 | |
paul@615 | 176 | /* NOTE: For inclusion in the C library: stream acquisition and access. */ |
paul@525 | 177 | |
paul@615 | 178 | client_flush(output); |
paul@525 | 179 | |
paul@525 | 180 | return 0; |
paul@525 | 181 | } |
paul@525 | 182 | |
paul@525 | 183 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@525 | 184 | */ |