localedata: Locale and test name are the same.
[glibc.git] / locale / programs / record-status.h
blobb6bc58cddc0fee3e6a00b2e5342e7f44ff26ebbd
1 /* General definitions for recording error and warning status.
2 Copyright (C) 1998-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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
7 by the Free Software Foundation; version 2 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 <http://www.gnu.org/licenses/>. */
18 #ifndef _RECORD_STATUS_H
19 #define _RECORD_STATUS_H 1
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <error.h>
24 #include <locale.h>
25 #include <string.h>
27 /* We tentatively define all of the global data we use:
28 * recorded_warning_count: Number of warnings counted.
29 * recorded_error_count: Number of errors counted.
30 * be_quiet: Should all calls be silent?
31 * verbose: Should verbose messages be printed? */
32 int recorded_warning_count;
33 int recorded_error_count;
34 int be_quiet;
35 int verbose;
37 /* Saved locale state. */
38 struct locale_state
40 /* The current in-use locale. */
41 char *cur_locale;
44 /* Alter the current locale to match the locale configured by the
45 user, and return the previous saved state. */
46 static struct locale_state
47 push_locale (void)
49 int saved_errno;
50 const char *orig;
51 char *copy = NULL;
53 saved_errno = errno;
55 orig = setlocale (LC_CTYPE, NULL);
56 if (orig == NULL)
57 error (0, 0, "failed to read locale!");
59 if (setlocale (LC_CTYPE, "") == NULL)
60 error (0, 0, "failed to set locale!");
62 errno = saved_errno;
64 if (orig != NULL)
65 copy = strdup (orig);
67 /* We will return either a valid locale or NULL if we failed
68 to save the locale. */
69 return (struct locale_state) { .cur_locale = copy };
72 /* Use the saved state to restore the locale. */
73 static void
74 pop_locale (struct locale_state ls)
76 const char *set = NULL;
77 /* We might have failed to save the locale, so only attempt to
78 restore a validly saved non-NULL locale. */
79 if (ls.cur_locale != NULL)
81 set = setlocale (LC_CTYPE, ls.cur_locale);
82 if (set == NULL)
83 error (0, 0, "failed to restore %s locale!", ls.cur_locale);
85 free (ls.cur_locale);
89 /* Wrapper to print verbose informative messages.
90 Verbose messages are only printed if --verbose
91 is in effect and --quiet is not. */
92 static void
93 __attribute__ ((__format__ (__printf__, 2, 3), nonnull (1, 2), unused))
94 record_verbose (FILE *stream, const char *format, ...)
96 char *str;
97 va_list arg;
99 if (!verbose)
100 return;
102 if (!be_quiet)
104 struct locale_state ls;
105 int ret;
107 va_start (arg, format);
108 ls = push_locale ();
110 ret = vasprintf (&str, format, arg);
111 if (ret == -1)
112 abort ();
114 pop_locale (ls);
115 va_end (arg);
117 fprintf (stream, "%s\n", str);
119 free (str);
123 /* Wrapper to print warning messages. We keep track of how
124 many were called because this effects our exit code.
125 Nothing is printed if --quiet is in effect, but warnings
126 are always counted. */
127 static void
128 __attribute__ ((__format__ (__printf__, 1, 2), nonnull (1), unused))
129 record_warning (const char *format, ...)
131 char *str;
132 va_list arg;
134 recorded_warning_count++;
136 if (!be_quiet)
138 struct locale_state ls;
139 int ret;
141 va_start (arg, format);
142 ls = push_locale ();
144 ret = vasprintf (&str, format, arg);
145 if (ret == -1)
146 abort ();
148 pop_locale (ls);
149 va_end (arg);
151 fprintf (stderr, "%s\n", str);
153 free (str);
157 /* Wrapper to print error messages. We keep track of how
158 many were called because this effects our exit code.
159 Nothing is printed if --quiet is in effect, but errors
160 are always counted, and fatal errors always exit the
161 program. */
162 static void
163 __attribute__ ((__format__ (__printf__, 3, 4), nonnull (3), unused))
164 record_error (int status, int errnum, const char *format, ...)
166 char *str;
167 va_list arg;
169 recorded_error_count++;
171 /* The existing behaviour is that even if you use --quiet, a fatal
172 error is always printed and terminates the process. */
173 if (!be_quiet || status != 0)
175 struct locale_state ls;
176 int ret;
178 va_start (arg, format);
179 ls = push_locale ();
181 ret = vasprintf (&str, format, arg);
182 if (ret == -1)
183 abort ();
185 pop_locale (ls);
186 va_end (arg);
188 error (status, errnum, "%s", str);
190 free (str);
193 /* ... likewise for error_at_line. */
194 static void
195 __attribute__ ((__format__ (__printf__, 5, 6), nonnull (3, 5), unused))
196 record_error_at_line (int status, int errnum, const char *filename,
197 unsigned int linenum, const char *format, ...)
199 char *str;
200 va_list arg;
202 recorded_error_count++;
204 /* The existing behaviour is that even if you use --quiet, a fatal
205 error is always printed and terminates the process. */
206 if (!be_quiet || status != 0)
208 struct locale_state ls;
209 int ret;
211 va_start (arg, format);
212 ls = push_locale ();
214 ret = vasprintf (&str, format, arg);
215 if (ret == -1)
216 abort ();
218 pop_locale (ls);
219 va_end (arg);
221 error_at_line (status, errnum, filename, linenum, "%s", str);
223 free (str);
227 #endif