<commit_before>#include "commands.h"

#define NL "\n" // New Line
#define TRUE 1
#define FALSE 0
typedef char bool;

struct _command commands[] =
{
  COMMAND (echo),
  COMMAND (calculator),
  COMMAND (system),
  COMMAND (help),
  COMMAND (not_valid)
};

//const unsigned n_commands = CMD_NOT_VALID;

/* separate_args: splits a message in arguments separated by ' ' */
unsigned separate_args(char *msg, char *argv[]) {
    unsigned argc = 0;

	while(*msg) {
		while(*msg == ' ') *msg++ = '\0';
		argv[argc++] = msg;
		while(*msg && *msg != ' ') msg++;
	}

	return argc;
}

/* execute_command: identifies the command from a list and executes it */
void execute_command(int argc, char* argv[]) {
	unsigned command;

	/* Identify the command */
	if (strlen(argv[0]))
	for (command = 0; strcmp(commands[command].name, "not_valid"); command++) {
        //printf("%s" NL, commands[command].name);
		if (strcmp(commands[command].name, argv[0]) == 0) {
			commands[command].function(argc, argv);
            return;
		}
	}

    cmd_not_valid(argc, argv);
    return;
}

/* cmd_echo: print every argument separated by space */
int cmd_echo(int argc, char *argv[]) {
	int c;
    unsigned i;

	enum {
		CMD_ECHO_ERROR_NONE = 0,
		CMD_ECHO_ERROR_OPTIONS,
		CMD_ECHO_ERROR_GETOPT_DEFAULT,
	} error = CMD_ECHO_ERROR_NONE;
	struct {
		bool not_new_line;
		bool help;
	} opt = {FALSE, FALSE};

    /* Get every option */
    while ((c = getopt(argc, argv, "hn"))!=-1 && opt.help==FALSE) {
        switch (c) {
        case '?':
        case 'h':
            opt.help = TRUE;
            break;
        case 'n':
            opt.not_new_line = TRUE;
            break;
        default:
        	error = CMD_ECHO_ERROR_GETOPT_DEFAULT;
        	break;
        }
    }
    // Use in case you expect 0 arguments
/*
    if (optind < argc) {
    	error = CMD_ECHO_ERROR_OPTIONS;
    }
*/

    /* Act according to the options */
    if (!opt.help && !error) {
        for (i = 1; i<argc; i++) {
            printf("%s ", argv[i]);
        }
        if (!opt.not_new_line) {
            printf("\n");
        }
    }

    /* Handle errors */
        switch (error) {
        case CMD_ECHO_ERROR_NONE:<commit_msg>Improve the behaviour of cmd_echo<commit_after>#include "commands.h"

#define NL "\n" // New Line
#define TRUE 1
#define FALSE 0
typedef char bool;

struct _command commands[] =
{
  COMMAND (echo),
  COMMAND (calculator),
  COMMAND (system),
  COMMAND (help),
  COMMAND (not_valid)
};

//const unsigned n_commands = CMD_NOT_VALID;

/* separate_args: splits a message in arguments separated by ' ' */
unsigned separate_args(char *msg, char *argv[]) {
    unsigned argc = 0;

	while(*msg) {
		while(*msg == ' ') *msg++ = '\0';
		argv[argc++] = msg;
		while(*msg && *msg != ' ') msg++;
	}

	return argc;
}

/* execute_command: identifies the command from a list and executes it */
void execute_command(int argc, char* argv[]) {
	unsigned command;

	/* Identify the command */
	if (strlen(argv[0]))
	for (command = 0; strcmp(commands[command].name, "not_valid"); command++) {
        //printf("%s" NL, commands[command].name);
		if (strcmp(commands[command].name, argv[0]) == 0) {
			commands[command].function(argc, argv);
            return;
		}
	}

    cmd_not_valid(argc, argv);
    return;
}

/* cmd_echo: print every argument separated by space */
int cmd_echo(int argc, char *argv[]) {
	int c;
    unsigned i;

	enum {
		CMD_ECHO_ERROR_NONE = 0,
		CMD_ECHO_ERROR_OPTIONS,
		CMD_ECHO_ERROR_GETOPT_DEFAULT,
	} error = CMD_ECHO_ERROR_NONE;
	struct {
		bool not_new_line;
		bool help;
	} opt = {FALSE, FALSE};

    /* Get every option */
    while ((c = getopt(argc, argv, "hn"))!=-1 && opt.help==FALSE) {
        switch (c) {
        case '?':
        case 'h':
            opt.help = TRUE;
            break;
        case 'n':
            opt.not_new_line = TRUE;
            break;
        default:
        	error = CMD_ECHO_ERROR_GETOPT_DEFAULT;
        	break;
        }
    }
    // Use in case you expect 0 arguments
/*
    if (optind < argc) {
    	error = CMD_ECHO_ERROR_OPTIONS;
    }
*/

    /* Act according to the options */
    if (!opt.help && !error) {
        for (i = opt.not_new_line?2:1; i<argc; i++) {
            printf("%s", argv[i]);
            if (i<argc-1) printf(" ");
        }
        if (!opt.not_new_line) {
            printf("\n");
        }
    }

    /* Handle errors */
        switch (error) {
        case CMD_ECHO_ERROR_NONE:
{'fraction_non_alphanumeric': 0.14873062233206022, 'fraction_numerical': 0.005841383958660975, 'mean_word_length': 2.4404945904173108, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, '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': '1044589', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1517, 'n_words': 394}