View of xos/usr/xsh/error.c


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
#include "error.h"
 
#include "builtins/builtins.h"
#include "vars.h"
 
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
 
static void print_error_message_list(int print_line_number, int errnum, const char *prefix, const char *format, va_list ap)
{
  fflush(stdout);
  fprintf(stderr, "%s: ", script_name ? : program_invocation_name);
  if (print_line_number && print_line_number_on_error)
    fprintf(stderr, "line %d: ", line_number);
  if (current_builtin)
    fprintf(stderr, "%s: ", current_builtin->name);
  if (prefix)
    fprintf(stderr, "%s: ", prefix);
  vfprintf(stderr, format, ap);
  if (errnum)
    fprintf(stderr, ": %s", strerror(errnum));
  fputc('\n', stderr);
}
 
static void print_error_message_var(int print_line_number, int errnum, const char *prefix, const char *format, ...)
{
  va_list ap;
 
  va_start(ap, format);
  print_error_message_list(print_line_number, errnum, prefix, format, ap);
  va_end(ap);
}
 
/* Erreurs techniques */
 
void sys_error(int errnum, const char *format, ...)
{
  va_list ap;
 
  va_start(ap, format);
  print_error_message_list(0, errnum, NULL, format, ap);
  va_end(ap);
}
 
void fatal_error(const char *format, ...)
{
  va_list ap;
 
  va_start(ap, format);
  print_error_message_list(0, 0, NULL, format, ap);
  va_end(ap);
  exit(EXIT_FAILURE);
}
 
/* Erreurs fonctionnelles */
 
void generic_error(const char *format, ...)
{
  va_list ap;
 
  va_start(ap, format);
  print_error_message_list(1, 0, NULL, format, ap);
  va_end(ap);
}
 
void syntax_error(const char *format, ...)
{
  va_list ap;
 
  va_start(ap, format);
  print_error_message_list(1, 0, "Syntax error", format, ap);
  va_end(ap);
}
 
void parse_error(const char *token)
{
  print_error_message_var(1, 0, NULL, "Parse error near token `%s'", token);
}
 
void file_error(int errnum, const char *path)
{
  print_error_message_var(1, errnum, NULL, "%s", path);
}
 
void process_error(int errnum, pid_t pid)
{
  print_error_message_var(1, errnum, NULL, "%d", pid);
}
 
void invalid_option_error(int opt)
{
  print_error_message_var(1, 0, NULL, "invalid option -- %c", opt);
}