From 77e8708934c5c792b1435fa11dfe3c0a6f636a8c Mon Sep 17 00:00:00 2001 From: Ian C Date: Mon, 7 Mar 2016 15:00:21 +0000 Subject: Updated README and copied latest version in. --- src/alias.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 src/alias.c (limited to 'src/alias.c') diff --git a/src/alias.c b/src/alias.c new file mode 100644 index 0000000..01017bd --- /dev/null +++ b/src/alias.c @@ -0,0 +1,146 @@ +/* + + casm - Simple, portable assembler + + Copyright (C) 2003-2015 Ian Cowburn (ianc@noddybox.demon.co.uk) + + 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 3 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, see . + + ------------------------------------------------------------------------- + + Collection for aliases. + +*/ +#include +#include +#include +#include + +#include "global.h" +#include "alias.h" + + +/* ---------------------------------------- TYPES +*/ + +typedef struct alias +{ + char *command; + char *alias; + struct alias *next; +} Alias; + + +/* ---------------------------------------- GLOBALS +*/ +static Alias *head; +static Alias *tail; + + +/* ---------------------------------------- PRIVATE FUNCTIONS INTERFACES +*/ +static Alias *FindAlias(const char *p) +{ + Alias *a = head; + + while(a) + { + if (CompareString(a->command, p)) + { + return a; + } + + a = a->next; + } + + return NULL; +} + + +static void AddAlias(const char *p, const char *r) +{ + Alias *a = FindAlias(p); + + if (!a) + { + a = Malloc(sizeof *a); + + a->command = DupStr(p); + a->alias = DupStr(r); + a->next = NULL; + + if (tail) + { + tail->next = a; + } + + tail = a; + + if (!head) + { + head = a; + } + } + else + { + free(a->alias); + a->alias = DupStr(r); + } +} + + +/* ---------------------------------------- INTERFACES +*/ + +void AliasClear() +{ + while(head) + { + Alias *a; + + free(head->command); + free(head->alias); + a = head; + head = head->next; + free(a); + } + + head = NULL; + tail = NULL; +} + + +void AliasCreate(const char *command, const char *alias) +{ + AddAlias(command, alias); +} + + +char *AliasExpand(char *command) +{ + Alias *a; + + if ((a = FindAlias(command))) + { + free(command); + command = DupStr(a->alias); + } + + return command; +} + + +/* +vim: ai sw=4 ts=8 expandtab +*/ -- cgit v1.2.3