]> Lady’s Gitweb - CGirls/blob - cgirls-test-pathinfo.c
Add basic Les·M·L documentation generation
[CGirls] / cgirls-test-pathinfo.c
1 // SPDX-FileCopyrightText: 2025 Lady <https://www.ladys.computer/about/#lady>
2 // SPDX-License-Identifier: GPL-2.0-only
3
4 #include "aa.h"
5 #include "request.h"
6
7 // Read in lines from standard input, parse them as path info, and then
8 // reserialize them to standard output.
9 int cmd_main(int argc, [[maybe_unused]] const char* argv[argc+1]) {
10 char* lineptr[1] = { nullptr };
11 size_t linelen[1] = { 0 };
12 while (true) {
13 ssize_t readlen = getline(lineptr, linelen, stdin);
14 if (!feof(stdin) && readlen > 0) {
15 char* line = nullptr;
16 char* reline = nullptr;
17 bool empty = false;
18 if (readlen == 1) {
19 line = "";
20 empty = true;
21 } else {
22 line = strndup(lineptr[0], readlen - 1);
23 if (!line) {
24 free(lineptr[0]);
25 fprintf(stderr, "Error: Failed to allocate string for line.\n");
26 return EXIT_FAILURE;
27 }
28 }
29 cgirls_req req = cgirls_path2req(line);
30 if (!empty) {
31 free(line);
32 }
33 line = cgirls_req2path(req);
34 cgirls_freereq(req);
35 if (!line) {
36 free(lineptr[0]);
37 fprintf(stderr, "Error: Failed to allocate string for path.\n");
38 return EXIT_FAILURE;
39 }
40 req = cgirls_path2req(line);
41 reline = cgirls_req2path(req);
42 cgirls_freereq(req);
43 if (!reline) {
44 free(lineptr[0]);
45 free(line);
46 fprintf(stderr, "Error: Failed to allocate another string for path.\n");
47 return EXIT_FAILURE;
48 }
49 if (strcmp(line, reline) != 0) {
50 free(lineptr[0]);
51 fprintf(stderr, "Error: Path normalization was not idempotent for path <%s> (got <%s>).\n", line, reline);
52 free(line);
53 free(reline);
54 return EXIT_FAILURE;
55 }
56 free(reline);
57 fprintf(stdout, "%s\n", line);
58 free(line);
59 } else if (!feof(stdin)) {
60 free(lineptr[0]);
61 fprintf(stderr, "Error: Got an error from trying to read from file.\n");
62 return EXIT_FAILURE;
63 } else if (readlen > 0) {
64 free(lineptr[0]);
65 fprintf(stderr, "Error: Final line in file was not blank.\n");
66 return EXIT_FAILURE;
67 } else {
68 free(lineptr[0]);
69 return EXIT_SUCCESS;
70 }
71 }
72 }
This page took 0.431004 seconds and 5 git commands to generate.