Subversion Repositories Tewi

Rev

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