]> icculus.org git repositories - taylor/freespace2.git/blob - src/freespace2/main.cpp
rename #progess to #loading
[taylor/freespace2.git] / src / freespace2 / main.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 #include <exception>
10
11 #include "pstypes.h"
12
13 #ifdef PLAT_UNIX
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <signal.h>
18 #include <fcntl.h>
19 #endif
20
21
22 extern "C" int game_main(const char *szCmdLine);
23
24
25 #if defined(PLAT_UNIX) && !defined(__EMSCRIPTEN__)
26 static void daemonize()
27 {
28         pid_t pid = fork();
29
30         if (pid == -1) {
31                 exit(EXIT_FAILURE);
32         } else if (pid != 0) {
33                 _exit(0);
34         }
35
36         if (setsid() == -1) {
37                 exit(EXIT_FAILURE);
38         }
39
40         signal(SIGHUP, SIG_IGN);
41
42         pid = fork();
43
44         if (pid == -1) {
45                 exit(EXIT_FAILURE);
46         } else if (pid != 0) {
47                 _exit(0);
48         }
49
50         if (chdir("/") == -1) {
51                 exit(EXIT_FAILURE);
52         }
53
54         umask(0);
55
56         close(STDIN_FILENO);
57         close(STDOUT_FILENO);
58         close(STDERR_FILENO);
59
60         open("/dev/null", O_RDONLY);
61         open("/dev/null", O_WRONLY);
62         open("/dev/null", O_RDWR);
63 }
64 #endif
65
66
67 extern "C"
68 int main(int argc, char *argv[])
69 {
70         char *argptr = NULL;
71         int i;
72         int len = 0;
73         int retr = 0;
74
75 #if defined(PLAT_UNIX) && !defined(__EMSCRIPTEN__)
76         // if we are standalone headless, daemonize
77         bool daemon = false;
78         bool standalone = false;
79
80         for (i = 1; i < argc; i++) {
81                 if ( !daemon && SDL_strstr(argv[i], "-daemon") ) {
82                         daemon = true;
83                 }
84
85                 if ( !standalone ) {
86                         if ( SDL_strstr(argv[i], "-standalone") ) {
87                                 standalone = true;
88                         }
89
90                         if ( !SDL_strcmp(argv[i], "-b") ) {
91                                 standalone = true;
92                         }
93                 }
94         }
95
96         if (standalone && daemon) {
97                 daemonize();
98         }
99
100         // make sure we create files with user access only
101         umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
102 #endif
103
104         for (i = 1; i < argc; i++) {
105                 len += strlen(argv[i]) + 1;
106         }
107
108         if (len > 0) {
109                 argptr = (char *)SDL_malloc(len+5);
110
111                 if (argptr == NULL) {
112                         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", "Ran out of memory in main()!", NULL);
113                         exit(1);
114                 }
115
116                 memset(argptr, 0, len+5);
117
118                 for (i = 1; i < argc; i++) {
119                         SDL_strlcat(argptr, argv[i], len+5);
120                         SDL_strlcat(argptr, " ", len+5);
121                 }
122         }
123
124         try {
125                 retr = game_main(argptr);
126         } catch(const std::exception &e) {
127                 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", e.what(), NULL);
128         } catch(...) {
129                 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", "Exception caught in main()!", NULL);
130         }
131
132         if (argptr) {
133                 SDL_free(argptr);
134         }
135
136         return retr;    
137 }