Qt: fix assertion failure when redissecting with a debug build of Qt
[metze/wireshark/wip.git] / randpkt.c
index 4f90231a9dd60936de81bfa61f4a89ec26b99de4..a39cbf53dedcee27a8c75d3adc4e999d6395cb9b 100644 (file)
--- a/randpkt.c
+++ b/randpkt.c
@@ -6,19 +6,7 @@
  *
  * Copyright (C) 1999 by Gilbert Ramirez <gram@alumni.rice.edu>
  *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include <config.h>
@@ -38,7 +26,7 @@
 #include <wsutil/plugins.h>
 #endif
 
-#include <wsutil/report_err.h>
+#include <wsutil/report_message.h>
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
 #endif
 
 #include "randpkt_core/randpkt_core.h"
 
+#define INVALID_OPTION 1
+#define INVALID_TYPE 2
+#define CLOSE_ERROR 2
+
 /*
- * General errors are reported with an console message in randpkt.
+ * General errors and warnings are reported with an console message
+ * in randpkt.
  */
 static void
-failure_message(const char *msg_format, va_list ap)
+failure_warning_message(const char *msg_format, va_list ap)
 {
        fprintf(stderr, "randpkt: ");
        vfprintf(stderr, msg_format, ap);
@@ -104,12 +97,12 @@ usage(gboolean is_error)
        g_strfreev(longname_list);
 
        fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n");
-
-       exit(is_error ? 1 : 0);
 }
-int
-main(int argc, char **argv)
+
+static int
+real_main(int argc, char **argv)
 {
+       char                   *init_progfile_dir_error;
        int                     opt;
        int                     produce_type = -1;
        char                    *produce_filename = NULL;
@@ -119,52 +112,39 @@ main(int argc, char **argv)
        guint8*                 type = NULL;
        int                     allrandom = FALSE;
        wtap_dumper             *savedump;
+       int                      ret = EXIT_SUCCESS;
        static const struct option long_options[] = {
                {"help", no_argument, NULL, 'h'},
                {0, 0, 0, 0 }
        };
 
-#ifdef HAVE_PLUGINS
-       char  *init_progfile_dir_error;
-#endif
-
        /*
         * Get credential information for later use.
         */
        init_process_policies();
-       init_open_routines();
 
-       cmdarg_err_init(failure_message, failure_message_cont);
-
-#ifdef _WIN32
-       arg_list_utf_16to8(argc, argv);
-       create_app_running_mutex();
-#endif /* _WIN32 */
-
-#ifdef HAVE_PLUGINS
-       /* Register wiretap plugins */
-       if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
-               g_warning("randpkt: init_progfile_dir(): %s", init_progfile_dir_error);
+       /*
+        * Attempt to get the pathname of the directory containing the
+        * executable file.
+        */
+       init_progfile_dir_error = init_progfile_dir(argv[0]);
+       if (init_progfile_dir_error != NULL) {
+               fprintf(stderr,
+                   "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
+                   init_progfile_dir_error);
                g_free(init_progfile_dir_error);
-       } else {
-               /* Register all the plugin types we have. */
-               wtap_register_plugin_types(); /* Types known to libwiretap */
+       }
 
-               init_report_err(failure_message,NULL,NULL,NULL);
+       init_report_message(failure_warning_message, failure_warning_message,
+                               NULL, NULL, NULL);
 
-               /* Scan for plugins.  This does *not* call their registration routines;
-                  that's done later.
+       wtap_init(TRUE);
 
-                  Don't report failures to load plugins because most
-                  (non-wiretap) plugins *should* fail to load (because
-                  we're not linked against libwireshark and dissector
-                  plugins need libwireshark). */
-               scan_plugins(DONT_REPORT_LOAD_FAILURE);
+       cmdarg_err_init(failure_warning_message, failure_message_cont);
 
-               /* Register all libwiretap plugin modules. */
-               register_all_wiretap_modules();
-       }
-#endif
+#ifdef _WIN32
+       create_app_running_mutex();
+#endif /* _WIN32 */
 
        while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {
                switch (opt) {
@@ -172,7 +152,8 @@ main(int argc, char **argv)
                                produce_max_bytes = get_positive_int(optarg, "max bytes");
                                if (produce_max_bytes > 65536) {
                                        cmdarg_err("max bytes is > 65536");
-                                       return 1;
+                                       ret = INVALID_OPTION;
+                                       goto clean_exit;
                                }
                                break;
 
@@ -186,6 +167,7 @@ main(int argc, char **argv)
 
                        case 'h':
                                usage(FALSE);
+                               goto clean_exit;
                                break;
 
                        case 'r':
@@ -194,6 +176,8 @@ main(int argc, char **argv)
 
                        default:
                                usage(TRUE);
+                               ret = INVALID_OPTION;
+                               goto clean_exit;
                                break;
                }
        }
@@ -204,6 +188,8 @@ main(int argc, char **argv)
        }
        else {
                usage(TRUE);
+               ret = INVALID_OPTION;
+               goto clean_exit;
        }
 
        if (!allrandom) {
@@ -211,41 +197,72 @@ main(int argc, char **argv)
                g_free(type);
 
                example = randpkt_find_example(produce_type);
-               if (!example)
-                       return 1;
+               if (!example) {
+                       ret = INVALID_OPTION;
+                       goto clean_exit;
+               }
 
-               randpkt_example_init(example, produce_filename, produce_max_bytes);
-               randpkt_loop(example, produce_count);
+               ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
+               if (ret != EXIT_SUCCESS)
+                       goto clean_exit;
+               randpkt_loop(example, produce_count, 0);
        } else {
                if (type) {
                        fprintf(stderr, "Can't set type in random mode\n");
-                       return 2;
+                       ret = INVALID_TYPE;
+                       goto clean_exit;
                }
 
                produce_type = randpkt_parse_type(NULL);
                example = randpkt_find_example(produce_type);
-               if (!example)
-                       return 1;
-               randpkt_example_init(example, produce_filename, produce_max_bytes);
+               if (!example) {
+                       ret = INVALID_OPTION;
+                       goto clean_exit;
+               }
+               ret = randpkt_example_init(example, produce_filename, produce_max_bytes);
+               if (ret != EXIT_SUCCESS)
+                       goto clean_exit;
 
                while (produce_count-- > 0) {
-                       randpkt_loop(example, 1);
+                       randpkt_loop(example, 1, 0);
                        produce_type = randpkt_parse_type(NULL);
 
                        savedump = example->dump;
 
                        example = randpkt_find_example(produce_type);
-                       if (!example)
-                               return 1;
+                       if (!example) {
+                               ret = INVALID_OPTION;
+                               goto clean_exit;
+                       }
                        example->dump = savedump;
                }
        }
-       if (!randpkt_example_close(example))
-               return 2;
-       return 0;
+       if (!randpkt_example_close(example)) {
+               ret = CLOSE_ERROR;
+       }
 
+clean_exit:
+       wtap_cleanup();
+       return ret;
 }
 
+#ifdef _WIN32
+int
+wmain(int argc, wchar_t **wc_argv)
+{
+       char **argv;
+
+       argv = arg_list_utf_16to8(argc, wc_argv);
+       return real_main(argc, argv);
+}
+#else
+int
+main(int argc, char **argv)
+{
+       return real_main(argc, argv);
+}
+#endif
+
 /*
  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
  *