Subversion Repositories Tewi

Rev

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