~ubuntu-branches/ubuntu/natty/glbsp/natty

« back to all changes in this revision

Viewing changes to src/system.c

  • Committer: Bazaar Package Importer
  • Author(s): Darren Salt
  • Date: 2008-01-30 13:33:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080130133349-kgojg33vyiu8xbvp
Tags: 2.24-1
* New upstream release.
* Bumped the lib soname and the library package name due to one silly
  little binary incompatibility caused by changes in an exported struct.
  (Safe; nothing else currently in the archive has ever used libglbsp2.)
* Removed my patches since they're all applied upstream.
* Updated the list of documentation files.
* Build-time changes:
  - Switched from dh_movefiles to dh_install.
  - Updated my makefile to cope with upstream changes.
  - Corrected for debian-rules-ignores-make-clean-error.
  - Corrected for substvar-source-version-is-deprecated.
  - Link libglbsp, rather than glbsp, with libm and libz.
* Fixed shlibdeps. (Closes: #460387)
* Bumped standards version to 3.7.3 (no other changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//------------------------------------------------------------------------
 
2
// SYSTEM : System specific code
 
3
//------------------------------------------------------------------------
 
4
//
 
5
//  GL-Friendly Node Builder (C) 2000-2007 Andrew Apted
 
6
//
 
7
//  Based on 'BSP 2.3' by Colin Reed, Lee Killough and others.
 
8
//
 
9
//  This program is free software; you can redistribute it and/or
 
10
//  modify it under the terms of the GNU General Public License
 
11
//  as published by the Free Software Foundation; either version 2
 
12
//  of the License, or (at your option) any later version.
 
13
//
 
14
//  This program is distributed in the hope that it will be useful,
 
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
//  GNU General Public License for more details.
 
18
//
 
19
//------------------------------------------------------------------------
 
20
 
 
21
#include "system.h"
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <stdarg.h>
 
27
#include <ctype.h>
 
28
#include <math.h>
 
29
#include <limits.h>
 
30
#include <assert.h>
 
31
 
 
32
 
 
33
#define DEBUG_ENABLED   0
 
34
 
 
35
#define DEBUGGING_FILE  "gb_debug.txt"
 
36
 
 
37
#define DEBUG_ENDIAN  0
 
38
 
 
39
static int cpu_big_endian = 0;
 
40
 
 
41
 
 
42
#define SYS_MSG_BUFLEN  4000
 
43
 
 
44
static char message_buf[SYS_MSG_BUFLEN];
 
45
 
 
46
#if DEBUG_ENABLED
 
47
static FILE *debug_fp = NULL;
 
48
#endif
 
49
 
 
50
 
 
51
//
 
52
// FatalError
 
53
//
 
54
void FatalError(const char *str, ...)
 
55
{
 
56
  va_list args;
 
57
 
 
58
  va_start(args, str);
 
59
  vsnprintf(message_buf, sizeof(message_buf), str, args);
 
60
  va_end(args);
 
61
 
 
62
  (* cur_funcs->fatal_error)("\nError: *** %s ***\n\n", message_buf);
 
63
}
 
64
 
 
65
//
 
66
// InternalError
 
67
//
 
68
void InternalError(const char *str, ...)
 
69
{
 
70
  va_list args;
 
71
 
 
72
  va_start(args, str);
 
73
  vsnprintf(message_buf, sizeof(message_buf), str, args);
 
74
  va_end(args);
 
75
 
 
76
  (* cur_funcs->fatal_error)("\nINTERNAL ERROR: *** %s ***\n\n", message_buf);
 
77
}
 
78
 
 
79
//
 
80
// PrintMsg
 
81
//
 
82
void PrintMsg(const char *str, ...)
 
83
{
 
84
  va_list args;
 
85
 
 
86
  va_start(args, str);
 
87
  vsnprintf(message_buf, sizeof(message_buf), str, args);
 
88
  va_end(args);
 
89
 
 
90
  (* cur_funcs->print_msg)("%s", message_buf);
 
91
 
 
92
#if DEBUG_ENABLED
 
93
  PrintDebug(">>> %s", message_buf);
 
94
#endif
 
95
}
 
96
 
 
97
//
 
98
// PrintVerbose
 
99
//
 
100
void PrintVerbose(const char *str, ...)
 
101
{
 
102
  va_list args;
 
103
 
 
104
  va_start(args, str);
 
105
  vsnprintf(message_buf, sizeof(message_buf), str, args);
 
106
  va_end(args);
 
107
 
 
108
  if (! cur_info->quiet)
 
109
    (* cur_funcs->print_msg)("%s", message_buf);
 
110
 
 
111
#if DEBUG_ENABLED
 
112
  PrintDebug(">>> %s", message_buf);
 
113
#endif
 
114
}
 
115
 
 
116
//
 
117
// PrintWarn
 
118
//
 
119
void PrintWarn(const char *str, ...)
 
120
{
 
121
  va_list args;
 
122
 
 
123
  va_start(args, str);
 
124
  vsnprintf(message_buf, sizeof(message_buf), str, args);
 
125
  va_end(args);
 
126
 
 
127
  (* cur_funcs->print_msg)("Warning: %s", message_buf);
 
128
 
 
129
  cur_comms->total_big_warn++;
 
130
 
 
131
#if DEBUG_ENABLED
 
132
  PrintDebug("Warning: %s", message_buf);
 
133
#endif
 
134
}
 
135
 
 
136
//
 
137
// PrintMiniWarn
 
138
//
 
139
void PrintMiniWarn(const char *str, ...)
 
140
{
 
141
  va_list args;
 
142
 
 
143
  va_start(args, str);
 
144
  vsnprintf(message_buf, sizeof(message_buf), str, args);
 
145
  va_end(args);
 
146
 
 
147
  if (cur_info->mini_warnings)
 
148
    (* cur_funcs->print_msg)("Warning: %s", message_buf);
 
149
 
 
150
  cur_comms->total_small_warn++;
 
151
 
 
152
#if DEBUG_ENABLED
 
153
  PrintDebug("MiniWarn: %s", message_buf);
 
154
#endif
 
155
}
 
156
 
 
157
//
 
158
// SetErrorMsg
 
159
//
 
160
void SetErrorMsg(const char *str, ...)
 
161
{
 
162
  va_list args;
 
163
 
 
164
  va_start(args, str);
 
165
  vsnprintf(message_buf, sizeof(message_buf), str, args);
 
166
  va_end(args);
 
167
 
 
168
  GlbspFree(cur_comms->message);
 
169
 
 
170
  cur_comms->message = GlbspStrDup(message_buf);
 
171
}
 
172
 
 
173
 
 
174
/* -------- debugging code ----------------------------- */
 
175
 
 
176
//
 
177
// InitDebug
 
178
//
 
179
void InitDebug(void)
 
180
{
 
181
#if DEBUG_ENABLED
 
182
  debug_fp = fopen(DEBUGGING_FILE, "w");
 
183
 
 
184
  if (! debug_fp)
 
185
    PrintWarn("Unable to open DEBUG FILE: %s\n", DEBUGGING_FILE);
 
186
 
 
187
  PrintDebug("=== START OF DEBUG FILE ===\n");
 
188
#endif
 
189
}
 
190
 
 
191
//
 
192
// TermDebug
 
193
//
 
194
void TermDebug(void)
 
195
{
 
196
#if DEBUG_ENABLED
 
197
  if (debug_fp)
 
198
  {
 
199
    PrintDebug("=== END OF DEBUG FILE ===\n");
 
200
 
 
201
    fclose(debug_fp);
 
202
    debug_fp = NULL;
 
203
  }
 
204
#endif
 
205
}
 
206
 
 
207
//
 
208
// PrintDebug
 
209
//
 
210
void PrintDebug(const char *str, ...)
 
211
{
 
212
#if DEBUG_ENABLED
 
213
  if (debug_fp)
 
214
  {
 
215
    va_list args;
 
216
 
 
217
    va_start(args, str);
 
218
    vfprintf(debug_fp, str, args);
 
219
    va_end(args);
 
220
 
 
221
    fflush(debug_fp);
 
222
  }
 
223
#else
 
224
  (void) str;
 
225
#endif
 
226
}
 
227
 
 
228
 
 
229
/* -------- endian code ----------------------------- */
 
230
 
 
231
//
 
232
// InitEndian
 
233
//
 
234
// Parts inspired by the Yadex endian.cc code.
 
235
//
 
236
void InitEndian(void)
 
237
{
 
238
  volatile union
 
239
  {
 
240
    uint8_g mem[32];
 
241
    uint32_g val;
 
242
  }
 
243
  u;
 
244
 
 
245
  /* sanity-check type sizes */
 
246
 
 
247
  if (sizeof(uint8_g) != 1)
 
248
    FatalError("Sanity check failed: sizeof(uint8_g) = %d", 
 
249
        (int)sizeof(uint8_g));
 
250
 
 
251
  if (sizeof(uint16_g) != 2)
 
252
    FatalError("Sanity check failed: sizeof(uint16_g) = %d", 
 
253
        (int)sizeof(uint16_g));
 
254
 
 
255
  if (sizeof(uint32_g) != 4)
 
256
    FatalError("Sanity check failed: sizeof(uint32_g) = %d", 
 
257
        (int)sizeof(uint32_g));
 
258
 
 
259
  /* check endianness */
 
260
 
 
261
  memset((uint32_g *) u.mem, 0, sizeof(u.mem));
 
262
 
 
263
  u.mem[0] = 0x70;  u.mem[1] = 0x71;
 
264
  u.mem[2] = 0x72;  u.mem[3] = 0x73;
 
265
 
 
266
# if DEBUG_ENDIAN
 
267
  PrintDebug("Endianness magic value: 0x%08x\n", u.val);
 
268
# endif
 
269
 
 
270
  if (u.val == 0x70717273)
 
271
    cpu_big_endian = 1;
 
272
  else if (u.val == 0x73727170)
 
273
    cpu_big_endian = 0;
 
274
  else
 
275
    FatalError("Sanity check failed: weird endianness (0x%08x)", u.val);
 
276
 
 
277
# if DEBUG_ENDIAN
 
278
  PrintDebug("Endianness = %s\n", cpu_big_endian ? "BIG" : "LITTLE");
 
279
 
 
280
  PrintDebug("Endianness check: 0x1234 --> 0x%04x\n", 
 
281
      (int) Endian_U16(0x1234));
 
282
  
 
283
  PrintDebug("Endianness check: 0x11223344 --> 0x%08x\n", 
 
284
      Endian_U32(0x11223344));
 
285
# endif
 
286
}
 
287
 
 
288
//
 
289
// Endian_U16
 
290
//
 
291
uint16_g Endian_U16(uint16_g x)
 
292
{
 
293
  if (cpu_big_endian)
 
294
    return (x >> 8) | (x << 8);
 
295
  else
 
296
    return x;
 
297
}
 
298
 
 
299
//
 
300
// Endian_U32
 
301
//
 
302
uint32_g Endian_U32(uint32_g x)
 
303
{
 
304
  if (cpu_big_endian)
 
305
    return (x >> 24) | ((x >> 8) & 0xff00) |
 
306
           ((x << 8) & 0xff0000) | (x << 24);
 
307
  else
 
308
    return x;
 
309
}
 
310