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

Last change on this file since 392 was 361, checked in by Nishi, on Oct 17, 2024 at 9:37:10 AM

works on netware

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