1 /* Dummy replacement for part of the public API of the libtextstyle library.
2 Copyright (C) 2006-2007, 2019-2024 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. */
33 /* This file uses _GL_ATTRIBUTE_MAYBE_UNUSED, HAVE_TCDRAIN. */
34 #if !_GL_CONFIG_H_INCLUDED
35 #error "Please include config.h first."
49 /* An __attribute__ __format__ specifier for a function that takes a format
50 string and arguments, where the format string directives are the ones
51 standardized by ISO C99 and POSIX.
52 _GL_ATTRIBUTE_SPEC_PRINTF_STANDARD */
53 /* __gnu_printf__ is supported in GCC >= 4.4. */
54 #ifndef _GL_ATTRIBUTE_SPEC_PRINTF_STANDARD
55 # if (__GNUC__ + (__GNUC_MINOR__ >= 4) > 4) && !defined __clang__
56 # define _GL_ATTRIBUTE_SPEC_PRINTF_STANDARD __gnu_printf__
58 # define _GL_ATTRIBUTE_SPEC_PRINTF_STANDARD __printf__
62 /* _GL_ATTRIBUTE_MAYBE_UNUSED declares that it is not a programming mistake if
63 the entity is not used. The compiler should not warn if the entity is not
65 #ifndef _GL_ATTRIBUTE_MAYBE_UNUSED
66 # if 0 /* no GCC or clang version supports this yet */
67 # define _GL_ATTRIBUTE_MAYBE_UNUSED [[__maybe_unused__]]
68 # elif defined __GNUC__ || defined __clang__
69 # define _GL_ATTRIBUTE_MAYBE_UNUSED __attribute__ ((__unused__))
71 # define _GL_ATTRIBUTE_MAYBE_UNUSED
75 /* ----------------------------- From ostream.h ----------------------------- */
77 /* Describes the scope of a flush operation. */
80 /* Flushes buffers in this ostream_t.
81 Use this value if you want to write to the underlying ostream_t. */
82 FLUSH_THIS_STREAM
= 0,
83 /* Flushes all buffers in the current process.
84 Use this value if you want to write to the same target through a
85 different file descriptor or a FILE stream. */
86 FLUSH_THIS_PROCESS
= 1,
87 /* Flushes buffers in the current process and attempts to flush the buffers
89 Use this value so that some other process (or the kernel itself)
90 may write to the same target. */
92 } ostream_flush_scope_t
;
95 /* An output stream is an object to which one can feed a sequence of bytes. */
97 typedef FILE * ostream_t
;
100 ostream_write_mem (ostream_t stream
, const void *data
, size_t len
)
103 fwrite (data
, 1, len
, stream
);
107 ostream_flush (ostream_t stream
, ostream_flush_scope_t scope
)
110 if (scope
== FLUSH_ALL
)
112 int fd
= fileno (stream
);
115 /* For streams connected to a disk file: */
118 /* For streams connected to a terminal: */
123 retval
= tcdrain (fd
);
124 while (retval
< 0 && errno
== EINTR
);
132 ostream_free (ostream_t stream
)
134 if (stream
== stdin
|| stream
== stderr
)
141 ostream_write_str (ostream_t stream
, const char *string
)
143 ostream_write_mem (stream
, string
, strlen (string
));
146 static inline ptrdiff_t ostream_printf (ostream_t stream
,
147 const char *format
, ...)
148 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || defined __clang__
149 __attribute__ ((__format__ (_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD
, 2, 3)))
152 static inline ptrdiff_t
153 ostream_printf (ostream_t stream
, const char *format
, ...)
159 va_start (args
, format
);
160 ret
= vasprintf (&temp_string
, format
, args
);
165 ostream_write_str (stream
, temp_string
);
171 static inline ptrdiff_t ostream_vprintf (ostream_t stream
,
172 const char *format
, va_list args
)
173 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || defined __clang__
174 __attribute__ ((__format__ (_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD
, 2, 0)))
177 static inline ptrdiff_t
178 ostream_vprintf (ostream_t stream
, const char *format
, va_list args
)
181 ptrdiff_t ret
= vasprintf (&temp_string
, format
, args
);
185 ostream_write_str (stream
, temp_string
);
191 /* ------------------------- From styled-ostream.h ------------------------- */
193 typedef ostream_t styled_ostream_t
;
195 #define styled_ostream_write_mem ostream_write_mem
196 #define styled_ostream_flush ostream_flush
197 #define styled_ostream_free ostream_free
200 styled_ostream_begin_use_class (_GL_ATTRIBUTE_MAYBE_UNUSED styled_ostream_t stream
,
201 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *classname
)
206 styled_ostream_end_use_class (_GL_ATTRIBUTE_MAYBE_UNUSED styled_ostream_t stream
,
207 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *classname
)
211 static inline const char *
212 styled_ostream_get_hyperlink_ref (_GL_ATTRIBUTE_MAYBE_UNUSED styled_ostream_t stream
)
217 static inline const char *
218 styled_ostream_get_hyperlink_id (_GL_ATTRIBUTE_MAYBE_UNUSED styled_ostream_t stream
)
224 styled_ostream_set_hyperlink (_GL_ATTRIBUTE_MAYBE_UNUSED styled_ostream_t stream
,
225 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *ref
,
226 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *id
)
231 styled_ostream_flush_to_current_style (_GL_ATTRIBUTE_MAYBE_UNUSED styled_ostream_t stream
)
236 is_instance_of_styled_ostream (_GL_ATTRIBUTE_MAYBE_UNUSED ostream_t stream
)
241 /* -------------------------- From file-ostream.h -------------------------- */
243 typedef ostream_t file_ostream_t
;
245 #define file_ostream_write_mem ostream_write_mem
246 #define file_ostream_flush ostream_flush
247 #define file_ostream_free ostream_free
250 file_ostream_get_stdio_stream (file_ostream_t stream
)
255 static inline file_ostream_t
256 file_ostream_create (FILE *fp
)
262 is_instance_of_file_ostream (_GL_ATTRIBUTE_MAYBE_UNUSED ostream_t stream
)
267 /* --------------------------- From fd-ostream.h --------------------------- */
269 typedef ostream_t fd_ostream_t
;
271 #define fd_ostream_write_mem ostream_write_mem
272 #define fd_ostream_flush ostream_flush
273 #define fd_ostream_free ostream_free
276 fd_ostream_get_descriptor (fd_ostream_t stream
)
278 return fileno (stream
);
281 static inline const char *
282 fd_ostream_get_filename (_GL_ATTRIBUTE_MAYBE_UNUSED fd_ostream_t stream
)
288 fd_ostream_is_buffered (_GL_ATTRIBUTE_MAYBE_UNUSED fd_ostream_t stream
)
293 static inline fd_ostream_t
294 fd_ostream_create (int fd
, _GL_ATTRIBUTE_MAYBE_UNUSED
const char *filename
,
295 _GL_ATTRIBUTE_MAYBE_UNUSED
bool buffered
)
302 return fdopen (fd
, "w");
306 is_instance_of_fd_ostream (ostream_t stream
)
308 return fileno (stream
) >= 0;
311 /* -------------------------- From term-ostream.h -------------------------- */
313 typedef int term_color_t
;
316 COLOR_DEFAULT
= -1 /* unknown */
323 WEIGHT_DEFAULT
= WEIGHT_NORMAL
329 POSTURE_ITALIC
, /* same as oblique */
330 POSTURE_DEFAULT
= POSTURE_NORMAL
337 UNDERLINE_DEFAULT
= UNDERLINE_OFF
342 TTYCTL_AUTO
= 0, /* Automatic best-possible choice. */
343 TTYCTL_NONE
, /* No control.
344 Result: Garbled output can occur, and the terminal can
345 be left in any state when the program is interrupted. */
346 TTYCTL_PARTIAL
, /* Signal handling.
347 Result: Garbled output can occur, but the terminal will
348 be left in the default state when the program is
350 TTYCTL_FULL
/* Signal handling and disabling echo and flush-upon-signal.
351 Result: No garbled output, and the terminal will
352 be left in the default state when the program is
356 typedef ostream_t term_ostream_t
;
358 #define term_ostream_write_mem ostream_write_mem
359 #define term_ostream_flush ostream_flush
360 #define term_ostream_free ostream_free
362 static inline term_color_t
363 term_ostream_rgb_to_color (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
,
364 _GL_ATTRIBUTE_MAYBE_UNUSED
int red
,
365 _GL_ATTRIBUTE_MAYBE_UNUSED
int green
,
366 _GL_ATTRIBUTE_MAYBE_UNUSED
int blue
)
368 return COLOR_DEFAULT
;
371 static inline term_color_t
372 term_ostream_get_color (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
)
374 return COLOR_DEFAULT
;
378 term_ostream_set_color (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
,
379 _GL_ATTRIBUTE_MAYBE_UNUSED term_color_t color
)
383 static inline term_color_t
384 term_ostream_get_bgcolor (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
)
386 return COLOR_DEFAULT
;
390 term_ostream_set_bgcolor (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
,
391 _GL_ATTRIBUTE_MAYBE_UNUSED term_color_t color
)
395 static inline term_weight_t
396 term_ostream_get_weight (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
)
398 return WEIGHT_DEFAULT
;
402 term_ostream_set_weight (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
,
403 _GL_ATTRIBUTE_MAYBE_UNUSED term_weight_t weight
)
407 static inline term_posture_t
408 term_ostream_get_posture (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
)
410 return POSTURE_DEFAULT
;
414 term_ostream_set_posture (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
,
415 _GL_ATTRIBUTE_MAYBE_UNUSED term_posture_t posture
)
419 static inline term_underline_t
420 term_ostream_get_underline (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
)
422 return UNDERLINE_DEFAULT
;
426 term_ostream_set_underline (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
,
427 _GL_ATTRIBUTE_MAYBE_UNUSED term_underline_t underline
)
431 static inline const char *
432 term_ostream_get_hyperlink_ref (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
)
437 static inline const char *
438 term_ostream_get_hyperlink_id (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
)
444 term_ostream_set_hyperlink (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
,
445 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *ref
,
446 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *id
)
451 term_ostream_flush_to_current_style (term_ostream_t stream
)
456 #define term_ostream_get_descriptor fd_ostream_get_descriptor
457 #define term_ostream_get_filename fd_ostream_get_filename
459 static inline ttyctl_t
460 term_ostream_get_tty_control (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
)
465 static inline ttyctl_t
466 term_ostream_get_effective_tty_control (_GL_ATTRIBUTE_MAYBE_UNUSED term_ostream_t stream
)
471 static inline term_ostream_t
472 term_ostream_create (int fd
, const char *filename
,
473 _GL_ATTRIBUTE_MAYBE_UNUSED ttyctl_t tty_control
)
475 return fd_ostream_create (fd
, filename
, true);
478 #define is_instance_of_term_ostream is_instance_of_fd_ostream
480 /* ------------------------- From memory-ostream.h ------------------------- */
482 typedef ostream_t memory_ostream_t
;
484 #define memory_ostream_write_mem ostream_write_mem
485 #define memory_ostream_flush ostream_flush
486 #define memory_ostream_free ostream_free
489 memory_ostream_contents (_GL_ATTRIBUTE_MAYBE_UNUSED memory_ostream_t stream
,
490 const void **bufp
, size_t *buflenp
)
496 static inline memory_ostream_t
497 memory_ostream_create (void)
499 /* Not supported without the real libtextstyle. */
505 is_instance_of_memory_ostream (_GL_ATTRIBUTE_MAYBE_UNUSED ostream_t stream
)
510 /* -------------------------- From html-ostream.h -------------------------- */
512 typedef ostream_t html_ostream_t
;
514 #define html_ostream_write_mem ostream_write_mem
515 #define html_ostream_flush ostream_flush
516 #define html_ostream_free ostream_free
519 html_ostream_begin_span (_GL_ATTRIBUTE_MAYBE_UNUSED html_ostream_t stream
,
520 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *classname
)
525 html_ostream_end_span (_GL_ATTRIBUTE_MAYBE_UNUSED html_ostream_t stream
,
526 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *classname
)
530 static inline const char *
531 html_ostream_get_hyperlink_ref (_GL_ATTRIBUTE_MAYBE_UNUSED html_ostream_t stream
)
537 html_ostream_set_hyperlink_ref (_GL_ATTRIBUTE_MAYBE_UNUSED html_ostream_t stream
,
538 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *ref
)
543 html_ostream_flush_to_current_style (_GL_ATTRIBUTE_MAYBE_UNUSED html_ostream_t stream
)
547 static inline ostream_t
548 html_ostream_get_destination (_GL_ATTRIBUTE_MAYBE_UNUSED html_ostream_t stream
)
553 static inline html_ostream_t
554 html_ostream_create (ostream_t destination
)
556 /* Not supported without the real libtextstyle. */
562 is_instance_of_html_ostream (_GL_ATTRIBUTE_MAYBE_UNUSED ostream_t stream
)
567 /* ----------------------- From term-styled-ostream.h ----------------------- */
569 typedef styled_ostream_t term_styled_ostream_t
;
571 #define term_styled_ostream_write_mem ostream_write_mem
572 #define term_styled_ostream_flush ostream_flush
573 #define term_styled_ostream_free ostream_free
574 #define term_styled_ostream_begin_use_class styled_ostream_begin_use_class
575 #define term_styled_ostream_end_use_class styled_ostream_end_use_class
576 #define term_styled_ostream_get_hyperlink_ref styled_ostream_get_hyperlink_ref
577 #define term_styled_ostream_get_hyperlink_id styled_ostream_get_hyperlink_id
578 #define term_styled_ostream_set_hyperlink styled_ostream_set_hyperlink
579 #define term_styled_ostream_flush_to_current_style styled_ostream_flush_to_current_style
581 static inline term_ostream_t
582 term_styled_ostream_get_destination (term_styled_ostream_t stream
)
587 static inline const char *
588 term_styled_ostream_get_css_filename (_GL_ATTRIBUTE_MAYBE_UNUSED term_styled_ostream_t stream
)
593 static inline term_styled_ostream_t
594 term_styled_ostream_create (int fd
, const char *filename
,
595 _GL_ATTRIBUTE_MAYBE_UNUSED ttyctl_t tty_control
,
596 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *css_filename
)
598 return fd_ostream_create (fd
, filename
, true);
601 #define is_instance_of_term_styled_ostream is_instance_of_term_ostream
603 /* ----------------------- From html-styled-ostream.h ----------------------- */
605 typedef styled_ostream_t html_styled_ostream_t
;
607 #define html_styled_ostream_write_mem ostream_write_mem
608 #define html_styled_ostream_flush ostream_flush
609 #define html_styled_ostream_free ostream_free
610 #define html_styled_ostream_begin_use_class styled_ostream_begin_use_class
611 #define html_styled_ostream_end_use_class styled_ostream_end_use_class
612 #define html_styled_ostream_get_hyperlink_ref styled_ostream_get_hyperlink_ref
613 #define html_styled_ostream_get_hyperlink_id styled_ostream_get_hyperlink_id
614 #define html_styled_ostream_set_hyperlink styled_ostream_set_hyperlink
615 #define html_styled_ostream_flush_to_current_style styled_ostream_flush_to_current_style
617 static inline ostream_t
618 html_styled_ostream_get_destination (_GL_ATTRIBUTE_MAYBE_UNUSED html_styled_ostream_t stream
)
623 static inline html_ostream_t
624 html_styled_ostream_get_html_destination (_GL_ATTRIBUTE_MAYBE_UNUSED html_styled_ostream_t stream
)
629 static inline const char *
630 html_styled_ostream_get_css_filename (_GL_ATTRIBUTE_MAYBE_UNUSED html_styled_ostream_t stream
)
635 static inline html_styled_ostream_t
636 html_styled_ostream_create (_GL_ATTRIBUTE_MAYBE_UNUSED ostream_t destination
,
637 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *css_filename
)
639 /* Not supported without the real libtextstyle. */
645 is_instance_of_html_styled_ostream (_GL_ATTRIBUTE_MAYBE_UNUSED ostream_t stream
)
650 /* ----------------------- From noop-styled-ostream.h ----------------------- */
652 typedef styled_ostream_t noop_styled_ostream_t
;
654 #define noop_styled_ostream_write_mem ostream_write_mem
655 #define noop_styled_ostream_flush ostream_flush
656 #define noop_styled_ostream_free ostream_free
657 #define noop_styled_ostream_begin_use_class styled_ostream_begin_use_class
658 #define noop_styled_ostream_end_use_class styled_ostream_end_use_class
659 #define noop_styled_ostream_get_hyperlink_ref styled_ostream_get_hyperlink_ref
660 #define noop_styled_ostream_get_hyperlink_id styled_ostream_get_hyperlink_id
661 #define noop_styled_ostream_set_hyperlink styled_ostream_set_hyperlink
662 #define noop_styled_ostream_flush_to_current_style styled_ostream_flush_to_current_style
664 static inline ostream_t
665 noop_styled_ostream_get_destination (noop_styled_ostream_t stream
)
671 noop_styled_ostream_is_owning_destination (_GL_ATTRIBUTE_MAYBE_UNUSED noop_styled_ostream_t stream
)
676 static inline noop_styled_ostream_t
677 noop_styled_ostream_create (ostream_t destination
, bool pass_ownership
)
680 /* Not supported without the real libtextstyle. */
686 is_instance_of_noop_styled_ostream (_GL_ATTRIBUTE_MAYBE_UNUSED ostream_t stream
)
691 /* ------------------------------ From color.h ------------------------------ */
693 #define color_test_mode false
695 enum color_option
{ color_no
, color_tty
, color_yes
, color_html
};
696 #define color_mode color_no
698 #define style_file_name NULL
701 handle_color_option (_GL_ATTRIBUTE_MAYBE_UNUSED
const char *option
)
707 handle_style_option (_GL_ATTRIBUTE_MAYBE_UNUSED
const char *option
)
712 print_color_test (void)
714 /* Not supported without the real libtextstyle. */
719 style_file_prepare (_GL_ATTRIBUTE_MAYBE_UNUSED
const char *style_file_envvar
,
720 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *stylesdir_envvar
,
721 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *stylesdir_after_install
,
722 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *default_style_file
)
726 /* ------------------------------ From misc.h ------------------------------ */
728 static inline styled_ostream_t
729 styled_ostream_create (int fd
, const char *filename
,
730 _GL_ATTRIBUTE_MAYBE_UNUSED ttyctl_t tty_control
,
731 _GL_ATTRIBUTE_MAYBE_UNUSED
const char *css_filename
)
733 return fd_ostream_create (fd
, filename
, true);
737 libtextstyle_set_failure_exit_code (_GL_ATTRIBUTE_MAYBE_UNUSED
int exit_code
)
741 #endif /* _TEXTSTYLE_H */