NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / sys / mac / macerrs.c
blobb501bbbd78d642bbd7c31c0ba22dbfc4570965d3
1 /* aNetHack 0.0.1 macerrs.c $ANH-Date: 1432512797 2015/05/25 00:13:17 $ $ANH-Branch: master $:$ANH-Revision: 1.10 $ */
2 /* Copyright (c) Michael Hamel, 1991 */
3 /* aNetHack may be freely redistributed. See license for details. */
5 #if defined(macintosh) && defined(__SC__) && !defined(__FAR_CODE__)
6 /* this needs to be resident always */
7 #pragma segment Main
8 #endif
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
18 void
19 error(const char *format, ...)
21 Str255 buf;
22 va_list ap;
24 va_start(ap, format);
25 vsprintf((char *) buf, format, ap);
26 va_end(ap);
28 C2P((char *) buf, buf);
29 ParamText(buf, (StringPtr) "", (StringPtr) "", (StringPtr) "");
30 Alert(128, (ModalFilterUPP) NULL);
31 ExitToShell();
34 #if 0 /* Remainder of file is obsolete and will be removed */
36 #define stackDepth 1
37 #define errAlertID 129
38 #define stdIOErrID 1999
40 static Str255 gActivities[stackDepth] = {""};
41 static short gTopactivity = 0;
43 void showerror(char * errdesc, const char * errcomment)
45 short itemHit;
46 Str255 paserr,
47 pascomment;
49 SetCursor(&qd.arrow);
50 if (errcomment == nil) errcomment = "";
51 C2P (errcomment, pascomment);
52 C2P (errdesc, paserr);
53 ParamText(paserr,pascomment,gActivities[gTopactivity],(StringPtr)"");
54 itemHit = Alert(errAlertID, (ModalFilterUPP)nil);
57 Boolean itworked(short errcode)
58 /* Return TRUE if it worked, do an error message and return false if it didn't. Error
59 strings for native C errors are in STR#1999, Mac errs in STR 2000-errcode, e.g
60 2108 for not enough memory */
63 if (errcode != 0) {
64 short itemHit;
65 Str255 errdesc;
66 StringHandle strh;
68 errdesc[0] = '\0';
69 if (errcode > 0) GetIndString(errdesc,stdIOErrID,errcode); /* STDIO file rres, etc */
70 else {
71 strh = GetString(2000-errcode);
72 if (strh != (StringHandle) nil) {
73 memcpy(errdesc,*strh,256);
74 ReleaseResource((Handle)strh);
77 if (errdesc[0] == '\0') { /* No description found, just give the number */
78 sprintf((char *)&errdesc[1],"a %d error occurred",errcode);
79 errdesc[0] = strlen((char*)&errdesc[1]);
81 SetCursor(&qd.arrow);
82 ParamText(errdesc,(StringPtr)"",gActivities[gTopactivity],(StringPtr)"");
83 itemHit = Alert(errAlertID, (ModalFilterUPP)nil);
85 return(errcode==0);
88 void mustwork(short errcode)
89 /* For cases where we can't recover from the error by any means */
91 if (itworked(errcode)) ;
92 else ExitToShell();
95 #if defined(USE_STDARG) || defined(USE_VARARGS)
96 #ifdef USE_STDARG
97 static void vprogerror(const char *line, va_list the_args);
98 #else
99 static void vprogerror();
100 #endif
102 /* Macro substitute for error() */
103 void error VA_DECL(const char *, line)
105 VA_START(line);
106 VA_INIT(line, char *);
107 vprogerror(line, VA_ARGS);
108 VA_END();
111 #ifdef USE_STDARG
112 static void
113 vprogerror(const char *line, va_list the_args)
114 #else
115 static void
116 vprogerror(line, the_args) const char *line; va_list the_args;
117 #endif
119 #else /* USE_STDARG | USE_VARARG */
121 void
122 error VA_DECL(const char *, line)
123 #endif
124 { /* opening brace for vprogerror(), nested block for USE_OLDARG error() */
125 char pbuf[BUFSZ];
127 if(index(line, '%')) {
128 Vsprintf(pbuf,line,VA_ARGS);
129 line = pbuf;
131 showerror("of an internal error",line);
133 #if !(defined(USE_STDARG) || defined(USE_VARARGS))
134 VA_END(); /* provides closing brace for USE_OLDARGS's nested block */
135 #endif
139 void attemptingto(char * activity)
140 /* Say what we are trying to do for subsequent error-handling: will appear as x in an
141 alert in the form "Could not x because y" */
142 { C2P(activity,gActivities[gTopactivity]);
145 void comment(char *s, long n)
147 Str255 paserr;
148 short itemHit;
150 sprintf((char *)&paserr[1], "%s - %d",s,n);
151 paserr[0] = strlen ((char*)&paserr[1]);
152 ParamText(paserr,(StringPtr)"",(StringPtr)"",(StringPtr)"");
153 itemHit = Alert(128, (ModalFilterUPP)nil);
156 void pushattemptingto(char * activity)
157 /* Push a new description onto stack so we can pop later to previous state */
159 if (gTopactivity < stackDepth) {
160 gTopactivity++;
161 attemptingto(activity);
163 else error("activity stack overflow");
166 void popattempt(void)
167 /* Pop to previous state */
169 if (gTopactivity > 1) --gTopactivity;
170 else error("activity stack underflow");
173 #endif /* Obsolete */