• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Multiline error-reporting functions.
2    Copyright (C) 2001-2003, 2006, 2019 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 # include "getprogname.h"
33 # define program_name getprogname ()
34 #else
35 # include "progname.h"
36 #endif
37 
38 /* Emit a multiline warning to stderr, consisting of MESSAGE, with the
39    first line prefixed with PREFIX and the remaining lines prefixed with
40    the same amount of spaces.  Reuse the spaces of the previous call if
41    PREFIX is NULL.  Free the PREFIX and MESSAGE when done.  */
42 void
multiline_warning(char * prefix,char * message)43 multiline_warning (char *prefix, char *message)
44 {
45   static int width;
46   const char *cp;
47   int i;
48 
49   fflush (stdout);
50 
51   cp = message;
52 
53   if (prefix != NULL)
54     {
55       width = 0;
56       if (error_with_progname)
57         {
58           fprintf (stderr, "%s: ", program_name);
59           width += mbswidth (program_name, 0) + 2;
60         }
61       fputs (prefix, stderr);
62       width += mbswidth (prefix, 0);
63       free (prefix);
64       goto after_indent;
65     }
66 
67   for (;;)
68     {
69       const char *np;
70 
71       for (i = width; i > 0; i--)
72         putc (' ', stderr);
73 
74     after_indent:
75       np = strchr (cp, '\n');
76 
77       if (np == NULL || np[1] == '\0')
78         {
79           fputs (cp, stderr);
80           break;
81         }
82 
83       np++;
84       fwrite (cp, 1, np - cp, stderr);
85       cp = np;
86     }
87 
88   free (message);
89 }
90 
91 /* Emit a multiline error to stderr, consisting of MESSAGE, with the
92    first line prefixed with PREFIX and the remaining lines prefixed with
93    the same amount of spaces.  Reuse the spaces of the previous call if
94    PREFIX is NULL.  Free the PREFIX and MESSAGE when done.  */
95 void
multiline_error(char * prefix,char * message)96 multiline_error (char *prefix, char *message)
97 {
98   if (prefix != NULL)
99     ++error_message_count;
100   multiline_warning (prefix, message);
101 }
102