Subversion Repositories Tewi

Rev

Rev 312 | Rev 319 | 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 315 2024-10-14 10:01:02Z 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__)
40
#include <nwconio.h>
41
#include <nwthread.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__)
300 nishi 77
#elif defined(_MSC_VER) || defined(__BORLANDC__) || defined(__USLC__)
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;
551
	DestroyScreen(GetCurrentScreen());
552
	SetCurrentScreen(CreateScreen("Tewi Console", 0));
553
	BeginThread(thread_stuff, NULL, 0, parg);
554
	ThreadSwitch();
555
	ExitThread(EXIT_THREAD, 0);
556
	return 0;
557
}
558
 
559
void thread_stuff(void* pargs) {
560
	int st;
561
	int argc = ((struct arg_struct*)pargs)->argc;
562
	char** argv = ((struct arg_struct*)pargs)->argv;
563
#endif
62 nishi 564
#ifdef SERVICE
565
	SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
284 nishi 566
	logfile = stderr;
310 nishi 567
	if(!StartServiceCtrlDispatcher(table)) {
284 nishi 568
		printf("Failed to start the service dispatcher\n");
569
		return 1;
570
	}
62 nishi 571
#else
284 nishi 572
	logfile = stderr;
182 nishi 573
#ifdef _PSP
574
	pspDebugScreenInit();
575
	pspDebugScreenSetXY(0, 0);
183 nishi 576
	printf("PSP Bootstrap, Tewi/%s\n", tw_get_version());
577
	int thid = sceKernelCreateThread("update_thread", psp_callback_thread, 0x11, 0xfa0, 0, NULL);
578
	if(thid >= 0) {
579
		sceKernelStartThread(thid, 0, NULL);
580
	} else {
581
		printf("Failed to start thread\n");
582
		while(running) sceKernelDelayThread(50 * 1000);
583
		sceKernelExitGame();
584
	}
585
	sceCtrlSetSamplingCycle(0);
586
	sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
587
	sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
588
	sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
589
	if(pspSdkInetInit()) {
590
		printf("Could not init the network\n");
591
		while(running) sceKernelDelayThread(50 * 1000);
592
		sceKernelExitGame();
593
	} else {
594
		printf("Network initialization successful\n");
595
	}
596
	if(sceWlanGetSwitchState() != 1) {
597
		printf("Turn the Wi-Fi switch on\n");
598
		while(sceWlanGetSwitchState() != 1) {
599
			sceKernelDelayThread(1000 * 1000);
600
		}
601
	} else {
602
		printf("Wi-Fi is turned on\n");
603
	}
604
	int i;
605
	int choice[100];
606
	int incr = 0;
607
	int last = 0;
608
	int cur = 0;
609
	for(i = 1; i < 100; i++) {
610
		choice[i - 1] = 0;
611
		netData name;
612
		netData data;
613
		if(sceUtilityCheckNetParam(i) != 0) continue;
614
		choice[incr++] = i;
615
		pspDebugScreenSetXY(0, 1 + 3 + incr - 1);
616
		if(incr == 1) printf("> ");
617
		pspDebugScreenSetXY(2, 1 + 3 + incr - 1);
618
		sceUtilityGetNetParam(i, 0, &name);
619
		sceUtilityGetNetParam(i, 1, &data);
620
		printf("SSID=%s", data.asString);
621
		sceUtilityGetNetParam(i, 4, &data);
622
		if(data.asString[0]) {
623
			sceUtilityGetNetParam(i, 5, &data);
624
			printf(" IPADDR=%s\n", data.asString);
625
		} else {
626
			printf(" DHCP\n");
627
		}
628
	}
629
	int press = 0;
630
	while(1) {
631
		if(!running) {
632
			sceKernelExitGame();
633
		}
634
		SceCtrlData c;
635
		sceCtrlReadBufferPositive(&c, 1);
636
		press = 0;
637
		if(c.Buttons & PSP_CTRL_DOWN) {
638
			if(cur < incr - 1) {
639
				cur++;
640
			}
641
			press = 1;
642
		} else if(c.Buttons & PSP_CTRL_UP) {
643
			if(cur > 0) {
644
				cur--;
645
			}
646
			press = -1;
647
		} else if(c.Buttons & PSP_CTRL_START) {
648
			break;
649
		}
650
		if(last != cur) {
651
			pspDebugScreenSetXY(0, 1 + 3 + last);
652
			printf("  ");
653
			pspDebugScreenSetXY(0, 1 + 3 + cur);
654
			printf("> ");
655
			last = cur;
656
		}
657
		if(press != 0) {
658
			while(1) {
659
				SceCtrlData c;
660
				sceCtrlReadBufferPositive(&c, 1);
661
				if(press == 1) {
662
					if(!(c.Buttons & PSP_CTRL_DOWN)) break;
663
				} else if(press == -1) {
664
					if(!(c.Buttons & PSP_CTRL_UP)) break;
665
				}
666
			}
667
		}
668
	}
669
	pspDebugScreenSetXY(0, 1 + 3 + incr + 1);
670
	int err = sceNetApctlConnect(choice[cur]);
671
	if(err != 0) {
672
		printf("Apctl initialization failure\n");
673
		while(running) sceKernelDelayThread(50 * 1000);
674
		sceKernelExitGame();
675
	} else {
676
		printf("Apctl initialization successful\n");
677
	}
678
	printf("Apctl connecting\n");
679
	while(1) {
680
		int state;
681
		err = sceNetApctlGetState(&state);
682
		if(err != 0) {
683
			printf("Apctl getting status failure\n");
684
			while(running) sceKernelDelayThread(50 * 1000);
685
			sceKernelExitGame();
686
		}
687
		if(state == 4) {
688
			break;
689
		}
690
		sceKernelDelayThread(50 * 1000);
691
	}
692
	union SceNetApctlInfo info;
693
	if(sceNetApctlGetInfo(8, &info) != 0) {
694
		printf("Got an unknown IP\n");
695
		while(running) sceKernelDelayThread(50 * 1000);
696
		sceKernelExitGame();
697
	}
698
	printf("Connected, My IP is %s\n", info.ip);
187 nishi 699
#elif defined(__PPU__)
191 nishi 700
	int i;
701
	ctx = init_screen();
702
	int w, h;
703
	get_resolution(&w, &h);
704
	tt_width = w / 6;
705
	tt_height = h / 8;
706
	tvram = malloc((w / 6) * (h / 8));
707
	for(i = 0; i < BUFFERS; i++) make_buffer(&buffers[i], i);
708
	flip(ctx, BUFFERS - 1);
709
	sys_ppu_thread_t id;
710
	sysThreadCreate(&id, text_thread, NULL, 1500, 0x1000, THREAD_JOINABLE, "TextThread");
187 nishi 711
	printf("PS3 Bootstrap, Tewi/%s\n", tw_get_version());
197 nishi 712
	show_png();
187 nishi 713
	netInitialize();
189 nishi 714
#elif defined(__ps2sdk__)
715
	SifInitRpc(0);
200 nishi 716
	while(!SifIopReset("", 0))
717
		;
718
	while(!SifIopSync())
719
		;
189 nishi 720
	init_scr();
721
	scr_printf("PS2 Bootstrap, Tewi/%s\n", tw_get_version());
200 nishi 722
	SleepThread();
182 nishi 723
#endif
212 nishi 724
	st = startup(argc, argv);
183 nishi 725
	if(st != -1) {
726
#ifdef _PSP
727
		printf("Error code %d\n", st);
728
		while(running) sceKernelDelayThread(50 * 1000);
729
		sceKernelExitGame();
730
#else
191 nishi 731
#ifdef __PPU__
732
		printf("Error code %d\n", st);
733
		while(1)
734
			;
735
#endif
315 nishi 736
#ifdef __NETWARE__
737
		return;
738
#else
183 nishi 739
		return st;
740
#endif
315 nishi 741
#endif
183 nishi 742
	}
62 nishi 743
	tw_server_loop();
744
#endif
183 nishi 745
#ifdef _PSP
746
	sceKernelExitGame();
747
#endif
315 nishi 748
#ifdef __NETWARE__
749
	return;
750
#else
168 nishi 751
	return 0;
315 nishi 752
#endif
62 nishi 753
}
244 nishi 754
#endif
62 nishi 755
 
