2 std_io.c - MaLa standard IO module parsers
4 Copyright (C) 2005, Christian Thaeter <chth@gmx.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, contact me.
27 mala_actioninit std_io
[] =
30 MALA_PARSER("--NL", mala_newline_parser
, NULL
, NULL
, NULL
),
31 MALA_PARSER_BRIEF("--NL", "prints a newline"),
32 // MALA_PARSER_HELP("--NL", ("TODO")),
33 //MALA_PARSER_RESULT_USAGE("--NL", ("")),
36 //TODO MALA_MACRO_PARSER("--PRINTL",("--PRINT", "--LITERAL", "%1", "--NL")),
37 MALA_PARSER_BRIEF("--PRINTL", "prints a textline to stdout"),
38 // MALA_PARSER_HELP("--PRINTL", ("TODO")),
39 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
40 // MALA_PARSER_RESULT_USAGE("--", ("")),
41 //MALA_PARSER_SIGNATURE_TYPE("--PRINTL", ("WORD-OR-BLOCK")),
42 //MALA_PARSER_RESULT_TYPE("--PRINTL", ("VOID")),
44 MALA_PARSER("--PRINT", mala_print_parser
, NULL
, NULL
, NULL
),
45 MALA_PARSER_BRIEF("--PRINT", "prints to stdout"),
46 // MALA_PARSER_HELP("--PRINT", ("TODO")),
47 // MALA_PARSER_SIGNATURE_USAGE("--", ("")),
48 // MALA_PARSER_RESULT_USAGE("--", ("")),
49 //MALA_PARSER_SIGNATURE_TYPE("--PRINT", ("WORD-OR-BLOCK")),
50 //MALA_PARSER_RESULT_TYPE("--PRINT", ("VOID")),
52 MALA_PARSER("--PRINTWRAPED",mala_printwraped_parser
, NULL
, NULL
, NULL
),
53 MALA_PARSER_BRIEF("--PRINTWRAPED", "prints formatted text"),
54 //TODO MALA_PARSER_HELP("--PRINTWRAPED", ("Wraps some text to fit nicely on the screen. Tabulators become aligned. The last tabulator delimited field of a line will be printed word-wraped and indented.")),
55 //TODO MALA_PARSER_SIGNATURE_USAGE("--PRINTWRAPED",
56 // ("Columns available on the screen",
57 // "Lines available on the screen",
58 // "Indent of the block in characters",
59 // "Tabwidth to be used",
60 // "Text to be printed")),
61 // //MALA_PARSER_RESULT_USAGE("--PRINTWRAPED", ("TODO")),
62 //TODO MALA_PARSER_SIGNATURE_TYPE("--PRINTWRAPED",
72 struct mala_printwraped_context
;
75 mala_predicate_greaterthan_int (int * a
, int * b
);
78 mala_estimate_tabwidth (struct mala_printwraped_context
* pwc
);
81 mala_insert_space (struct mala_printwraped_context
* pwc
, int stop
);
84 mala_ltab (struct mala_printwraped_context
* pwc
);
87 mala_rtab (struct mala_printwraped_context
* pwc
);
90 mala_wrap_words (struct mala_printwraped_context
* pwc
);
93 mala_print_wraped (struct mala_printwraped_context
* pwc
);
96 mala_put_word (struct mala_printwraped_context
* pwc
);
99 struct mala_printwraped_context
110 const char * out_itr
;
111 const char * parse_itr
;
115 int wrap_indent_locked
;
116 const char * lastspace
;
120 /* TODO to predicates */
122 mala_predicate_greaterthan_int (int * a
, int * b
)
128 mala_estimate_tabwidth (struct mala_printwraped_context
* pwc
)
134 for (current_tab
= 19; current_tab
; --current_tab
)
135 pwc
->tabstops
[current_tab
] = 0;
137 pwc
->tabstops
[0] = pwc
->indent
;
141 for (pwc
->parse_itr
= pwc
->text
, pwc
->pos
= pwc
->indent
;
145 switch (*pwc
->parse_itr
)
148 if (*(pwc
->parse_itr
+1) != ' ')
155 if (pwc
->tabstops
[ltab_cnt
] < pwc
->pos
)
157 pwc
->tabstops
[ltab_cnt
] = pwc
->indent
+
158 (pwc
->pos
- 1 + pwc
->tabwidth
) -
159 ((pwc
->pos
- 1 + pwc
->tabwidth
) % pwc
->tabwidth
);
162 pwc
->pos
= pwc
->tabstops
[ltab_cnt
];
164 if (pwc
->pos
+ pwc
->tabwidth
>= pwc
->columns
&& pwc
->tabwidth
> 2)
167 printf("tab decreased to %d\n",pwc
->tabwidth
);
176 pwc
->pos
= pwc
->indent
;
187 mala_insert_space (struct mala_printwraped_context
* pwc
, int stop
)
189 while (pwc
->pos
< stop
)
197 mala_ltab (struct mala_printwraped_context
* pwc
)
199 while(*pwc
->lastspace
== ' ')
203 if (pwc
->ltab_itr
< 19)
206 pwc
->wrap_indent
= pwc
->tabstops
[pwc
->ltab_itr
] -
207 (pwc
->parse_itr
- pwc
->lastspace
) - 1;
209 pwc
->wrap_indent_locked
= 0;
215 mala_rtab (struct mala_printwraped_context
* pwc
)
217 const char * tmp_itr
;
219 int old_indent
= pwc
->wrap_indent
;
222 while (isblank (*pwc
->parse_itr
))
225 pwc
->lastspace
= pwc
->parse_itr
;
227 tmp_itr
= pwc
->parse_itr
;
228 while (*tmp_itr
!= '\0' && *tmp_itr
!= '\n')
231 int length
= tmp_itr
- pwc
->parse_itr
;
233 if (pwc
->pos
+ 2 + length
< pwc
->columns
)
235 pwc
->wrap_indent
= pwc
->columns
- length
;
239 pwc
->wrap_indent
= old_indent
+ pwc
->tabwidth
;
243 pwc
->wrap_indent_locked
= 0;
247 mala_put_word (struct mala_printwraped_context
* pwc
)
256 length
= stop
- pwc
->lastspace
;
257 left
= pwc
->columns
- pwc
->pos
;
259 if (pwc
->pos
+ length
> pwc
->columns
)
261 while(*pwc
->lastspace
== ' ')
264 if (length
> pwc
->columns
- pwc
->wrap_indent
)
266 stop
= pwc
->lastspace
+ pwc
->columns
- pwc
->wrap_indent
- 1;
269 if (isalpha (*(pwc
->lastspace
- 1)))
278 if (pwc
->pos
< pwc
->wrap_indent
)
279 mala_insert_space (pwc
, pwc
->wrap_indent
);
281 while (pwc
->lastspace
< stop
)
283 if (isalpha (*pwc
->lastspace
))
284 pwc
->wrap_indent_locked
= 1;
285 else if (!pwc
->wrap_indent_locked
)
288 putchar (*pwc
->lastspace
);
294 while (stop
< pwc
->out_itr
);
298 mala_wrap_words (struct mala_printwraped_context
* pwc
)
300 /* prints all text until the next \t \n or \0 linewraped */
301 for (;pwc
->out_itr
< pwc
->parse_itr
; ++pwc
->out_itr
)
303 if (*pwc
->out_itr
== ' ')
312 mala_print_wraped (struct mala_printwraped_context
* pwc
)
315 - a line consists of many tab delimited fields with one optional right aligned field at the
316 end introduced with tab-space "\t "
317 - the last normal tab field gets word wraped and aligned the first alphabetic character of this
318 field. Too long words become (simple) hyphenated.
319 - tabs are aligned every 'tabwidth' characters, when the terminal is too small, tabwidth is
320 gradually decremented until the data fits more nicely until tabwidth = 2.
323 "foo \tbar\n\n\tfoo\t- foo explanation which gets word wraped because this text is long\n\tbaz\t- baz explanation\t [note]"
325 becomes something like:
329 foo - foo explanation which gets word wraped
330 because this text is long
331 baz - baz explanation [note]
334 for (pwc
->lastspace
= pwc
->out_itr
= pwc
->parse_itr
= pwc
->text
,
339 switch (*pwc
->parse_itr
)
342 mala_wrap_words (pwc
);
343 if (*(pwc
->parse_itr
+1) != ' ')
349 mala_wrap_words (pwc
);
352 pwc
->wrap_indent
= pwc
->indent
;
353 pwc
->wrap_indent_locked
= 0;
360 mala_wrap_words (pwc
);
366 mala_printwraped_parser (MalaEngine eng
,
367 MalaStringListNode_ref pptr
,
370 struct mala_printwraped_context pwc
;
375 if (mala_engine_arg_eval_fmt (eng
, pptr
, 1, "%d", &pwc
.columns
,
376 (MalaPredicate
) mala_predicate_greaterthan_int
, &zero
380 if (mala_engine_arg_eval_fmt (eng
, pptr
, 2, "%d", &pwc
.lines
,
381 (MalaPredicate
) mala_predicate_greaterthan_int
, &zero
385 if (mala_engine_arg_eval_fmt (eng
, pptr
, 3, "%d", &pwc
.indent
,
386 (MalaPredicate
) mala_predicate_greaterthan_int
, &zero
390 if (mala_engine_arg_eval_fmt (eng
, pptr
, 4, "%d", &pwc
.tabwidth
,
391 (MalaPredicate
) mala_predicate_greaterthan_int
, &zero
395 pwc
.text
= mala_string_cstr (mala_engine_arg_eval_string (eng
, pptr
, 5, NULL
, NULL
));
399 MALA_SIDEEFFECT_BEGIN
402 pwc
.wrap_indent_locked
= 0;
403 pwc
.wrap_indent
= pwc
.indent
;
405 mala_estimate_tabwidth (&pwc
);
406 mala_print_wraped (&pwc
);
410 mala_engine_command_done (eng
, pptr
, 5);
415 mala_newline_parser (MalaEngine eng
,
416 MalaStringListNode_ref pptr
,
421 MALA_SIDEEFFECT_BEGIN
425 mala_engine_command_done (eng
, pptr
, 0);
430 mala_print_parser (MalaEngine eng
,
431 MalaStringListNode_ref pptr
,
435 MalaStringListNode next
;
437 next
= mala_engine_arg_eval (eng
, pptr
, 1, MALA_LITERAL
);
442 // TODO print blocks ?
444 MALA_SIDEEFFECT_BEGIN
445 printf("%s",mala_string_cstr (mala_stringlistnode_string (next
)));
448 mala_engine_command_done (eng
, pptr
, 1);
453 mala_module_std_io_init (MalaEngine self
)
455 return mala_engine_actions_register (self
, std_io
);
462 // c-file-style: "gnu"
464 // arch-tag: 3d2a15ac-145d-42d7-98a5-14dc9b7d2003