1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009, 2010 Daniel Beer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "aliasdb.h"
#include "vector.h"
#include "output.h"
#include "util.h"
struct alias {
char src[256];
char dst[256];
};
static struct vector alias_list = {
.ptr = NULL,
.capacity = 0,
.size = 0,
.elemsize = sizeof(struct alias)
};
static int list_is_sorted;
static struct alias *find_alias(const char *name)
{
int i;
for (i = 0; i < alias_list.size; i++) {
struct alias *a =
VECTOR_PTR(alias_list, i, struct alias);
if (!strcasecmp(name, a->src))
return a;
}
return NULL;
}
struct recurse_list {
const char *cmd;
struct recurse_list *next;
};
static int translate_rec(struct recurse_list *l,
const char *command, const char *args,
char *out_cmd, int max_len)
{
struct recurse_list *c;
const struct alias *a;
if (*command == '\\') {
snprintf(out_cmd, max_len, "%s %s", command + 1, args);
return 0;
}
for (c = l; c; c = c->next)
if (!strcasecmp(c->cmd, command)) {
printc_err("recursive alias: %s\n", command);
return -1;
}
a = find_alias(command);
if (a) {
struct recurse_list r;
char tmp_buf[1024];
char *new_args = tmp_buf;
char *cmd;
snprintf(tmp_buf, sizeof(tmp_buf), "%s %s", a->dst, args);
r.next = l;
r.cmd = command;
cmd = get_arg(&new_args);
return translate_rec(&r, cmd, new_args, out_cmd, max_len);
}
snprintf(out_cmd, max_len, "%s %s", command, args);
return 0;
}
int translate_alias(const char *command, const char *args,
char *out_cmd, int max_len)
{
return translate_rec(NULL, command, args, out_cmd, max_len);
}
static int cmp_alias(const void *a, const void *b)
{
const struct alias *aa = (const struct alias *)a;
const struct alias *ab = (const struct alias *)b;
return strcasecmp(aa->src, ab->src);
}
int cmd_alias(char **arg)
{
const char *src = get_arg(arg);
const char *dst = get_arg(arg);
struct alias *a;
struct alias na;
if (!src) { /* List aliases */
int i;
if (!list_is_sorted) {
qsort(alias_list.ptr, alias_list.size,
alias_list.elemsize, cmp_alias);
list_is_sorted = 1;
}
printc("%d aliases defined:\n", alias_list.size);
for (i = 0; i < alias_list.size; i++) {
struct alias *a =
VECTOR_PTR(alias_list, i, struct alias);
printc(" %20s = %s\n", a->src, a->dst);
}
return 0;
}
a = find_alias(src);
if (!dst) { /* Delete alias */
struct alias *end = VECTOR_PTR(alias_list,
alias_list.size - 1, struct alias);
if (!a) {
printc_err("alias: no such alias defined: %s\n", src);
return -1;
}
if (end != a)
memcpy(a, end, sizeof(*a));
vector_pop(&alias_list);
list_is_sorted = 0;
return 0;
}
if (a) { /* Overwrite old alias */
strncpy(a->dst, dst, sizeof(a->dst));
a->dst[sizeof(a->dst) - 1] = 0;
return 0;
}
/* New alias */
strncpy(na.src, src, sizeof(na.src));
na.src[sizeof(na.src) - 1] = 0;
strncpy(na.dst, dst, sizeof(na.dst));
na.dst[sizeof(na.dst) - 1] = 0;
if (vector_push(&alias_list, &na, 1) < 0) {
printc_err("alias: can't allocate memory: %s\n",
last_error());
return -1;
}
list_is_sorted = 0;
return 0;
}
|