Remove C/C++11 header files that we don't really use
[satcheck.git] / main.cc
1 /*      Copyright (c) 2015 Regents of the University of California
2  *
3  *      Author: Brian Demsky <bdemsky@uci.edu>
4  *
5  *      This program is free software; you can redistribute it and/or
6  *      modify it under the terms of the GNU General Public License
7  *      version 2 as published by the Free Software Foundation.
8  */
9
10 /** @file main.cc
11  *  @brief Entry point for the model checker.
12  */
13
14 #include <unistd.h>
15 #include <getopt.h>
16 #include <string.h>
17 #include <stdlib.h>
18
19 #include "common.h"
20 #include "output.h"
21 #include "model.h"
22
23 /* global "model" object */
24 #include "params.h"
25 #include "snapshot-interface.h"
26
27 static void param_defaults(struct model_params *params)
28 {
29         params->branches = false;
30         params->noyields = false;
31         params->verbose = !!DBG_ENABLED();
32 }
33
34 static void print_usage(const char *program_name, struct model_params *params)
35 {
36         /* Reset defaults before printing */
37         param_defaults(params);
38
39         model_print(
40                 "Model-checker options:\n"
41                 "-h, --help                  Display this help message and exit\n"
42                 "-Y, --avoidyields           Fairness support by not executing yields\n"
43                 "-b, --branches              Only explore all branches\n"
44                 "-v[NUM], --verbose[=NUM]    Print verbose execution information. NUM is optional:\n"
45                 " --                         Program arguments follow.\n\n");
46         exit(EXIT_SUCCESS);
47 }
48
49 static void parse_options(struct model_params *params, int argc, char **argv)
50 {
51         const char *shortopts = "hbYv::";
52         const struct option longopts[] = {
53                 {"help", no_argument, NULL, 'h'},
54                 {"avoidyields", no_argument, NULL, 'Y'},
55                 {"branches", no_argument, NULL, 'b'},
56                 {"verbose", optional_argument, NULL, 'v'},
57                 {0, 0, 0, 0}                                            /* Terminator */
58         };
59         int opt, longindex;
60         bool error = false;
61         while (!error && (opt = getopt_long(argc, argv, shortopts, longopts, &longindex)) != -1) {
62                 switch (opt) {
63                 case 'h':
64                         print_usage(argv[0], params);
65                         break;
66                 case 'b':
67                         params->branches = true;
68                         break;
69                 case 'Y':
70                         params->noyields = true;
71                         break;
72                 case 'v':
73                         params->verbose = optarg ? atoi(optarg) : 1;
74                         break;
75                 default:                                                /* '?' */
76                         error = true;
77                         break;
78                 }
79         }
80
81         /* Pass remaining arguments to user program */
82         params->argc = argc - (optind - 1);
83         params->argv = argv + (optind - 1);
84
85         /* Reset program name */
86         params->argv[0] = argv[0];
87
88         /* Reset (global) optind for potential use by user program */
89         optind = 1;
90
91         if (error)
92                 print_usage(argv[0], params);
93 }
94
95 int main_argc;
96 char **main_argv;
97
98 /** The model_main function contains the main model checking loop. */
99 static void model_main()
100 {
101         struct model_params params;
102
103         param_defaults(&params);
104
105         parse_options(&params, main_argc, main_argv);
106
107 #ifdef TSO
108         model_print("TSO\n");
109 #endif
110         snapshot_stack_init();
111
112         model = new MC(params);
113
114         model->check();
115         delete model;
116         DEBUG("Exiting\n");
117 }
118
119 /**
120  * Main function.  Just initializes snapshotting library and the
121  * snapshotting library calls the model_main function.
122  */
123 int main(int argc, char **argv)
124 {
125         main_argc = argc;
126         main_argv = argv;
127
128         /*
129          * If this printf statement is removed, CDSChecker will fail on an
130          * assert on some versions of glibc.  The first time printf is
131          * called, it allocated internal buffers.  We can't easily snapshot
132          * libc since we also use it.
133          */
134
135         printf("SATCheck\n"
136                                  "Copyright (c) 2016 Regents of the University of California. All rights reserved.\n"
137                                  "Distributed under the GPLv2\n"
138                                  "Written by Brian Demsky and Patrick Lam\n\n");
139
140         /* Configure output redirection for the model-checker */
141         redirect_output();
142
143         /* Let's jump in quickly and start running stuff */
144         snapshot_system_init(200000, 1024, 1024, 90000, &model_main);
145 }