~ubuntu-branches/ubuntu/trusty/glbsp/trusty

« back to all changes in this revision

Viewing changes to cmdline/display.c

  • Committer: Bazaar Package Importer
  • Author(s): Darren Salt
  • Date: 2006-08-13 12:38:26 UTC
  • Revision ID: james.westby@ubuntu.com-20060813123826-3btvz77q5fu3fg6n
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//------------------------------------------------------------------------
 
2
// DISPLAY : Command-line display routines
 
3
//------------------------------------------------------------------------
 
4
//
 
5
//  GL-Friendly Node Builder (C) 2000-2005 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 "../glbsp.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
#if defined(MSDOS) || defined(__MSDOS__)
 
33
#include <dos.h>
 
34
#endif
 
35
 
 
36
#if defined(UNIX) || defined(__UNIX__)
 
37
#include <unistd.h>  // Unix: isatty()
 
38
#include <signal.h>  // Unix: raise()
 
39
#endif
 
40
 
 
41
#include "display.h"
 
42
 
 
43
 
 
44
// consistency check
 
45
#ifndef GLBSP_TEXT
 
46
#error GLBSP_TEXT should be defined when compiling this file
 
47
#endif
 
48
 
 
49
 
 
50
#define FATAL_COREDUMP  0
 
51
 
 
52
 
 
53
static boolean_g disable_progress = FALSE;
 
54
 
 
55
static displaytype_e curr_disp = DIS_INVALID;
 
56
 
 
57
static int progress_target = 0;
 
58
static int progress_shown;
 
59
 
 
60
 
 
61
const nodebuildfuncs_t cmdline_funcs =
 
62
{
 
63
  TextFatalError,
 
64
  TextPrintMsg,
 
65
  TextTicker,
 
66
 
 
67
  TextDisplayOpen,
 
68
  TextDisplaySetTitle,
 
69
  TextDisplaySetBar,
 
70
  TextDisplaySetBarLimit,
 
71
  TextDisplaySetBarText,
 
72
  TextDisplayClose
 
73
};
 
74
 
 
75
 
 
76
//
 
77
// TextStartup
 
78
//
 
79
void TextStartup(void)
 
80
{
 
81
  setbuf(stdout, NULL);
 
82
 
 
83
#ifdef UNIX  
 
84
  // no whirling baton if stderr is redirected
 
85
  if (! isatty(2))
 
86
    disable_progress = TRUE;
 
87
#endif
 
88
}
 
89
 
 
90
//
 
91
// TextShutdown
 
92
//
 
93
void TextShutdown(void)
 
94
{
 
95
  /* nothing to do */
 
96
}
 
97
 
 
98
//
 
99
// TextDisableProgress
 
100
//
 
101
void TextDisableProgress(void)
 
102
{
 
103
  disable_progress = TRUE;
 
104
}
 
105
 
 
106
//
 
107
// TextPrintMsg
 
108
//
 
109
void TextPrintMsg(const char *str, ...)
 
110
{
 
111
  va_list args;
 
112
 
 
113
  va_start(args, str);
 
114
  vprintf(str, args);
 
115
  va_end(args);
 
116
 
 
117
  fflush(stdout);
 
118
}
 
119
 
 
120
//
 
121
// TextFatalError
 
122
//
 
123
// Terminates the program reporting an error.
 
124
//
 
125
void TextFatalError(const char *str, ...)
 
126
{
 
127
  va_list args;
 
128
 
 
129
  va_start(args, str);
 
130
  vfprintf(stderr, str, args);
 
131
  va_end(args);
 
132
 
 
133
#if FATAL_COREDUMP && defined(UNIX)
 
134
  raise(SIGSEGV);
 
135
#endif
 
136
 
 
137
  exit(5);
 
138
}
 
139
 
 
140
//
 
141
// TextTicker
 
142
//
 
143
void TextTicker(void)
 
144
{
 
145
  /* does nothing */
 
146
}
 
147
 
 
148
 
 
149
static void ClearProgress(void)
 
150
{
 
151
  fprintf(stderr, "                \b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
 
152
}
 
153
 
 
154
//
 
155
// TextDisplayOpen
 
156
//
 
157
boolean_g TextDisplayOpen(displaytype_e type)
 
158
{
 
159
  // shutdown any existing display
 
160
  TextDisplayClose();
 
161
 
 
162
  switch (type)
 
163
  {
 
164
    case DIS_BUILDPROGRESS:
 
165
    case DIS_FILEPROGRESS:
 
166
      // these are OK
 
167
      break;
 
168
 
 
169
    default:
 
170
      curr_disp = DIS_INVALID;
 
171
      return FALSE;
 
172
  }
 
173
 
 
174
  curr_disp = type;
 
175
  progress_target = 0;
 
176
 
 
177
  return TRUE;
 
178
}
 
179
 
 
180
//
 
181
// TextDisplaySetTitle
 
182
//
 
183
void TextDisplaySetTitle(const char *str)
 
184
{
 
185
  /* does nothing */
 
186
}
 
187
 
 
188
//
 
189
// TextDisplaySetBarText
 
190
//
 
191
void TextDisplaySetBarText(int barnum, const char *str)
 
192
{
 
193
  /* does nothing */
 
194
}
 
195
 
 
196
//
 
197
// TextDisplaySetBarLimit
 
198
//
 
199
void TextDisplaySetBarLimit(int barnum, int limit)
 
200
{
 
201
  if (curr_disp == DIS_INVALID || disable_progress)
 
202
    return;
 
203
 
 
204
  // select the correct bar
 
205
  if ((curr_disp == DIS_FILEPROGRESS && barnum != 1) ||
 
206
      (curr_disp == DIS_BUILDPROGRESS && barnum != 1))
 
207
  {
 
208
    return;
 
209
  }
 
210
 
 
211
  progress_target = limit;
 
212
  progress_shown  = -1;
 
213
}
 
214
 
 
215
//
 
216
// TextDisplaySetBar
 
217
//
 
218
void TextDisplaySetBar(int barnum, int count)
 
219
{
 
220
  int perc;
 
221
  
 
222
  if (curr_disp == DIS_INVALID || disable_progress ||
 
223
      progress_target <= 0)
 
224
  {
 
225
    return;
 
226
  }
 
227
 
 
228
  // select the correct bar
 
229
  if ((curr_disp == DIS_FILEPROGRESS && barnum != 1) ||
 
230
      (curr_disp == DIS_BUILDPROGRESS && barnum != 1))
 
231
  {
 
232
    return;
 
233
  }
 
234
 
 
235
  if (count > progress_target)
 
236
    TextFatalError("\nINTERNAL ERROR: Progress went past target !\n\n");
 
237
 
 
238
  perc = count * 100 / progress_target;
 
239
 
 
240
  if (perc == progress_shown)
 
241
    return;
 
242
 
 
243
  if (perc == 0)
 
244
    ClearProgress();
 
245
  else
 
246
    fprintf(stderr, "--%3d%%--\b\b\b\b\b\b\b\b", perc);
 
247
 
 
248
  progress_shown = perc;
 
249
}
 
250
 
 
251
//
 
252
// TextDisplayClose
 
253
//
 
254
void TextDisplayClose(void)
 
255
{
 
256
  if (curr_disp == DIS_INVALID || disable_progress)
 
257
    return;
 
258
 
 
259
  ClearProgress();
 
260
}
 
261