Implement simple word-wrapping stream
* include/mailutils/stream.h (MU_IOCTL_WORDWRAPSTREAM): New ioctl. (mu_wordwrap_stream_create): New proto. * libmailutils/stream/Makefile.am: add wordwrap.c * libmailutils/stream/wordwrap.c: New file. * libmailutils/tests/Makefile.am: Add new testcases. * libmailutils/tests/testsuite.at: Include new testcases. * libmailutils/tests/wordwrap.c: New file. * libmailutils/tests/wordwrap00.at: New file. * libmailutils/tests/wordwrap01.at: New file. * libmailutils/tests/wordwrap02.at: New file. * libmailutils/tests/wordwrap03.at: New file.
Showing
10 changed files
with
660 additions
and
2 deletions
... | @@ -78,7 +78,8 @@ enum mu_buffer_type | ... | @@ -78,7 +78,8 @@ enum mu_buffer_type |
78 | always returns the topmost substream. | 78 | always returns the topmost substream. |
79 | */ | 79 | */ |
80 | #define MU_IOCTL_TLSSTREAM 13 /* TLS stream */ | 80 | #define MU_IOCTL_TLSSTREAM 13 /* TLS stream */ |
81 | 81 | #define MU_IOCTL_WORDWRAPSTREAM 14 /* Word-wrapper stream */ | |
82 | |||
82 | /* Opcodes common for various families */ | 83 | /* Opcodes common for various families */ |
83 | #define MU_IOCTL_OP_GET 0 | 84 | #define MU_IOCTL_OP_GET 0 |
84 | #define MU_IOCTL_OP_SET 1 | 85 | #define MU_IOCTL_OP_SET 1 |
... | @@ -206,6 +207,11 @@ enum mu_buffer_type | ... | @@ -206,6 +207,11 @@ enum mu_buffer_type |
206 | #define MU_TRANSPORT_VALID_TYPE(n) \ | 207 | #define MU_TRANSPORT_VALID_TYPE(n) \ |
207 | ((n) == MU_TRANSPORT_INPUT || (n) == MU_TRANSPORT_OUTPUT) | 208 | ((n) == MU_TRANSPORT_INPUT || (n) == MU_TRANSPORT_OUTPUT) |
208 | 209 | ||
210 | /* Word wrapper streams */ | ||
211 | #define MU_IOCTL_WORDWRAP_GET_MARGIN 0 | ||
212 | #define MU_IOCTL_WORDWRAP_SET_MARGIN 1 | ||
213 | #define MU_IOCTL_WORDWRAP_MOVE_MARGIN 2 | ||
214 | |||
209 | struct mu_nullstream_pattern | 215 | struct mu_nullstream_pattern |
210 | { | 216 | { |
211 | char *pattern; | 217 | char *pattern; |
... | @@ -360,6 +366,9 @@ int mu_rdcache_stream_create (mu_stream_t *pstream, mu_stream_t transport, | ... | @@ -360,6 +366,9 @@ int mu_rdcache_stream_create (mu_stream_t *pstream, mu_stream_t transport, |
360 | int flags); | 366 | int flags); |
361 | 367 | ||
362 | int mu_nullstream_create (mu_stream_t *pref, int flags); | 368 | int mu_nullstream_create (mu_stream_t *pref, int flags); |
369 | |||
370 | int mu_wordwrap_stream_create (mu_stream_t *pstream, mu_stream_t transport, | ||
371 | size_t left_margin, size_t right_margin); | ||
363 | 372 | ||
364 | #ifdef __cplusplus | 373 | #ifdef __cplusplus |
365 | } | 374 | } | ... | ... |
... | @@ -38,6 +38,7 @@ libstream_la_SOURCES = \ | ... | @@ -38,6 +38,7 @@ libstream_la_SOURCES = \ |
38 | syslogstream.c\ | 38 | syslogstream.c\ |
39 | tcp.c\ | 39 | tcp.c\ |
40 | temp_file_stream.c\ | 40 | temp_file_stream.c\ |
41 | wordwrap.c\ | ||
41 | xscript-stream.c | 42 | xscript-stream.c |
42 | 43 | ||
43 | AM_CPPFLAGS = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils | 44 | AM_CPPFLAGS = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils | ... | ... |
libmailutils/stream/wordwrap.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2016 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General Public License | ||
15 | along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #ifdef HAVE_CONFIG_H | ||
18 | # include <config.h> | ||
19 | #endif | ||
20 | #include <mailutils/types.h> | ||
21 | #include <mailutils/stream.h> | ||
22 | #include <mailutils/sys/stream.h> | ||
23 | #include <mailutils/alloc.h> | ||
24 | #include <mailutils/error.h> | ||
25 | #include <mailutils/errno.h> | ||
26 | #include <mailutils/nls.h> | ||
27 | #include <mailutils/cctype.h> | ||
28 | |||
29 | struct mu_wordwrap_stream | ||
30 | { | ||
31 | struct _mu_stream stream; | ||
32 | unsigned left_margin; | ||
33 | unsigned right_margin; | ||
34 | char *buffer; | ||
35 | unsigned offset; | ||
36 | mu_stream_t transport; | ||
37 | }; | ||
38 | |||
39 | static int | ||
40 | is_word (int c) | ||
41 | { | ||
42 | return !mu_isspace (c); | ||
43 | } | ||
44 | |||
45 | static int | ||
46 | full_write (struct mu_wordwrap_stream *str, size_t length) | ||
47 | { | ||
48 | size_t n, rdsize; | ||
49 | for (n = 0; n < length; ) | ||
50 | { | ||
51 | int rc = mu_stream_write (str->transport, | ||
52 | str->buffer + n, length - n, | ||
53 | &rdsize); | ||
54 | if (rc) | ||
55 | return rc; | ||
56 | n += rdsize; | ||
57 | } | ||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | static int | ||
62 | _wordwrap_flush_line (struct mu_wordwrap_stream *str, int lookahead) | ||
63 | { | ||
64 | size_t length, word_start, word_len = 0; | ||
65 | int nl = 0; | ||
66 | char savech; | ||
67 | int rc; | ||
68 | |||
69 | length = word_start = str->offset; | ||
70 | if (str->offset > 0 && lookahead) | ||
71 | { | ||
72 | if (is_word (str->buffer[str->offset - 1]) && is_word (lookahead)) | ||
73 | { | ||
74 | /* Find the nearest word boundary */ | ||
75 | for (length = str->offset; length > str->left_margin; length--) | ||
76 | { | ||
77 | if (!is_word (str->buffer[length - 1])) | ||
78 | break; | ||
79 | } | ||
80 | if (length == str->left_margin) | ||
81 | { | ||
82 | rc = full_write (str, str->offset); | ||
83 | if (rc == 0) | ||
84 | str->offset = 0; | ||
85 | return rc; | ||
86 | } | ||
87 | word_start = length; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | while (length > 0 && mu_isblank (str->buffer[length - 1])) | ||
92 | length--; | ||
93 | |||
94 | if (length == 0 || str->buffer[length - 1] != '\n') | ||
95 | { | ||
96 | savech = str->buffer[length]; | ||
97 | str->buffer[length] = '\n'; | ||
98 | nl = 1; | ||
99 | } | ||
100 | else | ||
101 | nl = 0; | ||
102 | |||
103 | /* Flush the line buffer content */ | ||
104 | rc = full_write (str, length + nl); | ||
105 | if (rc) | ||
106 | /* FIXME: Inconsistent state after error */ | ||
107 | return rc; | ||
108 | |||
109 | if (nl) | ||
110 | str->buffer[length] = savech; | ||
111 | |||
112 | /* Adjust the buffer */ | ||
113 | memset (str->buffer, ' ', str->left_margin); | ||
114 | if (word_start > str->left_margin) | ||
115 | { | ||
116 | word_len = str->offset - word_start; | ||
117 | if (word_len) | ||
118 | memmove (str->buffer + str->left_margin, str->buffer + word_start, | ||
119 | word_len); | ||
120 | } | ||
121 | str->offset = str->left_margin + word_len; | ||
122 | return 0; | ||
123 | } | ||
124 | |||
125 | static int | ||
126 | _wordwrap_write (mu_stream_t stream, const char *iptr, size_t isize, | ||
127 | size_t *nbytes) | ||
128 | { | ||
129 | struct mu_wordwrap_stream *str = (struct mu_wordwrap_stream *) stream; | ||
130 | size_t n; | ||
131 | |||
132 | for (n = 0; n < isize; n++) | ||
133 | { | ||
134 | if (str->offset == str->right_margin | ||
135 | || (str->offset > 0 && str->buffer[str->offset - 1] == '\n')) | ||
136 | _wordwrap_flush_line (str, iptr[n]); | ||
137 | if (str->offset == str->left_margin && mu_isblank (iptr[n])) | ||
138 | continue; | ||
139 | str->buffer[str->offset++] = iptr[n]; | ||
140 | } | ||
141 | if (nbytes) | ||
142 | *nbytes = isize; | ||
143 | return 0; | ||
144 | } | ||
145 | |||
146 | static int | ||
147 | _wordwrap_flush (mu_stream_t stream) | ||
148 | { | ||
149 | struct mu_wordwrap_stream *str = (struct mu_wordwrap_stream *)stream; | ||
150 | if (str->offset) | ||
151 | _wordwrap_flush_line (str, 0); | ||
152 | return mu_stream_flush (str->transport); | ||
153 | } | ||
154 | |||
155 | static void | ||
156 | _wordwrap_done (mu_stream_t stream) | ||
157 | { | ||
158 | struct mu_wordwrap_stream *str = (struct mu_wordwrap_stream *)stream; | ||
159 | mu_stream_destroy (&str->transport); | ||
160 | } | ||
161 | |||
162 | static int | ||
163 | _wordwrap_close (mu_stream_t stream) | ||
164 | { | ||
165 | struct mu_wordwrap_stream *str = (struct mu_wordwrap_stream *)stream; | ||
166 | return mu_stream_close (str->transport); | ||
167 | } | ||
168 | |||
169 | static int | ||
170 | set_margin (mu_stream_t stream, unsigned lmargin, int off) | ||
171 | { | ||
172 | struct mu_wordwrap_stream *str = (struct mu_wordwrap_stream *)stream; | ||
173 | |||
174 | if (off < 0 && -off > str->left_margin) | ||
175 | return EINVAL; | ||
176 | lmargin += off; | ||
177 | |||
178 | if (lmargin >= str->right_margin) | ||
179 | return EINVAL; | ||
180 | if (lmargin < str->offset) | ||
181 | { | ||
182 | str->left_margin = lmargin; | ||
183 | _wordwrap_flush (stream); | ||
184 | } | ||
185 | else if (lmargin > str->offset) | ||
186 | { | ||
187 | memset (str->buffer + str->offset, ' ', | ||
188 | lmargin - str->offset); | ||
189 | str->left_margin = lmargin; | ||
190 | str->offset = lmargin; | ||
191 | } | ||
192 | return 0; | ||
193 | } | ||
194 | |||
195 | static int | ||
196 | _wordwrap_ctl (mu_stream_t stream, int code, int opcode, void *arg) | ||
197 | { | ||
198 | struct mu_wordwrap_stream *str = (struct mu_wordwrap_stream *)stream; | ||
199 | int status; | ||
200 | |||
201 | switch (code) | ||
202 | { | ||
203 | case MU_IOCTL_WORDWRAPSTREAM: | ||
204 | switch (opcode) | ||
205 | { | ||
206 | case MU_IOCTL_WORDWRAP_GET_MARGIN: | ||
207 | /* Get left margin */ | ||
208 | if (!arg) | ||
209 | return MU_ERR_OUT_PTR_NULL; | ||
210 | *(unsigned *)arg = str->left_margin; | ||
211 | break; | ||
212 | |||
213 | case MU_IOCTL_WORDWRAP_SET_MARGIN: | ||
214 | /* Set left margin */ | ||
215 | if (!arg) | ||
216 | return EINVAL; | ||
217 | else | ||
218 | return set_margin (stream, *(unsigned*)arg, 0); | ||
219 | |||
220 | case MU_IOCTL_WORDWRAP_MOVE_MARGIN: | ||
221 | if (!arg) | ||
222 | return EINVAL; | ||
223 | else | ||
224 | return set_margin (stream, str->offset, *(int*)arg); | ||
225 | |||
226 | default: | ||
227 | return EINVAL; | ||
228 | } | ||
229 | break; | ||
230 | |||
231 | case MU_IOCTL_TRANSPORT: | ||
232 | if (!arg) | ||
233 | return EINVAL; | ||
234 | else | ||
235 | { | ||
236 | mu_transport_t *ptrans = arg; | ||
237 | switch (opcode) | ||
238 | { | ||
239 | case MU_IOCTL_OP_GET: | ||
240 | ptrans[0] = (mu_transport_t) str->transport; | ||
241 | ptrans[1] = NULL; | ||
242 | break; | ||
243 | |||
244 | case MU_IOCTL_OP_SET: | ||
245 | ptrans = arg; | ||
246 | if (ptrans[0]) | ||
247 | str->transport = (mu_stream_t) ptrans[0]; | ||
248 | break; | ||
249 | |||
250 | default: | ||
251 | return EINVAL; | ||
252 | } | ||
253 | } | ||
254 | break; | ||
255 | |||
256 | case MU_IOCTL_SUBSTREAM: | ||
257 | if (str->transport && | ||
258 | ((status = mu_stream_ioctl (str->transport, code, opcode, arg)) == 0 || | ||
259 | status != ENOSYS)) | ||
260 | return status; | ||
261 | /* fall through */ | ||
262 | |||
263 | case MU_IOCTL_TOPSTREAM: | ||
264 | if (!arg) | ||
265 | return EINVAL; | ||
266 | else | ||
267 | { | ||
268 | mu_stream_t *pstr = arg; | ||
269 | switch (opcode) | ||
270 | { | ||
271 | case MU_IOCTL_OP_GET: | ||
272 | pstr[0] = str->transport; | ||
273 | mu_stream_ref (pstr[0]); | ||
274 | pstr[1] = NULL; | ||
275 | break; | ||
276 | |||
277 | case MU_IOCTL_OP_SET: | ||
278 | mu_stream_unref (str->transport); | ||
279 | str->transport = pstr[0]; | ||
280 | mu_stream_ref (str->transport); | ||
281 | break; | ||
282 | |||
283 | default: | ||
284 | return EINVAL; | ||
285 | } | ||
286 | } | ||
287 | break; | ||
288 | |||
289 | case MU_IOCTL_FILTER: | ||
290 | return mu_stream_ioctl (str->transport, code, opcode, arg); | ||
291 | |||
292 | default: | ||
293 | return ENOSYS; | ||
294 | } | ||
295 | return 0; | ||
296 | } | ||
297 | |||
298 | int | ||
299 | mu_wordwrap_stream_create (mu_stream_t *pstream, mu_stream_t transport, | ||
300 | size_t left_margin, size_t right_margin) | ||
301 | { | ||
302 | int rc; | ||
303 | mu_stream_t stream; | ||
304 | struct mu_wordwrap_stream *str; | ||
305 | |||
306 | if (right_margin == 0 || left_margin >= right_margin) | ||
307 | return EINVAL; | ||
308 | |||
309 | str = (struct mu_wordwrap_stream *) | ||
310 | _mu_stream_create (sizeof (*str), MU_STREAM_APPEND); | ||
311 | if (!str) | ||
312 | return ENOMEM; | ||
313 | str->stream.close = _wordwrap_close; | ||
314 | str->stream.write = _wordwrap_write; | ||
315 | // str->stream.size = _wordwrap_size; | ||
316 | str->stream.done = _wordwrap_done; | ||
317 | str->stream.flush = _wordwrap_flush; | ||
318 | str->stream.ctl = _wordwrap_ctl; | ||
319 | |||
320 | str->transport = transport; | ||
321 | mu_stream_ref (transport); | ||
322 | str->left_margin = left_margin; | ||
323 | str->right_margin = right_margin; | ||
324 | str->buffer = mu_alloc (str->right_margin+1); | ||
325 | memset (str->buffer, ' ', str->left_margin); | ||
326 | str->offset = str->left_margin; | ||
327 | |||
328 | stream = (mu_stream_t) str; | ||
329 | rc = mu_stream_open (stream); | ||
330 | if (rc) | ||
331 | mu_stream_destroy (&stream); | ||
332 | else | ||
333 | *pstream = stream; | ||
334 | return rc; | ||
335 | } |
... | @@ -68,6 +68,7 @@ noinst_PROGRAMS = \ | ... | @@ -68,6 +68,7 @@ noinst_PROGRAMS = \ |
68 | url-comp\ | 68 | url-comp\ |
69 | url-parse\ | 69 | url-parse\ |
70 | wicket\ | 70 | wicket\ |
71 | wordwrap\ | ||
71 | wsp | 72 | wsp |
72 | 73 | ||
73 | LDADD = ${MU_LIB_MAILUTILS} | 74 | LDADD = ${MU_LIB_MAILUTILS} |
... | @@ -155,7 +156,11 @@ TESTSUITE_AT = \ | ... | @@ -155,7 +156,11 @@ TESTSUITE_AT = \ |
155 | url-comp.at\ | 156 | url-comp.at\ |
156 | xml.at\ | 157 | xml.at\ |
157 | wicket.at\ | 158 | wicket.at\ |
158 | wordsplit.at | 159 | wordsplit.at\ |
160 | wordwrap00.at\ | ||
161 | wordwrap01.at\ | ||
162 | wordwrap02.at\ | ||
163 | wordwrap03.at | ||
159 | 164 | ||
160 | TESTSUITE = $(srcdir)/testsuite | 165 | TESTSUITE = $(srcdir)/testsuite |
161 | M4=m4 | 166 | M4=m4 | ... | ... |
... | @@ -56,6 +56,12 @@ AT_INIT | ... | @@ -56,6 +56,12 @@ AT_INIT |
56 | AT_BANNER([Conversions]) | 56 | AT_BANNER([Conversions]) |
57 | m4_include([strtoc.at]) | 57 | m4_include([strtoc.at]) |
58 | 58 | ||
59 | AT_BANNER([Word wrapper]) | ||
60 | m4_include([wordwrap00.at]) | ||
61 | m4_include([wordwrap01.at]) | ||
62 | m4_include([wordwrap02.at]) | ||
63 | m4_include([wordwrap03.at]) | ||
64 | |||
59 | AT_BANNER([Command line parser]) | 65 | AT_BANNER([Command line parser]) |
60 | m4_define([PARSEOPT_DEFAULT],[ | 66 | m4_define([PARSEOPT_DEFAULT],[ |
61 | unset ARGP_HELP_FMT | 67 | unset ARGP_HELP_FMT | ... | ... |
libmailutils/tests/wordwrap.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2011-2012, 2014-2016 Free Software Foundation, Inc. | ||
3 | |||
4 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Mailutils is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #ifdef HAVE_CONFIG_H | ||
18 | # include <config.h> | ||
19 | #endif | ||
20 | #include <string.h> | ||
21 | #include <stdio.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <mailutils/stream.h> | ||
24 | #include <mailutils/stdstream.h> | ||
25 | #include <mailutils/diag.h> | ||
26 | #include <mailutils/debug.h> | ||
27 | #include <mailutils/errno.h> | ||
28 | |||
29 | static void | ||
30 | usage (FILE *fp) | ||
31 | { | ||
32 | fprintf (fp, | ||
33 | "usage: %s [-r N] [-l [+-]N] FILE [[-l [+-]N] FILE...]\n", | ||
34 | mu_program_name); | ||
35 | fprintf (fp, "\n"); | ||
36 | fprintf (fp, " -l N set left margin\n"); | ||
37 | fprintf (fp, " -l +N move left margin N chars to the right from the current position\n"); | ||
38 | fprintf (fp, " -l -N move left margin N chars to the left from the current position\n"); | ||
39 | fprintf (fp, " -r N set right margin\n"); | ||
40 | fprintf (fp, " -h, -? display this help\n"); | ||
41 | } | ||
42 | |||
43 | int | ||
44 | main (int argc, char **argv) | ||
45 | { | ||
46 | unsigned left = 0, right = 80; | ||
47 | mu_stream_t str; | ||
48 | int i; | ||
49 | |||
50 | mu_set_program_name (argv[0]); | ||
51 | |||
52 | for (i = 1; i < argc; i++) | ||
53 | { | ||
54 | char *arg = argv[i]; | ||
55 | if (strncmp (arg, "-l", 2) == 0) | ||
56 | { | ||
57 | if (arg[2] == 0) | ||
58 | { | ||
59 | ++i; | ||
60 | left = strtoul (argv[i], NULL, 10); | ||
61 | } | ||
62 | else | ||
63 | { | ||
64 | left = strtoul (argv[i] + 2, NULL, 10); | ||
65 | } | ||
66 | } | ||
67 | else if (strncmp (arg, "-r", 2) == 0) | ||
68 | { | ||
69 | if (arg[2] == 0) | ||
70 | { | ||
71 | ++i; | ||
72 | right = strtoul (argv[i], NULL, 10); | ||
73 | } | ||
74 | else | ||
75 | { | ||
76 | right = strtoul (argv[i] + 2, NULL, 10); | ||
77 | } | ||
78 | } | ||
79 | else if (strcmp (arg, "--") == 0) | ||
80 | { | ||
81 | i++; | ||
82 | break; | ||
83 | } | ||
84 | else if (arg[0] == '-') | ||
85 | { | ||
86 | if (arg[1] == 0) | ||
87 | break; | ||
88 | else if ((arg[1] == 'h' || arg[1] == '?') && arg[2] == 0) | ||
89 | { | ||
90 | usage (stdout); | ||
91 | return 0; | ||
92 | } | ||
93 | else | ||
94 | { | ||
95 | fprintf (stderr, "%s: unrecognized argument %s\n", | ||
96 | mu_program_name, arg); | ||
97 | usage (stderr); | ||
98 | return 1; | ||
99 | } | ||
100 | } | ||
101 | else | ||
102 | break; | ||
103 | } | ||
104 | |||
105 | if (i == argc) | ||
106 | { | ||
107 | fprintf (stderr, "%s: no files\n", mu_program_name); | ||
108 | usage (stderr); | ||
109 | return 1; | ||
110 | } | ||
111 | |||
112 | mu_stdstream_setup (MU_STDSTREAM_RESET_NONE); | ||
113 | MU_ASSERT (mu_wordwrap_stream_create (&str, mu_strout, left, right)); | ||
114 | |||
115 | for (; i < argc; i++) | ||
116 | { | ||
117 | char *arg = argv[i]; | ||
118 | if (strncmp (arg, "-l", 2) == 0) | ||
119 | { | ||
120 | if (arg[2] == 0) | ||
121 | { | ||
122 | ++i; | ||
123 | arg = argv[i]; | ||
124 | } | ||
125 | else | ||
126 | arg += 2; | ||
127 | |||
128 | if (arg[0] == '+' || arg[0] == '-') | ||
129 | { | ||
130 | int off = strtol (arg, NULL, 10); | ||
131 | MU_ASSERT (mu_stream_ioctl (str, MU_IOCTL_WORDWRAPSTREAM, | ||
132 | MU_IOCTL_WORDWRAP_MOVE_MARGIN, | ||
133 | &off)); | ||
134 | } | ||
135 | else | ||
136 | { | ||
137 | left = strtoul (arg, NULL, 10); | ||
138 | MU_ASSERT (mu_stream_ioctl (str, MU_IOCTL_WORDWRAPSTREAM, | ||
139 | MU_IOCTL_WORDWRAP_SET_MARGIN, | ||
140 | &left)); | ||
141 | } | ||
142 | } | ||
143 | else if (arg[0] == '-' && arg[1] == 0) | ||
144 | { | ||
145 | MU_ASSERT (mu_stream_copy (str, mu_strin, 0, NULL)); | ||
146 | mu_stream_close (mu_strin); | ||
147 | } | ||
148 | else | ||
149 | { | ||
150 | mu_stream_t in; | ||
151 | MU_ASSERT (mu_file_stream_create (&in, arg, MU_STREAM_READ)); | ||
152 | MU_ASSERT (mu_stream_copy (str, in, 0, NULL)); | ||
153 | mu_stream_destroy (&in); | ||
154 | } | ||
155 | } | ||
156 | mu_stream_destroy (&str); | ||
157 | return 0; | ||
158 | } | ||
159 | |||
160 | |||
161 | |||
162 |
libmailutils/tests/wordwrap00.at
0 → 100644
1 | # This file is part of GNU Mailutils. -*- Autotest -*- | ||
2 | # Copyright (C) 2016 Free Software Foundation, Inc. | ||
3 | # | ||
4 | # GNU Mailutils is free software; you can redistribute it and/or | ||
5 | # modify it under the terms of the GNU General Public License as | ||
6 | # published by the Free Software Foundation; either version 3, or (at | ||
7 | # your option) any later version. | ||
8 | # | ||
9 | # GNU Mailutils is distributed in the hope that it will be useful, but | ||
10 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | # General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU General Public License | ||
15 | # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. | ||
16 | |||
17 | AT_SETUP([Fixed margins]) | ||
18 | AT_KEYWORDS([wordwrap wordwrap00]) | ||
19 | AT_DATA([input],[My Father had a small Estate in Nottinghamshire; I was the Third of five Sons. He sent me to Emanuel-College in Cambridge, at Fourteen Years old, where I resided three Years, and applyed my self close to my Studies: But the Charge of maintaining me (although I had a very scanty Allowance) being too great for a narrow Fortune; I was bound Apprentice to Mr. James Bates, an eminent Surgeon in London, with whom I continued four Years; and my Father now and then sending me small Sums of Money, I laid them out in learning Navigation, and other parts of the Mathematicks, useful to those who intend to travel, as I always believed it would be some time or other my Fortune to do. When I left Mr. Bates, I went down to my Father; where, by the Assistance of him and my Uncle John, and some other Relations, I got Forty Pounds, and a Promise of Thirty Pounds a Year to maintain me at Leyden: There I studied Physick two Years and seven Months, knowing it would be useful in long Voyages. | ||
20 | ]) | ||
21 | AT_CHECK([wordwrap -l 20 -r 70 input], | ||
22 | [0], | ||
23 | [ My Father had a small Estate in Nottinghamshire; I | ||
24 | was the Third of five Sons. He sent me to | ||
25 | Emanuel-College in Cambridge, at Fourteen Years | ||
26 | old, where I resided three Years, and applyed my | ||
27 | self close to my Studies: But the Charge of | ||
28 | maintaining me (although I had a very scanty | ||
29 | Allowance) being too great for a narrow Fortune; I | ||
30 | was bound Apprentice to Mr. James Bates, an | ||
31 | eminent Surgeon in London, with whom I continued | ||
32 | four Years; and my Father now and then sending me | ||
33 | small Sums of Money, I laid them out in learning | ||
34 | Navigation, and other parts of the Mathematicks, | ||
35 | useful to those who intend to travel, as I always | ||
36 | believed it would be some time or other my Fortune | ||
37 | to do. When I left Mr. Bates, I went down to my | ||
38 | Father; where, by the Assistance of him and my | ||
39 | Uncle John, and some other Relations, I got Forty | ||
40 | Pounds, and a Promise of Thirty Pounds a Year to | ||
41 | maintain me at Leyden: There I studied Physick two | ||
42 | Years and seven Months, knowing it would be useful | ||
43 | in long Voyages. | ||
44 | ]) | ||
45 | AT_CLEANUP | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
libmailutils/tests/wordwrap01.at
0 → 100644
1 | # This file is part of GNU Mailutils. -*- Autotest -*- | ||
2 | # Copyright (C) 2016 Free Software Foundation, Inc. | ||
3 | # | ||
4 | # GNU Mailutils is free software; you can redistribute it and/or | ||
5 | # modify it under the terms of the GNU General Public License as | ||
6 | # published by the Free Software Foundation; either version 3, or (at | ||
7 | # your option) any later version. | ||
8 | # | ||
9 | # GNU Mailutils is distributed in the hope that it will be useful, but | ||
10 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | # General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU General Public License | ||
15 | # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. | ||
16 | |||
17 | AT_SETUP([Move margin right]) | ||
18 | AT_KEYWORDS([wordwrap wordwrap01]) | ||
19 | AT_DATA([input0],[My Father had a small Estate in Nottinghamshire; I was the Third of five Sons. | ||
20 | ]) | ||
21 | AT_DATA([input1],[He sent me to Emanuel-College in Cambridge, at Fourteen Years old, where I resided three Years, and applyed my self close to my Studies: | ||
22 | ]) | ||
23 | AT_CHECK([cat input0 | tr -d '\n' | wordwrap -l 20 -r 70 - -l +8 input1], | ||
24 | [0], | ||
25 | [ My Father had a small Estate in Nottinghamshire; I | ||
26 | was the Third of five Sons. He sent me to | ||
27 | Emanuel-College | ||
28 | in Cambridge, | ||
29 | at Fourteen | ||
30 | Years old, | ||
31 | where I resided | ||
32 | three Years, | ||
33 | and applyed my | ||
34 | self close to | ||
35 | my Studies: | ||
36 | ]) | ||
37 | AT_CLEANUP | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
libmailutils/tests/wordwrap02.at
0 → 100644
1 | # This file is part of GNU Mailutils. -*- Autotest -*- | ||
2 | # Copyright (C) 2016 Free Software Foundation, Inc. | ||
3 | # | ||
4 | # GNU Mailutils is free software; you can redistribute it and/or | ||
5 | # modify it under the terms of the GNU General Public License as | ||
6 | # published by the Free Software Foundation; either version 3, or (at | ||
7 | # your option) any later version. | ||
8 | # | ||
9 | # GNU Mailutils is distributed in the hope that it will be useful, but | ||
10 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | # General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU General Public License | ||
15 | # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. | ||
16 | |||
17 | AT_SETUP([Move margin left]) | ||
18 | AT_KEYWORDS([wordwrap wordwrap02]) | ||
19 | AT_DATA([input0],[My Father had a small Estate in Nottinghamshire; I was the Third of five Sons. | ||
20 | ]) | ||
21 | AT_DATA([input1],[He sent me to Emanuel-College in Cambridge, at Fourteen Years old, where I resided three Years, and applyed my self close to my Studies: | ||
22 | ]) | ||
23 | AT_CHECK([cat input0 | tr -d '\n' | wordwrap -l 20 -r 70 - -l -8 input1], | ||
24 | [0], | ||
25 | [ My Father had a small Estate in Nottinghamshire; I | ||
26 | was the Third of five Sons. | ||
27 | He sent me to Emanuel-College | ||
28 | in Cambridge, at Fourteen Years | ||
29 | old, where I resided three | ||
30 | Years, and applyed my self | ||
31 | close to my Studies: | ||
32 | ]) | ||
33 | AT_CLEANUP | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
libmailutils/tests/wordwrap03.at
0 → 100644
1 | # This file is part of GNU Mailutils. -*- Autotest -*- | ||
2 | # Copyright (C) 2016 Free Software Foundation, Inc. | ||
3 | # | ||
4 | # GNU Mailutils is free software; you can redistribute it and/or | ||
5 | # modify it under the terms of the GNU General Public License as | ||
6 | # published by the Free Software Foundation; either version 3, or (at | ||
7 | # your option) any later version. | ||
8 | # | ||
9 | # GNU Mailutils is distributed in the hope that it will be useful, but | ||
10 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | # General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU General Public License | ||
15 | # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. | ||
16 | AT_SETUP([Corner cases]) | ||
17 | AT_KEYWORDS([wordwrap wordwrap03]) | ||
18 | AT_CHECK([echo abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz|\ | ||
19 | wordwrap -r 10 - | ||
20 | ], | ||
21 | [0], | ||
22 | [abcdefghijklmnopqrstuvwxyz | ||
23 | abcdefghijklmnopqrstuvwxyz | ||
24 | ]) | ||
25 | AT_CLEANUP | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment