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
|
/* This file is part of GDBM, the GNU data base manager.
Copyright (C) 2018-2025 Free Software Foundation, Inc.
GDBM 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.
GDBM 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 GDBM. If not, see <http://www.gnu.org/licenses/>. */
#include "gdbmtool.h"
#include <stdlib.h>
struct instream_argv
{
struct instream base; /* Base structure */
int argc; /* Number of arguments */
char **argv; /* Vector of arguments */
int idx; /* Index of the current argument */
char *cur; /* Current position in argv[idx] */
int delim; /* True if cur points to a delimiter */
int quote; /* True if the argument must be quoted */
};
static ssize_t
instream_argv_read (instream_t istr, char *buf, size_t size)
{
size_t total = 0;
struct instream_argv *i = (struct instream_argv*)istr;
char const specials[] = " \"\t\n[]{},";
char const escapable[] = "\\\"";
while (total < size)
{
if (*i->cur == 0)
{
if (i->quote)
{
buf[total++] = '"';
i->quote = 0;
continue;
}
if (i->idx == i->argc)
{
if (!i->delim)
{
i->cur = "\n";
i->delim = 1;
}
else
break;
}
else if (!i->delim)
{
i->cur = " ";
i->delim = 1;
}
else
{
size_t len;
i->cur = i->argv[i->idx++];
i->delim = 0;
len = strlen (i->cur);
if (len > 1 && i->cur[0] == '"' && i->cur[len-1] == '"')
i->quote = 0;
else if (i->cur[strcspn (i->cur, specials)])
{
buf[total++] = '"';
i->quote = 1;
continue;
}
else
i->quote = 0;
}
}
if (strchr (escapable, *i->cur))
{
if (total + 2 > size)
break;
buf[total++] = '\\';
}
buf[total++] = *i->cur++;
}
return total;
}
static void
instream_argv_close (instream_t istr)
{
struct instream_argv *i = (struct instream_argv *)istr;
free (i);
}
static int
instream_argv_eq (instream_t a, instream_t b)
{
return 0;
}
instream_t
instream_argv_create (int argc, char **argv)
{
struct instream_argv *istr;
istr = emalloc (sizeof *istr);
istr->base.in_name = "argv";
istr->base.in_inter = 0;
istr->base.in_read = instream_argv_read;
istr->base.in_close = instream_argv_close;
istr->base.in_eq = instream_argv_eq;
istr->base.in_history_size = NULL;
istr->base.in_history_get = NULL;
istr->argc = argc;
istr->argv = argv;
istr->idx = 0;
istr->cur = "";
istr->delim = 1;
istr->quote = 0;
return (instream_t) istr;
}
|