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

Last change on this file since 349 was 349, checked in by Nishi, on Oct 16, 2024 at 5:08:08 AM

fix some stuff

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