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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/* This file is part of GNU Dico
Copyright (C) 2003-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 <config.h>
#include <dico.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "itrsh.h"
void
usage(int code)
{
printf("usage: listop..]\n");
exit(code);
}
dico_iterator_t
list_iterator(void *object)
{
return dico_list_iterator(object);
}
void
print_item(void *item)
{
printf("%s", (char*)item);
}
void
delete(void *object, int argc, char **argv)
{
dico_list_t list = object;
while (--argc) {
if (dico_list_remove(list, *++argv, NULL))
dico_log(L_ERR, errno, "dico_list_remove(%s)", *argv);
}
}
void
add(void *object, int argc, char **argv)
{
dico_list_t list = object;
while (--argc) {
if (dico_list_append(list, strdup(*++argv)))
dico_log(L_ERR, errno, "dico_list_append");
}
}
void
prep(void *object, int argc, char **argv)
{
dico_list_t list = object;
while (--argc) {
if (dico_list_prepend(list, strdup(*++argv)))
dico_log(L_ERR, errno, "dico_list_prepend");
}
}
void
find(void *object, int argc, char **argv)
{
dico_iterator_t itr;
char *arg = argv[1];
char *text;
itr = shell_iterator();
for (text = dico_iterator_first(itr);
text;
text = dico_iterator_next(itr)) {
if (strcmp(arg, text) == 0)
return;
}
dico_log(L_ERR, 0, "%s not in list", arg);
}
void
push(void *object, int argc, char **argv)
{
dico_list_t list = object;
while (--argc)
if (dico_list_push(list, strdup(*++argv)))
dico_log(L_ERR, errno, "dico_list_push");
}
void
pop(void *object, int argc, char **argv)
{
dico_list_t list = object;
char *p = dico_list_pop(list);
if (!p)
dico_log(L_NOTICE, 0, "nothing to pop");
else
printf("%s\n", p);
free(p);
}
void
number(void *object, int argc, char **argv)
{
dico_list_t list = object;
char *p;
size_t n = strtoul(argv[0], &p, 0);
if (*p != 0)
fprintf(stderr, "?\n");
else {
char *text = dico_list_item(list, n);
if (!text) {
if (errno == ENOENT)
dico_log(L_ERR, 0, "dico_list_item: item not found");
else
dico_log(L_ERR, errno, "dico_list_item");
return;
}
printf("%s\n", text);
}
}
static int
string_comp(const void *item, const void *value, void *ignored)
{
return strcmp(item, value);
}
struct itr_shell_command cmdtab[] = {
{ "del", 2, 0, delete, "item...", "delete items" },
{ "add", 2, 0, add, "item...", "add new items" },
{ "prep", 2, 0, prep, "item...", "add new items at the beginning" },
{ "find", 2, 2, find, "item", "find item in the list" },
{ "push", 2, 0, push, "item...", "push items to the list" },
{ "pop", 1, 1, pop, "", "pop item from the list" },
{ NULL, 1, 1, number }
};
static size_t
get_count(void *data)
{
return dico_list_count((dico_list_t)data);
}
int
main(int argc, char **argv)
{
struct itr_shell sh;
dico_list_t list;
dico_set_program_name(argv[0]);
while (--argc) {
char *arg = *++argv;
if (strcmp(arg, "-h") == 0)
usage(0);
else if (strcmp(arg, "--") == 0) {
--argc;
++argv;
break;
} else if (arg[0] == '-') {
dico_log(L_ERR, 0, "unknown option %s", arg);
exit(1);
} else
break;
}
list = dico_list_create();
if (!list) {
perror("dico_list_create");
exit(1);
}
dico_list_set_comparator(list, string_comp, NULL);
while (argc--) {
if (dico_list_append(list, *argv++)) {
perror("dico_list_append");
exit(1);
}
}
memset(&sh, 0, sizeof(sh));
sh.object = list;
sh.get_iterator = list_iterator;
sh.print_item = print_item;
sh.count = get_count;
sh.cmdtab = cmdtab;
shell(&sh);
return 0;
}
|