This source file includes following definitions.
- get_control_data
- restore
- async_restore
- async_set_attributes_from_default
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <config.h>
19
20
21 #include "term-style-control.h"
22
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "full-write.h"
29
30
31
32
33
34
35
36
37
38 static const char set_underline_on[] = "\033[4m";
39 static const char set_underline_off[] = "\033[24m";
40 static const char set_background_color_cyan[] = "\033[46m";
41 static const char set_background_color_default[] = "\033[49m";
42
43 struct term_style_user_data
44 {
45
46
47 bool volatile underline;
48 bool volatile background_color_cyan;
49
50 struct term_style_control_data ctrl_data;
51 };
52
53 static struct term_style_control_data *
54 get_control_data (struct term_style_user_data *user_data)
55 {
56 return &user_data->ctrl_data;
57 }
58
59 static void
60 restore (struct term_style_user_data *user_data)
61 {
62 fputs (set_underline_off, stdout);
63 fputs (set_background_color_default, stdout);
64 fflush (stdout);
65 }
66
67 static _GL_ASYNC_SAFE void
68 async_restore (struct term_style_user_data *user_data)
69 {
70
71 full_write (STDOUT_FILENO, set_underline_off,
72 strlen (set_underline_off));
73 full_write (STDOUT_FILENO, set_background_color_default,
74 strlen (set_background_color_default));
75 }
76
77 static _GL_ASYNC_SAFE void
78 async_set_attributes_from_default (struct term_style_user_data *user_data)
79 {
80
81 if (user_data->underline)
82 full_write (STDOUT_FILENO, set_underline_on,
83 strlen (set_underline_on));
84 if (user_data->background_color_cyan)
85 full_write (STDOUT_FILENO, set_background_color_cyan,
86 strlen (set_background_color_cyan));
87 }
88
89 static const struct term_style_controller controller =
90 {
91 get_control_data,
92 restore,
93 async_restore,
94 async_set_attributes_from_default
95 };
96
97 int
98 main (int argc, char *argv[])
99 {
100 struct term_style_user_data user_data;
101
102
103 user_data.underline = false;
104 user_data.background_color_cyan = false;
105
106 activate_term_style_controller (&controller, &user_data, STDOUT_FILENO,
107 TTYCTL_AUTO);
108
109
110 fputs ("Hello ", stdout);
111 fflush (stdout);
112
113
114 activate_term_non_default_mode (&controller, &user_data);
115
116
117
118
119 user_data.underline = true;
120 fputs (set_underline_on, stdout);
121 fflush (stdout);
122
123 fputs ("Dr. ", stdout);
124 fflush (stdout);
125
126
127
128
129 user_data.background_color_cyan = true;
130 fputs (set_background_color_cyan, stdout);
131 fflush (stdout);
132
133 fputs ("Linus", stdout);
134 fflush (stdout);
135
136 user_data.background_color_cyan = false;
137 fputs (set_background_color_default, stdout);
138 fflush (stdout);
139
140 fputs (" Pauling", stdout);
141 fflush (stdout);
142
143 user_data.underline = false;
144 fputs (set_underline_off, stdout);
145 fflush (stdout);
146
147
148
149 deactivate_term_non_default_mode (&controller, &user_data);
150
151 fputs ("!\n", stdout);
152
153
154
155
156
157 deactivate_term_style_controller (&controller, &user_data);
158
159 return 0;
160 }