templatecmd.c
author Myhailo Danylenko <isbear@ukrpost.net>
Sat, 05 Dec 2009 22:32:55 +0200
changeset 0 1c4eb7aed6c2
child 1 9eb794ebef85
permissions -rw-r--r--
Initial commit
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 <gmodule.h>
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
#include <stdlib.h>
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
#include <string.h>
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
#include "compl.h"
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
#include "commands.h"
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
#include "utils.h"
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
#include "logprint.h"
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
typedef struct {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
	gchar    *name;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
	gchar    *template;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
	int       maxarg;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
	gboolean  stararg;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
} tcmd_t;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
static GSList *template_commands = NULL;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    37
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
static void tcmd_callback (char *arg, gpointer udata)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
	tcmd_t   *tcmd     = (tcmd_t *) udata;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
	char    **args     = NULL;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
	GString  *command  = g_string_new (NULL);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
	char     *template = tcmd -> template;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
	char     *p;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
	if (tcmd -> maxarg > 0)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
		args = split_arg (arg, tcmd -> maxarg, TRUE);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
	for (p = template; *p; ++p) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
		if (*p != '$' || !*(p + 1) || !strchr ("*$0123456789", *(p + 1))) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    51
			g_string_append_c (command, *p);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
			continue;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    53
		}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    54
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    55
		++p;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    56
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
		if (*p == '*')
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
			g_string_append (command, arg);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
		else if (*p == '$')
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
			g_string_append_c (command, '$');
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
		else {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
			int argnum = atoi (p);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
			if (argnum == 0)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
				g_string_append (command, tcmd -> name);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
			else if (args[argnum - 1])
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
				g_string_append (command, args[argnum - 1]);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
			while (*(p + 1) && strchr ("0123456789", *(p + 1)))
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
				++p;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
		}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
	}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
	{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
		char *cmd = g_string_free (command, FALSE);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
		process_command (cmd, 1);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
		g_free (cmd);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
	}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
	if (args)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    81
		free_arg_lst (args);
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
static void do_templatecmd (char *arg)
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 (!*arg) { // list tcmds
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
		GSList *tel;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
		for (tel = template_commands; tel; tel = tel -> next) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
			tcmd_t *tcmd = (tcmd_t *) tel -> data;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
			scr_LogPrint (LPRINT_NORMAL, "Templatecmd %s = %s", tcmd -> name, tcmd -> template);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
		}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    94
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
	} else {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    96
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    97
		char *nend = strchr (arg, '=');
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    99
		if (!nend) { // show tcmd
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   100
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   101
			GSList *tel;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   102
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   103
			for (tel = template_commands; tel; tel = tel -> next) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   104
				tcmd_t *tcmd = (tcmd_t *) tel -> data;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
				if (!strcmp (arg, tcmd -> name)) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
					scr_LogPrint (LPRINT_NORMAL, "Templatecmd %s = %s", tcmd -> name, tcmd -> template);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
					return;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   109
				}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   110
			}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   111
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   112
			scr_LogPrint (LPRINT_NORMAL, "No template with such name.");
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   113
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   114
		} else if (nend == arg) // error
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   116
			scr_LogPrint (LPRINT_NORMAL, "You must specify command name.");
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   117
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   118
		else { // new/modify tcmd
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   119
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   120
			GSList *tel;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   121
			tcmd_t *template_command = NULL;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
			char   *astart           = nend + 1;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
			int     len;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
			for (--nend; nend > arg && *nend == ' '; --nend);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   126
			len = nend + 1 - 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
			for (tel = template_commands; tel; tel = tel -> next) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   129
				tcmd_t *tcmd = (tcmd_t *) tel -> data;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   131
				if (!strncmp (arg, tcmd -> name, len)) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
					template_command = tcmd;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   133
					break;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   134
				}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   135
			}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   136
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   137
			for (;*astart && *astart == ' '; ++astart);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   138
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   139
			if (!*astart) { // delete tcmd
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   140
				if (template_command) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   141
					cmd_del (template_command -> name);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   142
					template_commands = g_slist_remove (template_commands, template_command);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   143
					g_free (template_command -> name);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   144
					g_free (template_command -> template);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   145
					g_free (template_command);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   146
				}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   147
				return;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   148
			}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   149
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
			if (!template_command) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
				template_command = g_new (tcmd_t, 1);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   152
				template_command -> name = g_strndup (arg, len);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   153
			} else
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   154
				g_free (template_command -> template);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   155
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   156
			template_command -> template = g_strdup (astart);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   157
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   158
			{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   159
				int      maxarg  = -1;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   160
				gboolean stararg = FALSE;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   161
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   162
				for (astart = strchr (astart, '$'); astart; astart = strchr (astart, '$')) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   163
					++astart;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   164
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   165
					if (!*astart)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
						break;
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 (strchr ("0123456789", *astart)) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   169
						int anum = atoi (astart);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   170
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   171
						if (maxarg < anum)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   172
							maxarg = anum;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   173
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   174
					} else if (*astart == '*')
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   175
						stararg = TRUE;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   176
				}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   177
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   178
				template_command -> maxarg  = maxarg;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   179
				template_command -> stararg = stararg;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   180
			}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   181
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
			template_commands = g_slist_append (template_commands, template_command);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   184
			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
   185
		}
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
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   189
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   190
const gchar *g_module_check_init (GModule *module)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   191
{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   192
	cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   194
	return NULL;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   195
}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   196
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   197
void g_module_unload (GModule *module)
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   198
{
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   199
	GSList *tel;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   200
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   201
	cmd_del ("templatecmd");
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   202
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   203
	for (tel = template_commands; tel; tel = tel -> next) {
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   204
		tcmd_t *template_command = (tcmd_t *) tel -> data;
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   205
		cmd_del (template_command -> name);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   206
		g_free (template_command -> name);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   207
		g_free (template_command -> template);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   208
		g_free (template_command);
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
	g_slist_free (template_commands);
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   212
}
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   213
1c4eb7aed6c2 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   214
/* vim: se ts=4 sw=4: */