Mercurial > ~mikael > mcabber > hg
comparison mcabber/src/commands.c @ 285:edc263a5d350
Add /alias command
Add /alias command and update completion sytem.
Aliases are expanded before cmd_get() is called.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Thu, 07 Jul 2005 23:25:20 +0100 |
parents | 00b2377539ac |
children | 1eea0fa0955e |
comparison
equal
deleted
inserted
replaced
284:f879b17ecb8e | 285:edc263a5d350 |
---|---|
44 void do_clear(char *arg); | 44 void do_clear(char *arg); |
45 void do_info(char *arg); | 45 void do_info(char *arg); |
46 void do_rename(char *arg); | 46 void do_rename(char *arg); |
47 void do_move(char *arg); | 47 void do_move(char *arg); |
48 void do_set(char *arg); | 48 void do_set(char *arg); |
49 void do_alias(char *arg); | |
49 | 50 |
50 // Global variable for the commands list | 51 // Global variable for the commands list |
51 static GSList *Commands; | 52 static GSList *Commands; |
52 | 53 |
53 | 54 |
70 // cmd_init() | 71 // cmd_init() |
71 // ... | 72 // ... |
72 void cmd_init(void) | 73 void cmd_init(void) |
73 { | 74 { |
74 cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add); | 75 cmd_add("add", "Add a jabber user", COMPL_JID, 0, &do_add); |
76 cmd_add("alias", "Add an alias", 0, 0, &do_alias); | |
75 cmd_add("buffer", "Manipulate current buddy's buffer (chat window)", | 77 cmd_add("buffer", "Manipulate current buddy's buffer (chat window)", |
76 COMPL_BUFFER, 0, &do_buffer); | 78 COMPL_BUFFER, 0, &do_buffer); |
77 cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear); | 79 cmd_add("clear", "Clear the dialog window", 0, 0, &do_clear); |
78 cmd_add("del", "Delete the current buddy", 0, 0, &do_del); | 80 cmd_add("del", "Delete the current buddy", 0, 0, &do_del); |
79 cmd_add("group", "Change group display settings", COMPL_GROUP, 0, &do_group); | 81 cmd_add("group", "Change group display settings", COMPL_GROUP, 0, &do_group); |
128 compl_add_category_word(COMPL_MULTILINE, "begin"); | 130 compl_add_category_word(COMPL_MULTILINE, "begin"); |
129 compl_add_category_word(COMPL_MULTILINE, "send"); | 131 compl_add_category_word(COMPL_MULTILINE, "send"); |
130 compl_add_category_word(COMPL_MULTILINE, "verbatim"); | 132 compl_add_category_word(COMPL_MULTILINE, "verbatim"); |
131 } | 133 } |
132 | 134 |
135 // expandalias(line) | |
136 // If there is one, expand the alias in line and returns a new allocated line | |
137 // If no alias is found, returns line | |
138 // Note : if the returned pointer is different from line, the caller should | |
139 // g_free() the pointer after use | |
140 char *expandalias(char *line) | |
141 { | |
142 const char *p1, *p2; | |
143 char *word; | |
144 const gchar *value; | |
145 char *newline = line; | |
146 | |
147 // Ignore leading '/' | |
148 for (p1 = line ; *p1 == '/' ; p1++) | |
149 ; | |
150 // Locate the end of the word | |
151 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++) | |
152 ; | |
153 // Extract the word | |
154 word = g_strndup(p1, p2-p1); | |
155 | |
156 // Look for an alias in the list | |
157 value = settings_get(SETTINGS_TYPE_ALIAS, (const char*)word); | |
158 if (value) { | |
159 // There is an alias to expand | |
160 newline = g_new(char, strlen(value)+strlen(p2)+2); | |
161 *newline = '/'; | |
162 strcpy(newline+1, value); | |
163 strcat(newline, p2); | |
164 } | |
165 g_free(word); | |
166 | |
167 return newline; | |
168 } | |
169 | |
133 // cmd_get | 170 // cmd_get |
134 // Finds command in the command list structure. | 171 // Finds command in the command list structure. |
135 // Returns a pointer to the cmd entry, or NULL if command not found. | 172 // Returns a pointer to the cmd entry, or NULL if command not found. |
136 cmd *cmd_get(const char *command) | 173 cmd *cmd_get(const char *command) |
137 { | 174 { |
138 const char *p1, *p2; | 175 const char *p1, *p2; |
139 char *com; | 176 char *com; |
140 GSList *sl_com; | 177 GSList *sl_com; |
178 | |
141 // Ignore leading '/' | 179 // Ignore leading '/' |
142 for (p1 = command ; *p1 == '/' ; p1++) | 180 for (p1 = command ; *p1 == '/' ; p1++) |
143 ; | 181 ; |
144 // Locate the end of the command | 182 // Locate the end of the command |
145 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++) | 183 for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++) |
189 // If this isn't a command, this is a message and it is sent to the | 227 // If this isn't a command, this is a message and it is sent to the |
190 // currently selected buddy. | 228 // currently selected buddy. |
191 int process_line(char *line) | 229 int process_line(char *line) |
192 { | 230 { |
193 char *p; | 231 char *p; |
232 char *xpline; | |
194 cmd *curcmd; | 233 cmd *curcmd; |
195 | 234 |
196 if (!*line) { // User only pressed enter | 235 if (!*line) { // User only pressed enter |
197 if (scr_get_multimode()) { | 236 if (scr_get_multimode()) { |
198 scr_append_multiline(""); | 237 scr_append_multiline(""); |
220 for (p=line ; *p ; p++) | 259 for (p=line ; *p ; p++) |
221 ; | 260 ; |
222 for (p-- ; p>line && (*p == ' ') ; p--) | 261 for (p-- ; p>line && (*p == ' ') ; p--) |
223 *p = 0; | 262 *p = 0; |
224 | 263 |
264 // We do alias expansion here | |
265 xpline = expandalias(line); | |
266 | |
225 // Command "quit"? | 267 // Command "quit"? |
226 if ((!strncasecmp(line, "/quit", 5)) && (scr_get_multimode() != 2) ) | 268 if ((!strncasecmp(xpline, "/quit", 5)) && (scr_get_multimode() != 2) ) |
227 if (!line[5] || line[5] == ' ') | 269 if (!xpline[5] || xpline[5] == ' ') |
228 return 255; | 270 return 255; |
229 | 271 |
230 // If verbatim multi-line mode, we check if another /msay command is typed | 272 // If verbatim multi-line mode, we check if another /msay command is typed |
231 if ((scr_get_multimode() == 2) && (strncasecmp(line, "/msay ", 6))) { | 273 if ((scr_get_multimode() == 2) && (strncasecmp(xpline, "/msay ", 6))) { |
232 // It isn't an /msay command | 274 // It isn't an /msay command |
233 scr_append_multiline(line); | 275 scr_append_multiline(xpline); |
234 return 0; | 276 return 0; |
235 } | 277 } |
236 | 278 |
237 // Commands handling | 279 // Commands handling |
238 curcmd = cmd_get(line); | 280 curcmd = cmd_get(xpline); |
239 | 281 |
240 if (!curcmd) { | 282 if (!curcmd) { |
241 scr_LogPrint("Unrecognized command, sorry."); | 283 scr_LogPrint("Unrecognized command, sorry."); |
284 if (xpline != line) g_free(xpline); | |
242 return 0; | 285 return 0; |
243 } | 286 } |
244 if (!curcmd->func) { | 287 if (!curcmd->func) { |
245 scr_LogPrint("Not yet implemented, sorry."); | 288 scr_LogPrint("Not yet implemented, sorry."); |
289 if (xpline != line) g_free(xpline); | |
246 return 0; | 290 return 0; |
247 } | 291 } |
248 // Lets go to the command parameters | 292 // Lets go to the command parameters |
249 for (line++; *line && (*line != ' ') ; line++) | 293 for (p = xpline+1; *p && (*p != ' ') ; p++) |
250 ; | 294 ; |
251 // Skip spaces | 295 // Skip spaces |
252 while (*line && (*line == ' ')) | 296 while (*p && (*p == ' ')) |
253 line++; | 297 p++; |
254 // Call command-specific function | 298 // Call command-specific function |
255 (*curcmd->func)(line); | 299 (*curcmd->func)(p); |
300 if (xpline != line) g_free(xpline); | |
256 return 0; | 301 return 0; |
257 } | 302 } |
258 | 303 |
259 /* Commands callback functions */ | 304 /* Commands callback functions */ |
260 | 305 |
638 } else { | 683 } else { |
639 settings_set(SETTINGS_TYPE_OPTION, option, value); | 684 settings_set(SETTINGS_TYPE_OPTION, option, value); |
640 } | 685 } |
641 } | 686 } |
642 | 687 |
688 void do_alias(char *arg) | |
689 { | |
690 guint assign; | |
691 const gchar *alias, *value; | |
692 | |
693 assign = parse_assigment(arg, &alias, &value); | |
694 if (!alias) { | |
695 scr_LogPrint("Huh?"); | |
696 return; | |
697 } | |
698 if (!assign) { | |
699 // This is a query | |
700 value = settings_get(SETTINGS_TYPE_ALIAS, alias); | |
701 if (value) { | |
702 scr_LogPrint("%s = %s", alias, value); | |
703 } else | |
704 scr_LogPrint("Alias '%s' does not exist", alias); | |
705 return; | |
706 } | |
707 // Check the alias does not conflict with a registered command | |
708 if (cmd_get(alias)) { | |
709 scr_LogPrint("'%s' is a reserved word!", alias); | |
710 return; | |
711 } | |
712 // Update the alias | |
713 if (!value) { | |
714 if (settings_get(SETTINGS_TYPE_ALIAS, alias)) { | |
715 settings_del(SETTINGS_TYPE_ALIAS, alias); | |
716 // Remove alias from the completion list | |
717 compl_del_category_word(COMPL_CMD, alias); | |
718 } | |
719 } else { | |
720 // Add alias to the completion list, if not already in | |
721 if (!settings_get(SETTINGS_TYPE_ALIAS, alias)) | |
722 compl_add_category_word(COMPL_CMD, alias); | |
723 settings_set(SETTINGS_TYPE_ALIAS, alias, value); | |
724 } | |
725 } | |
726 |