Ken Yourek / ucmd

Dependents:   nucleo_ucmd_helloworld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ucCmdLine.c Source File

ucCmdLine.c

00001 #include <stdio.h>
00002 #include <stdarg.h>
00003 #include <string.h>
00004 #include "ucmd_internal.h"
00005 
00006 ucCmdLine *ucCmdLine_init(ucCmdLine *p) {
00007     if (NULL == p) return NULL;
00008     p->cmd_tok = NULL;
00009     p->command_acknowledgment = NULL;
00010     p->transmit = NULL;
00011     p->transmit_state = NULL;
00012     p->is_quiet = ucBool_FALSE;
00013     p->is_canceled = NULL;
00014     p->is_canceled_state = NULL;
00015     p->handle_invalid_command = NULL;
00016     p->handle_invalid_command_state = NULL;
00017     p->response_terminator = NULL;
00018     return p;
00019 }
00020 
00021 ucCmdLine *ucCmdLine_get_instance(void) {
00022     static ucCmdLine instance = { 0 };
00023     static ucCmdLine *pointer = NULL;
00024     if (pointer == NULL) {
00025         pointer = ucCmdLine_init(&instance);
00026     }
00027     return pointer;
00028 }
00029 
00030 ucCmdTok *ucCmdLine_get_cmd_tok(ucCmdLine *p) {
00031     if (NULL == p) return NULL;
00032     return p->cmd_tok;
00033 }
00034 
00035 void ucCmdLine_set_cmd_tok(ucCmdLine *p, ucCmdTok *value) {
00036     if (NULL == p) return;
00037     p->cmd_tok = value;
00038 }
00039 
00040 ucCmdLineToks *ucCmdLine_get_cmd_toks(ucCmdLine *p, ucCmdLineToks *buffer) {
00041     if (NULL == p) return NULL;
00042     if (NULL == buffer) return NULL;
00043 
00044     buffer->cmd_tok = ucCmdLine_get_cmd_tok(p);
00045     buffer->arg_tok = ucCmdTok_get_arg(buffer->cmd_tok);
00046     buffer->switch_tok = ucCmdTok_get_switch(buffer->cmd_tok);
00047 
00048     return buffer;
00049 }
00050 
00051 const char *ucCmdLine_format_response_va(ucCmdLine *p, const char *format, va_list arg_list) {
00052     if (NULL == p) return NULL;
00053 
00054     /* TODO: Buffer and copy were added because "usage" uses the command's response in the arg list.
00055        TODO: There's probably a better-performing way to handle that, though. */
00056     vsnprintf(p->response_buffer, sizeof(p->response_buffer) - 1, format, arg_list);
00057     strcpy(p->response, p->response_buffer);
00058     return p->response;
00059 }
00060 
00061 const char *ucCmdLine_format_response(ucCmdLine *p, const char *format, ...) {
00062     va_list arg_list;
00063     const char *response;
00064 
00065     va_start(arg_list, format);
00066     response = ucCmdLine_format_response_va(p, format, arg_list);
00067     va_end(arg_list);
00068 
00069     return response;
00070 }
00071 
00072 void ucCmdLine_respond(ucCmdLine *p, const char *response) {
00073     if (NULL == p) return;
00074     if (NULL == p->transmit) return;
00075     if (p->is_quiet) return;
00076     p->transmit(response, p->transmit_state);
00077 }
00078 
00079 void ucCmdLine_set_transmit(ucCmdLine *p, ucCmdLine_TransmitFunc *value) {
00080     if (NULL == p) return;
00081     p->transmit = value;
00082 }
00083 
00084 ucCmdLine_TransmitFunc *ucCmdLine_get_transmit(ucCmdLine *p) {
00085     if (NULL == p) return NULL;
00086     return p->transmit;
00087 }
00088 
00089 ucBool ucCmdLine_is_canceled(ucCmdLine *p) {
00090     if (NULL == p) return ucBool_FALSE;
00091     if (NULL == p->is_canceled) return ucBool_FALSE;
00092     return p->is_canceled(p->is_canceled_state);
00093 }
00094 
00095 void ucCmdLine_set_is_canceled(ucCmdLine *p, ucCmdLine_IsCanceledFunc *value) {
00096     if (NULL == p) return;
00097     p->is_canceled = value;
00098 }
00099 
00100 ucCmdLine_IsCanceledFunc *ucCmdLine_get_is_canceled(ucCmdLine *p) {
00101     if (NULL == p) return NULL;
00102     return p->is_canceled;
00103 }
00104 
00105 void *ucCmdLine_get_transmit_state(ucCmdLine *p) {
00106     if (NULL == p) return NULL;
00107     return p->transmit_state;
00108 }
00109 
00110 uc_EXPORTED void ucCmdLine_set_transmit_state(ucCmdLine *p, void *value) {
00111     if (NULL == p) return;
00112     p->transmit_state = value;
00113 }
00114 
00115 void *ucCmdLine_get_is_canceled_state(ucCmdLine *p) {
00116     if (NULL == p) return NULL;
00117     return p->is_canceled_state;
00118 }
00119 
00120 void ucCmdLine_set_is_canceled_state(ucCmdLine *p, void *value) {
00121     if (NULL == p) return;
00122     p->is_canceled_state = value;
00123 }
00124 
00125 void ucCmdLine_set_is_quiet(ucCmdLine *p, ucBool value) {
00126     if (NULL == p) return;
00127     p->is_quiet = value;
00128 }
00129 
00130 ucBool ucCmdLine_get_is_quiet(ucCmdLine *p) {
00131     if (NULL == p) return ucBool_FALSE;
00132     return p->is_quiet;
00133 }
00134 
00135 void ucCmdLine_set_handle_invalid_command(ucCmdLine *p, ucCmdLine_HandleInvalidCommandFunc *value) {
00136     if (NULL == p) return;
00137     p->handle_invalid_command = value;
00138 }
00139 
00140 ucCmdLine_HandleInvalidCommandFunc *ucCmdLine_get_handle_invalid_command(ucCmdLine *p) {
00141     if (NULL == p) return NULL;
00142     return p->handle_invalid_command;
00143 }
00144 
00145 void ucCmdLine_set_handle_invalid_command_state(ucCmdLine *p, void *value) {
00146     if (NULL == p) return;
00147     p->handle_invalid_command_state = value;
00148 }
00149 
00150 void *ucCmdLine_get_handle_invalid_command_state(ucCmdLine *p) {
00151     if (NULL == p) return NULL;
00152     return p->handle_invalid_command_state;
00153 }
00154 
00155 ucBool ucCmdLine_handle_invalid_command(ucCmdLine *p, const char *invalid_command) {
00156     if (NULL == p) return ucBool_FALSE;
00157     if (NULL == p->handle_invalid_command) return ucBool_FALSE;
00158     return p->handle_invalid_command(invalid_command, p->handle_invalid_command_state);
00159 }
00160 
00161 size_t ucCmdLine_get_response_size_max(ucCmdLine *p) {
00162     if (NULL == p) return 0;
00163     return sizeof(p->response);
00164 }
00165 
00166 void ucCmdLine_set_response_terminator(ucCmdLine *p, const char *value) {
00167     if (NULL == p) return;
00168     p->response_terminator = value;
00169 }
00170 
00171 const char *ucCmdLine_get_response_terminator(ucCmdLine *p) {
00172     if (NULL == p) return NULL;
00173     return p->response_terminator;
00174 }
00175 
00176 void ucCmdLine_set_command_acknowledgment(ucCmdLine *p, const char *value) {
00177     if (NULL == p) return;
00178     p->command_acknowledgment = value;
00179 }
00180 
00181 const char *ucCmdLine_get_command_acknowledgment(ucCmdLine *p) {
00182     if (NULL == p) return NULL;
00183     return p->command_acknowledgment;
00184 }
00185 
00186 void ucCmdLine_acknowledge_command(ucCmdLine *p) {
00187     const char *command_acknowledgment = ucCmdLine_get_command_acknowledgment(p);
00188     if (command_acknowledgment) {
00189         ucCmdLine_respond(p, command_acknowledgment);
00190     }
00191 }
00192 
00193 void ucCmdLine_terminate_response(ucCmdLine *p) {
00194     const char *response_terminator = ucCmdLine_get_response_terminator(p);
00195     if (response_terminator) {
00196         ucCmdLine_respond(p, response_terminator);
00197     }
00198 }
00199