C-chat
commands.h
Go to the documentation of this file.
1 
6 #ifndef C_CHAT_COMMANDS_H
7 #define C_CHAT_COMMANDS_H
8 
9 #include "ui.h"
10 #include <string.h>
11 
33 #define COMMAND_HANDLER(NAME, commands) \
34 void NAME##Handler (const char* command) { \
35  char* listOfCommands = malloc(16); \
36  memcpy(listOfCommands, "Known commands:", 16); \
37  listOfCommands[15] = '\0'; \
38  commands; \
39  ui_errorMessage(listOfCommands); \
40  free(listOfCommands); \
41 }
42 
77 #define COMMAND(NAME, usage, next) \
78 { \
79  unsigned int listLength = strlen(listOfCommands); \
80  unsigned int commandLength = strlen(usage); \
81  char* listOfCommandsCopy = malloc(listLength + 1 + commandLength + 1); \
82  memcpy(listOfCommandsCopy, listOfCommands, listLength); \
83  listOfCommandsCopy[listLength] = '\n'; \
84  memcpy(listOfCommandsCopy + listLength + 1, usage, commandLength); \
85  listOfCommandsCopy[listLength + 1 + commandLength] = '\0'; \
86  free(listOfCommands); \
87  listOfCommands = listOfCommandsCopy; \
88 } \
89 if ( \
90  strncmp(#NAME, command, strlen(#NAME)) == 0 && strlen(#NAME) == strlen(command) || \
91  strncmp(#NAME, command, strlen(#NAME)) == 0 && strncmp(" ", command + strlen(#NAME), 1) == 0 \
92 ) {\
93  command = command + strlen(#NAME); \
94  int commandLength##NAME = strlen(command); \
95  while (commandLength##NAME > 0 && command[0] == ' ') { \
96  command += 1; \
97  commandLength##NAME = strlen(command); \
98  } \
99  next; \
100  ui_errorMessage(usage); \
101  return;\
102 };
103 
104 #endif //C_CHAT_COMMANDS_H
ui.h
API to communicate with user.