µC/TELNETs External Command Processing
When no command match is found internally, the TELNETs_CmdHandlerExt()
callback function is automatically called. This function needs to be implemented in the user application, and has the following prototype:
CPU_INT16S TELNETs_CmdHandlerExt (CPU_CHAR *pcmd_line, void *pcwd, CPU_BOOLEAN *psession_active, void *pout_opt, TELNET_OUT_FNCT pout_fnct, TELNETs_ERR *perr);
where pcmd_line
is the received complete line; pcwd
, a pointer to the current working directory; and psession_active
, a pointer to a variable indicating whether or not the telnet session is active. As for the pout_fnct
parameter, it is a pointer to a function whose purpose is to provide a way for a command to transmit data to the telnet client, with the help of the pout_opt
parameter.
The return value is implementation specific, but in case of an error, TELNETs_CMDPROCESS_ERR
must be returned, and perr must be set accordingly:
TELNETs_ERR_NONE
No error.TELNETs_ERR_CMD_PROCESS
Error processing command.TELNETs_ERR_CMD_EXEC
Error executing command.
The pseudocode for TELNETs_CmdHandlerExt()
is shown in following listing.
CPU_INT16S TELNETs_CmdHandlerExt (CPU_CHAR *pcmd_line, void *pcwd, CPU_BOOLEAN *psession_active, void *pout_opt, TELNETs_SESSION *psession, TELNET_OUT_FNCT *pout_fnct, TELNETs_ERR *perr) { Parse the command line (pcmd_line). (1) Search corresponding command. (2) Execute command. (3) Transmit error message if needed. (4) }
- The command line received by the telnet server and passed in parameter has to be parsed in order to extract the command name, as well as its eventual arguments.
- Once the command name has been read from the command line,
TELNETs_CmdHandlerExt()
has to locate the corresponding function. The storage and search mechanism taking place here is implementation specific. - Finally, the function corresponding to the one passed in the command line is invoked, and output to the telnet client is possibly transmitted.
- If an error occurred while processing (i.e., searching for) the command, the function simply returns with the error variable set accordingly. However, if the error occurred inside the function, it is that function responsibility to transmit an appropriate message to the telnet client.