source: Main/trunk/Server/main.c@ 209

Last change on this file since 209 was 209, checked in by Nishi, on Oct 2, 2024 at 12:01:30 PM

[release 2.03D]

  • Property svn:keywords set to Id
File size: 18.2 KB
Line 
1/* $Id: main.c 209 2024-10-02 03:01:30Z nishi $ */
2
3#define SOURCE
4
5#include "../config.h"
6
7#include <unistd.h>
8#include <stdio.h>
9#include <stdbool.h>
10#include <string.h>
11#include <signal.h>
12#include <stdlib.h>
13
14#ifndef NO_SSL
15#include <openssl/opensslv.h>
16#endif
17
18#include <cm_log.h>
19#include <cm_string.h>
20
21#include "tw_config.h"
22#include "tw_server.h"
23#include "tw_version.h"
24
25#ifdef __MINGW32__
26#include <windows.h>
27#endif
28
29#ifdef _PSP
30#include <pspkernel.h>
31#include <pspdebug.h>
32#include <pspsdk.h>
33#include <psputility.h>
34#include <pspctrl.h>
35#include <pspnet_apctl.h>
36#include <pspwlan.h>
37
38PSP_MODULE_INFO("Tewi HTTPd", PSP_MODULE_USER, 1, 1);
39PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
40
41#define printf(...) pspDebugScreenPrintf(__VA_ARGS__)
42#define STDERR_LOG(...) pspDebugScreenPrintf(__VA_ARGS__)
43#elif defined(__ps2sdk__)
44#include <debug.h>
45#include <iopcontrol.h>
46#include <sifrpc.h>
47#include <kernel.h>
48
49#define printf(...) scr_printf(__VA_ARGS__)
50#define STDERR_LOG(...) scr_printf(__VA_ARGS__)
51#elif defined(__PPU__)
52#include <rsx/gcm_sys.h>
53#include <rsx/rsx.h>
54#include <sysutil/video.h>
55#include <malloc.h>
56#include <sys/thread.h>
57#include <stdarg.h>
58#include <png.h>
59
60#define printf(...) tt_printf(__VA_ARGS__)
61#define STDERR_LOG(...) tt_printf(__VA_ARGS__)
62#else
63#define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__)
64#endif
65
66extern bool cm_do_log;
67extern struct tw_config config;
68extern FILE* logfile;
69
70char tw_server[2048];
71
72int startup(int argc, char** argv);
73
74#ifdef __MINGW32__
75char* get_registry(const char* main, const char* sub) {
76 DWORD bufsize = 512;
77 HKEY handle;
78 char* value = malloc(513);
79 int err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, main, 0, KEY_QUERY_VALUE, &handle);
80 if(err == ERROR_SUCCESS) {
81 err = RegQueryValueEx(handle, sub, NULL, NULL, value, &bufsize);
82 if(err != ERROR_SUCCESS) {
83 free(value);
84 RegCloseKey(handle);
85 return NULL;
86 }
87 RegCloseKey(handle);
88 return value;
89 } else {
90 free(value);
91 return NULL;
92 }
93}
94#endif
95
96#ifdef SERVICE
97SERVICE_STATUS status;
98SERVICE_STATUS_HANDLE status_handle;
99
100void WINAPI servhandler(DWORD control) {
101 switch(control) {
102 case SERVICE_CONTROL_STOP:
103 case SERVICE_CONTROL_SHUTDOWN:
104 status.dwCurrentState = SERVICE_STOP_PENDING;
105 break;
106 }
107 SetServiceStatus(status_handle, &status);
108}
109
110void WINAPI servmain(DWORD argc, LPSTR* argv) {
111 char* path = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir");
112 if(path != NULL) {
113 char* lpath = cm_strcat(path, "/logs/tewi.log");
114 logfile = fopen(lpath, "a");
115 free(lpath);
116 free(path);
117 } else {
118 logfile = fopen(PREFIX "/logs/tewi.log", "a");
119 }
120 if(logfile == NULL) logfile = stderr;
121 status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
122 status.dwCurrentState = SERVICE_START_PENDING;
123 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
124 status.dwWin32ExitCode = NO_ERROR;
125 status.dwServiceSpecificExitCode = 0;
126 status.dwCheckPoint = 0;
127 status.dwWaitHint = 0;
128 status_handle = RegisterServiceCtrlHandler("Tewi HTTPd", servhandler);
129 if(status_handle == NULL) return;
130 if(SetServiceStatus(status_handle, &status) == 0) return;
131 int st = startup(argc, argv);
132 if(st != -1) {
133 status.dwWin32ExitCode = NO_ERROR;
134 status.dwServiceSpecificExitCode = st;
135 status.dwCurrentState = SERVICE_STOPPED;
136 SetServiceStatus(status_handle, &status);
137 return;
138 }
139 status.dwCurrentState = SERVICE_RUNNING;
140 SetServiceStatus(status_handle, &status);
141 tw_server_loop();
142 status.dwCurrentState = SERVICE_STOPPED;
143 SetServiceStatus(status_handle, &status);
144}
145#endif
146
147int running = 1;
148#ifdef _PSP
149
150int psp_exit_callback(int arg1, int arg2, void* arg3) { running = 0; }
151
152int psp_callback_thread(SceSize args, void* argp) {
153 int cid;
154 cid = sceKernelCreateCallback("Exit Call Back", psp_exit_callback, NULL);
155 sceKernelRegisterExitCallback(cid);
156 sceKernelSleepThreadCB();
157 return 0;
158}
159#endif
160
161#ifdef __PPU__
162uint32_t depth_pitch;
163uint32_t depth_offset;
164uint32_t* depth_buffer;
165
166#define CB_SIZE 0x100000
167#define HOST_SIZE (32 * 1024 * 1024)
168
169struct rsx_buffer {
170 int width, height, id;
171 uint32_t* ptr;
172 uint32_t offset;
173};
174
175void wait_rsx(gcmContextData* ctx, uint32_t label) {
176 rsxSetWriteBackendLabel(ctx, GCM_INDEX_TYPE_32B, label);
177
178 rsxFlushBuffer(ctx);
179
180 while(*(uint32_t*)gcmGetLabelAddress(GCM_INDEX_TYPE_32B) != label) usleep(50);
181
182 label++;
183}
184
185void wait_rsx_until_idle(gcmContextData* ctx) {
186 uint32_t label = 1;
187 rsxSetWriteBackendLabel(ctx, GCM_INDEX_TYPE_32B, label);
188 rsxSetWaitLabel(ctx, GCM_INDEX_TYPE_32B, label);
189 label++;
190 wait_rsx(ctx, label);
191}
192
193void get_resolution(int* width, int* height) {
194 videoState state;
195 videoResolution res;
196 if(videoGetState(0, 0, &state) != 0) {
197 return;
198 }
199
200 if(state.state != 0) {
201 return;
202 }
203
204 if(videoGetResolution(state.displayMode.resolution, &res) != 0) {
205 return;
206 }
207 *width = res.width;
208 *height = res.height;
209}
210
211void make_buffer(struct rsx_buffer* buffer, int id) {
212 int w, h;
213 get_resolution(&w, &h);
214
215 buffer->ptr = (uint32_t*)rsxMemalign(64, 4 * w * h);
216 if(buffer->ptr == NULL) return;
217
218 if(rsxAddressToOffset(buffer->ptr, &buffer->offset) != 0) return;
219
220 if(gcmSetDisplayBuffer(id, buffer->offset, 4 * w, w, h) != 0) return;
221 buffer->width = w;
222 buffer->height = h;
223 buffer->id = id;
224}
225
226gcmContextData* init_screen(void) {
227 void* host = memalign(1024 * 1024, HOST_SIZE);
228 gcmContextData* ctx = NULL;
229 videoState state;
230 videoConfiguration vconfig;
231 videoResolution res;
232 rsxInit(&ctx, CB_SIZE, HOST_SIZE, host);
233 if(ctx == NULL) {
234 free(host);
235 return NULL;
236 }
237
238 if(videoGetState(0, 0, &state) != 0) {
239 rsxFinish(ctx, 0);
240 free(host);
241 return NULL;
242 }
243
244 if(state.state != 0) {
245 rsxFinish(ctx, 0);
246 free(host);
247 return NULL;
248 }
249
250 if(videoGetResolution(state.displayMode.resolution, &res) != 0) {
251 rsxFinish(ctx, 0);
252 free(host);
253 return NULL;
254 }
255
256 memset(&vconfig, 0, sizeof(vconfig));
257 vconfig.resolution = state.displayMode.resolution;
258 vconfig.format = VIDEO_BUFFER_FORMAT_XRGB;
259 vconfig.pitch = res.width * 4;
260 vconfig.aspect = state.displayMode.aspect;
261
262 wait_rsx_until_idle(ctx);
263
264 if(videoConfigure(0, &vconfig, NULL, 0) != 0) {
265 rsxFinish(ctx, 0);
266 free(host);
267 return NULL;
268 }
269
270 if(videoGetState(0, 0, &state) != 0) {
271 rsxFinish(ctx, 0);
272 free(host);
273 return NULL;
274 }
275 gcmSetFlipMode(GCM_FLIP_VSYNC);
276
277 depth_pitch = res.width * 4;
278 depth_buffer = (uint32_t*)rsxMemalign(64, (res.height * depth_pitch) * 2);
279 rsxAddressToOffset(depth_buffer, &depth_offset);
280
281 gcmResetFlipStatus();
282
283 return ctx;
284}
285
286void set_render_target(gcmContextData* context, struct rsx_buffer* buffer) {
287 gcmSurface sf;
288
289 sf.colorFormat = GCM_SURFACE_X8R8G8B8;
290 sf.colorTarget = GCM_SURFACE_TARGET_0;
291 sf.colorLocation[0] = GCM_LOCATION_RSX;
292 sf.colorOffset[0] = buffer->offset;
293 sf.colorPitch[0] = depth_pitch;
294
295 sf.colorLocation[1] = GCM_LOCATION_RSX;
296 sf.colorLocation[2] = GCM_LOCATION_RSX;
297 sf.colorLocation[3] = GCM_LOCATION_RSX;
298 sf.colorOffset[1] = 0;
299 sf.colorOffset[2] = 0;
300 sf.colorOffset[3] = 0;
301 sf.colorPitch[1] = 64;
302 sf.colorPitch[2] = 64;
303 sf.colorPitch[3] = 64;
304
305 sf.depthFormat = GCM_SURFACE_ZETA_Z16;
306 sf.depthLocation = GCM_LOCATION_RSX;
307 sf.depthOffset = depth_offset;
308 sf.depthPitch = depth_pitch;
309
310 sf.type = GCM_TEXTURE_LINEAR;
311 sf.antiAlias = GCM_SURFACE_CENTER_1;
312
313 sf.width = buffer->width;
314 sf.height = buffer->height;
315 sf.x = 0;
316 sf.y = 0;
317
318 rsxSetSurface(context, &sf);
319}
320
321void wait_flip(void) {
322 while(gcmGetFlipStatus() != 0) usleep(200);
323 gcmResetFlipStatus();
324}
325
326void flip(gcmContextData* ctx, uint32_t buffer) {
327 if(gcmSetFlip(ctx, buffer) == 0) {
328 rsxFlushBuffer(ctx);
329 gcmSetWaitFlip(ctx);
330 }
331}
332
333uint8_t* tvram;
334
335extern uint8_t font[];
336
337int tt_x = 0;
338int tt_y = 0;
339int tt_width;
340int tt_height;
341
342void tt_putstr(const char* str) {
343 int i;
344 for(i = 0; str[i] != 0; i++) {
345 tvram[tt_y * tt_width + tt_x] = str[i];
346 if(str[i] == '\n') {
347 tt_x = 0;
348 tt_y++;
349 } else {
350 tt_x++;
351 if(tt_x == tt_width) {
352 tt_x = 0;
353 tt_y++;
354 }
355 }
356 if(tt_y == tt_height) {
357 tt_y--;
358 int x, y;
359 for(y = 0; y < tt_height - 1; y++) {
360 for(x = 0; x < tt_width; x++) {
361 tvram[y * tt_width + x] = tvram[(y + 1) * tt_width + x];
362 }
363 }
364 for(x = 0; x < tt_width; x++) {
365 tvram[(tt_height - 1) * tt_width + x] = 0x20;
366 }
367 }
368 }
369}
370
371void tt_putchar(struct rsx_buffer* buffer, int x, int y, uint8_t c) {
372 int i, j;
373 if(c == 0) return;
374 if(c < 0x20) c = 0x20;
375 if(c >= 0x7f) c = 0x20;
376 for(i = 0; i < 8; i++) {
377 uint8_t l = i == 7 ? 0 : font[(c - 0x20) * 8 + i];
378 for(j = 0; j < 6; j++) {
379 uint32_t col = 0;
380 if(l & (1 << 7)) {
381 col = 0xffffff;
382 }
383 l = l << 1;
384 buffer->ptr[(y * 8 + i) * buffer->width + x * 6 + j] = col;
385 }
386 }
387}
388
389void draw(struct rsx_buffer* buffer, int current) {
390 int i, j, c;
391 for(i = 0; i < buffer->height / 8; i++) {
392 for(j = 0; j < buffer->width / 6; j++) {
393 uint8_t c = tvram[i * (buffer->width / 6) + j];
394 tt_putchar(buffer, j, i, c);
395 }
396 }
397}
398
399#define BUFFERS 1
400gcmContextData* ctx;
401struct rsx_buffer buffers[BUFFERS];
402
403void text_thread(void* arg) {
404 int current = 0;
405 while(1) {
406 wait_flip();
407 draw(&buffers[current], current);
408 flip(ctx, buffers[current].id);
409 current++;
410 if(current >= BUFFERS) current = 0;
411 }
412}
413
414void tt_printf(const char* tmpl, ...) {
415 va_list va;
416 va_start(va, tmpl);
417 int i;
418 char cbuf[2];
419 cbuf[1] = 0;
420 char* log = cm_strdup("");
421 for(i = 0; tmpl[i] != 0; i++) {
422 if(tmpl[i] == '%') {
423 i++;
424 if(tmpl[i] == 's') {
425 char* tmp = log;
426 log = cm_strcat(tmp, va_arg(va, char*));
427 free(tmp);
428 } else if(tmpl[i] == 'd') {
429 char buf[513];
430 sprintf(buf, "%d", va_arg(va, int));
431 char* tmp = log;
432 log = cm_strcat(tmp, buf);
433 free(tmp);
434 } else if(tmpl[i] == '%') {
435 char* tmp = log;
436 log = cm_strcat(tmp, "%");
437 free(tmp);
438 }
439 } else {
440 cbuf[0] = tmpl[i];
441 char* tmp = log;
442 log = cm_strcat(tmp, cbuf);
443 free(tmp);
444 }
445 }
446 va_end(va);
447 tt_putstr(log);
448}
449
450void show_png(void) {
451 FILE* f = fopen(PREFIX "/pbtewi.png", "rb");
452 if(f == NULL) {
453 f = fopen(PREFIX "/../ICON0.PNG", "rb");
454 }
455 if(f == NULL) return;
456 png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
457 png_infop info = png_create_info_struct(png);
458 if(setjmp(png_jmpbuf(png))) {
459 png_destroy_read_struct(&png, &info, NULL);
460 fclose(f);
461 return;
462 }
463
464 png_init_io(png, f);
465 png_read_info(png, info);
466
467 int width = png_get_image_width(png, info);
468 int height = png_get_image_height(png, info);
469 int depth = png_get_bit_depth(png, info);
470 int type = png_get_color_type(png, info);
471
472 if(depth == 16) png_set_strip_16(png);
473 if(type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
474 if(type == PNG_COLOR_TYPE_GRAY && depth < 8) png_set_expand_gray_1_2_4_to_8(png);
475 if(png_get_valid(png, info, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png);
476 if(type == PNG_COLOR_TYPE_RGB || type == PNG_COLOR_TYPE_GRAY || type == PNG_COLOR_TYPE_PALETTE) png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
477 if(type == PNG_COLOR_TYPE_GRAY || type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png);
478 png_read_update_info(png, info);
479 png_bytep* rows = (png_bytep*)malloc(sizeof(*rows) * (height));
480
481 int i;
482
483 for(i = 0; i < height; i++) {
484 rows[i] = (png_byte*)malloc(png_get_rowbytes(png, info));
485 }
486
487 png_read_image(png, rows);
488
489 for(i = 0; i < height; i++) {
490 int j;
491 for(j = 0; j < width; j++) {
492 png_bytep byte = &(rows[i][j * 4]);
493 uint32_t col = (byte[0] << 16) | (byte[1] << 8) | (byte[2]);
494 int k;
495 for(k = 0; k < BUFFERS; k++) {
496 buffers[k].ptr[buffers[k].width * i - width + j] = col;
497 }
498 }
499 }
500
501 png_destroy_read_struct(&png, &info, NULL);
502 fclose(f);
503
504 for(i = 0; i < height; i++) {
505 free(rows[i]);
506 }
507 free(rows);
508}
509
510#endif
511
512int main(int argc, char** argv) {
513 logfile = stderr;
514#ifdef SERVICE
515 SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
516 StartServiceCtrlDispatcher(table);
517#else
518#ifdef _PSP
519 pspDebugScreenInit();
520 pspDebugScreenSetXY(0, 0);
521 printf("PSP Bootstrap, Tewi/%s\n", tw_get_version());
522 int thid = sceKernelCreateThread("update_thread", psp_callback_thread, 0x11, 0xfa0, 0, NULL);
523 if(thid >= 0) {
524 sceKernelStartThread(thid, 0, NULL);
525 } else {
526 printf("Failed to start thread\n");
527 while(running) sceKernelDelayThread(50 * 1000);
528 sceKernelExitGame();
529 }
530 sceCtrlSetSamplingCycle(0);
531 sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
532 sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
533 sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
534 if(pspSdkInetInit()) {
535 printf("Could not init the network\n");
536 while(running) sceKernelDelayThread(50 * 1000);
537 sceKernelExitGame();
538 } else {
539 printf("Network initialization successful\n");
540 }
541 if(sceWlanGetSwitchState() != 1) {
542 printf("Turn the Wi-Fi switch on\n");
543 while(sceWlanGetSwitchState() != 1) {
544 sceKernelDelayThread(1000 * 1000);
545 }
546 } else {
547 printf("Wi-Fi is turned on\n");
548 }
549 int i;
550 int choice[100];
551 int incr = 0;
552 int last = 0;
553 int cur = 0;
554 for(i = 1; i < 100; i++) {
555 choice[i - 1] = 0;
556 netData name;
557 netData data;
558 if(sceUtilityCheckNetParam(i) != 0) continue;
559 choice[incr++] = i;
560 pspDebugScreenSetXY(0, 1 + 3 + incr - 1);
561 if(incr == 1) printf("> ");
562 pspDebugScreenSetXY(2, 1 + 3 + incr - 1);
563 sceUtilityGetNetParam(i, 0, &name);
564 sceUtilityGetNetParam(i, 1, &data);
565 printf("SSID=%s", data.asString);
566 sceUtilityGetNetParam(i, 4, &data);
567 if(data.asString[0]) {
568 sceUtilityGetNetParam(i, 5, &data);
569 printf(" IPADDR=%s\n", data.asString);
570 } else {
571 printf(" DHCP\n");
572 }
573 }
574 int press = 0;
575 while(1) {
576 if(!running) {
577 sceKernelExitGame();
578 }
579 SceCtrlData c;
580 sceCtrlReadBufferPositive(&c, 1);
581 press = 0;
582 if(c.Buttons & PSP_CTRL_DOWN) {
583 if(cur < incr - 1) {
584 cur++;
585 }
586 press = 1;
587 } else if(c.Buttons & PSP_CTRL_UP) {
588 if(cur > 0) {
589 cur--;
590 }
591 press = -1;
592 } else if(c.Buttons & PSP_CTRL_START) {
593 break;
594 }
595 if(last != cur) {
596 pspDebugScreenSetXY(0, 1 + 3 + last);
597 printf(" ");
598 pspDebugScreenSetXY(0, 1 + 3 + cur);
599 printf("> ");
600 last = cur;
601 }
602 if(press != 0) {
603 while(1) {
604 SceCtrlData c;
605 sceCtrlReadBufferPositive(&c, 1);
606 if(press == 1) {
607 if(!(c.Buttons & PSP_CTRL_DOWN)) break;
608 } else if(press == -1) {
609 if(!(c.Buttons & PSP_CTRL_UP)) break;
610 }
611 }
612 }
613 }
614 pspDebugScreenSetXY(0, 1 + 3 + incr + 1);
615 int err = sceNetApctlConnect(choice[cur]);
616 if(err != 0) {
617 printf("Apctl initialization failure\n");
618 while(running) sceKernelDelayThread(50 * 1000);
619 sceKernelExitGame();
620 } else {
621 printf("Apctl initialization successful\n");
622 }
623 printf("Apctl connecting\n");
624 while(1) {
625 int state;
626 err = sceNetApctlGetState(&state);
627 if(err != 0) {
628 printf("Apctl getting status failure\n");
629 while(running) sceKernelDelayThread(50 * 1000);
630 sceKernelExitGame();
631 }
632 if(state == 4) {
633 break;
634 }
635 sceKernelDelayThread(50 * 1000);
636 }
637 union SceNetApctlInfo info;
638 if(sceNetApctlGetInfo(8, &info) != 0) {
639 printf("Got an unknown IP\n");
640 while(running) sceKernelDelayThread(50 * 1000);
641 sceKernelExitGame();
642 }
643 printf("Connected, My IP is %s\n", info.ip);
644#elif defined(__PPU__)
645 int i;
646 ctx = init_screen();
647 int w, h;
648 get_resolution(&w, &h);
649 tt_width = w / 6;
650 tt_height = h / 8;
651 tvram = malloc((w / 6) * (h / 8));
652 for(i = 0; i < BUFFERS; i++) make_buffer(&buffers[i], i);
653 flip(ctx, BUFFERS - 1);
654 sys_ppu_thread_t id;
655 sysThreadCreate(&id, text_thread, NULL, 1500, 0x1000, THREAD_JOINABLE, "TextThread");
656 printf("PS3 Bootstrap, Tewi/%s\n", tw_get_version());
657 show_png();
658 netInitialize();
659#elif defined(__ps2sdk__)
660 SifInitRpc(0);
661 while(!SifIopReset("", 0))
662 ;
663 while(!SifIopSync())
664 ;
665 init_scr();
666 scr_printf("PS2 Bootstrap, Tewi/%s\n", tw_get_version());
667 SleepThread();
668#endif
669 int st = startup(argc, argv);
670 if(st != -1) {
671#ifdef _PSP
672 printf("Error code %d\n", st);
673 while(running) sceKernelDelayThread(50 * 1000);
674 sceKernelExitGame();
675#else
676#ifdef __PPU__
677 printf("Error code %d\n", st);
678 while(1)
679 ;
680#endif
681 return st;
682#endif
683 }
684 tw_server_loop();
685#endif
686#ifdef _PSP
687 sceKernelExitGame();
688#endif
689 return 0;
690}
691
692int startup(int argc, char** argv) {
693 int i;
694#ifdef __MINGW32__
695 char* confpath = cm_strdup(PREFIX "/etc/tewi.conf");
696 char* regpath = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir");
697 if(regpath != NULL) {
698 free(confpath);
699 confpath = cm_strcat(regpath, "/etc/tewi.conf");
700 free(regpath);
701 }
702#else
703 const char* confpath = PREFIX "/etc/tewi.conf";
704#endif
705 if(argv != NULL) {
706 for(i = 1; i < argc; i++) {
707 if(argv[i][0] == '-') {
708 if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
709 if(!cm_do_log) {
710 cm_do_log = true;
711#ifndef NO_SSL
712 cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
713#else
714 cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
715#endif
716 } else {
717 cm_do_log = true;
718 }
719 } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
720 i++;
721 if(argv[i] == NULL) {
722 STDERR_LOG("Missing argument\n");
723 return 1;
724 }
725 confpath = argv[i];
726#ifndef _PSP
727 } else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
728 i++;
729 if(argv[i] == NULL) {
730 STDERR_LOG("Missing argument\n");
731 return 1;
732 }
733 if(logfile != NULL && logfile != stderr) {
734 fclose(logfile);
735 }
736 logfile = fopen(argv[i], "a");
737 if(logfile == NULL) {
738 STDERR_LOG("Failed to open logfile\n");
739 return 1;
740 }
741#endif
742 } else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
743 printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
744 printf("Under public domain.\n");
745 printf("Original by 2024 Nishi\n");
746 printf("\n");
747 printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
748 printf("--config | -C config : Specify config\n");
749#ifndef _PSP
750 printf("--logfile | -l logfile : Specify logfile\n");
751#endif
752 printf("--verbose | -v : Verbose mode\n");
753 printf("--version | -V : Version information\n");
754 return 0;
755 } else {
756 STDERR_LOG("Unknown option: %s\n", argv[i]);
757 return 1;
758 }
759 }
760 }
761 }
762 tw_config_init();
763 if(tw_config_read(confpath) != 0) {
764 STDERR_LOG("Could not read the config\n");
765 return 1;
766 }
767 if(tw_server_init() != 0) {
768 STDERR_LOG("Could not initialize the server\n");
769 return 1;
770 }
771 sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
772 char* r = cm_strcat(tw_server, " running...");
773 cm_force_log(r);
774 free(r);
775#ifndef __MINGW32__
776 signal(SIGCHLD, SIG_IGN);
777 signal(SIGPIPE, SIG_IGN);
778#else
779 SetConsoleTitle(tw_server);
780#endif
781 return -1;
782}
Note: See TracBrowser for help on using the repository browser.