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