[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bison as library & exit()

From: Hans Aberg
Subject: Bison as library & exit()
Date: Thu, 5 Oct 2000 14:50:26 +0200

I figured out a way to handle exit() when Bison is used as a library. There
are two problems to get around with as little effort as possible, namely to
pick up the exit status, and to make sure the functions registered by
atexit() are properly executed.

In a prefix file, read by all files, I have put
void Exit(int status);
#define exit Exit

Then, in the file where my real main (and not the bison_main) is, I have
#undef exit  /* Make sure the real exit is called in this file */
#include <stdlib.h>  /* contains real exit() */

int exit_status;
void exit_jump(void);

void Exit(int status) {
    exit_status = status;
    /* Make sure exit() calls exit() and not Exit() here: */

void exit_jump(void) {
    longjmp(main_env, 1);

And the code of the real main() will be:

main(...) {
    if (setjmp(main_env) == 0)  bison_main(argc, argv);

  /* After an exit() one ends up here, with the exit code in exit_status. */
    return exit_status;

This relates somewhat to Bison, because one can have both cleanup() and
done() registered by atexit(), but waiting with registering done() until
one is sure it is really needed. Thus, before done() has been registered,
exit() will behave more like abort(), the latter which preferably should
not be used, as it fails to remove the temporary files.

Then, with my construction, it does not really matter how additional
functions are registered with atexit().

  Hans Aberg

reply via email to

[Prev in Thread] Current Thread [Next in Thread]