server: Convert non-afs commands to lopsub.
[paraslash.git] / command.c
index fe4b923..491ce39 100644 (file)
--- a/command.c
+++ b/command.c
@@ -15,6 +15,7 @@
 #include <arpa/inet.h>
 #include <sys/un.h>
 #include <netdb.h>
+#include <lopsub.h>
 
 #include "para.h"
 #include "error.h"
 #include "daemon.h"
 #include "fd.h"
 #include "ipc.h"
+#include "server_cmd.lsg.h"
 #include "user_list.h"
-#include "server.command_list.h"
 #include "afs.command_list.h"
 #include "signal.h"
 #include "version.h"
 
+#define SERVER_CMD_AUX_INFO(_arg) _arg,
+static const unsigned server_command_perms[] = {LSG_SERVER_CMD_AUX_INFOS};
+#undef SERVER_CMD_AUX_INFO
+#define SERVER_CMD_AUX_INFO(_arg) #_arg,
+static const char * const server_command_perms_txt[] = {LSG_SERVER_CMD_AUX_INFOS};
+#undef SERVER_CMD_AUX_INFO
+
 typedef int server_command_handler_t(struct command_context *);
-static server_command_handler_t SERVER_COMMAND_HANDLERS;
 server_command_handler_t AFS_COMMAND_HANDLERS;
 
 /* Defines one command of para_server. */
@@ -61,7 +68,6 @@ struct server_command {
 };
 
 static struct server_command afs_cmds[] = {DEFINE_AFS_CMD_ARRAY};
-static struct server_command server_cmds[] = {DEFINE_SERVER_CMD_ARRAY};
 
 /** Commands including options must be shorter than this. */
 #define MAX_COMMAND_LEN 32768
@@ -93,21 +99,6 @@ static char *vss_status_tohuman(unsigned int flags)
        return para_strdup("paused");
 }
 
-/*
- * return human readable permission string. Never returns NULL.
- */
-static char *cmd_perms_itohuman(unsigned int perms)
-{
-       char *msg = para_malloc(5 * sizeof(char));
-
-       msg[0] = perms & AFS_READ? 'a' : '-';
-       msg[1] = perms & AFS_WRITE? 'A' : '-';
-       msg[2] = perms & VSS_READ? 'v' : '-';
-       msg[3] = perms & VSS_WRITE? 'V' : '-';
-       msg[4] = '\0';
-       return msg;
-}
-
 /*
  * Never returns NULL.
  */
@@ -123,7 +114,7 @@ static char *vss_get_status_flags(unsigned int flags)
        return msg;
 }
 
-static unsigned get_status(struct misc_meta_data *nmmd, int parser_friendly,
+static unsigned get_status(struct misc_meta_data *nmmd, bool parser_friendly,
                char **result)
 {
        char *status, *flags; /* vss status info */
@@ -158,52 +149,6 @@ static unsigned get_status(struct misc_meta_data *nmmd, int parser_friendly,
        return b.offset;
 }
 
-static int check_sender_args(int argc, char * const * argv, struct sender_command_data *scd)
-{
-       int i;
-
-       const char *subcmds[] = {SENDER_SUBCOMMANDS NULL};
-       scd->sender_num = -1;
-       if (argc < 3)
-               return -E_COMMAND_SYNTAX;
-       for (i = 0; senders[i].name; i++)
-               if (!strcmp(senders[i].name, argv[1]))
-                       break;
-       PARA_DEBUG_LOG("%d:%s\n", argc, argv[1]);
-       if (!senders[i].name)
-               return -E_COMMAND_SYNTAX;
-       scd->sender_num = i;
-       for (i = 0; subcmds[i]; i++)
-               if (!strcmp(subcmds[i], argv[2]))
-                       break;
-       if (!subcmds[i])
-               return -E_COMMAND_SYNTAX;
-       scd->cmd_num = i;
-       if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
-               return -E_SENDER_CMD;
-       switch (scd->cmd_num) {
-       case SENDER_on:
-       case SENDER_off:
-               if (argc != 3)
-                       return -E_COMMAND_SYNTAX;
-               break;
-       case SENDER_deny:
-       case SENDER_allow:
-               if (argc != 4 || parse_cidr(argv[3], scd->host,
-                               sizeof(scd->host), &scd->netmask) == NULL)
-                       return -E_COMMAND_SYNTAX;
-               break;
-       case SENDER_add:
-       case SENDER_delete:
-               if (argc != 4)
-                       return -E_COMMAND_SYNTAX;
-               return parse_fec_url(argv[3], scd);
-       default:
-               return -E_COMMAND_SYNTAX;
-       }
-       return 1;
-}
-
 /**
  * Send a sideband packet through a blocking file descriptor.
  *
@@ -279,6 +224,81 @@ int send_strerror(struct command_context *cc, int err)
        return send_sb_va(&cc->scc, SBD_ERROR_LOG, "%s\n", para_strerror(err));
 }
 
+/**
+ * Send an error context to a client,
+ *
+ * \param cc Client info.
+ * \param errctx The error context string.
+ *
+ * \return The return value of the underlying call to send_sb_va().
+ *
+ * This function frees the error context string after it was sent.
+ */
+int send_errctx(struct command_context *cc, char *errctx)
+{
+       int ret;
+
+       if (!errctx)
+               return 0;
+       ret = send_sb_va(&cc->scc, SBD_ERROR_LOG, "%s\n", errctx);
+       free(errctx);
+       return ret;
+}
+
+static int check_sender_args(struct command_context *cc,
+               struct lls_parse_result *lpr, struct sender_command_data *scd)
+{
+       int i, ret;
+       const char *subcmds[] = {SENDER_SUBCOMMANDS};
+       const char *arg;
+       char *errctx;
+       unsigned num_inputs = lls_num_inputs(lpr);
+
+       scd->sender_num = -1;
+       ret = lls(lls_check_arg_count(lpr, 2, INT_MAX, &errctx));
+       if (ret < 0) {
+               send_errctx(cc, errctx);
+               return ret;
+       }
+       arg = lls_input(0, lpr);
+       for (i = 0; senders[i].name; i++)
+               if (!strcmp(senders[i].name, arg))
+                       break;
+       if (!senders[i].name)
+               return -E_COMMAND_SYNTAX;
+       scd->sender_num = i;
+       arg = lls_input(1, lpr);
+       for (i = 0; subcmds[i]; i++)
+               if (!strcmp(subcmds[i], arg))
+                       break;
+       if (!subcmds[i])
+               return -E_COMMAND_SYNTAX;
+       scd->cmd_num = i;
+       if (!senders[scd->sender_num].client_cmds[scd->cmd_num])
+               return -E_SENDER_CMD;
+       switch (scd->cmd_num) {
+       case SENDER_on:
+       case SENDER_off:
+               if (num_inputs != 2)
+                       return -E_COMMAND_SYNTAX;
+               break;
+       case SENDER_deny:
+       case SENDER_allow:
+               if (num_inputs != 3 || parse_cidr(lls_input(2, lpr), scd->host,
+                               sizeof(scd->host), &scd->netmask) == NULL)
+                       return -E_COMMAND_SYNTAX;
+               break;
+       case SENDER_add:
+       case SENDER_delete:
+               if (num_inputs != 3)
+                       return -E_COMMAND_SYNTAX;
+               return parse_fec_url(lls_input(2, lpr), scd);
+       default:
+               return -E_COMMAND_SYNTAX;
+       }
+       return 1;
+}
+
 /**
  * Send a sideband packet through a blocking file descriptor.
  *
@@ -329,13 +349,13 @@ fail:
        return ret;
 }
 
-static int com_sender(struct command_context *cc)
+static int com_sender(struct command_context *cc, struct lls_parse_result *lpr)
 {
        int i, ret = 0;
        char *msg = NULL;
        struct sender_command_data scd;
 
-       if (cc->argc < 2) {
+       if (lls_num_inputs(lpr) == 0) {
                for (i = 0; senders[i].name; i++) {
                        char *tmp;
                        ret = xasprintf(&tmp, "%s%s\n", msg? msg : "",
@@ -345,11 +365,11 @@ static int com_sender(struct command_context *cc)
                }
                return send_sb(&cc->scc, msg, ret, SBD_OUTPUT, false);
        }
-       ret = check_sender_args(cc->argc, cc->argv, &scd);
+       ret = check_sender_args(cc, lpr, &scd);
        if (ret < 0) {
                if (scd.sender_num < 0)
                        return ret;
-               if (strcmp(cc->argv[2], "status") == 0)
+               if (strcmp(lls_input(1, lpr), "status") == 0)
                        msg = senders[scd.sender_num].status();
                else
                        msg = senders[scd.sender_num].help();
@@ -360,7 +380,8 @@ static int com_sender(struct command_context *cc)
        case SENDER_add:
        case SENDER_delete:
                assert(senders[scd.sender_num].resolve_target);
-               ret = senders[scd.sender_num].resolve_target(cc->argv[3], &scd);
+               ret = senders[scd.sender_num].resolve_target(lls_input(2, lpr),
+                       &scd);
                if (ret < 0)
                        return ret;
        }
@@ -380,17 +401,16 @@ static int com_sender(struct command_context *cc)
        }
        return (i < 10)? 1 : -E_LOCK;
 }
+EXPORT_SERVER_CMD_HANDLER(sender);
 
-/* server info */
-static int com_si(struct command_context *cc)
+static int com_si(struct command_context *cc,
+               __a_unused struct lls_parse_result *lpr)
 {
-       int ret;
        char *msg, *ut;
+       int ret;
 
-       if (cc->argc != 1)
-               return -E_COMMAND_SYNTAX;
-       mutex_lock(mmd_mutex);
        ut = daemon_get_uptime_str(now);
+       mutex_lock(mmd_mutex);
        ret = xasprintf(&msg,
                "up: %s\nplayed: %u\n"
                "server_pid: %d\n"
@@ -411,19 +431,20 @@ static int com_si(struct command_context *cc)
        free(ut);
        return send_sb(&cc->scc, msg, ret, SBD_OUTPUT, false);
 }
+EXPORT_SERVER_CMD_HANDLER(si);
 
-/* version */
-static int com_version(struct command_context *cc)
+static int com_version(struct command_context *cc, struct lls_parse_result *lpr)
 {
        char *msg;
        size_t len;
 
-       if (cc->argc > 1 && strcmp(cc->argv[1], "-v") == 0)
+       if (SERVER_CMD_OPT_GIVEN(VERSION, VERBOSE, lpr))
                len = xasprintf(&msg, "%s", version_text("server"));
        else
                len = xasprintf(&msg, "%s\n", version_single_line("server"));
        return send_sb(&cc->scc, msg, len, SBD_OUTPUT, false);
 }
+EXPORT_SERVER_CMD_HANDLER(version);
 
 /** These status items are cleared if no audio file is currently open. */
 #define EMPTY_STATUS_ITEMS \
@@ -463,7 +484,7 @@ static int com_version(struct command_context *cc)
  *
  * This is used by vss when currently no audio file is open.
  */
-static unsigned empty_status_items(int parser_friendly, char **result)
+static unsigned empty_status_items(bool parser_friendly, char **result)
 {
        char *esi;
        unsigned len;
@@ -491,39 +512,16 @@ static unsigned empty_status_items(int parser_friendly, char **result)
 }
 #undef EMPTY_STATUS_ITEMS
 
-/* stat */
-static int com_stat(struct command_context *cc)
+static int com_stat(struct command_context *cc, struct lls_parse_result *lpr)
 {
-       int i, ret;
+       int ret;
        struct misc_meta_data tmp, *nmmd = &tmp;
        char *s;
-       int32_t num = 0;
-       int parser_friendly = 0;
+       bool parser_friendly = SERVER_CMD_OPT_GIVEN(STAT, PARSER_FRIENDLY,
+               lpr) > 0;
+       uint32_t num = SERVER_CMD_UINT32_VAL(STAT, NUM, lpr);
 
        para_sigaction(SIGUSR1, dummy);
-
-       for (i = 1; i < cc->argc; i++) {
-               const char *arg = cc->argv[i];
-               if (arg[0] != '-')
-                       break;
-               if (!strcmp(arg, "--")) {
-                       i++;
-                       break;
-               }
-               if (!strncmp(arg, "-n=", 3)) {
-                       ret = para_atoi32(arg + 3, &num);
-                       if (ret < 0)
-                               return ret;
-                       continue;
-               }
-               if (!strcmp(arg, "-p")) {
-                       parser_friendly = 1;
-                       continue;
-               }
-               return -E_COMMAND_SYNTAX;
-       }
-       if (i != cc->argc)
-               return -E_COMMAND_SYNTAX;
        for (;;) {
                /*
                 * Copy the mmd structure to minimize the time we hold the mmd
@@ -555,21 +553,34 @@ static int com_stat(struct command_context *cc)
 out:
        return ret;
 }
+EXPORT_SERVER_CMD_HANDLER(stat);
 
-static int send_list_of_commands(struct command_context *cc, struct server_command *cmd,
-               const char *handler)
+/* fixed-length, human readable permission string */
+const char *server_cmd_perms_str(unsigned int perms)
 {
-       char *msg = NULL;
+       static char result[5];
+
+       result[0] = perms & AFS_READ? 'a' : '-';
+       result[1] = perms & AFS_WRITE? 'A' : '-';
+       result[2] = perms & VSS_READ? 'v' : '-';
+       result[3] = perms & VSS_WRITE? 'V' : '-';
+       result[4] = '\0';
+       return result;
+}
 
-       for (; cmd->name; cmd++) {
-               char *tmp, *perms = cmd_perms_itohuman(cmd->perms);
-               tmp = make_message("%s\t%s\t%s\t%s\n", cmd->name, handler,
-                       perms, cmd->description);
-               free(perms);
-               msg = para_strcat(msg, tmp);
-               free(tmp);
+static int send_list_of_commands(struct command_context *cc)
+{
+       int i;
+       const struct lls_command *cmd;
+       char *msg = para_strdup("");
+
+       for (i = 1; (cmd = lls_cmd(i, server_cmd_suite)); i++) {
+               const char *perms = server_cmd_perms_str(server_command_perms[i]);
+               char *tmp = make_message("%s%s\t%s\t%s\n", msg,
+                       lls_command_name(cmd), perms, lls_purpose(cmd));
+               free(msg);
+               msg = tmp;
        }
-       assert(msg);
        return send_sb(&cc->scc, msg, strlen(msg), SBD_OUTPUT, false);
 }
 
@@ -578,12 +589,6 @@ static struct server_command *get_cmd_ptr(const char *name, char **handler)
 {
        struct server_command *cmd;
 
-       for (cmd = server_cmds; cmd->name; cmd++)
-               if (!strcmp(cmd->name, name)) {
-                       if (handler)
-                               *handler = para_strdup("server"); /* server commands */
-                       return cmd;
-               }
        /* not found, look for commands supported by afs */
        for (cmd = afs_cmds; cmd->name; cmd++)
                if (!strcmp(cmd->name, name)) {
@@ -594,75 +599,67 @@ static struct server_command *get_cmd_ptr(const char *name, char **handler)
        return NULL;
 }
 
-/* help */
-static int com_help(struct command_context *cc)
+static int com_help(struct command_context *cc, struct lls_parse_result *lpr)
 {
-       struct server_command *cmd;
-       char *perms, *handler, *buf;
+       const char *perms;
+       char *long_help, *buf, *errctx;
        int ret;
+       const struct lls_command *cmd;
 
-       if (cc->argc < 2) {
-               /* no argument given, print list of commands */
-               if ((ret = send_list_of_commands(cc, server_cmds, "server")) < 0)
-                       return ret;
-               return send_list_of_commands(cc, afs_cmds, "afs");
+       ret = lls(lls_check_arg_count(lpr, 0, 1, &errctx));
+       if (ret < 0) {
+               send_errctx(cc, errctx);
+               return ret;
        }
+       if (lls_num_inputs(lpr) == 0)
+               return send_list_of_commands(cc);
        /* argument given for help */
-       cmd = get_cmd_ptr(cc->argv[1], &handler);
-       if (!cmd)
-               return -E_BAD_CMD;
-       perms = cmd_perms_itohuman(cmd->perms);
-       ret = xasprintf(&buf, "%s - %s\n\n"
-               "handler: %s\n"
-               "permissions: %s\n"
-               "usage: %s\n\n"
-               "%s\n",
-               cc->argv[1],
-               cmd->description,
-               handler,
-               perms,
-               cmd->usage,
-               cmd->help
-       );
-       free(perms);
-       free(handler);
+       ret = lls(lls_lookup_subcmd(lls_input(0, lpr), server_cmd_suite,
+               &errctx));
+       if (ret < 0) {
+               send_errctx(cc, errctx);
+               return ret;
+       }
+       cmd = lls_cmd(ret, server_cmd_suite);
+       perms = server_command_perms_txt[ret];
+       long_help = lls_long_help(cmd);
+       assert(long_help);
+       ret = xasprintf(&buf, "%spermissions: %s\n", long_help, perms);
+       free(long_help);
        return send_sb(&cc->scc, buf, ret, SBD_OUTPUT, false);
 }
+EXPORT_SERVER_CMD_HANDLER(help);
 
-/* hup */
-static int com_hup(struct command_context *cc)
+static int com_hup(__a_unused struct command_context *cc,
+               __a_unused struct lls_parse_result *lpr)
 {
-       if (cc->argc != 1)
-               return -E_COMMAND_SYNTAX;
        kill(getppid(), SIGHUP);
        return 1;
 }
+EXPORT_SERVER_CMD_HANDLER(hup);
 
-/* term */
-static int com_term(struct command_context *cc)
+static int com_term(__a_unused struct command_context *cc,
+               __a_unused struct lls_parse_result *lpr)
 {
-       if (cc->argc != 1)
-               return -E_COMMAND_SYNTAX;
        kill(getppid(), SIGTERM);
        return 1;
 }
+EXPORT_SERVER_CMD_HANDLER(term);
 
-static int com_play(struct command_context *cc)
+static int com_play(__a_unused struct command_context *cc,
+               __a_unused struct lls_parse_result *lpr)
 {
-       if (cc->argc != 1)
-               return -E_COMMAND_SYNTAX;
        mutex_lock(mmd_mutex);
        mmd->new_vss_status_flags |= VSS_PLAYING;
        mmd->new_vss_status_flags &= ~VSS_NOMORE;
        mutex_unlock(mmd_mutex);
        return 1;
 }
+EXPORT_SERVER_CMD_HANDLER(play);
 
-/* stop */
-static int com_stop(struct command_context *cc)
+static int com_stop(__a_unused struct command_context *cc,
+               __a_unused struct lls_parse_result *lpr)
 {
-       if (cc->argc != 1)
-               return -E_COMMAND_SYNTAX;
        mutex_lock(mmd_mutex);
        mmd->new_vss_status_flags &= ~VSS_PLAYING;
        mmd->new_vss_status_flags &= ~VSS_REPOS;
@@ -670,12 +667,11 @@ static int com_stop(struct command_context *cc)
        mutex_unlock(mmd_mutex);
        return 1;
 }
+EXPORT_SERVER_CMD_HANDLER(stop);
 
-/* pause */
-static int com_pause(struct command_context *cc)
+static int com_pause(__a_unused struct command_context *cc,
+               __a_unused struct lls_parse_result *lpr)
 {
-       if (cc->argc != 1)
-               return -E_COMMAND_SYNTAX;
        mutex_lock(mmd_mutex);
        if (!vss_paused() && !vss_stopped()) {
                mmd->events++;
@@ -685,42 +681,44 @@ static int com_pause(struct command_context *cc)
        mutex_unlock(mmd_mutex);
        return 1;
 }
+EXPORT_SERVER_CMD_HANDLER(pause);
 
-/* next */
-static int com_next(struct command_context *cc)
+static int com_next(__a_unused struct command_context *cc,
+               __a_unused struct lls_parse_result *lpr)
 {
-       if (cc->argc != 1)
-               return -E_COMMAND_SYNTAX;
        mutex_lock(mmd_mutex);
        mmd->events++;
        mmd->new_vss_status_flags |= VSS_NEXT;
        mutex_unlock(mmd_mutex);
        return 1;
 }
+EXPORT_SERVER_CMD_HANDLER(next);
 
-/* nomore */
-static int com_nomore(struct command_context *cc)
+static int com_nomore(__a_unused struct command_context *cc,
+               __a_unused struct lls_parse_result *lpr)
 {
-       if (cc->argc != 1)
-               return -E_COMMAND_SYNTAX;
        mutex_lock(mmd_mutex);
        if (vss_playing() || vss_paused())
                mmd->new_vss_status_flags |= VSS_NOMORE;
        mutex_unlock(mmd_mutex);
        return 1;
 }
+EXPORT_SERVER_CMD_HANDLER(nomore);
 
-/* ff */
-static int com_ff(struct command_context *cc)
+static int com_ff(__a_unused struct command_context *cc,
+               struct lls_parse_result *lpr)
 {
        long promille;
        int ret, backwards = 0;
        unsigned i;
-       char c;
+       char c, *errctx;
 
-       if (cc->argc != 2)
-               return -E_COMMAND_SYNTAX;
-       if (!(ret = sscanf(cc->argv[1], "%u%c", &i, &c)))
+       ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
+       if (ret < 0) {
+               send_errctx(cc, errctx);
+               return ret;
+       }
+       if (!(ret = sscanf(lls_input(0, lpr), "%u%c", &i, &c)))
                return -E_COMMAND_SYNTAX;
        if (ret > 1 && c == '-')
                backwards = 1; /* jmp backwards */
@@ -748,17 +746,22 @@ out:
        mutex_unlock(mmd_mutex);
        return ret;
 }
+EXPORT_SERVER_CMD_HANDLER(ff);
 
-/* jmp */
-static int com_jmp(struct command_context *cc)
+static int com_jmp(__a_unused struct command_context *cc,
+               struct lls_parse_result *lpr)
 {
        long unsigned int i;
        int ret;
+       char *errctx;
 
-       if (cc->argc != 2)
-               return -E_COMMAND_SYNTAX;
-       if (sscanf(cc->argv[1], "%lu", &i) <= 0)
-               return -E_COMMAND_SYNTAX;
+       ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
+       if (ret < 0) {
+               send_errctx(cc, errctx);
+               return ret;
+       }
+       if (sscanf(lls_input(0, lpr), "%lu", &i) <= 0)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
        mutex_lock(mmd_mutex);
        ret = -E_NO_AUDIO_FILE;
        if (!mmd->afd.afhi.chunks_total)
@@ -777,16 +780,16 @@ out:
        mutex_unlock(mmd_mutex);
        return ret;
 }
+EXPORT_SERVER_CMD_HANDLER(jmp);
 
-static int com_tasks(struct command_context *cc)
+static int com_tasks(struct command_context *cc,
+               __a_unused struct lls_parse_result *lpr)
 {
        char *tl = server_get_tasks();
-       int ret = 1;
-
-       if (tl)
-               ret = send_sb(&cc->scc, tl, strlen(tl), SBD_OUTPUT, false);
-       return ret;
+       assert(tl);
+       return send_sb(&cc->scc, tl, strlen(tl), SBD_OUTPUT, false);
 }
+EXPORT_SERVER_CMD_HANDLER(tasks);
 
 /*
  * check if perms are sufficient to exec a command having perms cmd_perms.
@@ -858,18 +861,34 @@ static int run_command(struct command_context *cc, struct iovec *iov,
 {
        int ret, i;
        char *p, *end;
-       struct server_command *cmd;
+       struct server_command *cmd = NULL;
+       const struct lls_command *lcmd = NULL;
+       unsigned perms;
+       struct lls_parse_result *lpr;
+       char *errctx;
 
        if (iov->iov_base == NULL || iov->iov_len == 0)
-               return -E_BAD_CMD;
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
        p = iov->iov_base;
        p[iov->iov_len - 1] = '\0'; /* just to be sure */
-       cmd = get_cmd_ptr(p, NULL);
-       if (!cmd)
-               return -E_BAD_CMD;
-       ret = check_perms(cc->u->perms, cmd);
-       if (ret < 0)
-               return ret;
+
+       ret = lls(lls_lookup_subcmd(p, server_cmd_suite, &errctx));
+       if (ret >= 0) {
+               perms = server_command_perms[ret];
+               if ((perms & cc->u->perms) != perms)
+                       return -E_PERM;
+               lcmd = lls_cmd(ret, server_cmd_suite);
+       } else {
+               cmd = get_cmd_ptr(p, NULL);
+               if (!cmd) {
+                       send_errctx(cc, errctx);
+                       return ret;
+               }
+               perms = cmd->perms;
+               ret = check_perms(cc->u->perms, cmd);
+               if (ret < 0)
+                       return ret;
+       }
        end = iov->iov_base + iov->iov_len;
        for (i = 0; p < end; i++)
                p += strlen(p) + 1;
@@ -880,13 +899,23 @@ static int run_command(struct command_context *cc, struct iovec *iov,
                p += strlen(p) + 1;
        }
        cc->argv[cc->argc] = NULL;
-       PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name,
-               cc->u->name, peername);
-       ret = cmd->handler(cc);
+       PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", lcmd?
+               lls_command_name(lcmd) : cmd->name, cc->u->name, peername);
+       if (lcmd) {
+               ret = lls(lls_parse(cc->argc, cc->argv, lcmd, &lpr, &errctx));
+               if (ret >= 0) {
+                       const struct server_cmd_user_data *ud = lls_user_data(lcmd);
+                       ret = ud->handler(cc, lpr);
+                       lls_free_parse_result(lpr, lcmd);
+               } else
+                       send_errctx(cc, errctx);
+       } else {
+               ret = cmd->handler(cc);
+       }
        free_argv(cc->argv);
        mutex_lock(mmd_mutex);
        mmd->num_commands++;
-       if (ret >= 0 && (cmd->perms & AFS_WRITE))
+       if (ret >= 0 && (perms & AFS_WRITE))
                mmd->events++;
        mutex_unlock(mmd_mutex);
        return ret;