Subversion Repositories Tewi

Rev

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