[2] | 1 | /* $Id: main.c 359 2024-10-16 14:34:51Z nishi $ */
|
---|
| 2 |
|
---|
[16] | 3 | #define SOURCE
|
---|
| 4 |
|
---|
[43] | 5 | #include "../config.h"
|
---|
| 6 |
|
---|
[215] | 7 | #ifdef __BORLANDC__
|
---|
| 8 |
|
---|
[244] | 9 | #pragma resource "gui_bcc.res"
|
---|
[215] | 10 |
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
| 13 | #if !defined(_MSC_VER) && !defined(__BORLANDC__)
|
---|
[189] | 14 | #include <unistd.h>
|
---|
[212] | 15 | #endif
|
---|
[3] | 16 | #include <stdio.h>
|
---|
| 17 | #include <stdbool.h>
|
---|
| 18 | #include <string.h>
|
---|
[16] | 19 | #include <signal.h>
|
---|
[116] | 20 | #include <stdlib.h>
|
---|
[3] | 21 |
|
---|
[43] | 22 | #ifndef NO_SSL
|
---|
[6] | 23 | #include <openssl/opensslv.h>
|
---|
[43] | 24 | #endif
|
---|
[6] | 25 |
|
---|
[312] | 26 | #ifdef __OS2__
|
---|
| 27 | #include <types.h>
|
---|
| 28 | #endif
|
---|
| 29 |
|
---|
[359] | 30 | #ifdef __DOS__
|
---|
| 31 | #include <tcp.h>
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
[3] | 34 | #include <cm_log.h>
|
---|
[62] | 35 | #include <cm_string.h>
|
---|
[3] | 36 |
|
---|
[4] | 37 | #include "tw_config.h"
|
---|
[8] | 38 | #include "tw_server.h"
|
---|
[3] | 39 | #include "tw_version.h"
|
---|
| 40 |
|
---|
[359] | 41 | #if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__) && !defined(__DOS__))
|
---|
[51] | 42 | #include <windows.h>
|
---|
[315] | 43 | #elif defined(__NETWARE__)
|
---|
[51] | 44 | #endif
|
---|
| 45 |
|
---|
[182] | 46 | #ifdef _PSP
|
---|
| 47 | #include <pspkernel.h>
|
---|
| 48 | #include <pspdebug.h>
|
---|
[183] | 49 | #include <pspsdk.h>
|
---|
| 50 | #include <psputility.h>
|
---|
| 51 | #include <pspctrl.h>
|
---|
| 52 | #include <pspnet_apctl.h>
|
---|
| 53 | #include <pspwlan.h>
|
---|
[182] | 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] | 59 | #define STDERR_LOG(...) pspDebugScreenPrintf(__VA_ARGS__)
|
---|
[189] | 60 | #elif defined(__ps2sdk__)
|
---|
| 61 | #include <debug.h>
|
---|
[200] | 62 | #include <iopcontrol.h>
|
---|
[189] | 63 | #include <sifrpc.h>
|
---|
[200] | 64 | #include <kernel.h>
|
---|
[189] | 65 |
|
---|
| 66 | #define printf(...) scr_printf(__VA_ARGS__)
|
---|
| 67 | #define STDERR_LOG(...) scr_printf(__VA_ARGS__)
|
---|
[191] | 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] | 75 | #include <png.h>
|
---|
[191] | 76 |
|
---|
| 77 | #define printf(...) tt_printf(__VA_ARGS__)
|
---|
| 78 | #define STDERR_LOG(...) tt_printf(__VA_ARGS__)
|
---|
[324] | 79 | #elif defined(_MSC_VER) || defined(__BORLANDC__) || defined(__USLC__) || defined(REALLY_OLD)
|
---|
[301] | 80 | #include <stdarg.h>
|
---|
| 81 |
|
---|
[213] | 82 | void STDERR_LOG(const char* format, ...) {
|
---|
[212] | 83 | va_list args;
|
---|
| 84 | va_start(args, format);
|
---|
| 85 | vfprintf(stderr, format, args);
|
---|
| 86 | va_end(args);
|
---|
| 87 | }
|
---|
[183] | 88 | #else
|
---|
| 89 | #define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__)
|
---|
[182] | 90 | #endif
|
---|
| 91 |
|
---|
[3] | 92 | extern bool cm_do_log;
|
---|
[18] | 93 | extern struct tw_config config;
|
---|
[62] | 94 | extern FILE* logfile;
|
---|
[3] | 95 |
|
---|
[18] | 96 | char tw_server[2048];
|
---|
| 97 |
|
---|
[62] | 98 | int startup(int argc, char** argv);
|
---|
| 99 |
|
---|
[359] | 100 | #if defined(__MINGW32__) || defined(_MSC_VER) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__) && !defined(__DOS__)) || defined(__BORLANDC__)
|
---|
[206] | 101 | char* get_registry(const char* main, const char* sub) {
|
---|
[208] | 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] | 106 | if(err == ERROR_SUCCESS) {
|
---|
[208] | 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] | 114 | return value;
|
---|
| 115 | } else {
|
---|
| 116 | free(value);
|
---|
| 117 | return NULL;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | #endif
|
---|
| 121 |
|
---|
[62] | 122 | #ifdef SERVICE
|
---|
| 123 | SERVICE_STATUS status;
|
---|
| 124 | SERVICE_STATUS_HANDLE status_handle;
|
---|
| 125 |
|
---|
[70] | 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] | 132 | }
|
---|
| 133 | SetServiceStatus(status_handle, &status);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[70] | 136 | void WINAPI servmain(DWORD argc, LPSTR* argv) {
|
---|
[206] | 137 | char* path = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir");
|
---|
[284] | 138 | int st;
|
---|
[206] | 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] | 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] | 158 | st = startup(argc, argv);
|
---|
[70] | 159 | if(st != -1) {
|
---|
[62] | 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] | 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] | 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] | 371 | for(i = 0; str[i] != 0; i++) {
|
---|
[191] | 372 | tvram[tt_y * tt_width + tt_x] = str[i];
|
---|
[193] | 373 | if(str[i] == '\n') {
|
---|
[191] | 374 | tt_x = 0;
|
---|
| 375 | tt_y++;
|
---|
[193] | 376 | } else {
|
---|
[191] | 377 | tt_x++;
|
---|
[193] | 378 | if(tt_x == tt_width) {
|
---|
[191] | 379 | tt_x = 0;
|
---|
| 380 | tt_y++;
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
[193] | 383 | if(tt_y == tt_height) {
|
---|
[191] | 384 | tt_y--;
|
---|
| 385 | int x, y;
|
---|
[193] | 386 | for(y = 0; y < tt_height - 1; y++) {
|
---|
| 387 | for(x = 0; x < tt_width; x++) {
|
---|
[191] | 388 | tvram[y * tt_width + x] = tvram[(y + 1) * tt_width + x];
|
---|
| 389 | }
|
---|
| 390 | }
|
---|
[193] | 391 | for(x = 0; x < tt_width; x++) {
|
---|
[197] | 392 | tvram[(tt_height - 1) * tt_width + x] = 0x20;
|
---|
[191] | 393 | }
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[193] | 398 | void tt_putchar(struct rsx_buffer* buffer, int x, int y, uint8_t c) {
|
---|
[191] | 399 | int i, j;
|
---|
[197] | 400 | if(c == 0) return;
|
---|
[191] | 401 | if(c < 0x20) c = 0x20;
|
---|
| 402 | if(c >= 0x7f) c = 0x20;
|
---|
[197] | 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] | 407 | if(l & (1 << 7)) {
|
---|
[197] | 408 | col = 0xffffff;
|
---|
[191] | 409 | }
|
---|
| 410 | l = l << 1;
|
---|
[197] | 411 | buffer->ptr[(y * 8 + i) * buffer->width + x * 6 + j] = col;
|
---|
[191] | 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] | 426 | #define BUFFERS 1
|
---|
[191] | 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] | 451 | if(tmpl[i] == 's') {
|
---|
[191] | 452 | char* tmp = log;
|
---|
| 453 | log = cm_strcat(tmp, va_arg(va, char*));
|
---|
| 454 | free(tmp);
|
---|
[193] | 455 | } else if(tmpl[i] == 'd') {
|
---|
[191] | 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] | 461 | } else if(tmpl[i] == '%') {
|
---|
[191] | 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] | 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] | 537 | #endif
|
---|
| 538 |
|
---|
[253] | 539 | #if !defined(BUILD_GUI_VALID)
|
---|
[315] | 540 | void thread_stuff(void* pargs);
|
---|
| 541 |
|
---|
| 542 | struct arg_struct {
|
---|
| 543 | int argc;
|
---|
| 544 | char** argv;
|
---|
| 545 | };
|
---|
| 546 |
|
---|
[3] | 547 | int main(int argc, char** argv) {
|
---|
[212] | 548 | int st;
|
---|
[315] | 549 | #ifdef __NETWARE__
|
---|
| 550 | struct arg_struct* parg = malloc(sizeof(*parg));
|
---|
| 551 | parg->argc = argc;
|
---|
| 552 | parg->argv = argv;
|
---|
[347] | 553 | thread_stuff(parg);
|
---|
[315] | 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] | 562 | #ifdef SERVICE
|
---|
| 563 | SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}};
|
---|
[284] | 564 | logfile = stderr;
|
---|
[310] | 565 | if(!StartServiceCtrlDispatcher(table)) {
|
---|
[284] | 566 | printf("Failed to start the service dispatcher\n");
|
---|
| 567 | return 1;
|
---|
| 568 | }
|
---|
[62] | 569 | #else
|
---|
[284] | 570 | logfile = stderr;
|
---|
[182] | 571 | #ifdef _PSP
|
---|
| 572 | pspDebugScreenInit();
|
---|
| 573 | pspDebugScreenSetXY(0, 0);
|
---|
[183] | 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] | 697 | #elif defined(__PPU__)
|
---|
[191] | 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] | 709 | printf("PS3 Bootstrap, Tewi/%s\n", tw_get_version());
|
---|
[197] | 710 | show_png();
|
---|
[187] | 711 | netInitialize();
|
---|
[189] | 712 | #elif defined(__ps2sdk__)
|
---|
| 713 | SifInitRpc(0);
|
---|
[200] | 714 | while(!SifIopReset("", 0))
|
---|
| 715 | ;
|
---|
| 716 | while(!SifIopSync())
|
---|
| 717 | ;
|
---|
[189] | 718 | init_scr();
|
---|
| 719 | scr_printf("PS2 Bootstrap, Tewi/%s\n", tw_get_version());
|
---|
[200] | 720 | SleepThread();
|
---|
[182] | 721 | #endif
|
---|
[212] | 722 | st = startup(argc, argv);
|
---|
[183] | 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] | 729 | #ifdef __PPU__
|
---|
| 730 | printf("Error code %d\n", st);
|
---|
| 731 | while(1)
|
---|
| 732 | ;
|
---|
| 733 | #endif
|
---|
[315] | 734 | #ifdef __NETWARE__
|
---|
| 735 | return;
|
---|
| 736 | #else
|
---|
[183] | 737 | return st;
|
---|
| 738 | #endif
|
---|
[315] | 739 | #endif
|
---|
[183] | 740 | }
|
---|
[62] | 741 | tw_server_loop();
|
---|
| 742 | #endif
|
---|
[183] | 743 | #ifdef _PSP
|
---|
| 744 | sceKernelExitGame();
|
---|
| 745 | #endif
|
---|
[315] | 746 | #ifdef __NETWARE__
|
---|
| 747 | return;
|
---|
| 748 | #else
|
---|
[168] | 749 | return 0;
|
---|
[315] | 750 | #endif
|
---|
[62] | 751 | }
|
---|
[244] | 752 | #endif
|
---|
[62] | 753 |
|
---|
[70] | 754 | int startup(int argc, char** argv) {
|
---|
[3] | 755 | int i;
|
---|
[212] | 756 | char* r;
|
---|
[359] | 757 | #if defined(__MINGW32__) || defined(_MSC_VER) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__) && !defined(__DOS__)) || defined(__BORLANDC__)
|
---|
[206] | 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] | 766 | const char* confpath = PREFIX "/etc/tewi.conf";
|
---|
[206] | 767 | #endif
|
---|
[70] | 768 | if(argv != NULL) {
|
---|
[62] | 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] | 774 | #ifndef NO_SSL
|
---|
[62] | 775 | cm_log("", "This is Tewi HTTPd, version %s, using %s", tw_get_version(), OPENSSL_VERSION_TEXT);
|
---|
[70] | 776 | #else
|
---|
[62] | 777 | cm_log("", "This is Tewi HTTPd, version %s", tw_get_version());
|
---|
[70] | 778 | #endif
|
---|
[62] | 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] | 785 | STDERR_LOG("Missing argument\n");
|
---|
[62] | 786 | return 1;
|
---|
| 787 | }
|
---|
| 788 | confpath = argv[i];
|
---|
[182] | 789 | #ifndef _PSP
|
---|
[117] | 790 | } else if(strcmp(argv[i], "--logfile") == 0 || strcmp(argv[i], "-l") == 0) {
|
---|
| 791 | i++;
|
---|
| 792 | if(argv[i] == NULL) {
|
---|
[183] | 793 | STDERR_LOG("Missing argument\n");
|
---|
[117] | 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] | 801 | STDERR_LOG("Failed to open logfile\n");
|
---|
[117] | 802 | return 1;
|
---|
| 803 | }
|
---|
[182] | 804 | #endif
|
---|
[62] | 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] | 811 | printf("--config | -C config : Specify config\n");
|
---|
[182] | 812 | #ifndef _PSP
|
---|
[119] | 813 | printf("--logfile | -l logfile : Specify logfile\n");
|
---|
[182] | 814 | #endif
|
---|
[119] | 815 | printf("--verbose | -v : Verbose mode\n");
|
---|
| 816 | printf("--version | -V : Version information\n");
|
---|
[62] | 817 | return 0;
|
---|
[3] | 818 | } else {
|
---|
[183] | 819 | STDERR_LOG("Unknown option: %s\n", argv[i]);
|
---|
[4] | 820 | return 1;
|
---|
| 821 | }
|
---|
[3] | 822 | }
|
---|
| 823 | }
|
---|
| 824 | }
|
---|
[6] | 825 | tw_config_init();
|
---|
[18] | 826 | if(tw_config_read(confpath) != 0) {
|
---|
[183] | 827 | STDERR_LOG("Could not read the config\n");
|
---|
[4] | 828 | return 1;
|
---|
| 829 | }
|
---|
[359] | 830 | #ifdef __DOS__
|
---|
| 831 | sock_init();
|
---|
| 832 | #endif
|
---|
[8] | 833 | if(tw_server_init() != 0) {
|
---|
[183] | 834 | STDERR_LOG("Could not initialize the server\n");
|
---|
[8] | 835 | return 1;
|
---|
| 836 | }
|
---|
[18] | 837 | sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension);
|
---|
[212] | 838 | r = cm_strcat(tw_server, " running...");
|
---|
[62] | 839 | cm_force_log(r);
|
---|
| 840 | free(r);
|
---|
[219] | 841 | #if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
|
---|
[16] | 842 | signal(SIGCHLD, SIG_IGN);
|
---|
[90] | 843 | signal(SIGPIPE, SIG_IGN);
|
---|
[359] | 844 | #elif !defined(BUILD_GUI) && !defined(__OS2__) && !defined(__NETWARE__) && !defined(__DOS__)
|
---|
[50] | 845 | SetConsoleTitle(tw_server);
|
---|
[16] | 846 | #endif
|
---|
[62] | 847 | return -1;
|
---|
[3] | 848 | }
|
---|