1 | /* $Id: main.c 50 2024-09-18 11:06:12Z nishi $ */
|
---|
2 |
|
---|
3 | #define SOURCE
|
---|
4 |
|
---|
5 | #include "../config.h"
|
---|
6 |
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <stdbool.h>
|
---|
9 | #include <string.h>
|
---|
10 | #include <signal.h>
|
---|
11 |
|
---|
12 | #ifdef __MINGW32__
|
---|
13 | #include <windows.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #ifndef NO_SSL
|
---|
17 | #include <openssl/opensslv.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include <cm_log.h>
|
---|
21 |
|
---|
22 | #include "tw_config.h"
|
---|
23 | #include "tw_server.h"
|
---|
24 | #include "tw_version.h"
|
---|
25 |
|
---|
26 | extern bool cm_do_log;
|
---|
27 | extern struct tw_config config;
|
---|
28 |
|
---|
29 | char tw_server[2048];
|
---|
30 |
|
---|
31 | int main(int argc, char** argv) {
|
---|
32 | int i;
|
---|
33 | const char* confpath = PREFIX "/etc/tewi.conf";
|
---|
34 | for(i = 1; i < argc; i++) {
|
---|
35 | if(argv[i][0] == '-') {
|
---|
36 | if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
|
---|
37 | if(!cm_do_log) {
|
---|
38 | cm_do_log = true;
|
---|
39 | #ifndef NO_SSL
|
---|
40 | cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
|
---|
41 | #else
|
---|
42 | cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
|
---|
43 | #endif
|
---|
44 | } else {
|
---|
45 | cm_do_log = true;
|
---|
46 | }
|
---|
47 | } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
|
---|
48 | i++;
|
---|
49 | if(argv[i] == NULL) {
|
---|
50 | fprintf(stderr, "Missing argument\n");
|
---|
51 | return 1;
|
---|
52 | }
|
---|
53 | confpath = argv[i];
|
---|
54 | } else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
|
---|
55 | printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
|
---|
56 | printf("Under public domain.\n");
|
---|
57 | printf("Original by 2024 Nishi\n");
|
---|
58 | printf("\n");
|
---|
59 | printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
|
---|
60 | printf("--config | -C config : Specify config\n");
|
---|
61 | printf("--verbose | -v : Verbose mode\n");
|
---|
62 | printf("--version | -V : Version information\n");
|
---|
63 | return 0;
|
---|
64 | } else {
|
---|
65 | fprintf(stderr, "Unknown option: %s\n", argv[i]);
|
---|
66 | return 1;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | tw_config_init();
|
---|
71 | if(tw_config_read(confpath) != 0) {
|
---|
72 | fprintf(stderr, "Could not read the config\n");
|
---|
73 | return 1;
|
---|
74 | }
|
---|
75 | if(tw_server_init() != 0) {
|
---|
76 | fprintf(stderr, "Could not initialize the server\n");
|
---|
77 | return 1;
|
---|
78 | }
|
---|
79 | sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
|
---|
80 | cm_log("Daemon", "Ready, server: %s", tw_server);
|
---|
81 | #ifndef __MINGW32__
|
---|
82 | signal(SIGCHLD, SIG_IGN);
|
---|
83 | #else
|
---|
84 | SetConsoleTitle(tw_server);
|
---|
85 | #endif
|
---|
86 | tw_server_loop();
|
---|
87 | }
|
---|