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
|
/*
* Copyright 2008-2013 Various Authors
* Copyright 2004 Timo Hirvonen
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "tabexp.h"
#include "xmalloc.h"
#include "xstrjoin.h"
#include "debug.h"
#include <stdlib.h>
struct tabexp tabexp = {
.head = NULL,
.tails = NULL,
.count = 0
};
char *tabexp_expand(const char *src, void (*load_matches)(const char *src), int direction)
{
static int idx = -1;
char *expanded;
if (tabexp.tails == NULL) {
load_matches(src);
if (tabexp.tails == NULL) {
BUG_ON(tabexp.head != NULL);
return NULL;
}
BUG_ON(tabexp.head == NULL);
idx = -1;
}
idx += direction;
if (idx >= tabexp.count)
idx = 0;
else if (idx < 0)
idx = tabexp.count - 1;
expanded = xstrjoin(tabexp.head, tabexp.tails[idx]);
if (tabexp.count == 1)
tabexp_reset();
return expanded;
}
void tabexp_reset(void)
{
int i;
for (i = 0; i < tabexp.count; i++)
free(tabexp.tails[i]);
free(tabexp.tails);
free(tabexp.head);
tabexp.tails = NULL;
tabexp.head = NULL;
tabexp.count = 0;
}
|