paul@65 | 1 | /* |
paul@65 | 2 | * Test pipe operations. |
paul@65 | 3 | * |
paul@65 | 4 | * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@65 | 5 | * |
paul@65 | 6 | * This program is free software; you can redistribute it and/or |
paul@65 | 7 | * modify it under the terms of the GNU General Public License as |
paul@65 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@65 | 9 | * the License, or (at your option) any later version. |
paul@65 | 10 | * |
paul@65 | 11 | * This program is distributed in the hope that it will be useful, |
paul@65 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@65 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@65 | 14 | * GNU General Public License for more details. |
paul@65 | 15 | * |
paul@65 | 16 | * You should have received a copy of the GNU General Public License |
paul@65 | 17 | * along with this program; if not, write to the Free Software |
paul@65 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@65 | 19 | * Boston, MA 02110-1301, USA |
paul@65 | 20 | */ |
paul@65 | 21 | |
paul@65 | 22 | #include <l4/re/env.h> |
paul@65 | 23 | #include <l4/sys/err.h> |
paul@65 | 24 | |
paul@65 | 25 | #include <stdio.h> |
paul@65 | 26 | #include <string.h> |
paul@65 | 27 | #include <stdlib.h> |
paul@65 | 28 | |
paul@65 | 29 | #include "file.h" |
paul@65 | 30 | #include "memory_utils.h" |
paul@65 | 31 | |
paul@65 | 32 | |
paul@65 | 33 | |
paul@65 | 34 | static void show(file_t *file, offset_t step, offset_t sample) |
paul@65 | 35 | { |
paul@65 | 36 | /* Allocate a buffer for sampling from the file. */ |
paul@65 | 37 | |
paul@65 | 38 | char buf[sample + 1]; |
paul@65 | 39 | |
paul@65 | 40 | for (offset_t offset = 0; offset < file_populated_span(file); offset += step) |
paul@65 | 41 | { |
paul@65 | 42 | printf("show %ld of %ld...\n", offset, file_populated_span(file)); |
paul@65 | 43 | |
paul@65 | 44 | unsigned long remaining = file_populated_span(file) - offset; |
paul@65 | 45 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@65 | 46 | |
paul@65 | 47 | printf("%ld bytes from %p...\n", sample_remaining, (file->memory + offset)); |
paul@65 | 48 | strncpy(buf, (file->memory + offset), sample_remaining); |
paul@65 | 49 | buf[sample_remaining] = '\0'; |
paul@65 | 50 | printf("%s\n", buf); |
paul@65 | 51 | } |
paul@65 | 52 | } |
paul@65 | 53 | |
paul@66 | 54 | |
paul@66 | 55 | |
paul@66 | 56 | const unsigned int PIPE_PAGES = 2; |
paul@66 | 57 | |
paul@65 | 58 | int main(void) |
paul@65 | 59 | { |
paul@65 | 60 | /* Obtain access to the filesystem. */ |
paul@65 | 61 | |
paul@65 | 62 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@65 | 63 | |
paul@65 | 64 | /* Invoke the open method to receive the file reference. */ |
paul@65 | 65 | |
paul@65 | 66 | file_t reader, writer; |
paul@66 | 67 | long err = pipe_open(page(PIPE_PAGES), &reader, &writer, server); |
paul@65 | 68 | |
paul@65 | 69 | if (err) |
paul@65 | 70 | { |
paul@65 | 71 | printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); |
paul@65 | 72 | return 1; |
paul@65 | 73 | } |
paul@65 | 74 | |
paul@65 | 75 | /* Use the writer to fill the pipe with data. */ |
paul@65 | 76 | |
paul@65 | 77 | for (int region = 0; region < 3; region++) |
paul@65 | 78 | { |
paul@65 | 79 | printf("Write %ld to pipe at %p...\n", file_span(&writer), writer.memory); |
paul@65 | 80 | |
paul@65 | 81 | memset(writer.memory, (int) 'a' + region, file_span(&writer)); |
paul@65 | 82 | |
paul@65 | 83 | err = pipe_written(&writer, file_span(&writer)); |
paul@65 | 84 | |
paul@65 | 85 | if (err) |
paul@65 | 86 | { |
paul@65 | 87 | printf("Written data error: %s\n", l4sys_errtostr(err)); |
paul@65 | 88 | return 1; |
paul@65 | 89 | } |
paul@65 | 90 | |
paul@65 | 91 | show(&writer, page(1), 60); |
paul@65 | 92 | |
paul@65 | 93 | err = pipe_next(&writer); |
paul@65 | 94 | |
paul@65 | 95 | if (err) |
paul@65 | 96 | { |
paul@72 | 97 | printf("Region traversal error at region %d: %s\n", region, l4sys_errtostr(err)); |
paul@72 | 98 | break; |
paul@65 | 99 | } |
paul@65 | 100 | } |
paul@65 | 101 | |
paul@65 | 102 | /* Use the reader to obtain data from the pipe. */ |
paul@65 | 103 | |
paul@65 | 104 | err = pipe_current(&reader); |
paul@65 | 105 | |
paul@65 | 106 | while (!err) |
paul@65 | 107 | { |
paul@65 | 108 | printf("show...\n"); |
paul@65 | 109 | show(&reader, page(1), 60); |
paul@65 | 110 | err = pipe_next(&reader); |
paul@65 | 111 | } |
paul@65 | 112 | |
paul@65 | 113 | if (err) |
paul@65 | 114 | printf("Reading termination condition: %s\n", l4sys_errtostr(err)); |
paul@65 | 115 | |
paul@65 | 116 | printf("Data shown.\n"); |
paul@65 | 117 | |
paul@65 | 118 | return 0; |
paul@65 | 119 | } |
paul@65 | 120 | |
paul@65 | 121 | // vim: tabstop=2 expandtab shiftwidth=2 |