1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/* This file is part of GNU Dico
Copyright (C) 2012-2024 Sergey Poznyakoff
GNU Dico 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 3, or (at your option)
any later version.
GNU Dico 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 GNU Dico. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dico.h>
#define CRLF_IN 1
#define CRLF_OUT 2
int
main(int argc, char **argv)
{
int rc;
char *filename = NULL;
size_t sz;
dico_stream_t in, out, s;
char buf[512];
int flags = 0;
size_t in_bufsize = 1024, out_bufsize = 1024;
int buffered;
dico_set_program_name(argv[0]);
while (--argc) {
char *arg = *++argv;
if (strncmp(arg, "-file=", 6) == 0)
filename = arg + 6;
else if (strcmp(arg, "-in") == 0)
flags |= CRLF_IN;
else if (strcmp(arg, "-out") == 0)
flags |= CRLF_OUT;
else if (strncmp(arg, "-bufsize=", 9) == 0) {
in_bufsize = out_bufsize = atoi(arg + 9);
buffered = CRLF_IN | CRLF_OUT;
} else if (strncmp(arg, "-outbufsize=", 12) == 0) {
out_bufsize = atoi(arg + 12);
buffered |= CRLF_OUT;
} else if (strncmp(arg, "-inbufsize=", 11) == 0)
in_bufsize |= atoi(arg + 11);
else if (strcmp(arg, "--") == 0) {
--argc;
++argv;
} else if (arg[0] == '-') {
dico_log(L_ERR, 0, "unknown option '%s'", arg);
return 1;
} else
break;
}
if (argc) {
fprintf(stderr, "Usage: %s [-file=S]\n", dico_program_name);
return 1;
}
if (filename) {
in = dico_mapfile_stream_create(filename, DICO_STREAM_READ);
if (!in) {
dico_log(L_ERR, errno, "cannot create stream `%s'", filename);
return 1;
}
} else {
in = dico_fd_stream_create(0, DICO_STREAM_READ, 1);
if (!in) {
dico_log(L_ERR, errno, "cannot create stdin stream");
return 1;
}
}
rc = dico_stream_open(in);
if (rc) {
dico_log(L_ERR, 0,
"cannot open stream `%s': %s",
filename ? filename : "<stdin>",
dico_stream_strerror(in, rc));
return 2;
}
if (flags & CRLF_IN) {
s = dico_crlf_stream(in, DICO_STREAM_READ, 0);
if (!s) {
dico_log(L_ERR, errno, "cannot create filter stream");
return 2;
}
if (filename || (buffered & CRLF_IN))
dico_stream_set_buffer(s, dico_buffer_full, in_bufsize);
in = s;
}
out = dico_fd_stream_create(1, DICO_STREAM_WRITE, 1);
if (!out) {
dico_log(L_ERR, errno, "cannot create stdout stream");
return 2;
}
rc = dico_stream_open(out);
if (rc) {
dico_log(L_ERR, 0,
"cannot open stream `%s': %s",
"<stdout>",
dico_stream_strerror(out, rc));
return 2;
}
if (flags & CRLF_OUT) {
s = dico_crlf_stream(out, DICO_STREAM_WRITE, 0);
if (!s) {
dico_log(L_ERR, errno, "cannot create filter stream");
return 2;
}
if (filename || (buffered & CRLF_OUT))
dico_stream_set_buffer(s, dico_buffer_full, out_bufsize);
out = s;
}
while ((rc = dico_stream_read(in, buf, sizeof(buf), &sz)) == 0 && sz) {
rc = dico_stream_write(out, buf, sz);
if (rc) {
dico_log(L_ERR, 0, "write error: %s",
dico_stream_strerror(out, rc));
return 2;
}
}
dico_stream_close(out);
return 0;
}
|