/
hello.c
276 lines (245 loc) · 7.82 KB
/
hello.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* Example program for GNU libtextstyle.
Copyright (C) 2018-2019 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>, 2018.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Source code of the C program. */
#include <textstyle.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/stat.h>
#include "names.c"
/* Returns the record for a given first name, or NULL if not found. */
static const struct first_name *
find_first_name (const char *name)
{
size_t i = 0;
size_t j = sizeof (names) / sizeof (names[0]);
/* Loop invariants:
1. j > i,
2. If name is in the table, then at an index >= i, < j. */
while (j - i > 1)
{
size_t k = i + (j - i) / 2;
if (strcmp (names[k].name, name) <= 0)
i = k;
else
j = k;
}
/* Here j = i + 1. */
if (strcmp (names[i].name, name) == 0)
return &names[i];
else
return NULL;
}
enum sex
{
UNKNOWN,
MALE,
FEMALE
};
/* Returns the known sex for a given first name, or UNKNOWN if unknown. */
static enum sex
classify_first_name (const char *name)
{
const struct first_name *p = find_first_name (name);
if (p)
{
if (p->p_boy >= 0.99f)
return MALE;
if (p->p_boy <= 0.01f)
return FEMALE;
}
return UNKNOWN;
}
/* Returns the full name of the current user, or NULL if unknown. */
static const char *
get_fullname (void)
{
const char *name = getlogin ();
if (name != NULL)
{
struct passwd *pwd = getpwnam (name);
if (pwd != NULL)
{
const char *gecos = pwd->pw_gecos;
if (gecos != NULL)
{
/* Use the part before the first comma.
See <https://en.wikipedia.org/wiki/Gecos_field>. */
const char *comma = strchr (gecos, ',');
if (comma != NULL)
{
char *part = (char *) malloc (comma - gecos + 1);
if (part != NULL)
{
memcpy (part, gecos, comma - gecos);
part[comma - gecos] = '\0';
return part;
}
}
else
return gecos;
}
}
}
return NULL;
}
int
main (int argc, char *argv[])
{
const char *program_name = argv[0];
const char *fullname = NULL;
int i;
/* Parse the command-line arguments. */
for (i = 1; i < argc; i++)
{
const char *arg = argv[i];
if (strncmp (arg, "--color=", 8) == 0)
handle_color_option (arg + 8);
else if (strncmp (arg, "--style=", 8) == 0)
handle_style_option (arg + 8);
else if (arg[0] == '-')
{
fprintf (stderr, "%s: invalid argument: %s\n", program_name, arg);
exit (1);
}
else
fullname = arg;
}
/* Handle the --color=test special argument. */
if (color_test_mode)
{
print_color_test ();
exit (0);
}
if (color_mode == color_yes
|| (color_mode == color_tty
&& isatty (STDOUT_FILENO)
&& getenv ("NO_COLOR") == NULL)
|| color_mode == color_html)
{
/* Find the style file. */
style_file_prepare ("HELLO_STYLE", "HELLO_STYLESDIR", STYLESDIR,
"hello-default.css");
/* As a fallback, use the default in the current directory. */
{
struct stat statbuf;
if (style_file_name == NULL || stat (style_file_name, &statbuf) < 0)
style_file_name = "hello-default.css";
}
}
else
/* No styling. */
style_file_name = NULL;
/* Create a terminal output stream that uses this style file. */
styled_ostream_t stream =
(color_mode == color_html
? html_styled_ostream_create (file_ostream_create (stdout),
style_file_name)
: styled_ostream_create (STDOUT_FILENO, "(stdout)", TTYCTL_AUTO,
style_file_name));
/* Determine the full name of the user. */
if (fullname == NULL)
fullname = get_fullname ();
if (fullname != NULL)
{
ostream_write_str (stream, "Hello ");
/* Associate the entire full name in CSS class 'name'. */
styled_ostream_begin_use_class (stream, "name");
const char *fullname_end = fullname + strlen (fullname);
/* Determine the extent of the first name in the full name. */
const char *firstname_start;
const char *firstname_end;
{
/* The full name can be of the form "FAMILYNAME, FIRSTNAME". */
const char *comma = strchr (fullname, ',');
if (comma != NULL)
{
firstname_start = comma + 1;
while (*firstname_start == ' ')
firstname_start++;
firstname_end = fullname_end;
}
else
{
/* Or it can be of the form "X. FIRSTNAME Y. FAMILYNAME". */
firstname_start = fullname;
for (;;)
{
const char *space = strchr (firstname_start, ' ');
if (space == NULL)
{
firstname_end = fullname_end;
break;
}
if (space == firstname_start || space[-1] == '.')
firstname_start = space + 1;
else
{
firstname_end = space;
break;
}
}
}
while (firstname_end > firstname_start && firstname_end[-1] == ' ')
firstname_end--;
}
/* Output the part of the full name before the first name. */
ostream_write_mem (stream, fullname, firstname_start - fullname);
/* Guess the sex, based on the first name. */
char *firstname = (char *) malloc (firstname_end - firstname_start + 1);
memcpy (firstname, firstname_start, firstname_end - firstname_start);
firstname[firstname_end - firstname_start] = '\0';
enum sex guessed_sex = classify_first_name (firstname);
free (firstname);
/* Associate the first name with the appropriate CSS class. */
switch (guessed_sex)
{
case MALE:
styled_ostream_begin_use_class (stream, "boy-name");
break;
case FEMALE:
styled_ostream_begin_use_class (stream, "girl-name");
break;
default:
break;
}
/* Output the first name. */
ostream_write_mem (stream, firstname_start,
firstname_end - firstname_start);
/* Terminate the first name. */
switch (guessed_sex)
{
case MALE:
styled_ostream_end_use_class (stream, "boy-name");
break;
case FEMALE:
styled_ostream_end_use_class (stream, "girl-name");
break;
default:
break;
}
/* Output the part of the full name after the first name. */
ostream_write_mem (stream, firstname_end, fullname_end - firstname_end);
/* Terminate the name. */
styled_ostream_end_use_class (stream, "name");
ostream_write_str (stream, "!\n");
}
else
ostream_write_str (stream, "Hello!\n");
/* Flush and close the terminal stream. */
styled_ostream_free (stream);
return 0;
}