| 18 |
#include "bbs.h" |
#include "bbs.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
|
#include "menu.h" |
| 22 |
|
#include <signal.h> |
| 23 |
#include <string.h> |
#include <string.h> |
| 24 |
|
#include <unistd.h> |
| 25 |
|
|
| 26 |
void |
void |
| 27 |
app_help(void) |
app_help (void) |
| 28 |
{ |
{ |
| 29 |
prints ( |
prints ("Usage: bbsd [-fhv] [...]\n\n" |
| 30 |
"Usage: bbsd [-fhv] [...]\n\n" |
"-f\t--foreground\t\tForce program run in foreground\n" |
| 31 |
"-f\t--foreground\t\tForce program run in foreground\n" |
"-h\t--help\t\t\tDisplay this help message\n" |
| 32 |
"-h\t--help\t\t\tDisplay this help message\n" |
"-v\t--version\t\tDisplay version information\n" |
| 33 |
"-v\t--version\t\tDisplay version information\n" |
"\t--display-log\t\tDisplay standard log information\n" |
| 34 |
"\t--display-log\t\tDisplay standard log information\n" |
"\t--display-error-log\tDisplay error log information\n" |
| 35 |
"\t--display-error-log\tDisplay error log information\n" |
"\n If meet any bug, please report to <leaflet@leafok.com>\n\n"); |
|
"\n If meet any bug, please report to <leaflet@leafok.com>\n\n" |
|
|
); |
|
| 36 |
} |
} |
| 37 |
|
|
| 38 |
void |
void |
| 39 |
arg_error(void) |
arg_error (void) |
| 40 |
{ |
{ |
| 41 |
prints ("Invalid arguments\n"); |
prints ("Invalid arguments\n"); |
| 42 |
app_help(); |
app_help (); |
| 43 |
} |
} |
| 44 |
|
|
| 45 |
int |
int |
| 46 |
main (int argc, char *argv[]) |
main (int argc, char *argv[]) |
| 47 |
{ |
{ |
| 48 |
char log_dir[256], file_log_std[256], file_log_error[256], file_config[256]; |
char log_dir[256], file_log_std[256], file_log_error[256], file_config[256]; |
| 49 |
int i,j; |
int i, j; |
| 50 |
int daemon = 1, std_log_redir = 0, error_log_redir = 0; |
int daemon = 1, std_log_redir = 0, error_log_redir = 0; |
| 51 |
|
|
| 52 |
//Parse args |
//Parse args |
| 53 |
for (i=1; i<argc; i++) |
for (i = 1; i < argc; i++) |
|
{ |
|
|
switch (argv[i][0]) |
|
| 54 |
{ |
{ |
| 55 |
case '-': |
switch (argv[i][0]) |
| 56 |
if (argv[i][1] != '-') |
{ |
| 57 |
{ |
case '-': |
| 58 |
for (j=1; j<strlen(argv[i]); j++) |
if (argv[i][1] != '-') |
| 59 |
{ |
{ |
| 60 |
switch (argv[i][j]) |
for (j = 1; j < strlen (argv[i]); j++) |
| 61 |
{ |
{ |
| 62 |
case 'f': |
switch (argv[i][j]) |
| 63 |
daemon = 0; |
{ |
| 64 |
break; |
case 'f': |
| 65 |
case 'h': |
daemon = 0; |
| 66 |
app_help(); |
break; |
| 67 |
exit(0); |
case 'h': |
| 68 |
case 'v': |
app_help (); |
| 69 |
puts (app_version); |
exit (0); |
| 70 |
exit(0); |
case 'v': |
| 71 |
default: |
puts (app_version); |
| 72 |
arg_error(); |
exit (0); |
| 73 |
exit(1); |
default: |
| 74 |
} |
arg_error (); |
| 75 |
} |
exit (1); |
| 76 |
} |
} |
| 77 |
else |
} |
| 78 |
{ |
} |
| 79 |
if (strcmp(argv[i]+2, "foreground") == 0) |
else |
| 80 |
{ |
{ |
| 81 |
daemon = 0; |
if (strcmp (argv[i] + 2, "foreground") == 0) |
| 82 |
break; |
{ |
| 83 |
} |
daemon = 0; |
| 84 |
if (strcmp(argv[i]+2, "help") == 0) |
break; |
| 85 |
{ |
} |
| 86 |
app_help(); |
if (strcmp (argv[i] + 2, "help") == 0) |
| 87 |
exit(0); |
{ |
| 88 |
} |
app_help (); |
| 89 |
if (strcmp(argv[i]+2, "version") == 0) |
exit (0); |
| 90 |
{ |
} |
| 91 |
puts (app_version); |
if (strcmp (argv[i] + 2, "version") == 0) |
| 92 |
exit(0); |
{ |
| 93 |
} |
puts (app_version); |
| 94 |
if (strcmp(argv[i]+2, "display-log") == 0) |
exit (0); |
| 95 |
{ |
} |
| 96 |
std_log_redir = 1; |
if (strcmp (argv[i] + 2, "display-log") == 0) |
| 97 |
} |
{ |
| 98 |
if (strcmp(argv[i]+2, "display-error-log") == 0) |
std_log_redir = 1; |
| 99 |
{ |
} |
| 100 |
error_log_redir = 1; |
if (strcmp (argv[i] + 2, "display-error-log") == 0) |
| 101 |
} |
{ |
| 102 |
} |
error_log_redir = 1; |
| 103 |
break; |
} |
| 104 |
|
} |
| 105 |
|
break; |
| 106 |
|
} |
| 107 |
} |
} |
| 108 |
} |
|
|
|
|
| 109 |
//Initialize daemon |
//Initialize daemon |
| 110 |
if (daemon) |
if (daemon) |
| 111 |
init_daemon (); |
init_daemon (); |
| 112 |
|
|
| 113 |
|
//Change current dir |
| 114 |
|
strncpy (app_home_dir, argv[0], rindex (argv[0], '/') - argv[0] + 1); |
| 115 |
|
strcat (app_home_dir, "../"); |
| 116 |
|
chdir (app_home_dir); |
| 117 |
|
|
| 118 |
//Initialize log |
//Initialize log |
| 119 |
strncpy(app_home_dir, argv[0], rindex(argv[0],'/')-argv[0]+1); |
strcpy (app_temp_dir, "/tmp/lbbs/"); |
| 120 |
strcat(app_home_dir, "../"); |
mkdir (app_temp_dir, 0750); |
| 121 |
strcpy(log_dir, app_home_dir); |
strcpy (log_dir, app_home_dir); |
| 122 |
strcat(log_dir, "log/"); |
strcat (log_dir, "log/"); |
| 123 |
strcpy(file_log_std, log_dir); |
strcpy (file_log_std, log_dir); |
| 124 |
strcpy(file_log_error, log_dir); |
strcpy (file_log_error, log_dir); |
| 125 |
strcat(file_log_std, "bbsd.log"); |
strcat (file_log_std, "bbsd.log"); |
| 126 |
strcat(file_log_error, "error.log"); |
strcat (file_log_error, "error.log"); |
| 127 |
mkdir(log_dir,0700); |
mkdir (log_dir, 0750); |
| 128 |
if (log_begin(file_log_std,file_log_error)<0) |
if (log_begin (file_log_std, file_log_error) < 0) |
| 129 |
exit(-1); |
exit (-1); |
| 130 |
|
|
| 131 |
if ((!daemon) && std_log_redir) |
if ((!daemon) && std_log_redir) |
| 132 |
log_std_redirect(2); |
log_std_redirect (2); |
| 133 |
if ((!daemon) && error_log_redir) |
if ((!daemon) && error_log_redir) |
| 134 |
log_err_redirect(3); |
log_err_redirect (3); |
| 135 |
|
|
| 136 |
//Load configuration |
//Load configuration |
| 137 |
strcpy(file_config, app_home_dir); |
strcpy (file_config, app_home_dir); |
| 138 |
strcat(file_config, "conf/bbsd.conf"); |
strcat (file_config, "conf/bbsd.conf"); |
| 139 |
if (load_conf(file_config)<0) |
if (load_conf (file_config) < 0) |
| 140 |
exit(-2); |
exit (-2); |
| 141 |
|
|
| 142 |
|
//Load menus |
| 143 |
|
strcpy (file_config, app_home_dir); |
| 144 |
|
strcat (file_config, "conf/menu.conf"); |
| 145 |
|
if (load_menu (&bbs_menu, file_config) < 0) |
| 146 |
|
exit (-3); |
| 147 |
|
|
| 148 |
|
//Set signal handler |
| 149 |
|
signal (SIG_RELOAD_MENU, reload_bbs_menu); |
| 150 |
|
|
| 151 |
//Initialize socket server |
//Initialize socket server |
| 152 |
net_server(BBS_address, BBS_port); |
net_server (BBS_address, BBS_port); |
| 153 |
|
|
| 154 |
return 0; |
return 0; |
| 155 |
} |
} |