utils.c revision 19370
11590Srgrimes/* General utility routines for the remote server for GDB.
21590Srgrimes   Copyright (C) 1986, 1989, 1993 Free Software Foundation, Inc.
31590Srgrimes
41590SrgrimesThis file is part of GDB.
51590Srgrimes
61590SrgrimesThis program is free software; you can redistribute it and/or modify
71590Srgrimesit under the terms of the GNU General Public License as published by
81590Srgrimesthe Free Software Foundation; either version 2 of the License, or
91590Srgrimes(at your option) any later version.
101590Srgrimes
111590SrgrimesThis program is distributed in the hope that it will be useful,
121590Srgrimesbut WITHOUT ANY WARRANTY; without even the implied warranty of
131590SrgrimesMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141590SrgrimesGNU General Public License for more details.
151590Srgrimes
161590SrgrimesYou should have received a copy of the GNU General Public License
171590Srgrimesalong with this program; if not, write to the Free Software
181590SrgrimesFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
191590Srgrimes
201590Srgrimes#include "server.h"
211590Srgrimes#include <stdio.h>
221590Srgrimes
231590Srgrimes/* Generally useful subroutines used throughout the program.  */
241590Srgrimes
251590Srgrimes/* Print the system error message for errno, and also mention STRING
261590Srgrimes   as the file name for which the error was encountered.
271590Srgrimes   Then return to command level.  */
281590Srgrimes
291590Srgrimesvoid
301590Srgrimesperror_with_name (string)
311590Srgrimes     char *string;
321590Srgrimes{
331590Srgrimes  extern int sys_nerr;
341590Srgrimes  extern char *sys_errlist[];
351590Srgrimes  extern int errno;
361590Srgrimes  char *err;
371590Srgrimes  char *combined;
381590Srgrimes
391590Srgrimes  if (errno < sys_nerr)
401590Srgrimes    err = sys_errlist[errno];
411590Srgrimes  else
421590Srgrimes    err = "unknown error";
431590Srgrimes
441590Srgrimes  combined = (char *) alloca (strlen (err) + strlen (string) + 3);
451590Srgrimes  strcpy (combined, string);
461590Srgrimes  strcat (combined, ": ");
471590Srgrimes  strcat (combined, err);
481590Srgrimes
491590Srgrimes  error ("%s.", combined);
501590Srgrimes}
511590Srgrimes
521590Srgrimes/* Print an error message and return to command level.
531590Srgrimes   STRING is the error message, used as a fprintf string,
541590Srgrimes   and ARG is passed as an argument to it.  */
551590Srgrimes
561590Srgrimes#ifdef ANSI_PROTOTYPES
571590SrgrimesNORETURN void
581590Srgrimeserror (char *string, ...)
591590Srgrimes#else
601590Srgrimesvoid
611590Srgrimeserror (va_alist)
621590Srgrimes     va_dcl
631590Srgrimes#endif
641590Srgrimes{
651590Srgrimes  extern jmp_buf toplevel;
661590Srgrimes  va_list args;
671590Srgrimes#ifdef ANSI_PROTOTYPES
681590Srgrimes  va_start (args, string);
691590Srgrimes#else
701590Srgrimes  va_start (args);
711590Srgrimes#endif
721590Srgrimes  fflush (stdout);
731590Srgrimes#ifdef ANSI_PROTOTYPES
741590Srgrimes  vfprintf (stderr, string, args);
751590Srgrimes#else
761590Srgrimes  {
771590Srgrimes    char *string1;
781590Srgrimes
791590Srgrimes    string1 = va_arg (args, char *);
801590Srgrimes    vfprintf (stderr, string1, args);
811590Srgrimes  }
821590Srgrimes#endif
831590Srgrimes  fprintf (stderr, "\n");
841590Srgrimes  longjmp(toplevel, 1);
851590Srgrimes}
861590Srgrimes
871590Srgrimes/* Print an error message and exit reporting failure.
881590Srgrimes   This is for a error that we cannot continue from.
891590Srgrimes   STRING and ARG are passed to fprintf.  */
901590Srgrimes
911590Srgrimes/* VARARGS */
921590SrgrimesNORETURN void
931590Srgrimes#ifdef ANSI_PROTOTYPES
941590Srgrimesfatal (char *string, ...)
951590Srgrimes#else
961590Srgrimesfatal (va_alist)
971590Srgrimes     va_dcl
981590Srgrimes#endif
991590Srgrimes{
1001590Srgrimes  va_list args;
1011590Srgrimes#ifdef ANSI_PROTOTYPES
1021590Srgrimes  va_start (args, string);
1031590Srgrimes#else
1041590Srgrimes  char *string;
1051590Srgrimes  va_start (args);
1061590Srgrimes  string = va_arg (args, char *);
1071590Srgrimes#endif
1081590Srgrimes  fprintf (stderr, "gdb: ");
1091590Srgrimes  vfprintf (stderr, string, args);
1101590Srgrimes  fprintf (stderr, "\n");
1111590Srgrimes  va_end (args);
1121590Srgrimes  exit (1);
1131590Srgrimes}
1141590Srgrimes