<commit_before>#include <malloc.h>
#include <stdarg.h>
#include <stdio.h>
#ifdef _3DS
#include <3ds.h>
#endif
#include "console.h"
#include "ftp.h"

#ifdef _3DS
#include <sf2d.h>
#include <sfil.h>
#include "app_bottom_png.h"
#endif

#ifndef _3DS
#include "getopt.h"
#include <signal.h>
#endif

#include "configread.h"

int sigint_force_exit = 0; // DO NOT WRITE FROM MAIN THREAD.

/*! looping mechanism
 *
 *  @param[in] callback function to call during each iteration
 *
 *  @returns loop status
 */
loop_status_t loop(loop_status_t (*callback)(void)) {
    loop_status_t status = LOOP_CONTINUE;

    // This is all fine on the 3DS, but eating 100% CPU is unacceptable under linux.

#ifdef _3DS
    /**PNG Graphics (sf2d)**/
    sf2d_set_clear_color(RGBA8(0x40, 0x40, 0x40, 0xFF));

    sf2d_texture *app_bottom = NULL;
    if (sett_app_bottom_path == NULL) {
        // Image was not specified on SD, so use the builtin default.
        app_bottom = sfil_load_PNG_buffer(app_bottom_png, SF2D_PLACE_RAM);
    } else {
        // Load image from SD.
        app_bottom = sfil_load_PNG_file(sett_app_bottom_path, SF2D_PLACE_RAM);
        if (app_bottom == NULL) {
            // Load failed, so load default and log it.
            console_print(RED "Loading PNG from SD failed. Using default.\n" RESET);
            app_bottom = sfil_load_PNG_buffer(app_bottom_png, SF2D_PLACE_RAM);
        }
    }

    while (aptMainLoop()) {
        hidScanInput();

        /**app_bottom graphic**/
        sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
        sf2d_draw_texture(app_bottom, 0, 0);
        sf2d_end_frame();

        sf2d_swapbuffers();

        status = callback();

        if (status != LOOP_CONTINUE)
            return status;
    }
    /**free graphics **/
    sf2d_free_texture(app_bottom);

    return LOOP_EXIT;
#else
    while (status == LOOP_CONTINUE) {
        status = callback();

        if(sigint_force_exit == 1) {
            status = LOOP_EXIT; // Break out.
        }
    }
    return status;
#endif
}

#ifdef _3DS
/*! wait until the B button is pressed (on 3DS)
 *
 *  @returns loop status
 */
static loop_status_t wait_for_b(void) {
    /* update button state */
    hidScanInput();

    /* check if B was pressed */
    if (hidKeysDown() & KEY_B)
        return LOOP_EXIT;

    /* B was not pressed */
    return LOOP_CONTINUE;
}
#endif

// Sockets leak without proper cleanup. We need to do this on Linux.
void sigint_handler(int sig) {
	console_print(YELLOW "SIGINT recieved. Cleaning up and exiting.\n" RESET);
    sigint_force_exit = 1;
}

/*! entry point
 *
 *  @param[in] argc unused
 *  @param[in] argv unused
 *
 *  returns exit status
 */
