1 /* Dummy replacement for part of the public API of the libtextstyle library.
2 Copyright (C) 2006-2007, 2019-2020 Free Software Foundation, Inc.
4 This program 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 of the License, or
7 (at your option) any later version.
9 This program 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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2019. */
19 /* This file is used as replacement when libtextstyle with its include file
20 <textstyle.h> is not found.
21 It supports the essential API and implements it in a way that does not
22 provide text styling. That is, it produces plain text output via <stdio.h>
24 Thus, it allows a package to be build with or without a dependency to
25 libtextstyle, with very few occurrences of '#if HAVE_LIBTEXTSTYLE'.
28 It assumes that freopen() is not being called on stdout and stderr. */
45 /* ----------------------------- From ostream.h ----------------------------- */
47 /* Describes the scope of a flush operation. */
50 /* Flushes buffers in this ostream_t.
51 Use this value if you want to write to the underlying ostream_t. */
52 FLUSH_THIS_STREAM
= 0,
53 /* Flushes all buffers in the current process.
54 Use this value if you want to write to the same target through a
55 different file descriptor or a FILE stream. */
56 FLUSH_THIS_PROCESS
= 1,
57 /* Flushes buffers in the current process and attempts to flush the buffers
59 Use this value so that some other process (or the kernel itself)
60 may write to the same target. */
62 } ostream_flush_scope_t
;
65 /* An output stream is an object to which one can feed a sequence of bytes. */
67 typedef FILE * ostream_t
;
70 ostream_write_mem (ostream_t stream
, const void *data
, size_t len
)
73 fwrite (data
, 1, len
, stream
);
77 ostream_flush (ostream_t stream
, ostream_flush_scope_t scope
)
80 if (scope
== FLUSH_ALL
)
82 int fd
= fileno (stream
);
85 /* For streams connected to a disk file: */
88 /* For streams connected to a terminal: */
93 retval
= tcdrain (fd
);
94 while (retval
< 0 && errno
== EINTR
);
102 ostream_free (ostream_t stream
)
104 if (stream
== stdin
|| stream
== stderr
)
111 ostream_write_str (ostream_t stream
, const char *string
)
113 ostream_write_mem (stream
, string
, strlen (string
));
116 static inline ptrdiff_t ostream_printf (ostream_t stream
,
117 const char *format
, ...)
118 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || defined __clang__
119 __attribute__ ((__format__ (__printf__
, 2, 3)))
122 static inline ptrdiff_t
123 ostream_printf (ostream_t stream
, const char *format
, ...)
129 va_start (args
, format
);
130 ret
= vasprintf (&temp_string
, format
, args
);
135 ostream_write_str (stream
, temp_string
);
141 static inline ptrdiff_t ostream_vprintf (ostream_t stream
,
142 const char *format
, va_list args
)
143 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || defined __clang__
144 __attribute__ ((__format__ (__printf__
, 2, 0)))
147 static inline ptrdiff_t
148 ostream_vprintf (ostream_t stream
, const char *format
, va_list args
)
151 ptrdiff_t ret
= vasprintf (&temp_string
, format
, args
);
155 ostream_write_str (stream
, temp_string
);
161 /* ------------------------- From styled-ostream.h ------------------------- */
163 typedef ostream_t styled_ostream_t
;
165 #define styled_ostream_write_mem ostream_write_mem
166 #define styled_ostream_flush ostream_flush
167 #define styled_ostream_free ostream_free
170 styled_ostream_begin_use_class (styled_ostream_t stream _GL_UNUSED
,
171 const char *classname _GL_UNUSED
)
176 styled_ostream_end_use_class (styled_ostream_t stream _GL_UNUSED
,
177 const char *classname _GL_UNUSED
)
181 static inline const char *
182 styled_ostream_get_hyperlink_ref (styled_ostream_t stream _GL_UNUSED
)
187 static inline const char *
188 styled_ostream_get_hyperlink_id (styled_ostream_t stream _GL_UNUSED
)
194 styled_ostream_set_hyperlink (styled_ostream_t stream _GL_UNUSED
,
195 const char *ref _GL_UNUSED
,
196 const char *id _GL_UNUSED
)
201 styled_ostream_flush_to_current_style (styled_ostream_t stream _GL_UNUSED
)
205 /* -------------------------- From file-ostream.h -------------------------- */
207 typedef ostream_t file_ostream_t
;
209 #define file_ostream_write_mem ostream_write_mem
210 #define file_ostream_flush ostream_flush
211 #define file_ostream_free ostream_free
213 static inline file_ostream_t
214 file_ostream_create (FILE *fp
)
219 /* --------------------------- From fd-ostream.h --------------------------- */
221 typedef ostream_t fd_ostream_t
;
223 #define fd_ostream_write_mem ostream_write_mem
224 #define fd_ostream_flush ostream_flush
225 #define fd_ostream_free ostream_free
227 static inline fd_ostream_t
228 fd_ostream_create (int fd
, const char *filename _GL_UNUSED
,
229 bool buffered _GL_UNUSED
)
236 return fdopen (fd
, "w");
239 /* -------------------------- From term-ostream.h -------------------------- */
241 typedef int term_color_t
;
244 COLOR_DEFAULT
= -1 /* unknown */
251 WEIGHT_DEFAULT
= WEIGHT_NORMAL
257 POSTURE_ITALIC
, /* same as oblique */
258 POSTURE_DEFAULT
= POSTURE_NORMAL
265 UNDERLINE_DEFAULT
= UNDERLINE_OFF
268 typedef ostream_t term_ostream_t
;
270 #define term_ostream_write_mem ostream_write_mem
271 #define term_ostream_flush ostream_flush
272 #define term_ostream_free ostream_free
274 static inline term_color_t
275 term_ostream_get_color (term_ostream_t stream _GL_UNUSED
)
277 return COLOR_DEFAULT
;
281 term_ostream_set_color (term_ostream_t stream _GL_UNUSED
,
282 term_color_t color _GL_UNUSED
)
286 static inline term_color_t
287 term_ostream_get_bgcolor (term_ostream_t stream _GL_UNUSED
)
289 return COLOR_DEFAULT
;
293 term_ostream_set_bgcolor (term_ostream_t stream _GL_UNUSED
,
294 term_color_t color _GL_UNUSED
)
298 static inline term_weight_t
299 term_ostream_get_weight (term_ostream_t stream _GL_UNUSED
)
301 return WEIGHT_DEFAULT
;
305 term_ostream_set_weight (term_ostream_t stream _GL_UNUSED
,
306 term_weight_t weight _GL_UNUSED
)
310 static inline term_posture_t
311 term_ostream_get_posture (term_ostream_t stream _GL_UNUSED
)
313 return POSTURE_DEFAULT
;
317 term_ostream_set_posture (term_ostream_t stream _GL_UNUSED
,
318 term_posture_t posture _GL_UNUSED
)
322 static inline term_underline_t
323 term_ostream_get_underline (term_ostream_t stream _GL_UNUSED
)
325 return UNDERLINE_DEFAULT
;
329 term_ostream_set_underline (term_ostream_t stream _GL_UNUSED
,
330 term_underline_t underline _GL_UNUSED
)
334 static inline const char *
335 term_ostream_get_hyperlink_ref (term_ostream_t stream _GL_UNUSED
)
340 static inline const char *
341 term_ostream_get_hyperlink_id (term_ostream_t stream _GL_UNUSED
)
347 term_ostream_set_hyperlink (term_ostream_t stream _GL_UNUSED
,
348 const char *ref _GL_UNUSED
,
349 const char *id _GL_UNUSED
)
354 term_ostream_flush_to_current_style (term_ostream_t stream
)
361 TTYCTL_AUTO
= 0, /* Automatic best-possible choice. */
362 TTYCTL_NONE
, /* No control.
363 Result: Garbled output can occur, and the terminal can
364 be left in any state when the program is interrupted. */
365 TTYCTL_PARTIAL
, /* Signal handling.
366 Result: Garbled output can occur, but the terminal will
367 be left in the default state when the program is
369 TTYCTL_FULL
/* Signal handling and disabling echo and flush-upon-signal.
370 Result: No garbled output, and the the terminal will
371 be left in the default state when the program is
375 static inline term_ostream_t
376 term_ostream_create (int fd
, const char *filename
,
377 ttyctl_t tty_control _GL_UNUSED
)
379 return fd_ostream_create (fd
, filename
, true);
382 /* ----------------------- From term-styled-ostream.h ----------------------- */
384 typedef styled_ostream_t term_styled_ostream_t
;
386 #define term_styled_ostream_write_mem ostream_write_mem
387 #define term_styled_ostream_flush ostream_flush
388 #define term_styled_ostream_free ostream_free
389 #define term_styled_ostream_begin_use_class styled_ostream_begin_use_class
390 #define term_styled_ostream_end_use_class styled_ostream_end_use_class
391 #define term_styled_ostream_get_hyperlink_ref styled_ostream_get_hyperlink_ref
392 #define term_styled_ostream_get_hyperlink_id styled_ostream_get_hyperlink_id
393 #define term_styled_ostream_set_hyperlink styled_ostream_set_hyperlink
394 #define term_styled_ostream_flush_to_current_style styled_ostream_flush_to_current_style
396 static inline term_styled_ostream_t
397 term_styled_ostream_create (int fd
, const char *filename
,
398 ttyctl_t tty_control _GL_UNUSED
,
399 const char *css_filename _GL_UNUSED
)
401 return fd_ostream_create (fd
, filename
, true);
404 /* ----------------------- From html-styled-ostream.h ----------------------- */
406 typedef styled_ostream_t html_styled_ostream_t
;
408 static inline html_styled_ostream_t
409 html_styled_ostream_create (ostream_t destination _GL_UNUSED
,
410 const char *css_filename _GL_UNUSED
)
416 /* ------------------------------ From color.h ------------------------------ */
418 #define color_test_mode false
420 enum color_option
{ color_no
, color_tty
, color_yes
, color_html
};
421 #define color_mode color_no
423 #define style_file_name NULL
426 handle_color_option (const char *option _GL_UNUSED
)
432 handle_style_option (const char *option _GL_UNUSED
)
437 print_color_test (void)
443 style_file_prepare (const char *style_file_envvar _GL_UNUSED
,
444 const char *stylesdir_envvar _GL_UNUSED
,
445 const char *stylesdir_after_install _GL_UNUSED
,
446 const char *default_style_file _GL_UNUSED
)
450 /* ------------------------------ From misc.h ------------------------------ */
452 static inline styled_ostream_t
453 styled_ostream_create (int fd
, const char *filename
,
454 ttyctl_t tty_control _GL_UNUSED
,
455 const char *css_filename _GL_UNUSED
)
457 return fd_ostream_create (fd
, filename
, true);
461 libtextstyle_set_failure_exit_code (int exit_code _GL_UNUSED
)
465 #endif /* _TEXTSTYLE_H */