templatecmd.c
author Myhailo Danylenko <isbear@ukrpost.net>
Tue, 30 Mar 2010 23:30:35 +0300
changeset 8 56c82958c730
parent 7 b87a5d8cd8ad
child 9 da83916efe80
permissions -rw-r--r--
Enable compiler warnings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     1
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     2
/* Copyright 2009 Myhailo Danylenko
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     3
 *
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     4
 * This file is part of mcabber-templatecmd
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     5
 *
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     6
 * mcabber-templatecmd is free software: you can redistribute it and/or modify
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     8
 * the Free Software Foundation, either version 2 of the License, or
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     9
 * (at your option) any later version.
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    10
 *
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful,
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    14
 * GNU General Public License for more details.
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    15
 *
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
#include <glib.h>
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
#include <stdlib.h>
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
#include <string.h>
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
1
9eb794ebef85 Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    23
#include <mcabber/compl.h>
9eb794ebef85 Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    24
#include <mcabber/commands.h>
9eb794ebef85 Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    25
#include <mcabber/utils.h>
9eb794ebef85 Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    26
#include <mcabber/logprint.h>
5
c403a516dc04 New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    27
#include <mcabber/modules.h>
c403a516dc04 New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    28
6
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    29
#include "config.h"
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    30
5
c403a516dc04 New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    31
static void tcmd_init (void);
c403a516dc04 New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    32
static void tcmd_uninit (void);
c403a516dc04 New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    33
6
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    34
#define DESCRIPTION ( "Templated commands\nProvides command /templatecmd (and any commands, you define with it)" )
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    35
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    36
static module_info_t info_templatecmd_experimental = {
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    37
	.branch      = "experimental",
7
b87a5d8cd8ad Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    38
	.api         = 10,
6
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    39
	.version     = PROJECT_VERSION,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    40
	.description = DESCRIPTION,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    41
	.requires    = NULL,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    42
	.init        = tcmd_init,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    43
	.uninit      = tcmd_uninit,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    44
	.next        = NULL,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    45
};
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    46
5
c403a516dc04 New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    47
module_info_t info_templatecmd = {
6
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    48
	.branch      = "dev",
7
b87a5d8cd8ad Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
    49
	.api         = 11,
6
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    50
	.version     = PROJECT_VERSION,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    51
	.description = DESCRIPTION,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    52
	.requires    = NULL,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    53
	.init        = tcmd_init,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    54
	.uninit      = tcmd_uninit,
2cc481d6b686 Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents: 5
diff changeset
    55
	.next        = &info_templatecmd_experimental,
5
c403a516dc04 New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    56
};
0
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
typedef struct {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
	gchar    *name;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
	gchar    *template;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
	int       maxarg;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
	gboolean  stararg;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
} tcmd_t;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
static GSList *template_commands = NULL;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
static void tcmd_callback (char *arg, gpointer udata)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
	tcmd_t   *tcmd     = (tcmd_t *) udata;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
	char    **args     = NULL;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
	GString  *command  = g_string_new (NULL);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
	char     *template = tcmd -> template;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
	char     *p;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
	if (tcmd -> maxarg > 0)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
		args = split_arg (arg, tcmd -> maxarg, TRUE);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
	for (p = template; *p; ++p) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
		if (*p != '$' || !*(p + 1) || !strchr ("*$0123456789", *(p + 1))) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
			g_string_append_c (command, *p);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    81
			continue;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    82
		}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    83
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    84
		++p;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    85
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    86
		if (*p == '*')
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
			g_string_append (command, arg);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
		else if (*p == '$')
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
			g_string_append_c (command, '$');
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
		else {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
			int argnum = atoi (p);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
			if (argnum == 0)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    94
				g_string_append (command, tcmd -> name);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
			else if (args[argnum - 1])
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    96
				g_string_append (command, args[argnum - 1]);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    97
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
			while (*(p + 1) && strchr ("0123456789", *(p + 1)))
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    99
				++p;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   100
		}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   101
	}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   102
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   103
	{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   104
		char *cmd = g_string_free (command, FALSE);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
		process_command (cmd, 1);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
		g_free (cmd);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
	}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   109
	if (args)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   110
		free_arg_lst (args);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   111
}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   112
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   113
static void do_templatecmd (char *arg)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   114
{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
	if (!*arg) { // list tcmds
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   116
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   117
		GSList *tel;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   118
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   119
		for (tel = template_commands; tel; tel = tel -> next) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   120
			tcmd_t *tcmd = (tcmd_t *) tel -> data;
7
b87a5d8cd8ad Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   121
			scr_log_print (LPRINT_NORMAL, "Templatecmd %s = %s", tcmd -> name, tcmd -> template);
0
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
		}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
	} else {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   126
		char *nend = strchr (arg, '=');
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   127
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
		if (!nend) { // show tcmd
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   129
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
			GSList *tel;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   131
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
			for (tel = template_commands; tel; tel = tel -> next) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   133
				tcmd_t *tcmd = (tcmd_t *) tel -> data;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   134
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   135
				if (!strcmp (arg, tcmd -> name)) {
7
b87a5d8cd8ad Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   136
					scr_log_print (LPRINT_NORMAL, "Templatecmd %s = %s", tcmd -> name, tcmd -> template);
0
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   137
					return;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   138
				}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   139
			}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   140
7
b87a5d8cd8ad Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   141
			scr_log_print (LPRINT_NORMAL, "No template with such name.");
0
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   142
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   143
		} else if (nend == arg) // error
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   144
7
b87a5d8cd8ad Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   145
			scr_log_print (LPRINT_NORMAL, "You must specify command name.");
0
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   146
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   147
		else { // new/modify tcmd
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   148
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   149
			GSList *tel;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
			tcmd_t *template_command = NULL;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
			char   *astart           = nend + 1;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   152
			int     len;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   153
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   154
			for (--nend; nend > arg && *nend == ' '; --nend);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   155
			len = nend + 1 - arg;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   156
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   157
			for (tel = template_commands; tel; tel = tel -> next) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   158
				tcmd_t *tcmd = (tcmd_t *) tel -> data;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   159
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   160
				if (!strncmp (arg, tcmd -> name, len)) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   161
					template_command = tcmd;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   162
					break;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   163
				}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   164
			}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   165
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
			for (;*astart && *astart == ' '; ++astart);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   167
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   168
			if (!*astart) { // delete tcmd
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   169
				if (template_command) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   170
					cmd_del (template_command -> name);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   171
					template_commands = g_slist_remove (template_commands, template_command);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   172
					g_free (template_command -> name);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   173
					g_free (template_command -> template);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   174
					g_free (template_command);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   175
				}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   176
				return;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   177
			}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   178
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   179
			if (!template_command) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   180
				template_command = g_new (tcmd_t, 1);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   181
				template_command -> name = g_strndup (arg, len);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
			} else
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
				g_free (template_command -> template);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   184
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   185
			template_command -> template = g_strdup (astart);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   186
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   187
			{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   188
				int      maxarg  = -1;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   189
				gboolean stararg = FALSE;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   190
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   191
				for (astart = strchr (astart, '$'); astart; astart = strchr (astart, '$')) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   192
					++astart;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   194
					if (!*astart)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   195
						break;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   196
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   197
					if (strchr ("0123456789", *astart)) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   198
						int anum = atoi (astart);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   199
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   200
						if (maxarg < anum)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   201
							maxarg = anum;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   202
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   203
					} else if (*astart == '*')
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   204
						stararg = TRUE;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   205
				}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   206
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   207
				template_command -> maxarg  = maxarg;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   208
				template_command -> stararg = stararg;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   209
			}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   210
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   211
			template_commands = g_slist_append (template_commands, template_command);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   212
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   213
			cmd_add (template_command -> name, "", 0, 0, (void (*) (char *arg)) tcmd_callback, template_command);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   214
		}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   215
	}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   216
}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   217
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   218
5
c403a516dc04 New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   219
static void tcmd_init (void)
0
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   220
{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   221
	cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   222
}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   223
5
c403a516dc04 New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   224
static void tcmd_uninit (void)
0
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   225
{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   226
	GSList *tel;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   227
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   228
	cmd_del ("templatecmd");
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   229
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   230
	for (tel = template_commands; tel; tel = tel -> next) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   231
		tcmd_t *template_command = (tcmd_t *) tel -> data;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   232
		cmd_del (template_command -> name);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   233
		g_free (template_command -> name);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   234
		g_free (template_command -> template);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   235
		g_free (template_command);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   236
	}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   237
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   238
	g_slist_free (template_commands);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   239
}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   240
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   241
/* vim: se ts=4 sw=4: */