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

Last change on this file since 208 was 208, checked in by Nishi, on Oct 2, 2024 at 11:59:59 AM

do this instead

  • Property svn:keywords set to Id
File size: 18.4 KB
Line 
1/* $Id: main.c 208 2024-10-02 02:59:59Z 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 printf("%s\n", get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir"));
514 logfile = stderr;
515#ifdef SERVICE
516 SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
517 StartServiceCtrlDispatcher(table);
518#else
519#ifdef _PSP
520 pspDebugScreenInit();
521 pspDebugScreenSetXY(0, 0);
522 printf("PSP Bootstrap, Tewi/%s\n", tw_get_version());
523 int thid = sceKernelCreateThread("update_thread", psp_callback_thread, 0x11, 0xfa0, 0, NULL);
524 if(thid >= 0) {
525 sceKernelStartThread(thid, 0, NULL);
526 } else {
527 printf("Failed to start thread\n");
528 while(running) sceKernelDelayThread(50 * 1000);
529 sceKernelExitGame();
530 }
531 sceCtrlSetSamplingCycle(0);
532 sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
533 sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
534 sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
535 if(pspSdkInetInit()) {
536 printf("Could not init the network\n");
537 while(running) sceKernelDelayThread(50 * 1000);
538 sceKernelExitGame();
539 } else {
540 printf("Network initialization successful\n");
541 }
542 if(sceWlanGetSwitchState() != 1) {
543 printf("Turn the Wi-Fi switch on\n");
544 while(sceWlanGetSwitchState() != 1) {
545 sceKernelDelayThread(1000 * 1000);
546 }
547 } else {
548 printf("Wi-Fi is turned on\n");
549 }
550 int i;
551 int choice[100];
552 int incr = 0;
553 int last = 0;
554 int cur = 0;
555 for(i = 1; i < 100; i++) {
556 choice[i - 1] = 0;
557 netData name;
558 netData data;
559 if(sceUtilityCheckNetParam(i) != 0) continue;
560 choice[incr++] = i;
561 pspDebugScreenSetXY(0, 1 + 3 + incr - 1);
562 if(incr == 1) printf("> ");
563 pspDebugScreenSetXY(2, 1 + 3 + incr - 1);
564 sceUtilityGetNetParam(i, 0, &name);
565 sceUtilityGetNetParam(i, 1, &data);
566 printf("SSID=%s", data.asString);
567 sceUtilityGetNetParam(i, 4, &data);
568 if(data.asString[0]) {
569 sceUtilityGetNetParam(i, 5, &data);
570 printf(" IPADDR=%s\n", data.asString);
571 } else {
572 printf(" DHCP\n");
573 }
574 }
575 int press = 0;
576 while(1) {
577 if(!running) {
578 sceKernelExitGame();
579 }
580 SceCtrlData c;
581 sceCtrlReadBufferPositive(&c, 1);
582 press = 0;
583 if(c.Buttons & PSP_CTRL_DOWN) {
584 if(cur < incr - 1) {
585 cur++;
586 }
587 press = 1;
588 } else if(c.Buttons & PSP_CTRL_UP) {
589 if(cur > 0) {
590 cur--;
591 }
592 press = -1;
593 } else if(c.Buttons & PSP_CTRL_START) {
594 break;
595 }
596 if(last != cur) {
597 pspDebugScreenSetXY(0, 1 + 3 + last);
598 printf(" ");
599 pspDebugScreenSetXY(0, 1 + 3 + cur);
600 printf("> ");
601 last = cur;
602 }
603 if(press != 0) {
604 while(1) {
605 SceCtrlData c;
606 sceCtrlReadBufferPositive(&c, 1);
607 if(press == 1) {
608 if(!(c.Buttons & PSP_CTRL_DOWN)) break;
609 } else if(press == -1) {
610 if(!(c.Buttons & PSP_CTRL_UP)) break;
611 }
612 }
613 }
614 }
615 pspDebugScreenSetXY(0, 1 + 3 + incr + 1);
616 int err = sceNetApctlConnect(choice[cur]);
617 if(err != 0) {
618 printf("Apctl initialization failure\n");
619 while(running) sceKernelDelayThread(50 * 1000);
620 sceKernelExitGame();
621 } else {
622 printf("Apctl initialization successful\n");
623 }
624 printf("Apctl connecting\n");
625 while(1) {
626 int state;
627 err = sceNetApctlGetState(&state);
628 if(err != 0) {
629 printf("Apctl getting status failure\n");
630 while(running) sceKernelDelayThread(50 * 1000);
631 sceKernelExitGame();
632 }
633 if(state == 4) {
634 break;
635 }
636 sceKernelDelayThread(50 * 1000);
637 }
638 union SceNetApctlInfo info;
639 if(sceNetApctlGetInfo(8, &info) != 0) {
640 printf("Got an unknown IP\n");
641 while(running) sceKernelDelayThread(50 * 1000);
642 sceKernelExitGame();
643 }
644 printf("Connected, My IP is %s\n", info.ip);
645#elif defined(__PPU__)
646 int i;
647 ctx = init_screen();
648 int w, h;
649 get_resolution(&w, &h);
650 tt_width = w / 6;
651 tt_height = h / 8;
652 tvram = malloc((w / 6) * (h / 8));
653 for(i = 0; i < BUFFERS; i++) make_buffer(&buffers[i], i);
654 flip(ctx, BUFFERS - 1);
655 sys_ppu_thread_t id;
656 sysThreadCreate(&id, text_thread, NULL, 1500, 0x1000, THREAD_JOINABLE, "TextThread");
657 printf("PS3 Bootstrap, Tewi/%s\n", tw_get_version());
658 show_png();
659 netInitialize();
660#elif defined(__ps2sdk__)
661 SifInitRpc(0);
662 while(!SifIopReset("", 0))
663 ;
664 while(!SifIopSync())
665 ;
666 init_scr();
667 scr_printf("PS2 Bootstrap, Tewi/%s\n", tw_get_version());
668 SleepThread();
669#endif
670 int st = startup(argc, argv);
671 if(st != -1) {
672#ifdef _PSP
673 printf("Error code %d\n", st);
674 while(running) sceKernelDelayThread(50 * 1000);
675 sceKernelExitGame();
676#else
677#ifdef __PPU__
678 printf("Error code %d\n", st);
679 while(1)
680 ;
681#endif
682 return st;
683#endif
684 }
685 tw_server_loop();
686#endif
687#ifdef _PSP
688 sceKernelExitGame();
689#endif
690 return 0;
691}
692
693int startup(int argc, char** argv) {
694 int i;
695#ifdef __MINGW32__
696 char* confpath = cm_strdup(PREFIX "/etc/tewi.conf");
697 char* regpath = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir");
698 if(regpath != NULL) {
699 free(confpath);
700 confpath = cm_strcat(regpath, "/etc/tewi.conf");
701 free(regpath);
702 }
703#else
704 const char* confpath = PREFIX "/etc/tewi.conf";
705#endif
706 if(argv != NULL) {
707 for(i = 1; i < argc; i++) {
708 if(argv[i][0] == '-') {
709 if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
710 if(!cm_do_log) {
711 cm_do_log = true;
712#ifndef NO_SSL
713 cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
714#else
715 cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
716#endif
717 } else {
718 cm_do_log = true;
719 }
720 } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
721 i++;
722 if(argv[i] == NULL) {
723 STDERR_LOG("Missing argument\n");
724 return 1;
725 }
726 confpath = argv[i];
727#ifndef _PSP
728 } else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
729 i++;
730 if(argv[i] == NULL) {
731 STDERR_LOG("Missing argument\n");
732 return 1;
733 }
734 if(logfile != NULL && logfile != stderr) {
735 fclose(logfile);
736 }
737 logfile = fopen(argv[i], "a");
738 if(logfile == NULL) {
739 STDERR_LOG("Failed to open logfile\n");
740 return 1;
741 }
742#endif
743 } else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
744 printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
745 printf("Under public domain.\n");
746 printf("Original by 2024 Nishi\n");
747 printf("\n");
748 printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
749 printf("--config | -C config : Specify config\n");
750#ifndef _PSP
751 printf("--logfile | -l logfile : Specify logfile\n");
752#endif
753 printf("--verbose | -v : Verbose mode\n");
754 printf("--version | -V : Version information\n");
755 return 0;
756 } else {
757 STDERR_LOG("Unknown option: %s\n", argv[i]);
758 return 1;
759 }
760 }
761 }
762 }
763 tw_config_init();
764 if(tw_config_read(confpath) != 0) {
765 STDERR_LOG("Could not read the config\n");
766 return 1;
767 }
768 if(tw_server_init() != 0) {
769 STDERR_LOG("Could not initialize the server\n");
770 return 1;
771 }
772 sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
773 char* r = cm_strcat(tw_server, " running...");
774 cm_force_log(r);
775 free(r);
776#ifndef __MINGW32__
777 signal(SIGCHLD, SIG_IGN);
778 signal(SIGPIPE, SIG_IGN);
779#else
780 SetConsoleTitle(tw_server);
781#endif
782 return -1;
783}
Note: See TracBrowser for help on using the repository browser.