select: Fix "warning: no previous prototype for function".
[gnulib.git] / lib / textstyle.in.h
blobb888378f00635d2e6ffe97afaa8fc7c2a6df2088
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>
23 FILE objects.
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'.
27 Restriction:
28 It assumes that freopen() is not being called on stdout and stderr. */
30 #ifndef _TEXTSTYLE_H
31 #define _TEXTSTYLE_H
33 #include <errno.h>
34 #include <stdarg.h>
35 #include <stdbool.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #if HAVE_TCDRAIN
42 # include <termios.h>
43 #endif
45 /* ----------------------------- From ostream.h ----------------------------- */
47 /* Describes the scope of a flush operation. */
48 typedef enum
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
58 in the kernel.
59 Use this value so that some other process (or the kernel itself)
60 may write to the same target. */
61 FLUSH_ALL = 2
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;
69 static inline void
70 ostream_write_mem (ostream_t stream, const void *data, size_t len)
72 if (len > 0)
73 fwrite (data, 1, len, stream);
76 static inline void
77 ostream_flush (ostream_t stream, ostream_flush_scope_t scope)
79 fflush (stream);
80 if (scope == FLUSH_ALL)
82 int fd = fileno (stream);
83 if (fd >= 0)
85 /* For streams connected to a disk file: */
86 fsync (fd);
87 #if HAVE_TCDRAIN
88 /* For streams connected to a terminal: */
90 int retval;
93 retval = tcdrain (fd);
94 while (retval < 0 && errno == EINTR);
96 #endif
101 static inline void
102 ostream_free (ostream_t stream)
104 if (stream == stdin || stream == stderr)
105 fflush (stream);
106 else
107 fclose (stream);
110 static inline void
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)))
120 #endif
122 static inline ptrdiff_t
123 ostream_printf (ostream_t stream, const char *format, ...)
125 va_list args;
126 char *temp_string;
127 ptrdiff_t ret;
129 va_start (args, format);
130 ret = vasprintf (&temp_string, format, args);
131 va_end (args);
132 if (ret >= 0)
134 if (ret > 0)
135 ostream_write_str (stream, temp_string);
136 free (temp_string);
138 return ret;
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)))
145 #endif
147 static inline ptrdiff_t
148 ostream_vprintf (ostream_t stream, const char *format, va_list args)
150 char *temp_string;
151 ptrdiff_t ret = vasprintf (&temp_string, format, args);
152 if (ret >= 0)
154 if (ret > 0)
155 ostream_write_str (stream, temp_string);
156 free (temp_string);
158 return ret;
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
169 static inline void
170 styled_ostream_begin_use_class (styled_ostream_t stream _GL_UNUSED,
171 const char *classname _GL_UNUSED)
175 static inline void
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)
184 return NULL;
187 static inline const char *
188 styled_ostream_get_hyperlink_id (styled_ostream_t stream _GL_UNUSED)
190 return NULL;
193 static inline void
194 styled_ostream_set_hyperlink (styled_ostream_t stream _GL_UNUSED,
195 const char *ref _GL_UNUSED,
196 const char *id _GL_UNUSED)
200 static inline void
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)
216 return 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)
231 if (fd == 1)
232 return stdout;
233 else if (fd == 2)
234 return stderr;
235 else
236 return fdopen (fd, "w");
239 /* -------------------------- From term-ostream.h -------------------------- */
241 typedef int term_color_t;
242 enum
244 COLOR_DEFAULT = -1 /* unknown */
247 typedef enum
249 WEIGHT_NORMAL = 0,
250 WEIGHT_BOLD,
251 WEIGHT_DEFAULT = WEIGHT_NORMAL
252 } term_weight_t;
254 typedef enum
256 POSTURE_NORMAL = 0,
257 POSTURE_ITALIC, /* same as oblique */
258 POSTURE_DEFAULT = POSTURE_NORMAL
259 } term_posture_t;
261 typedef enum
263 UNDERLINE_OFF = 0,
264 UNDERLINE_ON,
265 UNDERLINE_DEFAULT = UNDERLINE_OFF
266 } term_underline_t;
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;
280 static inline void
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;
292 static inline void
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;
304 static inline void
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;
316 static inline void
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;
328 static inline void
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)
337 return NULL;
340 static inline const char *
341 term_ostream_get_hyperlink_id (term_ostream_t stream _GL_UNUSED)
343 return NULL;
346 static inline void
347 term_ostream_set_hyperlink (term_ostream_t stream _GL_UNUSED,
348 const char *ref _GL_UNUSED,
349 const char *id _GL_UNUSED)
353 static inline void
354 term_ostream_flush_to_current_style (term_ostream_t stream)
356 fflush (stream);
359 typedef enum
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
368 interrupted. */
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
372 interrupted. */
373 } ttyctl_t;
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)
412 abort ();
413 return NULL;
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
425 static inline bool
426 handle_color_option (const char *option _GL_UNUSED)
428 return false;
431 static inline void
432 handle_style_option (const char *option _GL_UNUSED)
436 static inline void
437 print_color_test (void)
439 abort ();
442 static inline 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);
460 static inline void
461 libtextstyle_set_failure_exit_code (int exit_code _GL_UNUSED)
465 #endif /* _TEXTSTYLE_H */