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