/[LeafOK_CVS]/lbbs/src/main.c
ViewVC logotype

Diff of /lbbs/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.91 by sysadm, Fri Dec 19 14:08:44 2025 UTC Revision 1.94 by sysadm, Thu Jan 8 14:22:57 2026 UTC
# Line 3  Line 3 
3   * main   * main
4   *   - entry of server program   *   - entry of server program
5   *   *
6   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>   * Copyright (C) 2004-2026  Leaflet <leaflet@leafok.com>
7   */   */
8    
9  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
# Line 34  Line 34 
34  #include <sys/types.h>  #include <sys/types.h>
35  #include <sys/wait.h>  #include <sys/wait.h>
36    
37  static inline void app_help(void)  typedef void (*arg_option_handler_t)(void);
38    
39    struct arg_option_t
40    {
41            const char *short_arg;
42            const char *long_arg;
43            const char *description;
44            arg_option_handler_t handler;
45    };
46    typedef struct arg_option_t ARG_OPTION;
47    
48    static void arg_foreground(void);
49    static void arg_help(void);
50    static void arg_version(void);
51    static void arg_display_log(void);
52    static void arg_display_error_log(void);
53    static void arg_compile_info(void);
54    static void arg_error(void);
55    
56    static const ARG_OPTION arg_options[] = {
57            {"-f", "--foreground", "Run in foreground", arg_foreground},
58            {"-h", "--help", "Display help message", arg_help},
59            {"-v", "--version", "Display version information", arg_version},
60            {NULL, "--display-log", "Display standard log information", arg_display_log},
61            {NULL, "--display-error-log", "Display error log information", arg_display_error_log},
62            {"-C", "--compile-config", "Display compile configuration", arg_compile_info}};
63    
64    static const int arg_options_count = sizeof(arg_options) / sizeof(ARG_OPTION);
65    
66    static int daemon_service = 1;
67    static int std_log_redir = 0;
68    static int error_log_redir = 0;
69    
70    static void arg_foreground(void)
71  {  {
72          fprintf(stderr, "Usage: bbsd [-fhv] [...]\n\n"          daemon_service = 0;
                                         "-f\t--foreground\t\tForce program run in foreground\n"  
                                         "-h\t--help\t\t\tDisplay this help message\n"  
                                         "-v\t--version\t\tDisplay version information\n"  
                                         "\t--display-log\t\tDisplay standard log information\n"  
                                         "\t--display-error-log\tDisplay error log information\n"  
                                         "-C\t--compile-config\tDisplay compile configuration\n"  
                                         "\n    If meet any bug, please report to <leaflet@leafok.com>\n\n");  
73  }  }
74    
75  static inline void arg_error(void)  static void arg_usage(void)
76  {  {
77          fprintf(stderr, "Invalid arguments\n");          fprintf(stderr, "Usage: bbsd [-fhvC] [...]\n\n");
78          app_help();  
79            for (int i = 0; i < arg_options_count; i++)
80            {
81                    fprintf(stderr, "%s%*s%s%*s%s\n",
82                                    arg_options[i].short_arg ? arg_options[i].short_arg : "",
83                                    8 - (arg_options[i].short_arg ? (int)strlen(arg_options[i].short_arg) : 0), "",
84                                    arg_options[i].long_arg ? arg_options[i].long_arg : "",
85                                    24 - (arg_options[i].long_arg ? (int)strlen(arg_options[i].long_arg) : 0), "",
86                                    arg_options[i].description ? arg_options[i].description : "");
87            }
88    
89            fprintf(stderr, "\n    If meet any bug, please report to <leaflet@leafok.com>\n\n");
90    }
91    
92    static void arg_help(void)
93    {
94            arg_usage();
95    
96            exit(0);
97    }
98    
99    static void arg_version(void)
100    {
101            fprintf(stderr, "%s\n", APP_INFO);
102            fprintf(stderr, "%s\n", COPYRIGHT_INFO);
103    
104            exit(0);
105  }  }
106    
107  static inline void app_compile_info(void)  static void arg_display_log(void)
108  {  {
109          printf("%s\n"          std_log_redir = 1;
110                     "--enable-shared\t\t[%s]\n"  }
111                     "--enable-systemd\t[%s]\n"  
112                     "--with-debug\t\t[%s]\n"  static void arg_display_error_log(void)
113                     "--with-epoll\t\t[%s]\n"  {
114                     "--with-mariadb\t\t[%s]\n"          error_log_redir = 1;
115                     "--with-sysv\t\t[%s]\n",  }
116                     APP_INFO,  
117    static void arg_compile_info(void)
118    {
119            fprintf(stderr, "%s\n"
120                                            "--enable-shared\t\t[%s]\n"
121                                            "--enable-systemd\t[%s]\n"
122                                            "--with-debug\t\t[%s]\n"
123                                            "--with-epoll\t\t[%s]\n"
124                                            "--with-mariadb\t\t[%s]\n"
125                                            "--with-sysv\t\t[%s]\n",
126                            APP_INFO,
127  #ifdef _ENABLE_SHARED  #ifdef _ENABLE_SHARED
128                     "yes",                          "yes",
129  #else  #else
130                     "no",                          "no",
131  #endif  #endif
132  #ifdef HAVE_SYSTEMD_SD_DAEMON_H  #ifdef HAVE_SYSTEMD_SD_DAEMON_H
133                     "yes",                          "yes",
134  #else  #else
135                     "no",                          "no",
136  #endif  #endif
137  #ifdef _DEBUG  #ifdef _DEBUG
138                     "yes",                          "yes",
139  #else  #else
140                     "no",                          "no",
141  #endif  #endif
142  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
143                     "yes",                          "yes",
144  #else  #else
145                     "no",                          "no",
146  #endif  #endif
147  #ifdef HAVE_MARIADB_CLIENT  #ifdef HAVE_MARIADB_CLIENT
148                     "yes",                          "yes",
149  #else  #else
150                     "no",                          "no",
151  #endif  #endif
152  #ifdef HAVE_SYSTEM_V  #ifdef HAVE_SYSTEM_V
153                     "yes"                          "yes"
154  #else  #else
155                     "no"                          "no"
156  #endif  #endif
157          );          );
158    
159            exit(0);
160    }
161    
162    static void arg_error(void)
163    {
164            fprintf(stderr, "Invalid arguments\n");
165            arg_usage();
166    
167            exit(1);
168  }  }
169    
170  int main(int argc, char *argv[])  int main(int argc, char *argv[])
171  {  {
172          char file_path_temp[FILE_PATH_LEN];          char file_path_temp[FILE_PATH_LEN];
         int daemon = 1;  
         int std_log_redir = 0;  
         int error_log_redir = 0;  
173          FILE *fp;          FILE *fp;
174          int ret;          int ret;
175          int last_aid;          int last_aid;
# Line 112  int main(int argc, char *argv[]) Line 181  int main(int argc, char *argv[])
181          // Parse args          // Parse args
182          for (i = 1; i < argc; i++)          for (i = 1; i < argc; i++)
183          {          {
184                  switch (argv[i][0])                  for (j = 0; j < arg_options_count; j++)
185                  {                  {
186                  case '-':                          if (arg_options[j].short_arg && strcmp(argv[i], arg_options[j].short_arg) == 0)
                         if (argv[i][1] != '-')  
187                          {                          {
188                                  for (j = 1; j < strlen(argv[i]); j++)                                  arg_options[j].handler();
189                                  {                                  break;
                                         switch (argv[i][j])  
                                         {  
                                         case 'f':  
                                                 daemon = 0;  
                                                 break;  
                                         case 'h':  
                                                 app_help();  
                                                 return 0;  
                                         case 'v':  
                                                 printf("%s\n", APP_INFO);  
                                                 printf("%s\n", COPYRIGHT_INFO);  
                                                 return 0;  
                                         case 'C':  
                                                 app_compile_info();  
                                                 return 0;  
                                         default:  
                                                 arg_error();  
                                                 return 1;  
                                         }  
                                 }  
190                          }                          }
191                          else                          else if (arg_options[j].long_arg && strcmp(argv[i], arg_options[j].long_arg) == 0)
192                          {                          {
193                                  if (strcmp(argv[i] + 2, "foreground") == 0)                                  arg_options[j].handler();
194                                  {                                  break;
                                         daemon = 0;  
                                         break;  
                                 }  
                                 if (strcmp(argv[i] + 2, "help") == 0)  
                                 {  
                                         app_help();  
                                         return 0;  
                                 }  
                                 if (strcmp(argv[i] + 2, "version") == 0)  
                                 {  
                                         printf("%s\n", APP_INFO);  
                                         printf("%s\n", COPYRIGHT_INFO);  
                                         return 0;  
                                 }  
                                 if (strcmp(argv[i] + 2, "display-log") == 0)  
                                 {  
                                         std_log_redir = 1;  
                                 }  
                                 if (strcmp(argv[i] + 2, "display-error-log") == 0)  
                                 {  
                                         error_log_redir = 1;  
                                 }  
                                 if (strcmp(argv[i] + 2, "compile-config") == 0)  
                                 {  
                                         app_compile_info();  
                                         return 0;  
                                 }  
195                          }                          }
196                          break;                  }
197                    if (j == arg_options_count)
198                    {
199                            arg_error();
200                            return -1;
201                  }                  }
202          }          }
203    
204          // Initialize daemon          // Initialize daemon
205          if (daemon)          if (daemon_service)
206          {          {
207                  init_daemon();                  init_daemon();
208          }          }
# Line 204  int main(int argc, char *argv[]) Line 229  int main(int argc, char *argv[])
229                  return -1;                  return -1;
230          }          }
231    
232            fprintf(stderr, "%s\n", APP_INFO);
233    
234          // Initialize log          // Initialize log
235          if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0)          if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0)
236          {          {
237                  return -1;                  return -1;
238          }          }
239    
240          if ((!daemon) && std_log_redir)          if ((!daemon_service) && std_log_redir)
241          {          {
242                  log_common_redir(STDERR_FILENO);                  log_common_redir(STDERR_FILENO);
243          }          }
244          if ((!daemon) && error_log_redir)          if ((!daemon_service) && error_log_redir)
245          {          {
246                  log_error_redir(STDERR_FILENO);                  log_error_redir(STDERR_FILENO);
247          }          }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1