ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libukcprog/errf.c
Revision: 1.1
Committed: Fri Mar 8 14:37:29 2002 UTC (22 years, 8 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: IHOST_1_5_3, IHOST_1_5_2, IHOST_1_5_1, IHOST_1_5, IHOST_1_0_RC1
Log Message:
I'm not usually up for putting third party sources in here, but in this
case I'll make an exception. This is ukcprog, a set of useful C functions
which the ihost plugins Pete's writing uses. It's got a pretty free license
too. I've munged the Makefile around, as all it needs to do now is make the
library, not install anything. The idea is to statically compile the other
programs against this library, making the final binary independent of this
code etc.

File Contents

# Content
1 /* errf.c -- formatted error messages */
2
3 /* Copyright 1992 Godfrey Paul, University of Kent at Canterbury.
4 *
5 * You can do what you like with this source code as long as
6 * you don't try to make money out of it and you include an
7 * unaltered copy of this message (including the copyright).
8 */
9
10 char ukcprog_errf_sccsid[] = "$Id: errf.c,v 1.16 1995/11/26 16:05:56 mtr Exp $ UKC";
11
12 #ifdef __STDC__
13 #include <stdarg.h>
14 #else
15 #include <varargs.h>
16 #endif
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "ukcprog.h"
23
24 static const char *munge_progname PROTO((const char *));
25 static void default_ofunc PROTO((const char *str));
26
27 static errf_ofunc_t User_errf_ofunc = default_ofunc;
28 static const char *Output_prefix = "";
29 static char *Progname = NULL;
30
31 static void
32 default_ofunc(str)
33 const char *str;
34 {
35 fprintf(stderr, "%s%s\n", Output_prefix, str);
36 fflush(stderr);
37 }
38
39 errf_ofunc_t
40 errf_set_ofunc(func)
41 errf_ofunc_t func;
42 {
43 errf_ofunc_t old;
44
45 if (func == NULL)
46 func = default_ofunc;
47
48 old = User_errf_ofunc;
49 User_errf_ofunc = func;
50
51 return old;
52 }
53
54
55 /* errf_set_prefix()
56 * Sets the string that errf() prefixes to messages.
57 * Returns the old one.
58 */
59 const char *
60 errf_set_prefix(prefix)
61 const char *prefix;
62 {
63 const char *old;
64
65 old = Output_prefix;
66 Output_prefix = prefix;
67
68 return old;
69 }
70
71
72 /* errf_get_prefix()
73 * Return the current prefix.
74 */
75 const char *
76 errf_get_prefix()
77 {
78 return Output_prefix;
79 }
80
81
82 static const char *
83 munge_progname(progname)
84 const char *progname;
85 {
86 #ifdef VMS
87 const char *name, *dot;
88 char *s;
89 int i;
90
91 if ((name = strrchr(progname, ']')) == NULL)
92 name = progname;
93 else
94 ++name;
95
96 if ((dot = strchr(name, '.')) == NULL)
97 dot = name + strlen(name);
98
99 s = strf("%.*s", dot - name, name);
100
101 for (i = 0; i < dot - name; ++i)
102 s[i] = toupper(s[i]);
103
104 if (Progname != NULL)
105 free(Progname);
106
107 Progname = s;
108
109 return "%%%s, ";
110 #else
111 #ifdef MSDOS
112 const char *name;
113
114 if ((name = strrchr(progname, '\\')) == NULL)
115 name = progname;
116
117 else
118 ++name;
119
120 if (Progname != NULL)
121 free(Progname);
122
123 Progname = strsave(name);
124
125 return "%s: ";
126 #else
127 /* Everything is assumed to be some variant of Unix.
128 */
129 const char *name;
130
131 if ((name = strrchr(progname, '/')) == NULL)
132 name = progname;
133
134 else
135 ++name;
136
137 if (Progname != NULL)
138 free(Progname);
139
140 Progname = strsave(name);
141
142 return "%s: ";
143 #endif /* !MSDOS */
144 #endif /* !VMS */
145 }
146
147
148 /* errf_set_progname()
149 * Convenience routine to set the errf prefix to include the progname.
150 */
151 void
152 errf_set_progname(name)
153 const char *name;
154 {
155 const char *fmt;
156
157 fmt = munge_progname(name);
158 errf_set_prefix(strf(fmt, Progname));
159 }
160
161
162 /* errf_get_progname()
163 */
164 const char *
165 errf_get_progname()
166 {
167 return Progname;
168 }
169
170
171 #ifdef __STDC__
172 void
173 errf(const char *fmt, ...)
174 {
175
176 #else /* !__STDC__ */
177 void
178 errf(va_alist)
179 va_dcl
180 {
181 char *fmt;
182 #endif /* !__STDC__ */
183 va_list args;
184 char buffer[100];
185 char *s;
186
187 #ifdef __STDC__
188 va_start(args, fmt);
189 #else
190 va_start(args);
191 fmt = va_arg(args, char *);
192 #endif
193
194 s = formf(buffer, sizeof(buffer), fmt, args);
195
196 va_end(args);
197
198 (*User_errf_ofunc)(s);
199
200 if (s != buffer) /* must have been obtained from malloc */
201 free(s);
202 }
203
204
205 /* errf_usage()
206 * Output a usage string prefixed by the program name and exit(1).
207 */
208 void
209 errf_usage(usage)
210 const char *usage;
211 {
212 if (User_errf_ofunc != default_ofunc)
213 (*User_errf_ofunc)(strf("Usage: %s %s", Progname, usage));
214 else
215 fprintf(stderr, "Usage: %s %s\n", Progname, usage);
216 exit(1);
217 }