119370Spst/* General utility routines for the remote server for GDB.
2130809Smarcel   Copyright 1986, 1989, 1993, 1995, 1996, 1997, 1999, 2000, 2002, 2003
398948Sobrien   Free Software Foundation, Inc.
419370Spst
598948Sobrien   This file is part of GDB.
619370Spst
798948Sobrien   This program is free software; you can redistribute it and/or modify
898948Sobrien   it under the terms of the GNU General Public License as published by
998948Sobrien   the Free Software Foundation; either version 2 of the License, or
1098948Sobrien   (at your option) any later version.
1119370Spst
1298948Sobrien   This program is distributed in the hope that it will be useful,
1398948Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1498948Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1598948Sobrien   GNU General Public License for more details.
1619370Spst
1798948Sobrien   You should have received a copy of the GNU General Public License
1898948Sobrien   along with this program; if not, write to the Free Software
1998948Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2098948Sobrien   Boston, MA 02111-1307, USA.  */
2119370Spst
2219370Spst#include "server.h"
2319370Spst#include <stdio.h>
2446289Sdfr#include <string.h>
2519370Spst
2619370Spst/* Generally useful subroutines used throughout the program.  */
2719370Spst
2819370Spst/* Print the system error message for errno, and also mention STRING
2919370Spst   as the file name for which the error was encountered.
3019370Spst   Then return to command level.  */
3119370Spst
3219370Spstvoid
3398948Sobrienperror_with_name (char *string)
3419370Spst{
3598948Sobrien#ifndef STDC_HEADERS
3698948Sobrien  extern int errno;
3798948Sobrien#endif
3824563Spst  const char *err;
3919370Spst  char *combined;
4019370Spst
41130809Smarcel  err = strerror (errno);
42130809Smarcel  if (err == NULL)
4319370Spst    err = "unknown error";
4419370Spst
4519370Spst  combined = (char *) alloca (strlen (err) + strlen (string) + 3);
4619370Spst  strcpy (combined, string);
4719370Spst  strcat (combined, ": ");
4819370Spst  strcat (combined, err);
4919370Spst
5019370Spst  error ("%s.", combined);
5119370Spst}
5219370Spst
5319370Spst/* Print an error message and return to command level.
5419370Spst   STRING is the error message, used as a fprintf string,
5519370Spst   and ARG is passed as an argument to it.  */
5619370Spst
57130809Smarcelvoid
5898948Sobrienerror (const char *string,...)
5919370Spst{
6019370Spst  extern jmp_buf toplevel;
6119370Spst  va_list args;
6219370Spst  va_start (args, string);
6319370Spst  fflush (stdout);
6419370Spst  vfprintf (stderr, string, args);
6519370Spst  fprintf (stderr, "\n");
6698948Sobrien  longjmp (toplevel, 1);
6719370Spst}
6819370Spst
6919370Spst/* Print an error message and exit reporting failure.
7019370Spst   This is for a error that we cannot continue from.
7119370Spst   STRING and ARG are passed to fprintf.  */
7219370Spst
7319370Spst/* VARARGS */
74130809Smarcelvoid
7598948Sobrienfatal (const char *string,...)
7619370Spst{
7719370Spst  va_list args;
7819370Spst  va_start (args, string);
7919370Spst  fprintf (stderr, "gdb: ");
8019370Spst  vfprintf (stderr, string, args);
8119370Spst  fprintf (stderr, "\n");
8219370Spst  va_end (args);
8319370Spst  exit (1);
8419370Spst}
8598948Sobrien
8698948Sobrien/* VARARGS */
8798948Sobrienvoid
8898948Sobrienwarning (const char *string,...)
8998948Sobrien{
9098948Sobrien  va_list args;
9198948Sobrien  va_start (args, string);
9298948Sobrien  fprintf (stderr, "gdb: ");
9398948Sobrien  vfprintf (stderr, string, args);
9498948Sobrien  fprintf (stderr, "\n");
9598948Sobrien  va_end (args);
9698948Sobrien}
97