summaryrefslogtreecommitdiff
path: root/plugins/Variables/src/parse_alias.cpp
blob: 9a29840a18caced808bb3f36df7d31037510d6cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
    Variables Plugin for Miranda-IM (www.miranda-im.org)
    Copyright 2003-2006 P. Boon

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "stdafx.h"

struct ALIASREGISTER
{
	TCHAR *szAlias;
	unsigned int argc;
	TCHAR **argv;
	TCHAR *szTranslation;
};

static LIST<ALIASREGISTER> arAliases(5);
static mir_cs csAliasRegister;

static ALIASREGISTER* searchAliasRegister(TCHAR *szAlias)
{
	if (szAlias == NULL || *szAlias == 0)
		return NULL;

	mir_cslock lck(csAliasRegister);
	for (int i = 0; i < arAliases.getCount(); i++)
	if (!mir_tstrcmp(arAliases[i]->szAlias, szAlias))
		return arAliases[i];

	return NULL;
}

static TCHAR *replaceArguments(TCHAR *res, TCHAR *tArg, TCHAR *rArg)
{
	if (mir_tstrlen(tArg) == 0)
		return res;

	unsigned int cur = 0, ecur = 0;
	while (*(res + cur) != 0) {
		if ((*(res + cur) == '(') || (*(res + cur) == ',')) {
			ecur = ++cur;
			while ((*(res + ecur) != ')') && (*(res + ecur) != ','))
				ecur++;

			if (((signed int)mir_tstrlen(tArg) == (ecur - cur)) && (!_tcsncmp(tArg, res + cur, mir_tstrlen(tArg)))) {
				if (mir_tstrlen(rArg) > mir_tstrlen(tArg)) {
					res = (TCHAR*)mir_realloc(res, (mir_tstrlen(res) + (mir_tstrlen(rArg) - mir_tstrlen(tArg)) + 1)*sizeof(TCHAR));
					if (res == NULL)
						return NULL;
				}
				memmove(res + ecur + (mir_tstrlen(rArg) - mir_tstrlen(tArg)), res + ecur, (mir_tstrlen(res + ecur) + 1)*sizeof(TCHAR));
				_tcsncpy(res + cur, rArg, mir_tstrlen(rArg));
			}
		}
		cur++;
	}

	return res;
}

static TCHAR *parseTranslateAlias(ARGUMENTSINFO *ai)
{
	ALIASREGISTER *areg = searchAliasRegister(ai->targv[0]);
	if (areg == NULL || areg->argc != ai->argc - 1)
		return NULL;

	TCHAR *res = mir_tstrdup(areg->szTranslation);
	for (unsigned i = 0; i < areg->argc; i++) {
		res = replaceArguments(res, areg->argv[i], ai->targv[i + 1]);
		if (res == NULL)
			return NULL;
	}

	return res;
}

static int addToAliasRegister(TCHAR *szAlias, unsigned int argc, TCHAR** argv, TCHAR *szTranslation)
{
	if (szAlias == NULL || szTranslation == NULL || mir_tstrlen(szAlias) == 0)
		return -1;

	mir_cslock lck(csAliasRegister);
	for (int i = 0; i < arAliases.getCount(); i++) {
		ALIASREGISTER *p = arAliases[i];
		if (mir_tstrcmp(p->szAlias, szAlias))
			continue;

		mir_free(p->szTranslation);
		p->szTranslation = mir_tstrdup(szTranslation);
		for (unsigned j = 0; j < p->argc; j++)
			mir_free(p->argv[j]);

		p->argc = argc;
		p->argv = (TCHAR**)mir_realloc(p->argv, argc * sizeof(TCHAR*));
		if (p->argv == NULL)
			return -1;

		for (unsigned j = 0; j < argc; j++) {
			if (argv[j] != NULL)
				p->argv[j] = mir_tstrdup(argv[j]);
			else
				p->argv[j] = NULL;
		}
		return 0;
	}
	TCHAR **pargv = (TCHAR**)mir_alloc(argc * sizeof(TCHAR*));
	if (pargv == NULL)
		return -1;

	ALIASREGISTER *p = new ALIASREGISTER;
	p->szAlias = mir_tstrdup(szAlias);
	p->szTranslation = mir_tstrdup(szTranslation);
	p->argc = argc;
	p->argv = pargv;

	for (unsigned j = 0; j < p->argc; j++) {
		if (argv[j] != NULL)
			p->argv[j] = mir_tstrdup(argv[j]);
		else
			p->argv[j] = NULL;
	}
	arAliases.insert(p);
	return 0;
}

static TCHAR *parseAddAlias(ARGUMENTSINFO *ai)
{
	if (ai->argc != 3)
		return NULL;

	char *szHelp, *szArgsA;
	TCHAR *cur = ai->targv[1];
	while (isValidTokenChar(*cur))
		cur++;

	ptrT alias(mir_tstrndup(ai->targv[1], cur - ai->targv[1]));

	TArgList argv;
	getArguments(cur, argv);

	deRegisterToken(alias);
	addToAliasRegister(alias, argv.getCount(), argv.getArray(), ai->targv[2]);
	TCHAR *szArgs = NULL;
	for (int i = 0; i < argv.getCount(); i++) {
		if (i == 0)
			szArgs = (TCHAR*)mir_calloc((mir_tstrlen(argv[i]) + 2)*sizeof(TCHAR));
		else
			szArgs = (TCHAR*)mir_realloc(szArgs, (mir_tstrlen(szArgs) + mir_tstrlen(argv[i]) + 2)*sizeof(TCHAR));

		mir_tstrcat(szArgs, argv[i]);
		if (i != argv.getCount() - 1)
			mir_tstrcat(szArgs, L",");
	}
	int res;
	if (szArgs != NULL && argv.getCount() > 0) {
		szArgsA = mir_t2a(szArgs);

		size_t size = 32 + mir_strlen(szArgsA);
		szHelp = (char *)mir_alloc(size);
		memset(szHelp, '\0', 32 + mir_strlen(szArgsA));
		mir_snprintf(szHelp, size, LPGEN("Alias") "\t(%s)\t" LPGEN("user defined"), szArgsA);
		res = registerIntToken(alias, parseTranslateAlias, TRF_FUNCTION | TRF_UNPARSEDARGS, szHelp);
		mir_free(szArgsA);
	}
	else {
		szHelp = (char*)mir_alloc(32);
		memset(szHelp, '\0', 32);
		mir_snprintf(szHelp, 32, LPGEN("Alias") "\t\t" LPGEN("user defined"));
		res = registerIntToken(alias, parseTranslateAlias, TRF_FIELD | TRF_UNPARSEDARGS, szHelp);
	}
	mir_free(szArgs);
	mir_free(szHelp);
	argv.destroy();
	return (res == 0) ? mir_tstrdup(L"") : NULL;
}

void registerAliasTokens()
{
	registerIntToken(ADDALIAS, parseAddAlias, TRF_FUNCTION | TRF_UNPARSEDARGS, LPGEN("Variables") "\t(x,y)\t" LPGEN("stores y as alias named x"));//TRF_UNPARSEDARGS);
}

void unregisterAliasTokens()
{
	for (int i = 0; i < arAliases.getCount(); i++) {
		ALIASREGISTER *p = arAliases[i];
		for (unsigned j = 0; j < p->argc; j++)
			mir_free(p->argv[j]);
		mir_free(p->argv);
		mir_free(p->szAlias);
		mir_free(p->szTranslation);
		delete p;
	}
}