xref: /netbsd-src/external/gpl2/gettext/dist/gnulib-local/lib/xerror.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /* Multiline error-reporting functions.
2    Copyright (C) 2001-2003, 2006 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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18 
19 
20 #include <config.h>
21 
22 /* Specification.  */
23 #include "xerror.h"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "error.h"
30 #include "progname.h"
31 #include "error-progname.h"
32 #include "mbswidth.h"
33 
34 /* Emit a multiline warning to stderr, consisting of MESSAGE, with the
35    first line prefixed with PREFIX and the remaining lines prefixed with
36    the same amount of spaces.  Reuse the spaces of the previous call if
37    PREFIX is NULL.  Free the PREFIX and MESSAGE when done.  */
38 void
multiline_warning(char * prefix,char * message)39 multiline_warning (char *prefix, char *message)
40 {
41   static int width;
42   const char *cp;
43   int i;
44 
45   fflush (stdout);
46 
47   cp = message;
48 
49   if (prefix != NULL)
50     {
51       width = 0;
52       if (error_with_progname)
53 	{
54 	  fprintf (stderr, "%s: ", program_name);
55 	  width += mbswidth (program_name, 0) + 2;
56 	}
57       fputs (prefix, stderr);
58       width += mbswidth (prefix, 0);
59       free (prefix);
60       goto after_indent;
61     }
62 
63   for (;;)
64     {
65       const char *np;
66 
67       for (i = width; i > 0; i--)
68 	putc (' ', stderr);
69 
70     after_indent:
71       np = strchr (cp, '\n');
72 
73       if (np == NULL || np[1] == '\0')
74 	{
75 	  fputs (cp, stderr);
76 	  break;
77 	}
78 
79       np++;
80       fwrite (cp, 1, np - cp, stderr);
81       cp = np;
82     }
83 
84   free (message);
85 }
86 
87 /* Emit a multiline error to stderr, consisting of MESSAGE, with the
88    first line prefixed with PREFIX and the remaining lines prefixed with
89    the same amount of spaces.  Reuse the spaces of the previous call if
90    PREFIX is NULL.  Free the PREFIX and MESSAGE when done.  */
91 void
multiline_error(char * prefix,char * message)92 multiline_error (char *prefix, char *message)
93 {
94   if (prefix != NULL)
95     ++error_message_count;
96   multiline_warning (prefix, message);
97 }
98