0

I'm reading the source code iw and I'm stucked in this struct.

I dont't understand what these code mean? (.handler = (_handler), etc...)

How can I understand that or any documents to learn that.

    static struct cmd                       \
    __cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden = {\
        .name = (_name),                    \
        .args = (_args),                    \
        .cmd = (_nlcmd),                    \
        .nl_msg_flags = (_flags),               \
        .hidden = (_hidden),                    \
        .idby = (_idby),                    \
        .handler = (_handler),                  \
        .help = (_help),                    \
        .parent = _section,                 \
        .selector = (_sel),                 \
    };                              \


  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 19 '23 at 14:44

1 Answers1

1

This is a macro that unfolds in a static structure.

#define __COMMAND(_section, _symname, _name, _args, _nlcmd, _flags, _hidden, _idby, _handler, _help, _sel)\
    static struct cmd                       \
    __cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden\
    __attribute__((used)) __attribute__((section("__cmd"))) = { \
        .name = (_name),                    \
        .args = (_args),                    \
        .cmd = (_nlcmd),                    \
        .nl_msg_flags = (_flags),               \
        .hidden = (_hidden),                    \
        .idby = (_idby),                    \
        .handler = (_handler),                  \
        .help = (_help),                    \
        .parent = _section,                 \
        .selector = (_sel),                 \
    }

If you take a look at the definition of cmd struct from iw/iw.h file, you would see that the handler member is a function pointer taking a whole bunch of parameters and returning int.

struct cmd {
    const char *name;
    const char *args;
    const char *help;
    const enum nl80211_commands cmd;
    int nl_msg_flags;
    int hidden;
    const enum command_identify_by idby;
    /*
     * The handler should return a negative error code,
     * zero on success, 1 if the arguments were wrong
     * and the usage message should and 2 otherwise.
     */
    int (*handler)(struct nl80211_state *state,
               struct nl_cb *cb,
               struct nl_msg *msg,
               int argc, char **argv,
               enum id_input id);
    const struct cmd *(*selector)(int argc, char **argv);
    const struct cmd *parent;
};

The macro initializes the struct assigning the provided function address to this pointer. The handler member now can be used as sort of interface for the struct and one may call the provided function via it.

cafce25
  • 15,907
  • 4
  • 25
  • 31
CuriousPan
  • 783
  • 1
  • 8
  • 14
  • can you explain function of "handler = (_handler),", I don't understand what it is used for? – tân Hoàng Feb 19 '23 at 10:30
  • @tânHoàng You may want to take a look at "designated initializers". `.handler = (_handler)` initialized the field `handler` of the struct with the `_handler` argument from the macro invocation. – Gerhardh Feb 19 '23 at 10:37
  • @tânHoàng, I have updated my answer for your question from the comment. – CuriousPan Feb 19 '23 at 10:40