sigprocmask: Fix configuration failure on Solaris 10 (regr. 2020-07-25).
[gnulib.git] / tests / test-term-style-control-yes.c
blob0172d1a1c2202e8538b337a33e7d69ba928fb629
1 /* Interactive test program for the term-style-control module.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2019.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "term-style-control.h"
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "full-write.h"
30 /* This program outputs an endless amount of lines, each consisting of a
31 single 'y', in red color and underlined.
32 It can be used to exercise race conditions caused by
33 - simultaneous keyboard input on the terminal,
34 - pressing Ctrl-C,
35 - pressing Ctrl-Z and then "fg". */
37 /* ECMA-48 / ISO 6429 escape sequences. See
38 https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
40 static const char set_underline_on[] = "\033[4m";
41 static const char set_underline_off[] = "\033[24m";
42 static const char set_foreground_color_red[] = "\033[31m";
43 static const char set_foreground_color_default[] = "\033[39m";
45 struct term_style_user_data
47 /* This field is marked volatile, because it is accessed from the
48 async-safe function async_set_attributes_from_default. */
49 bool volatile red_and_underline;
51 struct term_style_control_data ctrl_data;
54 static struct term_style_control_data *
55 get_control_data (struct term_style_user_data *user_data)
57 return &user_data->ctrl_data;
60 static void
61 restore (struct term_style_user_data *user_data)
63 fputs (set_underline_off, stdout);
64 fputs (set_foreground_color_default, stdout);
65 fflush (stdout);
68 static _GL_ASYNC_SAFE void
69 async_restore (struct term_style_user_data *user_data)
71 /* No <stdio.h> calls here! */
72 full_write (STDOUT_FILENO, set_underline_off,
73 strlen (set_underline_off));
74 full_write (STDOUT_FILENO, set_foreground_color_default,
75 strlen (set_foreground_color_default));
78 static _GL_ASYNC_SAFE void
79 async_set_attributes_from_default (struct term_style_user_data *user_data)
81 /* No <stdio.h> calls here! */
82 if (user_data->red_and_underline)
84 full_write (STDOUT_FILENO, set_underline_on,
85 strlen (set_underline_on));
86 full_write (STDOUT_FILENO, set_foreground_color_red,
87 strlen (set_foreground_color_red));
91 static const struct term_style_controller controller =
93 get_control_data,
94 restore,
95 async_restore,
96 async_set_attributes_from_default
99 int
100 main (int argc, char *argv[])
102 struct term_style_user_data user_data;
104 /* Initialization. */
105 user_data.red_and_underline = false;
107 activate_term_style_controller (&controller, &user_data, STDOUT_FILENO,
108 TTYCTL_AUTO);
110 for (;;)
112 /* Before any styling, enable the non-default mode. */
113 activate_term_non_default_mode (&controller, &user_data);
115 /* Set user_data.red_and_underline *before* emitting the appropriate
116 escape sequences, otherwise async_set_attributes_from_default will not
117 do its job correctly. */
118 user_data.red_and_underline = true;
119 fputs (set_underline_on, stdout);
120 fputs (set_foreground_color_red, stdout);
121 fflush (stdout);
123 fputs ("y", stdout);
124 fflush (stdout);
126 /* Revert to the default style before emitting a newline. */
127 user_data.red_and_underline = false;
128 fputs (set_underline_off, stdout);
129 fputs (set_foreground_color_default, stdout);
130 fflush (stdout);
132 /* Optional. */
133 deactivate_term_non_default_mode (&controller, &user_data);
135 fputs ("\n", stdout);
136 fflush (stdout);