~ubuntu-branches/ubuntu/saucy/rabbitsign/saucy

« back to all changes in this revision

Viewing changes to src/error.c

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2009-10-21 23:02:36 UTC
  • Revision ID: james.westby@ubuntu.com-20091021230236-y2gj8x2vu4fb6xqo
Tags: upstream-2.1+dmca1
ImportĀ upstreamĀ versionĀ 2.1+dmca1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * RabbitSign - Tools for signing TI graphing calculator software
 
3
 * Copyright (C) 2009 Benjamin Moody
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License as
 
7
 * published by the Free Software Foundation; either version 3 of the
 
8
 * License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * 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
#ifdef HAVE_CONFIG_H
 
20
# include <config.h>
 
21
#endif
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdarg.h>
 
25
 
 
26
#ifdef HAVE_STRING_H
 
27
# include <string.h>
 
28
#else
 
29
# ifdef HAVE_STRINGS_H
 
30
#  include <strings.h>
 
31
# endif
 
32
#endif
 
33
 
 
34
#include "rabbitsign.h"
 
35
#include "internal.h"
 
36
 
 
37
static const char* progname;
 
38
static int verbose;
 
39
static RSMessageFunc errorfunc, messagefunc;
 
40
static void *errorfuncdata, *messagefuncdata;
 
41
 
 
42
void rs_set_progname(s)
 
43
     const char* s;
 
44
{
 
45
  progname = s;
 
46
}
 
47
 
 
48
void rs_set_verbose(v)
 
49
     int v;
 
50
{
 
51
  verbose = v;
 
52
}
 
53
 
 
54
void rs_set_error_func(RSMessageFunc func, void* data)
 
55
{
 
56
  errorfunc = func;
 
57
  errorfuncdata = data;
 
58
}
 
59
 
 
60
void rs_set_message_func(RSMessageFunc func, void* data)
 
61
{
 
62
  messagefunc = func;
 
63
  messagefuncdata = data;
 
64
}
 
65
 
 
66
static void print_message(const RSKey* key, const RSProgram* prgm,
 
67
                          const char* msg)
 
68
{
 
69
  if (prgm && prgm->filename)
 
70
    fprintf(stderr, "%s: ", prgm->filename);
 
71
  else if (key && key->filename)
 
72
    fprintf(stderr, "%s: ", key->filename);
 
73
  else if (progname)
 
74
    fprintf(stderr, "%s: ", progname);
 
75
  fputs(msg, stderr);
 
76
  fputc('\n', stderr);
 
77
}
 
78
 
 
79
/* Display a critical error */
 
80
void rs_error(const RSKey* key, const RSProgram* prgm, const char* fmt, ...)
 
81
{
 
82
  char msg[512];
 
83
  va_list ap;
 
84
 
 
85
  va_start(ap, fmt);
 
86
  strcpy(msg, "error: ");
 
87
  rs_vsnprintf(msg + 7, sizeof(msg) - 7, fmt, ap);
 
88
  va_end(ap);
 
89
 
 
90
  if (errorfunc)
 
91
    (*errorfunc)(key, prgm, msg, errorfuncdata);
 
92
  else
 
93
    print_message(key, prgm, msg);
 
94
}
 
95
 
 
96
/* Display a warning message */
 
97
void rs_warning(const RSKey* key, const RSProgram* prgm, const char* fmt, ...)
 
98
{
 
99
  char msg[512];
 
100
  va_list ap;
 
101
 
 
102
  va_start(ap, fmt);
 
103
  strcpy(msg, "warning: ");
 
104
  rs_vsnprintf(msg + 9, sizeof(msg) - 9, fmt, ap);
 
105
  va_end(ap);
 
106
 
 
107
  if (errorfunc)
 
108
    (*errorfunc)(key, prgm, msg, errorfuncdata);
 
109
  else
 
110
    print_message(key, prgm, msg);
 
111
}
 
112
 
 
113
/* Display an informative message */
 
114
void rs_message(int level, const RSKey* key, const RSProgram* prgm,
 
115
                const char* fmt, ...)
 
116
{
 
117
  char msg[512];
 
118
  va_list ap;
 
119
 
 
120
  if (level > verbose)
 
121
    return;
 
122
 
 
123
  va_start(ap, fmt);
 
124
  rs_vsnprintf(msg, sizeof(msg), fmt, ap);
 
125
  va_end(ap);
 
126
 
 
127
  if (messagefunc)
 
128
    (*messagefunc)(key, prgm, msg, messagefuncdata);
 
129
  else
 
130
    print_message(key, prgm, msg);
 
131
}