70 nishi 756
int startup(int argc, char** argv) {
3 nishi 757
	int i;
212 nishi 758
	char* r;
315 nishi 759
#if defined(__MINGW32__) || defined(_MSC_VER) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__)) || defined(__BORLANDC__)
206 nishi 760
	char* confpath = cm_strdup(PREFIX "/etc/tewi.conf");
761
	char* regpath = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir");
762
	if(regpath != NULL) {
763
		free(confpath);
764
		confpath = cm_strcat(regpath, "/etc/tewi.conf");
765
		free(regpath);
766
	}
767
#else
18 nishi 768
	const char* confpath = PREFIX "/etc/tewi.conf";
206 nishi 769
#endif
70 nishi 770
	if(argv != NULL) {
62 nishi 771
		for(i = 1; i < argc; i++) {
772
			if(argv[i][0] == '-') {
773
				if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
774
					if(!cm_do_log) {
775
						cm_do_log = true;
70 nishi 776
#ifndef NO_SSL
62 nishi 777
						cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
70 nishi 778
#else
62 nishi 779
						cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
70 nishi 780
#endif
62 nishi 781
					} else {
782
						cm_do_log = true;
783
					}
784
				} else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0) {
785
					i++;
786
					if(argv[i] == NULL) {
183 nishi 787
						STDERR_LOG("Missing argument\n");
62 nishi 788
						return 1;
789
					}
790
					confpath = argv[i];
182 nishi 791
#ifndef _PSP
117 nishi 792
				} else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
793
					i++;
794
					if(argv[i] == NULL) {
183 nishi 795
						STDERR_LOG("Missing argument\n");
117 nishi 796
						return 1;
797
					}
798
					if(logfile != NULL && logfile != stderr) {
799
						fclose(logfile);
800
					}
801
					logfile = fopen(argv[i], "a");
802
					if(logfile == NULL) {
183 nishi 803
						STDERR_LOG("Failed to open logfile\n");
117 nishi 804
						return 1;
805
					}
182 nishi 806
#endif
62 nishi 807
				} else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
808
					printf("Tewi HTTPd Tewi/%s\n", tw_get_version());
809
					printf("Under public domain.\n");
810
					printf("Original by 2024 Nishi\n");
811
					printf("\n");
812
					printf("Usage: %s [--config|-C config] [--verbose|-v] [--version|-V]\n", argv[0]);
119 nishi 813
					printf("--config  | -C config      : Specify config\n");
182 nishi 814
#ifndef _PSP
119 nishi 815
					printf("--logfile | -l logfile     : Specify logfile\n");
182 nishi 816
#endif
119 nishi 817
					printf("--verbose | -v             : Verbose mode\n");
818
					printf("--version | -V             : Version information\n");
62 nishi 819
					return 0;
3 nishi 820
				} else {
183 nishi 821
					STDERR_LOG("Unknown option: %s\n", argv[i]);
4 nishi 822
					return 1;
823
				}
3 nishi 824
			}
825
		}
826
	}
6 nishi 827
	tw_config_init();
18 nishi 828
	if(tw_config_read(confpath) != 0) {
183 nishi 829
		STDERR_LOG("Could not read the config\n");
4 nishi 830
		return 1;
831
	}
8 nishi 832
	if(tw_server_init() != 0) {
183 nishi 833
		STDERR_LOG("Could not initialize the server\n");
8 nishi 834
		return 1;
835
	}
18 nishi 836
	sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
212 nishi 837
	r = cm_strcat(tw_server, " running...");
62 nishi 838
	cm_force_log(r);
839
	free(r);
219 nishi 840
#if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
16 nishi 841
	signal(SIGCHLD, SIG_IGN);
90 nishi 842
	signal(SIGPIPE, SIG_IGN);
315 nishi 843
#elif !defined(BUILD_GUI) && !defined(__OS2__) && !defined(__NETWARE__)
50 nishi 844
	SetConsoleTitle(tw_server);
16 nishi 845
#endif
62 nishi 846
	return -1;
3 nishi 847
}