Skip to content

Commit

Permalink
cli: Use macro for flags check
Browse files Browse the repository at this point in the history
  • Loading branch information
walid-git committed Nov 10, 2023
1 parent ac5dd3e commit 701a090
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
4 changes: 2 additions & 2 deletions bin/varnishd/cache/cache_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ cli_cb_before(const struct cli *cli, struct cli_proto *clp, const char * const *

if (clp != NULL &&
!DO_DEBUG(DBG_CLI_SHOW_SENSIITIVE) &&
(clp->desc->flags & CLI_F_SENSITIVE)) {
CLP_SENSITIVE(clp)) {
VSB_clear(cli->cmd);
if (clp->desc->logfunc != NULL)
clp->desc->logfunc(cli, av, cli->cmd);
Expand All @@ -97,7 +97,7 @@ cli_cb_after(const struct cli *cli, struct cli_proto *clp, const char * const *a

if (clp == NULL ||
DO_DEBUG(DBG_CLI_SHOW_SENSIITIVE) ||
!(clp->desc->flags & CLI_F_SENSITIVE)) {
!CLP_SENSITIVE(clp)) {
VSL(SLT_CLI, NO_VXID, "Wr %03u %zd %s",
cli->result, VSB_len(cli->sb), VSB_data(cli->sb));
} else {
Expand Down
8 changes: 4 additions & 4 deletions bin/varnishd/mgt/mgt_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ mcf_askchild(struct cli *cli, const char * const *av, void *priv)
}

cmd = mgt_cmd_lookup(av[1]);
if (cmd != NULL && cmd->flags & CLI_F_INTERNAL) {
if (cmd != NULL && CMD_INTERNAL(cmd)) {
VCLI_Out(cli, "Unknown request.\nType 'help' for more info.\n");
VCLI_SetResult(cli, CLIS_UNKNOWN);
return;
Expand Down Expand Up @@ -374,7 +374,7 @@ mgt_cli_cb_before(const struct cli *cli, struct cli_proto *clp, const char * con

if (cmd != NULL &&
!MGT_DO_DEBUG(DBG_CLI_SHOW_SENSIITIVE) &&
(cmd->flags & CLI_F_SENSITIVE)) {
CMD_SENSITIVE(cmd)) {
d = (*VSB_data(cli->cmd) == '-');
VSB_clear(cli->cmd);
VSB_printf(cli->cmd, "%s", d ? "-" : "");
Expand All @@ -397,7 +397,7 @@ mgt_cli_cb_after(const struct cli *cli, struct cli_proto *clp, const char * cons
cmd = (clp == NULL ? mgt_cmd_lookup(av[1]) : clp->desc);
if (cmd == NULL ||
MGT_DO_DEBUG(DBG_CLI_SHOW_SENSIITIVE) ||
!(cmd->flags & CLI_F_SENSITIVE)) {
!CMD_SENSITIVE(cmd)) {
MGT_Complain(C_CLI, "CLI %s Wr %03u %s",
cli->ident, cli->result, VSB_data(cli->sb));
} else {
Expand Down Expand Up @@ -757,7 +757,7 @@ mgt_DumpRstCli(void)
cp = cmds[z];
if (!strncmp(cp->request, "debug.", 6))
continue;
if (cp->flags & CLI_F_INTERNAL)
if (CMD_INTERNAL(cp))
continue;
printf(".. _ref_cli_");
for (p = cp->request; *p; p++)
Expand Down
12 changes: 12 additions & 0 deletions include/vcli_serve.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ enum cli_flags {
#include "tbl/cli_flags.h"
};

#define _FLG_IS(d, f) (d->flags & f)

#define CMD_AUTH(c) _FLG_IS(c, CLI_F_AUTH)
#define CMD_INTERNAL(c) _FLG_IS(c, CLI_F_INTERNAL)
#define CMD_SENSITIVE(c) _FLG_IS(c, CLI_F_SENSITIVE)
#define CMD_DEBUG(c) _FLG_IS(c, CLI_F_DEBUG)

#define CLP_AUTH(c) CMD_AUTH(c->desc)
#define CLP_INTERNAL(c) CMD_INTERNAL(c->desc)
#define CLP_SENSITIVE(c) CMD_SENSITIVE(c->desc)
#define CLP_DEBUG(c) CMD_DEBUG(c->desc)

struct cli_cmd_desc {
/* Must match CLI_CMD macro in include/tbl/cli_cmds.h */
const char *request;
Expand Down
14 changes: 7 additions & 7 deletions lib/libvarnish/vcli_serve.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ VCLS_func_help(struct cli *cli, const char * const *av, void *priv)
}
}
VTAILQ_FOREACH(clp, &cs->funcs, list) {
if ((clp->desc->flags & CLI_F_AUTH) && !cli->auth)
if (CLP_AUTH(clp) && !cli->auth)
continue;
if (clp->desc->flags & CLI_F_INTERNAL)
if (CLP_INTERNAL(clp))
continue;
if (av[0] != NULL && !strcmp(clp->desc->request, av[0])) {
help_helper(cli, clp, av);
return;
} else if (av[0] == NULL) {
d = clp->desc->flags & CLI_F_DEBUG ? 2 : 1;
d = CLP_DEBUG(clp) ? 2 : 1;
if (filter & d)
help_helper(cli, clp, av);
}
Expand All @@ -211,9 +211,9 @@ VCLS_func_help_json(struct cli *cli, const char * const *av, void *priv)
VCLI_JSON_begin(cli, 2, av);
VTAILQ_FOREACH(clp, &cs->funcs, list) {
sep = "";
if ((clp->desc->flags & CLI_F_AUTH) && !cli->auth)
if (CLP_AUTH(clp) && !cli->auth)
continue;
if (clp->desc->flags & CLI_F_INTERNAL)
if (CLP_INTERNAL(clp))
continue;
VCLI_Out(cli, ",\n {\n");
VSB_indent(cli->sb, 2);
Expand Down Expand Up @@ -323,7 +323,7 @@ cls_lookup(char * const *av, struct cli *cli, struct VCLS *cs, enum cmd_error_e


VTAILQ_FOREACH(clp, &cs->funcs, list) {
if ((clp->desc->flags & CLI_F_AUTH) && !cli->auth)
if (CLP_AUTH(clp) && !cli->auth)
continue;
if (!strcmp(clp->desc->request, av[1]))
break;
Expand Down Expand Up @@ -387,7 +387,7 @@ cls_exec(struct VCLS_fd *cfd, char * const *av)
case (CMD_ERR_UNKNOWN):
break;
default:
if (cs->wildcard && (!(cs->wildcard->desc->flags & CLI_F_AUTH) || cli->auth))
if (cs->wildcard && (!CLP_AUTH(cs->wildcard) || cli->auth))
cls_dispatch(cli, cs->wildcard, av, na);
break;
}
Expand Down

0 comments on commit 701a090

Please sign in to comment.