int main(int argc, char *argv[]) {
    loop_status_t status = LOOP_RESTART;

#ifdef _3DS
    /* initialize needed 3DS services */

    // Perform CPU limit init.
    aptInit();
    aptOpenSession();
    APT_SetAppCpuTimeLimit(0);
    aptCloseSession();

    sf2d_init();
    acInit();
    gfxInitDefault();
    gfxSet3D(false);
    sdmcWriteSafe(false);
#endif

#ifndef _3DS
    signal(SIGINT, sigint_handler); // Register cleanup-on-ctrl-c
#endif

    /* initialize console subsystem */
    console_init();
#ifdef _3DS
    console_set_status("\n" GREEN STATUS_STRING RESET);
#endif

    // Load config.
    int cfg = load_config_file();
    if (cfg) {
        // Error.
        console_print(RED "Default config file '%s' not found. Using builtin defaults.\n" RESET, config_file);
    }

#ifdef _3DS
    // If user has an n3DS and clock rate option is set, jack up the clock.
    if (sett_high_clock_rate == 1) {
        uint8_t sysType = 0;
        CFGU_GetSystemModel(&sysType);
        if (sysType == 2 || sysType == 4) { // Only set clock rate on N3DS units.
            osSetSpeedupEnable(1);
            console_print(YELLOW "New 3DS clock rate enabled.\n" RESET);
        }
    }
#endif

    char* root_dir = NULL; // Directory to jail to.
	int ro = 0; // Read-only. No sends are allowed.

#ifndef _3DS
    // On a linux system. Extract argv and argc if there.
    int c;
    while ( (c = getopt(argc, argv, "p:R:rC:hn")) != -1) {
        switch(c) {
            case 'p':
                sscanf(optarg, "%d", &sett_port);
                break;
            case 'R':
                root_dir = optarg;
                break;
            case 'n':
                sett_disable_color = 1;
                break;<commit_msg>Debugging info.<commit_after>#include <malloc.h>
#include <stdarg.h>
#include <stdio.h>
#ifdef _3DS
#include <3ds.h>
#endif
#include "console.h"
#include "ftp.h"

#ifdef _3DS
#include <sf2d.h>
#include <sfil.h>
#include "app_bottom_png.h"
#endif

#ifndef _3DS
#include "getopt.h"
#include <signal.h>
#endif

#include "configread.h"

#ifndef _3DS
int sigint_force_exit = 0; // DO NOT WRITE FROM MAIN THREAD.
#endif

/*! looping mechanism
 *
 *  @param[in] callback function to call during each iteration
 *
 *  @returns loop status
 */
loop_status_t loop(loop_status_t (*callback)(void)) {
    loop_status_t status = LOOP_CONTINUE;

    // This is all fine on the 3DS, but eating 100% CPU is unacceptable under linux.

#ifdef _3DS
    /**PNG Graphics (sf2d)**/
    sf2d_set_clear_color(RGBA8(0x40, 0x40, 0x40, 0xFF));

    sf2d_texture *app_bottom = NULL;
    if (sett_app_bottom_path == NULL) {
        // Image was not specified on SD, so use the builtin default.
        app_bottom = sfil_load_PNG_buffer(app_bottom_png, SF2D_PLACE_RAM);
    } else {
        // Load image from SD.
        app_bottom = sfil_load_PNG_file(sett_app_bottom_path, SF2D_PLACE_RAM);
        if (app_bottom == NULL) {
            // Load failed, so load default and log it.
            console_print(RED "Loading PNG from SD failed. Using default.\n" RESET);
            app_bottom = sfil_load_PNG_buffer(app_bottom_png, SF2D_PLACE_RAM);
        } else {
            console_print(GREEN "Loaded user image '%s'.\n" RESET, sett_app_bottom_path);
        }
    }

    while (aptMainLoop()) {
        hidScanInput();

        /**app_bottom graphic**/
        sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
        sf2d_draw_texture(app_bottom, 0, 0);
        sf2d_end_frame();

        sf2d_swapbuffers();

        status = callback();

        if (status != LOOP_CONTINUE)
            return status;
    }
    /**free graphics **/
    sf2d_free_texture(app_bottom);

    return LOOP_EXIT;
#else
    while (status == LOOP_CONTINUE) {
        status = callback();

        if(sigint_force_exit == 1) {
            status = LOOP_EXIT; // Break out.
        }
    }
    return status;
#endif
}

#ifdef _3DS
/*! wait until the B button is pressed (on 3DS)
 *
 *  @returns loop status
 */
static loop_status_t wait_for_b(void) {
    /* update button state */
    hidScanInput();

    /* check if B was pressed */
    if (hidKeysDown() & KEY_B)
        return LOOP_EXIT;

    /* B was not pressed */
    return LOOP_CONTINUE;
}
#endif

// Sockets leak without proper cleanup. We need to do this on Linux.
void sigint_handler(int sig) {
	console_print(YELLOW "SIGINT recieved. Cleaning up and exiting.\n" RESET);
    sigint_force_exit = 1;
}

/*! entry point
 *
 *  @param[in] argc unused
 *  @param[in] argv unused
 *
 *  returns exit status
 */
int main(int argc, char *argv[]) {
    loop_status_t status = LOOP_RESTART;

#ifdef _3DS
    /* initialize needed 3DS services */

    // Perform CPU limit init.
    aptInit();
    aptOpenSession();
    APT_SetAppCpuTimeLimit(0);
    aptCloseSession();

    sf2d_init();
    acInit();
    gfxInitDefault();
    gfxSet3D(false);
    sdmcWriteSafe(false);
#endif

#ifndef _3DS
    signal(SIGINT, sigint_handler); // Register cleanup-on-ctrl-c
#endif

    /* initialize console subsystem */
    console_init();
#ifdef _3DS
    console_set_status("\n" GREEN STATUS_STRING RESET);
#endif

    // Load config.
    int cfg = load_config_file();
    if (cfg) {
        // Error.
        console_print(RED "Default config file '%s' not found. Using builtin defaults.\n" RESET, config_file);
    } else {
        console_print(RED "Default config file '%s' was loaded.\n" RESET, config_file);
    }

#ifdef _3DS
    // If user has an n3DS and clock rate option is set, jack up the clock.
    if (sett_high_clock_rate == 1) {
        uint8_t sysType = 0;
        CFGU_GetSystemModel(&sysType);
        if (sysType == 2 || sysType == 4) { // Only set clock rate on N3DS units.
            osSetSpeedupEnable(1);
            console_print(YELLOW "New 3DS clock rate enabled.\n" RESET);
        } else {
            console_print(RED "System type wrong, not setting clock rate despite config.\n" RESET);
        }
    }
#endif

    char* root_dir = NULL; // Directory to jail to.
	int ro = 0; // Read-only. No sends are allowed.

#ifndef _3DS
    // On a linux system. Extract argv and argc if there.
    int c;
    while ( (c = getopt(argc, argv, "p:R:rC:hn")) != -1) {
        switch(c) {
            case 'p':
                sscanf(optarg, "%d", &sett_port);
                break;
            case 'R':
                root_dir = optarg;
                break;
            case 'n':
                sett_disable_color = 1;
                break;
{'fraction_non_alphanumeric': 0.11517007546757083, 'fraction_numerical': 0.013234168216121623, 'mean_word_length': 2.437593984962406, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2522230', 'n_tokens_mistral': 3228, 'n_tokens_neox': 3106, 'n_words': 857}