1 /* SCCS Id: @(#)macerrs.c 3.1 93/01/24 */
2 /* Copyright (c) Michael Hamel, 1991 */
3 /* NetHack may be freely redistributed. See license for details. */
4
5 #if defined(macintosh) && defined(__SC__) && !defined(__FAR_CODE__)
6 /* this needs to be resident always */
7 #pragma segment Main
8 #endif
9
10 #include "hack.h"
11 #include "macwin.h"
12 #if !TARGET_API_MAC_CARBON
13 #include <Dialogs.h>
14 #include <TextUtils.h>
15 #include <Resources.h>
16 #endif
17
18
error(const char * format,...)19 void error(const char *format,...)
20 {
21 Str255 buf;
22 va_list ap;
23
24 va_start(ap, format);
25 vsprintf((char *)buf, format, ap);
26 va_end(ap);
27
28 C2P((char *)buf, buf);
29 ParamText(buf, (StringPtr)"", (StringPtr)"", (StringPtr)"");
30 Alert(128, (ModalFilterUPP) NULL);
31 ExitToShell();
32 }
33
34
35 #if 0 /* Remainder of file is obsolete and will be removed */
36
37 #define stackDepth 1
38 #define errAlertID 129
39 #define stdIOErrID 1999
40
41 static Str255 gActivities[stackDepth] = {""};
42 static short gTopactivity = 0;
43
44 void showerror(char * errdesc, const char * errcomment)
45 {
46 short itemHit;
47 Str255 paserr,
48 pascomment;
49
50 SetCursor(&qd.arrow);
51 if (errcomment == nil) errcomment = "";
52 C2P (errcomment, pascomment);
53 C2P (errdesc, paserr);
54 ParamText(paserr,pascomment,gActivities[gTopactivity],(StringPtr)"");
55 itemHit = Alert(errAlertID, (ModalFilterUPP)nil);
56 }
57
58 Boolean itworked(short errcode)
59 /* Return TRUE if it worked, do an error message and return false if it didn't. Error
60 strings for native C errors are in STR#1999, Mac errs in STR 2000-errcode, e.g
61 2108 for not enough memory */
62
63 {
64 if (errcode != 0) {
65 short itemHit;
66 Str255 errdesc;
67 StringHandle strh;
68
69 errdesc[0] = '\0';
70 if (errcode > 0) GetIndString(errdesc,stdIOErrID,errcode); /* STDIO file rres, etc */
71 else {
72 strh = GetString(2000-errcode);
73 if (strh != (StringHandle) nil) {
74 memcpy(errdesc,*strh,256);
75 ReleaseResource((Handle)strh);
76 }
77 }
78 if (errdesc[0] == '\0') { /* No description found, just give the number */
79 sprintf((char *)&errdesc[1],"a %d error occurred",errcode);
80 errdesc[0] = strlen((char*)&errdesc[1]);
81 }
82 SetCursor(&qd.arrow);
83 ParamText(errdesc,(StringPtr)"",gActivities[gTopactivity],(StringPtr)"");
84 itemHit = Alert(errAlertID, (ModalFilterUPP)nil);
85 }
86 return(errcode==0);
87 }
88
89 void mustwork(short errcode)
90 /* For cases where we can't recover from the error by any means */
91 {
92 if (itworked(errcode)) ;
93 else ExitToShell();
94 }
95
96
97 #if defined(USE_STDARG) || defined(USE_VARARGS)
98 # ifdef USE_STDARG
99 static void vprogerror(const char *line, va_list the_args);
100 # else
101 static void vprogerror();
102 # endif
103
104 /* Macro substitute for error() */
105 void error VA_DECL(const char *, line)
106 VA_START(line);
107 VA_INIT(line, char *);
108 vprogerror(line, VA_ARGS);
109 VA_END();
110 }
111
112 # ifdef USE_STDARG
113 static void
114 vprogerror(const char *line, va_list the_args) {
115 # else
116 static void
117 vprogerror(line, the_args) const char *line; va_list the_args; {
118 # endif
119
120 #else /* USE_STDARG | USE_VARARG */
121
122 void
123 error VA_DECL(const char *, line)
124 #endif
125 /* Do NOT use VA_START and VA_END in here... see above */
126 char pbuf[BUFSZ];
127
128 if(index(line, '%')) {
129 Vsprintf(pbuf,line,VA_ARGS);
130 line = pbuf;
131 }
132 showerror("of an internal error",line);
133 }
134
135
136 void attemptingto(char * activity)
137 /* Say what we are trying to do for subsequent error-handling: will appear as x in an
138 alert in the form "Could not x because y" */
139 { C2P(activity,gActivities[gTopactivity]);
140 }
141
142 void comment(char *s, long n)
143 {
144 Str255 paserr;
145 short itemHit;
146
147 sprintf((char *)&paserr[1], "%s - %d",s,n);
148 paserr[0] = strlen ((char*)&paserr[1]);
149 ParamText(paserr,(StringPtr)"",(StringPtr)"",(StringPtr)"");
150 itemHit = Alert(128, (ModalFilterUPP)nil);
151 }
152
153 void pushattemptingto(char * activity)
154 /* Push a new description onto stack so we can pop later to previous state */
155 {
156 if (gTopactivity < stackDepth) {
157 gTopactivity++;
158 attemptingto(activity);
159 }
160 else error("activity stack overflow");
161 }
162
163 void popattempt(void)
164 /* Pop to previous state */
165 {
166 if (gTopactivity > 1) --gTopactivity;
167 else error("activity stack underflow");
168 }
169
170 #endif /* Obsolete */
171