File: quit.cc

package info (click to toggle)
cssc 1.0.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,612 kB
  • ctags: 1,424
  • sloc: cpp: 13,502; sh: 4,759; ansic: 2,971; perl: 342; makefile: 339; awk: 11
file content (417 lines) | stat: -rw-r--r-- 8,439 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * quit.cc: Part of GNU CSSC.
 * 
 *    Copyright (C) 1997,1998,1999,2001 Free Software Foundation, Inc. 
 * 
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 * 
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 * 
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
 * 
 * CSSC was originally Based on MySC, by Ross Ridge, which was 
 * placed in the Public Domain.
 *
 *
 * Functions for cleaning up and quitting.
 *
 */

#ifdef __GNUC__
#pragma implementation "quit.h"
#endif

#include "cssc.h"
#include "sysdep.h"
#include "quit.h"
#include "sysnerr.h"
#include "except.h"
#include "err_no.h"

// #include "pipe.h"

#include <stdarg.h>

#ifdef CONFIG_SCCS_IDS
static const char rcs_id[] = "CSSC $Id: quit.cc,v 1.30 2003/12/07 21:07:28 james_youngman Exp $";
#endif

const char *prg_name = NULL;

void
set_prg_name(const char *name) {
        prg_name = name;
}

static void
v_errormsg(const char *fmt, va_list ap)
{
  if (prg_name != NULL)
    fprintf(stderr, "%s: ", prg_name);

  vfprintf(stderr, fmt, ap);
}

void errormsg(const char *fmt, ...)
{
  va_list ap;
  va_start(ap, fmt);
  v_errormsg(fmt, ap);
  va_end(ap);
  putc('\n', stderr);
}

void warning (const char *fmt, ...)
{
  va_list ap;
  va_start(ap, fmt);
  fprintf(stderr, "%s: warning: ", prg_name);
  vfprintf(stderr, fmt, ap);
  va_end(ap);
  putc('\n', stderr);
}

void v_errormsg_with_errno(const char *fmt, va_list ap)
{
  int saved_errno = errno;
  
  v_errormsg(fmt, ap);
  errno = saved_errno;
  perror(" ");
}

void errormsg_with_errno(const char *fmt, ...)
{
  va_list ap;
  va_start(ap, fmt);
  v_errormsg_with_errno(fmt, ap);
  va_end(ap);
  putc('\n', stderr);
}

static void print_err(int err)
{
#ifndef HAVE_STRERROR
  if (err <= sys_nerr)
    fprintf(stderr, "%d - %s\n", err, sys_errlist[err]);
  else
    fprintf(stderr, "%d - Unknown error\n", err);
#else
  fprintf(stderr, "%d - %s\n", err, strerror(err));
#endif
}

static NORETURN
v_quit(int err, const char *fmt, va_list ap) {
        fflush(stdout);
        cleanup::run_cleanups();

        fflush(stdout);

	// We used to call usage() is err was -2, but
	// nobody ever actually used that.
	
	putc('\n', stderr);
        if (fmt)
          {
            v_errormsg(fmt, ap);
            putc('\n', stderr);
          }
        
        if (err >= 1) {
          print_err(err);
        }

        fflush(stderr); // probably not required, stderr is unbuffered.

#ifdef HAVE_ABORT
        if (err == -3) {
                abort();
        }
#endif

#ifdef USE_PIPE
        if (cleanup::in_child()) {
                if (err > 0) {
                        _exit(1);
                } else {
                        _exit(-err);
                }
        }
#endif

#ifdef HAVE_EXCEPTIONS  
        if (err < 0)
          err = -err;
        throw CsscQuitException(err);
#else
        if (err > 0) {
                exit(1);
        } else {
                exit(-err);
        }
#endif  
}

// We eventually want to change the code to eliminate most calls
// to quit (because processing should continue with the next input
// file).  We just use this differently-named function to indicate
// the places where a failure odducrs in a constructor, which cannot
// return a failure status.  When they become commonly supported,
// we'll use exceptions.
NORETURN
ctor_fail(int err, const char *fmt, ...) {
        va_list ap;


        va_start(ap, fmt);
#ifdef HAVE_EXCEPTIONS
        if (fmt)
          {
            v_errormsg(fmt, ap);
            putc('\n', stderr);
          }
        throw CsscContstructorFailedException(err);
#else
        v_quit(err, fmt, ap);
#endif
        /*NOTREACHED*/
        va_end(ap);
        ASSERT(0);              // not reached.
}


/* We have to pass a va_list to v_quit, to we have to have 
 * a varargs function to do it...  
 */

static NORETURN  ctor_fail_nomsg_helper(int err, ...)
{
    va_list ap;
    va_start(ap, err);
    v_quit(err, NULL, ap);
    /*NOTREACHED*/
    va_end(ap);
}

