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