1/* -*- mode: C; coding: macintosh; -*- */
2#ifdef __APPLE_CC__ //  das 111200 compiling with gcc on OS X
3#include <Carbon/Carbon.h>
4#else
5#include <ConditionalMacros.h>
6#include <MacTypes.h>
7#include <CodeFragments.h>
8#endif
9
10#include "tclMacOSError.h"
11
12#ifndef _TCL
13#include <tcl.h>
14#endif
15#include <string.h>
16#include <stdio.h>
17//  das 130700
18//  FindErrorLib only on PPC non-carbon
19#if __POWERPC__ && !TARGET_API_MAC_CARBON
20#define USE_FIND_ERROR_LIB 1
21#include <FindError.h>
22#else
23#define USE_FIND_ERROR_LIB 0
24#endif
25
26/*
27 *----------------------------------------------------------------------
28 *
29 * Tcl_MacOSError --
30 *
31 *	This procedure is typically called after MacOS ToolBox calls return
32 *	errors.  It stores machine-readable information about the error in
33 *	$errorCode and returns an information string for the caller's use.
34 *	It's a bit like Tcl_PosixError().
35 *
36 *  To get the most bang for your buck, install FindErrorLib.
37 *
38 * Results:
39 *	The return value is a human-readable string describing the error.
40 *
41 * Side effects:
42 *	The global variable $errorCode is reset to:
43 *
44 *		 {theErrorName theErrorNumber theErrorDescription}
45 *
46 *  or at least as much as is available.
47 *
48 *----------------------------------------------------------------------
49 */
50char *
51Tcl_MacOSError(Tcl_Interp  *interp,			/* to assign global error code */
52			   OSStatus    err)				/* error code to interpret */
53{
54	char        theErrorNumber[132];        /* text representation of 'err' */
55	char		theErrorName[132];          /* from FindErrorLib */
56	char 		theErrorDescription[132];   /* from FindErrorLib */
57	static char	theErrorString[132];		/* static for return */
58
59	theErrorDescription[0] = 0;  // (bd 2003-10-10)
60
61//  FindErrorLib exists only for PPC
62#if USE_FIND_ERROR_LIB
63	/* Try to use FindErrorLib to interpret result */
64	if ((((long) GetFindErrorLibDataInfo) != kUnresolvedCFragSymbolAddress)
65	&&  (LookupError(err, theErrorName, theErrorDescription))) {
66		/* error was found by FindErrorLib */
67		if (strlen(theErrorDescription) > 0) {
68			strcpy(theErrorString, theErrorDescription);
69		} else {
70			/*
71			 * No description was found in database.
72			 * Make as much of an error string as we can.
73			 */
74			snprintf(theErrorString, 132, "OSErr %d, %s", err, theErrorName);
75		}
76	} else
77#endif
78	{
79
80		/*
81		 * FindErrorLib is not installed or error wasn't found in database.
82		 * Make a generic error string.
83		 */
84		strcpy(theErrorName, "OSErr");
85		snprintf(theErrorString, 132, "OSErr %ld", (long)err);
86	}
87
88	/* string representation of the number */
89	snprintf(theErrorNumber, 132, "%ld", (long)err);
90
91	if (interp) {
92		/* Set up Tcl error code with all info */
93		Tcl_SetErrorCode(interp,
94						 theErrorName,
95						 theErrorNumber,
96						 theErrorDescription,
97						 (char *) NULL);
98	}
99
100	/* Return the error description for output in the Tcl result */
101    return theErrorString;
102}
103
104