NORETURN ctor_fail_nomsg(int err)  
{
#ifdef HAVE_EXCEPTIONS
    throw CsscContstructorFailedException(err);
#else
    ctor_fail_nomsg_helper(err);
    /*NOTREACHED*/
#endif
}



NORETURN
s_corrupt_quit(const char *fmt, ...) {
        va_list ap;


        va_start(ap, fmt);
#ifdef HAVE_EXCEPTIONS  
        v_errormsg(fmt, ap);
        putc('\n', stderr);
        throw CsscSfileCorruptException();
#else
        v_quit(-1, fmt, ap);
#endif
        /*NOTREACHED*/
        va_end(ap);
        ASSERT(0);              // not reached.
}

NORETURN
s_missing_quit(const char *fmt, ...) {
        va_list ap;


#ifdef HAVE_EXCEPTIONS  
        va_start(ap, fmt);
        v_errormsg_with_errno(fmt, ap);
        va_end(ap);
        
        putc('\n', stderr);
        throw CsscSfileMissingException();
#else
        va_start(ap, fmt);
        v_quit(-1, NULL, ap);
        va_end(ap);
#endif
        /*NOTREACHED*/
        ASSERT(0);              // not reached.
}


/* s_unrecognised_feature_quit is usually called by 
 * sccs_file::saw_unknown_feature().
 */
NORETURN
s_unrecognised_feature_quit(const char *fmt, va_list ap)
{
  if (prg_name != NULL)
    fprintf(stderr, "%s: Warning: unknown feature: ", prg_name);
  
#ifdef HAVE_EXCEPTIONS  
  vfprintf(stderr, fmt, ap);
  putc('\n', stderr);
  throw CsscUnrecognisedFeatureException();
#else
  v_quit(-1, fmt, ap);
#endif
}

void
v_unknown_feature_warning(const char *fmt, va_list ap)
{
  if (prg_name != NULL)
    fprintf(stderr, "%s: Warning: unknown feature: ", prg_name);

  vfprintf(stderr, fmt, ap);
}


NORETURN
p_corrupt_quit(const char *fmt, ...) {
        va_list ap;


        va_start(ap, fmt);
#ifdef HAVE_EXCEPTIONS  
        v_errormsg(fmt, ap);
        putc('\n', stderr);
        throw CsscPfileCorruptException();
#else
        v_quit(-1, fmt, ap);
#endif
        /*NOTREACHED*/
        va_end(ap);
        ASSERT(0);              // not reached.
}

// fatal_quit() is ALWAYS immediately fatal.
NORETURN
fatal_quit(int err, const char *fmt, ...) {
        va_list ap;

        va_start(ap, fmt);
        v_quit(err, fmt, ap);
        /*NOTREACHED*/
        va_end(ap);
        ASSERT(0);              // not reached.
}



NORETURN
nomem() {
        fatal_quit(-4, "Out of memory!");
}

NORETURN
assert_failed(const char *file, int line,
              const char *func,
              const char *test)
{
  if (func[0])
    {
      fatal_quit(-3, "Assertion failed in %s:\nFile: %s  Line: %d: assert(%s)",
                 func, file, line, test);
    }
  else
    {
      fatal_quit(-3, "Assertion failed: %s\nFile: %s  Line: %d",
                 test, file, line);
    }
}


class cleanup *cleanup::head = NULL;
int cleanup::running = 0;
int cleanup::all_disabled = 0;
#if HAVE_FORK
int cleanup::in_child_flag = 0;
#endif

cleanup::cleanup() {
        next = head;
        head = this;
}

cleanup::~cleanup()
{
  if (head == NULL)
    {
      // SourceForge bug # 816679; cleanup::cleanup() 
      // can be called before there are any entries on the 
      // list.
      return; // nothing to do.
    }
  else
    {
      class cleanup *p = head;
      
      if (p == this) 
	{
	  head = next;
	  return;
        }
      
      while (p->next != this) 
	{
	  p = p->next;
	  ASSERT(p != NULL);
        }
      
      if (p != NULL) // 'if' fixes SourceForge bug # 816679
	{
	  p->next = next;
	}
    }
}               

void
cleanup::run_cleanups() {
        if (running || all_disabled) {
                return;
        }
        running = 1;

        class cleanup *p = head;
        while (p != NULL) {
                p->do_cleanup();
                p = p->next;
        }

        running = 0;
        all_disabled++;
        return;
}


Cleaner::~Cleaner()
{
  cleanup::run_cleanups();
}

Cleaner::Cleaner()
{
  quit_on_fatal_signals();
}



/* Local variables: */
/* mode: c++ */
/* End: */