]> granicus.if.org Git - flex/commitdiff
Replaced exit(2) calls with longjmps (in the form of FLEX_EXIT macro).
authorJohn Millaway <john43@users.sourceforge.net>
Sat, 13 Apr 2002 04:08:57 +0000 (04:08 +0000)
committerJohn Millaway <john43@users.sourceforge.net>
Sat, 13 Apr 2002 04:08:57 +0000 (04:08 +0000)
Moved main() to flex_main() to allow flex to be called from a library.

main.c
misc.c

diff --git a/main.c b/main.c
index bdbdaaa3897876228e2481d9d7536b96725c9e67..957a72f0d2c08faa189c8646a541b17bb9c1a70b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -103,6 +103,7 @@ FILE *backing_up_file;
 int end_of_buffer_state;
 char **input_files;
 int num_input_files;
+jmp_buf flex_main_jmp_buf;
 
 /* Make sure program_name is initialized so we don't crash if writing
  * out an error message before getting the program name from argv[0].
@@ -130,15 +131,22 @@ static int outfile_created = 0;
 static char *skelname = NULL;
 
 
-int main( argc, argv )
+int flex_main( argc, argv )
 int argc;
 char **argv;
        {
-       int i;
-
-       setlocale(LC_MESSAGES, "");
-       textdomain(PACKAGE);
-       bindtextdomain(PACKAGE, LOCALEDIR);
+       int i,exit_status;
+
+       /* Set a longjmp target. Yes, I know it's a hack, but it gets worse: The
+        * return value of setjmp, if non-zero, is the desired exit code PLUS ONE.
+        * For example, if you want 'main' to return with code '2', then call
+        * longjmp() with an argument of 3. This is because it is invalid to
+        * specify a value of 0 to longjmp. FLEX_EXIT(n) should be used instead of
+        * exit(n);
+        */
+       exit_status = setjmp(flex_main_jmp_buf);
+       if ( exit_status )
+               return exit_status - 1;
 
 #ifdef THINK_C
        argc = ccommand( &argv );
@@ -171,6 +179,15 @@ char **argv;
        return 0;       /* keep compilers/lint happy */
        }
 
+/* Wrapper around flex_main, so flex_main can be built as a library. */
+int main( argc, argv )
+{
+       setlocale(LC_MESSAGES, "");
+       textdomain(PACKAGE);
+       bindtextdomain(PACKAGE, LOCALEDIR);
+
+    return flex_main(argc,argv);
+}
 
 /* check_options - check user-specified options */
 
@@ -465,7 +482,7 @@ int exit_status;
        int unlink();
 
        if( ++called_before )
-               exit( exit_status );
+               FLEX_EXIT( exit_status );
 
        if ( skelfile != NULL )
                {
@@ -889,7 +906,7 @@ _( "  %d/%d character classes needed %d/%d words of storage, %d reused\n" ),
                        tblsiz );
                }
 
-       exit( exit_status );
+       FLEX_EXIT( exit_status );
        }
 
 
@@ -943,17 +960,16 @@ char **argv;
             /* This will only happen when flexopts array is altered. */
             fprintf(stderr,
                     _("Internal error. flexopts are malformed.\n"));
-            exit(1);
+            FLEX_EXIT(1);
         }
 
         while((rv=scanopt(sopt, &arg, &optind)) != 0){
 
             if (rv < 0) {
                 /* Scanopt has already printed an option-specific error message. */
-                fprintf( stderr, _( "For usage, try\n\t%s --help\n" ),
+                fprintf( stderr, _( "Try `%s --help' for more information.\n" ),
                     program_name );
-                exit( 1 );
-                break;
+                FLEX_EXIT(1);
             }
 
             switch ((enum flexopt_flag_t)rv){
@@ -1036,7 +1052,7 @@ char **argv;
 
             case OPT_HELP:
                     usage();
-                    exit( 0 );
+                    FLEX_EXIT( 0 );
 
             case OPT_INTERACTIVE:
                     interactive = true;
@@ -1128,7 +1144,7 @@ char **argv;
             case OPT_VERSION:
                     printf( _( "%s %s\n" ),
                             program_name, flex_version );
-                    exit( 0 );
+                    FLEX_EXIT( 0 );
 
             case OPT_WARN:
                     nowarn = false;
diff --git a/misc.c b/misc.c
index 967a4136d80adae9c6e9b8be7ed248eba49101ca..ef8eced40bfbfc55fa015a2436d013bb1dcc92a4 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -348,7 +348,7 @@ const char msg[];
        {
        fprintf( stderr, _( "%s: fatal internal error, %s\n" ),
                program_name, msg );
-       exit( 1 );
+       FLEX_EXIT( 1 );
        }