<commit_before>/**
 * @file
 * @brief Command registry and invocation code.
 *
 * @date 01.03.11
 * @author <NAME>
 */

#include <framework/cmd/api.h>

#include <ctype.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>

#include <kernel/printk.h>
#include <framework/mod/ops.h>
#include <util/array.h>

#define APP_DATA_RESERVE_OFFSET ({ \
		extern char __app_data_start; \
		extern char __app_reserve_start; \
		&__app_reserve_start - &__app_data_start; \
	})

ARRAY_SPREAD_DEF(const struct cmd, __cmd_registry);

static int cmd_mod_enable(struct mod_info *mod);

const struct mod_ops __cmd_mod_ops = {
	.enable = &cmd_mod_enable,
};

static int cmd_mod_enable(struct mod_info *mod_info) {
	const struct mod_extra *extra = mod_info->mod->extra;

	printk("\tcmd: loading %s.%s: data=0x%08lx, data_sz=%zu, reserve=0x%08lx\n",
		mod_info->mod->package->name, mod_info->mod->name,
		(unsigned long) extra->data, extra->data_sz,
		(unsigned long) extra->data + APP_DATA_RESERVE_OFFSET);

	memcpy(extra->data + APP_DATA_RESERVE_OFFSET, extra->data, extra->data_sz);

	return 0;
}

int cmd_exec(const struct cmd *cmd, int argc, char **argv) {
	const struct mod *mod;
	const struct mod_extra *extra;

	if (!cmd)
		return -EINVAL;

	mod = cmd->mod;
	if (!mod_is_running(mod))
		return -ENOENT;

	extra = mod->extra;
	memcpy(extra->data, extra->data + APP_DATA_RESERVE_OFFSET, extra->data_sz);
	memset(extra->bss, 0, extra->bss_sz);

	return cmd->exec(argc, argv);
}

const struct cmd *cmd_lookup(const char *name) {
	const struct cmd *cmd = NULL;

	cmd_foreach(cmd) {
		if (strcmp(cmd_name(cmd), name) == 0) {
			return cmd;
		}
	}

	return NULL;
}

<commit_msg>appmods: error message in case of loading a cmd built without @Cmd<commit_after>/**
 * @file
 * @brief Command registry and invocation code.
 *
 * @date 01.03.11
 * @author <NAME>
 */

#include <framework/cmd/api.h>

#include <ctype.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>

#include <kernel/printk.h>
#include <framework/mod/ops.h>
#include <util/array.h>

#define APP_DATA_RESERVE_OFFSET ({ \
		extern char __app_data_start; \
		extern char __app_reserve_start; \
		&__app_reserve_start - &__app_data_start; \
	})

ARRAY_SPREAD_DEF(const struct cmd, __cmd_registry);

static int cmd_mod_enable(struct mod_info *mod);

const struct mod_ops __cmd_mod_ops = {
	.enable = &cmd_mod_enable,
};

static int cmd_mod_enable(struct mod_info *mod_info) {
	const struct mod_extra *extra = mod_info->mod->extra;

	printk("\tcmd: loading %s.%s: ",
		mod_info->mod->package->name, mod_info->mod->name);

	if (!extra) {
		printk("no app metadata available (missing @Cmd?)\n");
		return -ENOENT;
	}

	printk("data=0x%08lx, data_sz=%zu, reserve=0x%08lx\n",
		(unsigned long) extra->data, extra->data_sz,
		(unsigned long) extra->data + APP_DATA_RESERVE_OFFSET);

	memcpy(extra->data + APP_DATA_RESERVE_OFFSET, extra->data, extra->data_sz);

	return 0;
}

int cmd_exec(const struct cmd *cmd, int argc, char **argv) {
	const struct mod *mod;
	const struct mod_extra *extra;

	if (!cmd)
		return -EINVAL;

	mod = cmd->mod;
	if (!mod_is_running(mod))
		return -ENOENT;

	extra = mod->extra;
	memcpy(extra->data, extra->data + APP_DATA_RESERVE_OFFSET, extra->data_sz);
	memset(extra->bss, 0, extra->bss_sz);

	return cmd->exec(argc, argv);
}

const struct cmd *cmd_lookup(const char *name) {
	const struct cmd *cmd = NULL;

	cmd_foreach(cmd) {
		if (strcmp(cmd_name(cmd), name) == 0) {
			return cmd;
		}
	}

	return NULL;
}


{'fraction_non_alphanumeric': 0.14895445431108564, 'fraction_numerical': 0.00859352621025494, 'mean_word_length': 4.525316455696203, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 63, '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': '7116389', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1326, 'n_words': 309}