Commit c3b51339 authored by Martin Blix Grydeland's avatar Martin Blix Grydeland

Add a VSLQ_Name2Grouping function to parse -g arguments

parent 81898e08
......@@ -80,13 +80,12 @@ main(int argc, char * const *argv)
{
char optchar;
int d_opt = 0;
char *g_arg = NULL;
struct VSL_data *vsl;
struct VSM_data *vsm;
struct VSL_cursor *c;
struct VSLQ *q;
enum VSL_grouping_e grouping = VSL_g_vxid;
int grouping = VSL_g_vxid;
int i;
vsl = VSL_New();
......@@ -101,7 +100,11 @@ main(int argc, char * const *argv)
break;
case 'g':
/* Grouping mode */
g_arg = optarg;
grouping = VSLQ_Name2Grouping(optarg, -1);
if (grouping == -2)
error(1, "Ambiguous grouping type: %s", optarg);
else if (grouping < 0)
error(1, "Unknown grouping type: %s", optarg);
break;
case 'n':
/* Instance name */
......@@ -112,19 +115,7 @@ main(int argc, char * const *argv)
usage();
}
}
if (g_arg) {
if (!strcmp(g_arg, "raw"))
grouping = VSL_g_raw;
else if (!strcmp(g_arg, "vxid"))
grouping = VSL_g_vxid;
else if (!strcmp(g_arg, "request"))
grouping = VSL_g_request;
else if (!strcmp(g_arg, "session"))
grouping = VSL_g_session;
else
error(1, "Wrong -g argument: %s", g_arg);
}
assert(grouping >= 0 && grouping <= VSL_g_session);
/* Create cursor */
if (VSM_Open(vsm))
......
......@@ -99,6 +99,17 @@ int VSL_Name2Tag(const char *name, int l);
* -2: Multiple tags match substring
*/
int VSLQ_Name2Grouping(const char *name, int l);
/*
* Convert string to grouping (= enum VSL_grouping_e)
*
* Return values:
* >=0: Grouping value
* -1: No grouping type matches
* -2: Multiple grouping types match substring
*/
struct VSL_data *VSL_New(void);
int VSL_Arg(struct VSL_data *vsl, int opt, const char *arg);
/*
......
......@@ -110,5 +110,6 @@ LIBVARNISHAPI_1.3 {
VSLQ_Delete;
VSLQ_Dispatch;
VSLQ_Flush;
VSLQ_Name2Grouping;
# Variables:
} LIBVARNISHAPI_1.0;
......@@ -83,6 +83,36 @@ VSL_Name2Tag(const char *name, int l)
return (n);
}
static const char *vsl_grouping[] = {
[VSL_g_raw] = "raw",
[VSL_g_vxid] = "vxid",
[VSL_g_request] = "request",
[VSL_g_session] = "session",
};
int
VSLQ_Name2Grouping(const char *name, int l)
{
int i, n;
if (l == -1)
l = strlen(name);
n = -1;
for (i = 0; i < sizeof vsl_grouping / sizeof vsl_grouping[0]; i++) {
if (!strncasecmp(name, vsl_grouping[i], l)) {
if (strlen(vsl_grouping[i]) == l) {
/* Exact match */
return (i);
}
if (n == -1)
n = i;
else
n = -2;
}
}
return (n);
}
static int
vsl_ix_arg(struct VSL_data *vsl, int opt, const char *arg)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment