server: Convert com_mvatt() to lopsub.
[paraslash.git] / attribute.c
index f3f8ea7a45d0fb1136827cc19c8a715fdd3773a6..2beabafa7a3d4b65f7ea87207b0c1e020f15a1d3 100644 (file)
@@ -8,7 +8,9 @@
 
 #include <regex.h>
 #include <osl.h>
+#include <lopsub.h>
 
+#include "server_cmd.lsg.h"
 #include "para.h"
 #include "error.h"
 #include "crypt.h"
@@ -106,30 +108,16 @@ int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum)
        return 1;
 }
 
-/**
- * Flags used by the lsatt command.
- *
- * \param \sa com_lsatt().
- */
-enum lsatt_flags {
-       /** Whether "-a" was given for the lsatt command. */
-       LSATT_FLAG_SORT_BY_ID = 1,
-       /** Whether "-l" was given for the lsatt command. */
-       LSATT_FLAG_LONG = 2,
-       /** Reverse sort order. */
-       LSATT_FLAG_REVERSE = 4
-};
-
 /** Data passed to the action function of lsatt */
 static int print_attribute(struct osl_table *table, struct osl_row *row,
                const char *name, void *data)
 {
        struct afs_callback_arg *aca = data;
-       unsigned flags = *(unsigned *)aca->query.data;
+       bool l_given = SERVER_CMD_OPT_GIVEN(LSATT, LONG, aca->lpr);
        struct osl_object bitnum_obj;
        int ret;
 
-       if (!(flags & LSATT_FLAG_LONG)) {
+       if (!l_given) {
                para_printf(&aca->pbout, "%s\n", name);
                return 1;
        }
@@ -145,21 +133,27 @@ static int print_attribute(struct osl_table *table, struct osl_row *row,
 
 static int com_lsatt_callback(struct afs_callback_arg *aca)
 {
-       unsigned flags = *(unsigned *)aca->query.data;
+       const struct lls_command *cmd = SERVER_CMD_CMD_PTR(LSATT);
+       bool i_given, r_given;
        int ret;
        struct pattern_match_data pmd = {
                .table = attribute_table,
                .loop_col_num = ATTCOL_NAME,
                .match_col_num = ATTCOL_NAME,
-               .patterns = {.data = (char *)aca->query.data + sizeof(flags),
-                       .size = aca->query.size - sizeof(flags)},
                .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING,
                .data = aca,
                .action = print_attribute
        };
-       if (flags & LSATT_FLAG_SORT_BY_ID)
+
+       ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
+       assert(ret >= 0);
+       pmd.lpr = aca->lpr;
+       i_given = SERVER_CMD_OPT_GIVEN(LSATT, ID_SORT, aca->lpr);
+       r_given = SERVER_CMD_OPT_GIVEN(LSATT, REVERSE, aca->lpr);
+
+       if (i_given)
                pmd.loop_col_num = ATTCOL_BITNUM;
-       if (flags & LSATT_FLAG_REVERSE)
+       if (r_given)
                pmd.pm_flags |= PM_REVERSE_LOOP;
        ret = for_each_matching_row(&pmd);
        if (ret < 0)
@@ -167,46 +161,22 @@ static int com_lsatt_callback(struct afs_callback_arg *aca)
        if (pmd.num_matches == 0)
                ret = -E_NO_MATCH;
 out:
+       lls_free_parse_result(aca->lpr, cmd);
        return ret;
 }
 
-int com_lsatt(struct command_context *cc)
+static int com_lsatt(struct command_context *cc, struct lls_parse_result *lpr)
 {
-       unsigned flags = 0;
-       struct osl_object options = {.data = &flags, .size = sizeof(flags)};
-       int i;
-
-       for (i = 1; i < cc->argc; i++) {
-               const char *arg = cc->argv[i];
-               if (arg[0] != '-')
-                       break;
-               if (!strcmp(arg, "--")) {
-                       i++;
-                       break;
-               }
-               if (!strcmp(arg, "-i")) {
-                       flags |= LSATT_FLAG_SORT_BY_ID;
-                       continue;
-               }
-               if (!strcmp(arg, "-l")) {
-                       flags |= LSATT_FLAG_LONG;
-                       continue;
-               }
-               if (!strcmp(arg, "-r")) {
-                       flags |= LSATT_FLAG_REVERSE;
-                       continue;
-               }
-       }
-       return send_option_arg_callback_request(&options, cc->argc - i, cc->argv + i,
-               com_lsatt_callback, afs_cb_result_handler, cc);
+       const struct lls_command *cmd = SERVER_CMD_CMD_PTR(LSATT);
+       return send_lls_callback_request(com_lsatt_callback, cmd, lpr, cc);
 }
+EXPORT_SERVER_CMD_HANDLER(lsatt);
 
 struct addatt_event_data {
        const char *name;
        unsigned char bitnum;
 };
 
-
 static int com_addatt_callback(struct afs_callback_arg *aca)
 {
        char *p;
@@ -284,34 +254,46 @@ int com_addatt(struct command_context *cc)
 
 static int com_mvatt_callback(struct afs_callback_arg *aca)
 {
-       char *old = aca->query.data;
-       size_t size = strlen(old) + 1;
-       char *new = old + size;
-       struct osl_object obj = {.data = old, .size = size};
+       const struct lls_command *cmd = SERVER_CMD_CMD_PTR(MVATT);
+       const char *old, *new;
+       struct osl_object obj;
        struct osl_row *row;
        int ret;
 
+       ret = lls(lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr));
+       assert(ret >= 0);
+       old = lls_input(0, aca->lpr);
+       new = lls_input(1, aca->lpr);
+       obj.data = (char *)old;
+       obj.size = strlen(old) + 1;
        ret = osl(osl_get_row(attribute_table, ATTCOL_NAME, &obj, &row));
        if (ret < 0)
                goto out;
-       obj.data = new;
+       obj.data = (char *)new;
        obj.size = strlen(new) + 1;
+       /* The update fails if the destination attribute exists. */
        ret = osl(osl_update_object(attribute_table, row, ATTCOL_NAME, &obj));
 out:
        if (ret < 0)
                para_printf(&aca->pbout, "cannot rename %s to %s\n", old, new);
        else
                ret = afs_event(ATTRIBUTE_RENAME, &aca->pbout, NULL);
+       lls_free_parse_result(aca->lpr, cmd);
        return ret;
 }
 
-int com_mvatt(struct command_context *cc)
+static int com_mvatt(struct command_context *cc, struct lls_parse_result *lpr)
 {
-       if (cc->argc != 3)
-               return -E_ATTR_SYNTAX;
-       return send_standard_callback_request(cc->argc - 1, cc->argv + 1,
-               com_mvatt_callback, afs_cb_result_handler, cc);
+       const struct lls_command *cmd = SERVER_CMD_CMD_PTR(MVATT);
+       char *errctx;
+       int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx));
+       if (ret < 0) {
+               send_errctx(cc, errctx);
+               return ret;
+       }
+       return send_lls_callback_request(com_mvatt_callback, cmd, lpr, cc);
 }
+EXPORT_SERVER_CMD_HANDLER(mvatt);
 
 static int remove_attribute(struct osl_table *table, struct osl_row *row,
                const char *name, void *data)
@@ -437,13 +419,13 @@ err:
 
 static int att_logical_or(struct osl_row *row, void *data)
 {
-       uint64_t *att_mask = data;
+       uint64_t *att_mask = data, one = 1;
        struct osl_object bitnum_obj;
        int ret = osl_get_object(attribute_table, row, ATTCOL_BITNUM, &bitnum_obj);
 
        if (ret < 0)
                return ret;
-       *att_mask |= 1 << *(unsigned char *)bitnum_obj.data;
+       *att_mask |= one << *(unsigned char *)bitnum_obj.data;
        return 0;
 }