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
|
/* This file is part of GNU Dico.
Copyright (C) 2008-2024 Sergey Poznyakoff
GNU Dico 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, or (at your option)
any later version.
GNU Dico 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 GNU Dico. If not, see <http://www.gnu.org/licenses/>. */
#include <dicod.h>
#include <hash.h>
struct alias {
char *kw; /* Keyword */
int argc;
char **argv;
grecs_locus_t locus;
};
static Hash_table *alias_table;
/* Calculate the hash of a struct input_file_ident. */
static size_t
alias_hasher(void const *data, size_t n_buckets)
{
const struct alias *ap = data;
return hash_string(ap->kw, n_buckets);
}
/* Compare two input_file_idents for equality. */
static bool
alias_compare(void const *data1, void const *data2)
{
const struct alias *ap1 = data1;
const struct alias *ap2 = data2;
return strcmp(ap1->kw, ap2->kw) == 0;
}
int
alias_install(const char *kw, int argc, char **argv, grecs_locus_t *ploc)
{
struct alias *sample = xmalloc(sizeof(*sample)),
*ap;
sample->kw = xstrdup(kw);
sample->argc = argc;
sample->argv = argv;
sample->locus = *ploc;
if (! ((alias_table
|| (alias_table = hash_initialize(0, 0,
alias_hasher,
alias_compare,
NULL)))
&& (ap = hash_insert(alias_table, sample))))
xalloc_die();
if (ap != sample) {
free(sample->kw);
free(sample);
grecs_error(ploc, 0, _("alias `%s' already defined"), kw);
grecs_error(&ap->locus, 0,
_("this is the location of the previous definition"));
return 1; /* Found */
}
return 0;
}
static int
list_alias_cmp(const void *a, const void *b, void *ignore)
{
const struct alias *ap1 = a;
const struct alias *ap2 = b;
return strcmp(ap1->kw, ap2->kw);
}
int
alias_expand(int argc, char **argv, int *pargc, char ***pargv)
{
struct alias sample, *ap;
dico_list_t alist = NULL;
if (!alias_table)
return 1;
for (sample.kw = argv[0]; (ap = hash_lookup(alias_table, &sample));) {
if (alist && dico_list_locate(alist, ap))
break;
sample.kw = (char*) ap->argv[0];
if (!alist) {
alist = xdico_list_create();
dico_list_set_comparator (alist, list_alias_cmp, NULL);
}
xdico_list_append(alist, ap);
}
if (alist) {
int pos;
int nargc;
char **nargv;
dico_iterator_t itr = xdico_list_iterator(alist);
char *kw;
nargc = 1;
for (ap = dico_iterator_first(itr); ap; ap = dico_iterator_next(itr))
nargc += ap->argc - 1;
pos = nargc;
nargc += argc - 1;
nargv = xcalloc(nargc + 1, sizeof(nargv[0]));
memcpy(nargv + pos, argv + 1, argc * sizeof(argv[0]));
pos--;
for (ap = dico_iterator_first(itr); ap; ap = dico_iterator_next(itr)) {
int i;
for (i = ap->argc-1; i > 0; i--)
nargv[pos--] = ap->argv[i];
kw = ap->argv[0];
}
nargv[pos] = kw;
*pargc = nargc;
*pargv = nargv;
dico_list_destroy(&alist);
return 0;
}
return 1;
}
|