1 /* misc.c:
2 *
3 * miscellaneous funcs
4 *
5 * jim frost 10.05.89
6 *
7 * Copyright 1989, 1990, 1991 Jim Frost.
8 * See included file "copyright.h" for complete copyright information.
9 */
10
11 #include "copyright.h"
12 #include "xloadimage.h"
13 #include "misc.h"
14 #ifdef VMS
15 #include "patchlevel."
16 #else
17 #include "patchlevel"
18 #endif
19 #include <signal.h>
20
21 extern int _Xdebug;
22 extern char *ProgramName;
23 extern Display *Disp;
24 extern int Scrn;
25 extern char *BuildDate;
26 extern char *BuildUser;
27 extern char *BuildSystem;
28
signalName(sig)29 static char *signalName(sig)
30 int sig;
31 { static char buf[32];
32
33 switch (sig) {
34 case SIGSEGV:
35 return("SEGV");
36 #ifdef SIGBUS
37 case SIGBUS:
38 return("BUS");
39 #endif
40 case SIGFPE:
41 return("FPE");
42 case SIGILL:
43 return("ILL");
44 default:
45 sprintf(buf, "Signal %d", sig);
46 return(buf);
47 }
48 }
49
memoryExhausted()50 void memoryExhausted()
51 {
52 fprintf(stderr,
53 "\n\nMemory has been exhausted; operation cannot continue (sorry).\n");
54 if (_Xdebug)
55 abort();
56 else
57 exit(1);
58 }
59
internalError(sig)60 void internalError(sig)
61 int sig;
62 { static int handling_error= 0;
63 int a, b;
64 Screen *screen;
65
66 switch(handling_error++) {
67 case 0:
68 printf("\n\n\
69 An internal error (%s) has occurred. If you would like to file a bug\n\
70 report, please send email to %s\n\
71 with a description of how you triggered the bug, the output of xloadimage\n\
72 before the failure, and the following information:\n\n", signalName(sig),
73 AUTHOR_EMAIL);
74 printf("Xloadimage Version %s.%s\n\n", VERSION, PATCHLEVEL);
75 if (BuildUser)
76 printf("Built by: %s\n", BuildUser);
77 if (BuildDate)
78 printf("Built on: %s\n", BuildDate);
79 printf("Build system: %s\n", BuildSystem);
80
81 if (Disp) {
82 screen= ScreenOfDisplay(Disp, Scrn);
83 printf("Server: %s Version %d\n", ServerVendor(Disp), VendorRelease(Disp));
84 printf("Depths and visuals supported:\n");
85 for (a= 0; a < screen->ndepths; a++) {
86 printf("%2d:", screen->depths[a].depth);
87 for (b= 0; b < screen->depths[a].nvisuals; b++)
88 printf(" %s", nameOfVisualClass(screen->depths[a].visuals[b].class));
89 printf("\n");
90 }
91 }
92 else
93 printf("[No information on server; error occurred before connection]\n");
94 break;
95 case 1:
96 fprintf(stderr, "\n\n\
97 An internal error has occurred within the internal error handler. No more\n\
98 information about the error is available, sorry.\n");
99 exit(1);
100 break;
101 }
102 if (_Xdebug) /* dump core if -debug is on */
103 abort();
104 exit(1);
105 }
106
version()107 void version()
108 {
109 printf("Xloadimage version %s.%s by Jim Frost.\n",
110 VERSION, PATCHLEVEL);
111 printf("Built on %s\n", BuildSystem);
112 printf("Please send email to %s for\npraise or bug reports.\n",
113 AUTHOR_EMAIL);
114 }
115
usageHelp()116 void usageHelp()
117 {
118 printf("\nUsage: %s [global options] {[image options] image_name ...}\n\n",
119 tail(ProgramName));
120 printf("\
121 Type `%s -help [option ...]' for information on a particular option, or\n\
122 `%s -help' to enter the interactive help facility.\n",
123 tail(ProgramName), tail(ProgramName));
124 exit(1);
125 }
126
usage()127 void usage()
128 {
129 version();
130 usageHelp();
131 /* NOTREACHED */
132 }
133
tail(path)134 char *tail(path)
135 char *path;
136 { int s;
137 char *t;
138
139 t= path;
140 for (s= 0; *(path + s) != '\0'; s++)
141 if (*(path + s) == '/')
142 t= path + s + 1;
143 return(t);
144 }
145
146 /* simple error handler. this provides us with some kind of error recovery.
147 */
148
errorHandler(disp,error)149 int errorHandler(disp, error)
150 Display *disp;
151 XErrorEvent *error;
152 { char errortext[BUFSIZ];
153
154 XGetErrorText(disp, error->error_code, errortext, BUFSIZ);
155 fprintf(stderr, "xloadimage: X Error: %s on 0x%lx\n",
156 errortext, error->resourceid);
157 if (_Xdebug) /* if -debug mode is enabled, dump a core when we hit this */
158 abort();
159 else
160 return(0);
161 }
162
163 /*
164 findstr - public-domain implementation of standard C 'strstr' library
165 function (renamed and slightly modified to avoid naming
166 conflicts - jimf)
167
168 last edit: 02-Sep-1990 D A Gwyn
169
170 This is an original implementation based on an idea by D M Sunday,
171 essentially the "quick search" algorithm described in CACM V33 N8.
172 Unlike Sunday's implementation, this one does not wander past the
173 ends of the strings (which can cause malfunctions under certain
174 circumstances), nor does it require the length of the searched
175 text to be determined in advance. There are numerous other subtle
176 improvements too. The code is intended to be fully portable, but in
177 environments that do not conform to the C standard, you should check
178 the sections below marked "configure as required". There are also
179 a few compilation options, as follows:
180 */
181
182 #define BYTE_MAX 255
183
184 #define EOS '\0' /* C string terminator */
185
186 char * /* returns -> leftmost occurrence,
187 or null pointer if not present */
findstr(s1,s2)188 findstr( s1, s2 )
189 char *s1; /* -> string to be searched */
190 char *s2; /* -> search-pattern string */
191 {
192 register byte *t; /* -> text character being tested */
193 register byte *p; /* -> pattern char being tested */
194 register byte *tx; /* -> possible start of match */
195 register unsigned int m; /* length of pattern */
196 register byte *top; /* -> high water mark in text */
197 unsigned int shift[BYTE_MAX + 1]; /* pattern shift table */
198
199 if ( s1 == NULL || s2 == NULL )
200 return NULL; /* certainly, no match is found! */
201
202 /* Precompute shift intervals based on the pattern;
203 the length of the pattern is determined as a side effect: */
204
205 bzero(&shift[1], (BYTE_MAX * sizeof(unsigned int)));
206
207 /* Note: shift[0] is undefined at this point (fixed later). */
208
209 for ( m = 1, p = (byte *)s2; *p != EOS; ++m, ++p )
210 shift[(byte)*p] = m;
211
212 {
213 register byte c;
214
215 c = BYTE_MAX;
216 do
217 shift[c] = m - shift[c];
218 while ( --c > 0 );
219 /* Note: shift[0] is still undefined at this point. */
220 }
221
222 shift[0] = --m; /* shift[EOS]; important details! */
223
224 /* Try to find the pattern in the text string: */
225
226 for ( top = tx = (byte *)s1; ; tx += shift[*(top = t)] ) {
227 for ( t = tx, p = (byte *)s2; ; ++t, ++p ) {
228 if ( *p == EOS ) /* entire pattern matched */
229 return (char *)tx;
230 if ( *p != *t )
231 break;
232 }
233 if ( t < top ) /* idea due to ado@elsie.nci.nih.gov */
234 t = top; /* already scanned this far for EOS */
235 do {
236 if ( *t == EOS )
237 return NULL; /* no match */
238 } while ( ++t - tx != m ); /* < */
239 }
240 }
241