Subversion Repositories Tewi

Rev

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