HelenOS sources
This source file includes following definitions.
- list_aliases
- print_alias
- set_alias
- validate_name
- help_cmd_alias
- cmd_alias
#include <stdio.h>
#include <stdlib.h>
#include <str.h>
#include "config.h"
#include "util.h"
#include "errors.h"
#include "entry.h"
#include "alias.h"
#include "cmds.h"
#include <adt/odict.h>
static const char *cmdname = "alias";
static const char *alias_format = "%s='%s'\n";
static void list_aliases()
{
odlink_t *alias_link = odict_first(&alias_dict);
while (alias_link != NULL) {
alias_t *data = odict_get_instance(alias_link, alias_t, odict);
printf(alias_format, data->name, data->value);
alias_link = odict_next(alias_link, &alias_dict);
}
}
static bool print_alias(const char *name)
{
odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)name, NULL);
if (alias_link != NULL) {
alias_t *data = odict_get_instance(alias_link, alias_t, odict);
printf(alias_format, data->name, data->value);
return true;
}
cli_error(CL_ENOENT, "%s: No alias with the name '%s' exists\n", cmdname, name);
return false;
}
static errno_t set_alias(const char *name, const char *value)
{
odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)name, NULL);
if (alias_link != NULL) {
alias_t *data = odict_get_instance(alias_link, alias_t, odict);
char *dup_value = str_dup(value);
if (dup_value == NULL) {
cli_error(CL_ENOMEM, "%s: failing to allocate memory for value\n", cmdname);
return ENOMEM;
}
free(data->value);
data->value = dup_value;
} else {
alias_t *data = (alias_t *)calloc(1, sizeof(alias_t));
if (data == NULL) {
cli_error(CL_ENOMEM, "%s: failing to allocate memory for data container\n", cmdname);
return ENOMEM;
}
data->name = str_dup(name);
if (data->name == NULL) {
cli_error(CL_ENOMEM, "%s: failing to allocate memory for name\n", cmdname);
free(data);
return ENOMEM;
}
data->value = str_dup(value);
if (data->value == NULL) {
cli_error(CL_ENOMEM, "%s: failing to allocate memory for value\n", cmdname);
free(data->name);
free(data);
return ENOMEM;
}
odict_insert(&data->odict, &alias_dict, NULL);
}
return EOK;
}
static bool validate_name(const char *name)
{
while (*name != '\0') {
if (*name == '/')
return false;
if (*name == ' ')
return false;
if (*name == '\"')
return false;
if (*name == '\'')
return false;
if (*name == '|')
return false;
name++;
}
return true;
}
void help_cmd_alias(unsigned int level)
{
if (level == HELP_SHORT) {
printf("`%s' sets an alias, displays an alias or lists all aliases\n", cmdname);
} else {
help_cmd_alias(HELP_SHORT);
printf("Usage: `%s' [newalias[='existingCMD --flags] ...]'\n\n"
"If no parameters are given it will display all existing aliases.\n"
"If a parameter without an assignment is given, the value of the given alias will be returned.\n"
"If a parameter with an assignment is given, the alias will be created or updated for the given value. "
"It is possible to create an alias to a different alias. A circularity will prevent an alias to be resolved.\n",
cmdname);
}
}
int cmd_alias(char **argv)
{
if (argv[1] == NULL) {
list_aliases();
return CMD_SUCCESS;
}
size_t i;
for (i = 1; argv[i] != NULL; i++) {
char *name = argv[i];
char *value;
if ((value = str_chr(name, '=')) != NULL) {
name[value - name] = '\0';
if (!validate_name(name)) {
cli_error(CL_EFAIL, "%s: invalid alias name given\n", cmdname);
return CMD_FAILURE;
}
if (set_alias(name, value + 1) != EOK) {
return CMD_FAILURE;
}
} else {
if (!print_alias(name)) {
return CMD_FAILURE;
}
}
}
return CMD_SUCCESS;
}
HelenOS homepage, sources at GitHub