-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalias.c
145 lines (129 loc) · 4.12 KB
/
alias.c
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
/*********************************************************************/
/* file: alias.c - functions related the the alias command */
/* TINTIN III */
/* (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t */
/* coded by peter unold 1992 */
/*********************************************************************/
#include "tintin.h"
#include "protos/glob.h"
#include "protos/globals.h"
#include "protos/hash.h"
#include "protos/print.h"
#include "protos/parse.h"
#include "protos/utils.h"
extern struct session *if_command(const char *arg, struct session *ses);
void show_hashlist(struct session *ses, struct hashtable *h, const char *pat, const char *msg_all, const char *msg_none)
{
struct pairlist *pl;
if (!*pat)
{
tintin_printf(ses, msg_all);
pl=hash2list(h, 0);
}
else
pl=hash2list(h, pat);
if (pl->size)
{
struct pair *end = &pl->pairs[0] + pl->size;
for (struct pair *n = &pl->pairs[0]; n<end; n++)
tintin_printf(ses, "~7~{%s~7~}={%s~7~}", n->left, n->right);
}
else if (*pat)
tintin_printf(ses, msg_none, pat);
free(pl);
}
void delete_hashlist(struct session *ses, struct hashtable *h, const char *pat, const char *msg_ok, const char *msg_none)
{
if (is_literal(pat))
{
if (delete_hash(h, pat))
{
if (msg_ok)
tintin_printf(ses, msg_ok, pat);
}
else
{
if (msg_none)
tintin_printf(ses, msg_none, pat);
}
return;
}
struct pairlist *pl = hash2list(h, pat);
struct pair *end = &pl->pairs[0] + pl->size;
for (struct pair *ln = &pl->pairs[0]; ln<end; ln++)
{
if (msg_ok)
tintin_printf(ses, msg_ok, ln->left);
delete_hash(h, ln->left);
}
if (msg_none && !pl->size)
tintin_printf(ses, msg_none, pat);
free(pl);
}
/**********************/
/* the #alias command */
/**********************/
void alias_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE], *ch;
arg = get_arg_in_braces(arg, left, 0);
arg = get_arg_in_braces(arg, right, 1);
if (*left && *right)
{
if ((ch=strchr(left, ' ')))
{
tintin_eprintf(ses, "#ERROR: aliases cannot contain spaces! Bad alias: {%s}", left);
if (ch==left)
return;
*ch=0;
tintin_printf(ses, "#Converted offending alias to {%s}.", left);
}
set_hash(ses->aliases, left, right);
if (ses->mesvar[MSG_ALIAS])
tintin_printf(ses, "#Ok. {%s} aliases {%s}.", left, right);
alnum++;
return;
}
show_hashlist(ses, ses->aliases, left,
"#Defined aliases:",
"#No match(es) found for {%s}.");
}
/************************/
/* the #unalias command */
/************************/
void unalias_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE];
arg = get_arg_in_braces(arg, left, 1);
delete_hashlist(ses, ses->aliases, left,
ses->mesvar[MSG_ALIAS]? "#Ok. {%s} is no longer an alias." : 0,
ses->mesvar[MSG_ALIAS]? "#No match(es) found for {%s}" : 0);
}
/******************************/
/* the #ifaliasexists command */
/******************************/
struct session *ifaliasexists_command(const char *line, struct session *ses)
{
char left[BUFFER_SIZE], cmd[BUFFER_SIZE];
line = get_arg(line, left, 0, ses);
line = get_arg_in_braces(line, cmd, 1);
if (!*cmd)
{
tintin_eprintf(ses, "#Syntax: #ifaliasexists <alias> <command> [#else <command>]");
return ses;
}
if (get_hash(ses->aliases, left))
return parse_input(cmd, true, ses);
line = get_arg_in_braces(line, left, 0);
if (*left == tintin_char)
{
if (is_abrev(left + 1, "else"))
{
line = get_arg_in_braces(line, cmd, 1);
ses=parse_input(cmd, true, ses);
}
if (is_abrev(left + 1, "elif"))
ses=if_command(line, ses);
}
return ses;
}