1 /* Multiline error-reporting functions.
2 Copyright (C) 2001-2003, 2006, 2019, 2023 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
4
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.
9
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.
14
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/>. */
17
18
19 #include <config.h>
20
21 /* Specification. */
22 #include "xerror.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "error.h"
29 #include "error-progname.h"
30 #include "mbswidth.h"
31 #if IN_LIBGETTEXTPO
32 # define program_name getprogname ()
33 #else
34 # include "progname.h"
35 #endif
36
37 /* Emit a multiline warning to stderr, consisting of MESSAGE, with the
38 first line prefixed with PREFIX and the remaining lines prefixed with
39 the same amount of spaces. Reuse the spaces of the previous call if
40 PREFIX is NULL. Free the PREFIX and MESSAGE when done. */
41 void
42 multiline_warning (char *prefix, char *message)
43 {
44 static int width;
45 const char *cp;
46 int i;
47
48 fflush (stdout);
49
50 cp = message;
51
52 if (prefix != NULL)
53 {
54 width = 0;
55 if (error_with_progname)
56 {
57 fprintf (stderr, "%s: ", program_name);
58 width += mbswidth (program_name, 0) + 2;
59 }
60 fputs (prefix, stderr);
61 width += mbswidth (prefix, 0);
62 free (prefix);
63 goto after_indent;
64 }
65
66 for (;;)
67 {
68 const char *np;
69
70 for (i = width; i > 0; i--)
71 putc (' ', stderr);
72
73 after_indent:
74 np = strchr (cp, '\n');
75
76 if (np == NULL || np[1] == '\0')
77 {
78 fputs (cp, stderr);
79 break;
80 }
81
82 np++;
83 fwrite (cp, 1, np - cp, stderr);
84 cp = np;
85 }
86
87 free (message);
88 }
89
90 /* Emit a multiline error to stderr, consisting of MESSAGE, with the
91 first line prefixed with PREFIX and the remaining lines prefixed with
92 the same amount of spaces. Reuse the spaces of the previous call if
93 PREFIX is NULL. Free the PREFIX and MESSAGE when done. */
94 void
95 multiline_error (char *prefix, char *message)
96 {
97 if (prefix != NULL)
98 ++error_message_count;
99 multiline_warning (prefix, message);
100 }