DaZeus  2.0
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
main.cpp
Go to the documentation of this file.
1 
6 #include "dazeus.h"
7 #include "dazeusglobal.h"
8 
9 #include <time.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <signal.h>
14 #include <assert.h>
15 
16 void usage(char*);
17 
18 dazeus::DaZeus *d = NULL;
19 
20 void sigchild_handler(int sig);
21 void sigchild_handler(int sig) {
22  assert(sig == SIGCHLD);
23  assert(d != NULL);
24  d->sigchild();
25 
26  if(signal(sig, sigchild_handler) == SIG_ERR) {
27  perror("Failed to install SIGCHLD handler");
28  }
29 }
30 
31 int main(int argc, char *argv[])
32 {
33  fprintf(stderr, "DaZeus version: %s\n", DAZEUS_VERSION_STR);
34 
35  // Initialise random seed
36  srand(time(NULL));
37 
38  std::string configfile = DAZEUS_DEFAULT_CONFIGFILE;
39 
40  std::string type;
41  for(int i = 1; i < argc; ++i) {
42  if(type.length() == 0) {
43  if(strcmp(argv[i], "-h") == 0) {
44  usage(argv[0]);
45  return 1;
46  } else if(strcmp(argv[i], "-c") == 0) {
47  type = "config";
48  } else {
49  fprintf(stderr, "Didn't understand option: %s\n", argv[i]);
50  usage(argv[0]);
51  return 2;
52  }
53  } else {
54  if(type == "config") {
55  configfile = std::string(argv[i]);
56  } else {
57  abort();
58  }
59  type = "";
60  }
61  }
62 
63  if(signal(SIGCHLD, sigchild_handler) == SIG_ERR) {
64  perror("Failed to install SIGCHLD handler");
65  }
66 
67  d = new dazeus::DaZeus(configfile);
68  if(!d->loadConfig()) {
69  fprintf(stderr, "Failed to load configuration, bailing out.\n");
70  return 3;
71  }
72  d->initPlugins();
73  d->autoConnect();
74  d->run();
75  delete d;
76  return 0;
77 }
78 
79 void usage(char *exec)
80 {
81  fprintf(stderr, "Usage: %s options\n", exec);
82  fprintf(stderr, "Available options:\n");
83  fprintf(stderr, " -c configfile - Use this configuration file\n");
84  fprintf(stderr, " -h - Display this help message\n");
85 }