Subversion Repositories Tewi

Rev

Rev 200 | Rev 208 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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