textstyle.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
  2. /* Dummy replacement for part of the public API of the libtextstyle library.
  3. 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.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. /* Written by Bruno Haible <bruno@clisp.org>, 2019. */
  15. /* This file is used as replacement when libtextstyle with its include file
  16. <textstyle.h> is not found.
  17. It supports the essential API and implements it in a way that does not
  18. provide text styling. That is, it produces plain text output via <stdio.h>
  19. FILE objects.
  20. Thus, it allows a package to be build with or without a dependency to
  21. libtextstyle, with very few occurrences of '#if HAVE_LIBTEXTSTYLE'.
  22. Restriction:
  23. It assumes that freopen() is not being called on stdout and stderr. */
  24. #ifndef _TEXTSTYLE_H
  25. #define _TEXTSTYLE_H
  26. #include <errno.h>
  27. #include <stdarg.h>
  28. #include <stdbool.h>
  29. #include <stddef.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #if HAVE_TCDRAIN
  35. # include <termios.h>
  36. #endif
  37. /* ----------------------------- From ostream.h ----------------------------- */
  38. /* Describes the scope of a flush operation. */
  39. typedef enum
  40. {
  41. /* Flushes buffers in this ostream_t.
  42. Use this value if you want to write to the underlying ostream_t. */
  43. FLUSH_THIS_STREAM = 0,
  44. /* Flushes all buffers in the current process.
  45. Use this value if you want to write to the same target through a
  46. different file descriptor or a FILE stream. */
  47. FLUSH_THIS_PROCESS = 1,
  48. /* Flushes buffers in the current process and attempts to flush the buffers
  49. in the kernel.
  50. Use this value so that some other process (or the kernel itself)
  51. may write to the same target. */
  52. FLUSH_ALL = 2
  53. } ostream_flush_scope_t;
  54. /* An output stream is an object to which one can feed a sequence of bytes. */
  55. typedef FILE * ostream_t;
  56. static inline void
  57. ostream_write_mem (ostream_t stream, const void *data, size_t len)
  58. {
  59. if (len > 0)
  60. fwrite (data, 1, len, stream);
  61. }
  62. static inline void
  63. ostream_flush (ostream_t stream, ostream_flush_scope_t scope)
  64. {
  65. fflush (stream);
  66. if (scope == FLUSH_ALL)
  67. {
  68. int fd = fileno (stream);
  69. if (fd >= 0)
  70. {
  71. /* For streams connected to a disk file: */
  72. fsync (fd);
  73. #if HAVE_TCDRAIN
  74. /* For streams connected to a terminal: */
  75. {
  76. int retval;
  77. do
  78. retval = tcdrain (fd);
  79. while (retval < 0 && errno == EINTR);
  80. }
  81. #endif
  82. }
  83. }
  84. }
  85. static inline void
  86. ostream_free (ostream_t stream)
  87. {
  88. if (stream == stdin || stream == stderr)
  89. fflush (stream);
  90. else
  91. fclose (stream);
  92. }
  93. static inline void
  94. ostream_write_str (ostream_t stream, const char *string)
  95. {
  96. ostream_write_mem (stream, string, strlen (string));
  97. }
  98. static inline ptrdiff_t ostream_printf (ostream_t stream,
  99. const char *format, ...)
  100. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || defined __clang__
  101. __attribute__ ((__format__ (__printf__, 2, 3)))
  102. #endif
  103. ;
  104. static inline ptrdiff_t
  105. ostream_printf (ostream_t stream, const char *format, ...)
  106. {
  107. va_list args;
  108. char *temp_string;
  109. ptrdiff_t ret;
  110. va_start (args, format);
  111. ret = vasprintf (&temp_string, format, args);
  112. va_end (args);
  113. if (ret >= 0)
  114. {
  115. if (ret > 0)
  116. ostream_write_str (stream, temp_string);
  117. free (temp_string);
  118. }
  119. return ret;
  120. }
  121. static inline ptrdiff_t ostream_vprintf (ostream_t stream,
  122. const char *format, va_list args)
  123. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || defined __clang__
  124. __attribute__ ((__format__ (__printf__, 2, 0)))
  125. #endif
  126. ;
  127. static inline ptrdiff_t
  128. ostream_vprintf (ostream_t stream, const char *format, va_list args)
  129. {
  130. char *temp_string;
  131. ptrdiff_t ret = vasprintf (&temp_string, format, args);
  132. if (ret >= 0)
  133. {
  134. if (ret > 0)
  135. ostream_write_str (stream, temp_string);
  136. free (temp_string);
  137. }
  138. return ret;
  139. }
  140. /* ------------------------- From styled-ostream.h ------------------------- */
  141. typedef ostream_t styled_ostream_t;
  142. #define styled_ostream_write_mem ostream_write_mem
  143. #define styled_ostream_flush ostream_flush
  144. #define styled_ostream_free ostream_free
  145. static inline void
  146. styled_ostream_begin_use_class (styled_ostream_t stream _GL_UNUSED,
  147. const char *classname _GL_UNUSED)
  148. {
  149. }
  150. static inline void
  151. styled_ostream_end_use_class (styled_ostream_t stream _GL_UNUSED,
  152. const char *classname _GL_UNUSED)
  153. {
  154. }
  155. static inline const char *
  156. styled_ostream_get_hyperlink_ref (styled_ostream_t stream _GL_UNUSED)
  157. {
  158. return NULL;
  159. }
  160. static inline const char *
  161. styled_ostream_get_hyperlink_id (styled_ostream_t stream _GL_UNUSED)
  162. {
  163. return NULL;
  164. }
  165. static inline void
  166. styled_ostream_set_hyperlink (styled_ostream_t stream _GL_UNUSED,
  167. const char *ref _GL_UNUSED,
  168. const char *id _GL_UNUSED)
  169. {
  170. }
  171. static inline void
  172. styled_ostream_flush_to_current_style (styled_ostream_t stream _GL_UNUSED)
  173. {
  174. }
  175. /* -------------------------- From file-ostream.h -------------------------- */
  176. typedef ostream_t file_ostream_t;
  177. #define file_ostream_write_mem ostream_write_mem
  178. #define file_ostream_flush ostream_flush
  179. #define file_ostream_free ostream_free
  180. static inline file_ostream_t
  181. file_ostream_create (FILE *fp)
  182. {
  183. return fp;
  184. }
  185. /* --------------------------- From fd-ostream.h --------------------------- */
  186. typedef ostream_t fd_ostream_t;
  187. #define fd_ostream_write_mem ostream_write_mem
  188. #define fd_ostream_flush ostream_flush
  189. #define fd_ostream_free ostream_free
  190. static inline fd_ostream_t
  191. fd_ostream_create (int fd, const char *filename _GL_UNUSED,
  192. bool buffered _GL_UNUSED)
  193. {
  194. if (fd == 1)
  195. return stdout;
  196. else if (fd == 2)
  197. return stderr;
  198. else
  199. return fdopen (fd, "w");
  200. }
  201. /* -------------------------- From term-ostream.h -------------------------- */
  202. typedef int term_color_t;
  203. enum
  204. {
  205. COLOR_DEFAULT = -1 /* unknown */
  206. };
  207. typedef enum
  208. {
  209. WEIGHT_NORMAL = 0,
  210. WEIGHT_BOLD,
  211. WEIGHT_DEFAULT = WEIGHT_NORMAL
  212. } term_weight_t;
  213. typedef enum
  214. {
  215. POSTURE_NORMAL = 0,
  216. POSTURE_ITALIC, /* same as oblique */
  217. POSTURE_DEFAULT = POSTURE_NORMAL
  218. } term_posture_t;
  219. typedef enum
  220. {
  221. UNDERLINE_OFF = 0,
  222. UNDERLINE_ON,
  223. UNDERLINE_DEFAULT = UNDERLINE_OFF
  224. } term_underline_t;
  225. typedef ostream_t term_ostream_t;
  226. #define term_ostream_write_mem ostream_write_mem
  227. #define term_ostream_flush ostream_flush
  228. #define term_ostream_free ostream_free
  229. static inline term_color_t
  230. term_ostream_get_color (term_ostream_t stream _GL_UNUSED)
  231. {
  232. return COLOR_DEFAULT;
  233. }
  234. static inline void
  235. term_ostream_set_color (term_ostream_t stream _GL_UNUSED,
  236. term_color_t color _GL_UNUSED)
  237. {
  238. }
  239. static inline term_color_t
  240. term_ostream_get_bgcolor (term_ostream_t stream _GL_UNUSED)
  241. {
  242. return COLOR_DEFAULT;
  243. }
  244. static inline void
  245. term_ostream_set_bgcolor (term_ostream_t stream _GL_UNUSED,
  246. term_color_t color _GL_UNUSED)
  247. {
  248. }
  249. static inline term_weight_t
  250. term_ostream_get_weight (term_ostream_t stream _GL_UNUSED)
  251. {
  252. return WEIGHT_DEFAULT;
  253. }
  254. static inline void
  255. term_ostream_set_weight (term_ostream_t stream _GL_UNUSED,
  256. term_weight_t weight _GL_UNUSED)
  257. {
  258. }
  259. static inline term_posture_t
  260. term_ostream_get_posture (term_ostream_t stream _GL_UNUSED)
  261. {
  262. return POSTURE_DEFAULT;
  263. }
  264. static inline void
  265. term_ostream_set_posture (term_ostream_t stream _GL_UNUSED,
  266. term_posture_t posture _GL_UNUSED)
  267. {
  268. }
  269. static inline term_underline_t
  270. term_ostream_get_underline (term_ostream_t stream _GL_UNUSED)
  271. {
  272. return UNDERLINE_DEFAULT;
  273. }
  274. static inline void
  275. term_ostream_set_underline (term_ostream_t stream _GL_UNUSED,
  276. term_underline_t underline _GL_UNUSED)
  277. {
  278. }
  279. static inline const char *
  280. term_ostream_get_hyperlink_ref (term_ostream_t stream _GL_UNUSED)
  281. {
  282. return NULL;
  283. }
  284. static inline const char *
  285. term_ostream_get_hyperlink_id (term_ostream_t stream _GL_UNUSED)
  286. {
  287. return NULL;
  288. }
  289. static inline void
  290. term_ostream_set_hyperlink (term_ostream_t stream _GL_UNUSED,
  291. const char *ref _GL_UNUSED,
  292. const char *id _GL_UNUSED)
  293. {
  294. }
  295. static inline void
  296. term_ostream_flush_to_current_style (term_ostream_t stream)
  297. {
  298. fflush (stream);
  299. }
  300. typedef enum
  301. {
  302. TTYCTL_AUTO = 0, /* Automatic best-possible choice. */
  303. TTYCTL_NONE, /* No control.
  304. Result: Garbled output can occur, and the terminal can
  305. be left in any state when the program is interrupted. */
  306. TTYCTL_PARTIAL, /* Signal handling.
  307. Result: Garbled output can occur, but the terminal will
  308. be left in the default state when the program is
  309. interrupted. */
  310. TTYCTL_FULL /* Signal handling and disabling echo and flush-upon-signal.
  311. Result: No garbled output, and the the terminal will
  312. be left in the default state when the program is
  313. interrupted. */
  314. } ttyctl_t;
  315. static inline term_ostream_t
  316. term_ostream_create (int fd, const char *filename,
  317. ttyctl_t tty_control _GL_UNUSED)
  318. {
  319. return fd_ostream_create (fd, filename, true);
  320. }
  321. /* ----------------------- From term-styled-ostream.h ----------------------- */
  322. typedef styled_ostream_t term_styled_ostream_t;
  323. #define term_styled_ostream_write_mem ostream_write_mem
  324. #define term_styled_ostream_flush ostream_flush
  325. #define term_styled_ostream_free ostream_free
  326. #define term_styled_ostream_begin_use_class styled_ostream_begin_use_class
  327. #define term_styled_ostream_end_use_class styled_ostream_end_use_class
  328. #define term_styled_ostream_get_hyperlink_ref styled_ostream_get_hyperlink_ref
  329. #define term_styled_ostream_get_hyperlink_id styled_ostream_get_hyperlink_id
  330. #define term_styled_ostream_set_hyperlink styled_ostream_set_hyperlink
  331. #define term_styled_ostream_flush_to_current_style styled_ostream_flush_to_current_style
  332. static inline term_styled_ostream_t
  333. term_styled_ostream_create (int fd, const char *filename,
  334. ttyctl_t tty_control _GL_UNUSED,
  335. const char *css_filename _GL_UNUSED)
  336. {
  337. return fd_ostream_create (fd, filename, true);
  338. }
  339. /* ----------------------- From html-styled-ostream.h ----------------------- */
  340. typedef styled_ostream_t html_styled_ostream_t;
  341. static inline html_styled_ostream_t
  342. html_styled_ostream_create (ostream_t destination _GL_UNUSED,
  343. const char *css_filename _GL_UNUSED)
  344. {
  345. abort ();
  346. return NULL;
  347. }
  348. /* ------------------------------ From color.h ------------------------------ */
  349. #define color_test_mode false
  350. enum color_option { color_no, color_tty, color_yes, color_html };
  351. #define color_mode color_no
  352. #define style_file_name NULL
  353. static inline bool
  354. handle_color_option (const char *option _GL_UNUSED)
  355. {
  356. return false;
  357. }
  358. static inline void
  359. handle_style_option (const char *option _GL_UNUSED)
  360. {
  361. }
  362. static inline void
  363. print_color_test (void)
  364. {
  365. abort ();
  366. }
  367. static inline void
  368. style_file_prepare (const char *style_file_envvar _GL_UNUSED,
  369. const char *stylesdir_envvar _GL_UNUSED,
  370. const char *stylesdir_after_install _GL_UNUSED,
  371. const char *default_style_file _GL_UNUSED)
  372. {
  373. }
  374. /* ------------------------------ From misc.h ------------------------------ */
  375. static inline styled_ostream_t
  376. styled_ostream_create (int fd, const char *filename,
  377. ttyctl_t tty_control _GL_UNUSED,
  378. const char *css_filename _GL_UNUSED)
  379. {
  380. return fd_ostream_create (fd, filename, true);
  381. }
  382. static inline void
  383. libtextstyle_set_failure_exit_code (int exit_code _GL_UNUSED)
  384. {
  385. }
  386. #endif /* _TEXTSTYLE_H */