forked from kilobyte/kbtin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.c
223 lines (193 loc) · 5.92 KB
/
path.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*********************************************************************/
/* file: path.c - stuff for the path feature */
/* TINTIN III */
/* (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t */
/* coded by peter unold 1992 */
/* recoded by Jeremy C. Jack 1994 */
/*********************************************************************/
#include "tintin.h"
#include "protos/action.h"
#include "protos/alias.h"
#include "protos/globals.h"
#include "protos/hash.h"
#include "protos/lists.h"
#include "protos/print.h"
#include "protos/parse.h"
#include "protos/path.h"
#include "protos/string.h"
#include "protos/utils.h"
#include "protos/vars.h"
#define PATHP(i) (&ses->path[(ses->path_begin+(i))%MAX_PATH_LENGTH])
static bool return_flag = true;
void mark_command(const char *arg, struct session *ses)
{
ses->path_length = 0;
if (ses->mesvar[MSG_PATH])
tintin_printf(ses, "#Beginning of path marked.");
}
void map_command(const char *arg, struct session *ses)
{
char cmd[BUFFER_SIZE];
get_arg_in_braces(arg, cmd, 1);
substitute_vars(cmd, cmd, ses);
check_insert_path(cmd, ses);
}
void savepath_command(const char *arg, struct session *ses)
{
char alias[BUFFER_SIZE], result[BUFFER_SIZE], *r=result;
get_arg_in_braces(arg, alias, 1);
if (!*arg)
return tintin_eprintf(ses, "#Syntax: savepath <alias>");
if (!ses->path_length)
{
tintin_eprintf(ses, "#No path to save!");
return;
}
r+=snprintf(r, BUFFER_SIZE, "%calias {%s} {", tintin_char, alias);
for (int i=0; i<ses->path_length; i++)
{
struct pair *ln = PATHP(i);
int dirlen = strlen(ln->left);
if (r-result + dirlen >= BUFFER_SIZE - 10)
{
tintin_eprintf(ses, "#Error - buffer too small to contain alias");
break;
}
else
r += sprintf(r, "%s%s", ln->left, (i==ses->path_length-1)?";":"");
}
*r++='}', *r=0;
parse_input(result, true, ses);
}
void path2var(char *var, struct session *ses)
{
char *r;
int dirlen, len = 0;
if (!ses->path_length)
{
*var=0;
return;
}
len = 0;
r=var;
for (int i=0; i<ses->path_length; i++)
{
struct pair *ln = PATHP(i);
dirlen = strlen(ln->left);
if (dirlen + len < BUFFER_SIZE - 10)
{
r+=sprintf(r, isatom(ln->left)? "%s" : "{%s}" , ln->left);
len += dirlen + 1;
if (i<ses->path_length-1)
*r++=' ';
}
else
{
tintin_eprintf(ses, "#Error - buffer too small to contain alias");
*r++=0;
break;
}
}
}
void path_command(const char *arg, struct session *ses)
{
char mypath[BUFFER_SIZE];
char *r=mypath+sprintf(mypath, "#Path: ");
for (int i=0; i<ses->path_length; i++)
{
struct pair *ln = PATHP(i);
int dirlen = strlen(ln->left);
if (r-mypath + dirlen > (COLS?COLS:BUFFER_SIZE)-10)
{
r[-1]=0;
tintin_printf(ses, "%s", mypath);
r=mypath+sprintf(mypath, "#Path: ");
}
r+=sprintf(r, "%s;", ln->left);
}
r[-1]=0;
tintin_printf(ses, "%s", mypath);
}
void return_command(const char *arg, struct session *ses)
{
int n;
char *err, how[BUFFER_SIZE];
get_arg_in_braces(arg, how, 1);
if (!ses->path_length)
return tintin_eprintf(ses, "#No place to return from!");
if (!*how)
n=1;
else if (!strcmp(how, "all") || !strcmp(how, "ALL"))
n=ses->path_length;
else
{
n=strtol(how, &err, 10);
if (*err || n<0)
return tintin_eprintf(ses, "#return [<num>|all], got {%s}", how);
if (!n) /* silently ignore "#return 0" */
return;
}
return_flag = false; /* temporarily turn off path tracking */
while (n--)
{
char command[BUFFER_SIZE];
if (!ses->path_length)
break;
struct pair *ln = PATHP(--ses->path_length);
strcpy(command, ln->right);
parse_input(command, false, ses);
}
return_flag = true; /* restore path tracking */
}
void unmap_command(const char *arg, struct session *ses)
{
if (!ses->path_length)
return tintin_eprintf(ses, "#No move to forget!");
ses->path_length--;
if (ses->mesvar[MSG_PATH])
tintin_eprintf(ses, "#Ok. Forgot that move.");
}
void check_insert_path(const char *command, struct session *ses)
{
if (!return_flag)
return;
char *ret=get_hash(ses->pathdirs, command);
if (!ret)
return;
if (ses->path_length != MAX_PATH_LENGTH)
ses->path_length++;
else
ses->path_begin++;
struct pair *p = PATHP(ses->path_length-1);
free((char*)p->left);
free((char*)p->right);
p->left =mystrdup(command);
p->right=mystrdup(ret);
}
void pathdir_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE], right[BUFFER_SIZE];
arg = get_arg_in_braces(arg, left, 0);
arg = get_arg_in_braces(arg, right, 1);
if (*left && *right)
{
set_hash(ses->pathdirs, left, right);
if (ses->mesvar[MSG_PATH])
tintin_printf(ses, "#Ok. {%s} is marked in #path. {%s} is its #return.",
left, right);
pdnum++;
return;
}
show_hashlist(ses, ses->pathdirs, left,
"#These PATHDIRS have been defined:",
"#That PATHDIR (%s) is not defined.");
}
void unpathdir_command(const char *arg, struct session *ses)
{
char left[BUFFER_SIZE];
arg = get_arg_in_braces(arg, left, 1);
substitute_vars(left, left, ses);
delete_hashlist(ses, ses->pathdirs, left,
ses->mesvar[MSG_PATH]? "#Ok. $%s is no longer recognized as a pathdir." : 0,
ses->mesvar[MSG_PATH]? "#THAT PATHDIR (%s) IS NOT DEFINED." : 0);
}