CoCalc Public Fileswww / onhand / fton02 / rupcommand.cOpen with one click!
Author: William A. Stein
1
/* rupcommand.c
2
* Copyright (C) 1998 N.Fukase
3
*
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA
18
*/
19
20
#include <stdio.h>
21
#include <string.h>
22
#include <stdlib.h>
23
24
#include "rupcommand.h"
25
#include "rupserial.h"
26
/*
27
gtime �Ļ��֤μ���
28
stime �Ļ��֤�����
29
reset �ĥꥻ�åȽ�����Ԥ�
30
31
format �ĥե�å������ǥ�������ե����ޥåȤ���
32
chkdsk �ĥե�å������������������å�
33
gpower �Ľ����ť�������ͤμ���
34
*/
35
36
/*----- �Ƽ拾�����ѥޥ��� --------------------------------------------------*/
37
/* Ruputer Rshell ���ޥ�� */
38
#define RCOM_END 0
39
#define RCOM_COPY 1
40
#define RCOM_DEL 2
41
#define RCOM_DIR 3
42
#define RCOM_FORMAT 4
43
#define RCOM_REN 5
44
#define RCOM_RESET 6
45
#define RCOM_VER 7
46
#define RCOM_DOWNLOAD 8
47
#define RCOM_UPLOAD 9
48
#define RCOM_MKDIR 10
49
#define RCOM_RMDIR 11
50
#define RCOM_CHDIR 12
51
#define RCOM_GTIME 13
52
#define RCOM_STIME 14
53
#define RCOM_GPOWER 15
54
#define RCOM_CHKDSK 18
55
56
/*_____ debug print ________________________________________________________*/
57
#ifdef DEBUG
58
static inline void
59
debug_print(LENDAT *d)
60
{
61
int i;
62
for (i = 0; i < d->length ; i++) {
63
printf("%02x ", d->data[i] & 0xff);
64
}
65
printf("\n");
66
}
67
#else
68
static inline void
69
debug_print(LENDAT *d)
70
{
71
}
72
#endif
73
/*_____ END���ޥ�� ________________________________________________________*/
74
int
75
command_end(void)
76
{
77
LENDAT data;
78
79
if (ruputer_command(RCOM_END, 0, 4, NULL)) {
80
return -1;
81
}
82
if(chk_ack())
83
return -1;
84
if (ruputer_response(RCOM_END, 0, &data) < 0) {
85
fprintf(stderr, "bad data");
86
return -1;
87
}
88
put_ack();
89
debug_print(&data);
90
free(data.data);
91
return 0;
92
}
93
/*_____ DIR���ޥ�� ________________________________________________________*/
94
int
95
command_dir(RDENT *dent)
96
{
97
LENDAT buf;
98
RFINFO *rfi;
99
int seq = 1, nf = 0 , nfiles = 0, len, i, diskfree;
100
char path[64], *p;
101
102
dent->nfiles = 0;
103
len = strlen(dent->path) + 1;
104
memcpy(path, dent->path, len);
105
len += 4;
106
if (ruputer_command(RCOM_DIR, 1, len, path))
107
return -1;
108
if(chk_ack())
109
return -1;
110
if (ruputer_response(RCOM_DIR, 1, &buf) < 0) {
111
fprintf(stderr, "1bad data");
112
return -1;
113
}
114
put_ack();
115
debug_print(&buf);
116
rfi = NULL;
117
while(1) {
118
/* �ǡ��������å� */
119
if (buf.length < 10) {
120
fprintf(stderr, "command: dir Bad Length\n");
121
free(buf.data);
122
free(rfi);
123
return -1;
124
}
125
p = buf.data;
126
if((WORD(p) != 0)) {
127
fprintf(stderr, "command: dir Bad Return Status\n");
128
free(buf.data);
129
free(rfi);
130
return -1;
131
}
132
p += 2;
133
if (WORD(p) != seq) {
134
fprintf(stderr, "command: dir Bad Sequence Number\n");
135
free(buf.data);
136
free(rfi);
137
return -1;
138
}
139
p += 2;
140
diskfree = DWORD(p); p += 4;
141
nf = WORD(p); p += 2;
142
rfi = (RFINFO *)realloc(rfi, (nfiles + nf) * sizeof(RFINFO));
143
/* �ե��������򥳥ԡ� */
144
for (i = nfiles, nfiles += nf; i < nfiles; ++i) {
145
rfi[i].ctime = WORD(p); p += 2;
146
rfi[i].cdate = WORD(p); p += 2;
147
rfi[i].size = DWORD(p); p += 4;
148
rfi[i].attr = *p; p++;
149
memcpy(rfi[i].name, p, 13); p += 13;
150
}
151
free(buf.data);
152
if(nf < 5)
153
break;
154
/* ���Υǡ�����GET */
155
seq++;
156
if (ruputer_command(RCOM_DIR, 2, 4, NULL))
157
return -1;
158
if(chk_ack())
159
return -1;
160
if (ruputer_response(RCOM_DIR, 2, &buf) < 0) {
161
fprintf(stderr, "2bad data");
162
return -1;
163
}
164
put_ack();
165
debug_print(&buf);
166
}
167
dent->rfi = rfi;
168
dent->nfiles = nfiles;
169
return 0;
170
}
171
/*_____ VER���ޥ�� ________________________________________________________*/
172
int
173
command_ver(void)
174
{
175
LENDAT data;
176
int r;
177
178
if (ruputer_command(RCOM_VER, 0, 4, NULL)) {
179
return -1;
180
}
181
if(chk_ack()) {
182
fprintf(stderr, "bad response\n");
183
return -1;
184
}
185
if (ruputer_response(RCOM_VER, 0, &data) < 0) {
186
fprintf(stderr, "bad data");
187
return -1;
188
}
189
put_ack();
190
debug_print(&data);
191
r = WORD(data.data + 2);
192
free(data.data);
193
return r;
194
}
195
/*_____ UPLOAD���ޥ�� _____________________________________________________*/
196
int
197
command_upload(LENDAT *dat, RFINFO *rfi, char *path)
198
{
199
LENDAT data;
200
unsigned char buf[76];
201
int i;
202
203
strcpy(buf + 2, path);
204
strcat(buf + 2, rfi->name);
205
printf("%s\n", buf + 2);
206
i = strlen(buf + 2); i++;
207
WORD2(buf, i); i += 2;
208
DWORD2(buf + i, rfi->size); i += 4;
209
WORD2(buf + i, rfi->ctime); i += 2;
210
WORD2(buf + i, rfi->cdate); i += 2;
211
buf[i] = rfi->attr; i++;
212
if (ruputer_command(RCOM_UPLOAD, 0, i + 4, buf)) {
213
fprintf(stderr, "bad command\\n");
214
return -1;
215
}
216
if(chk_ack()) {
217
fprintf(stderr, "bad response\n");
218
return -1;
219
}
220
if (ruputer_response(RCOM_UPLOAD, 0, &data) < 0) {
221
fprintf(stderr, "bad data");
222
return -1;
223
}
224
put_ack();
225
debug_print(&data);
226
xmodem_up(dat);
227
free(data.data);
228
return 0;
229
}
230
/*_____ DOWNLOAD���ޥ�� ____________________________________________________*/
231
int
232
command_download(LENDAT *dat, RFINFO *rfi, char *path)
233
{
234
int i, ret;
235
LENDAT data;
236
char buf[64];
237
238
strcpy(buf, path);
239
strcat(buf, rfi->name);
240
i = strlen(buf) + 5;
241
dat->data = NULL;
242
if (ruputer_command(RCOM_DOWNLOAD, 0, i, buf))
243
return -1;
244
if(chk_ack())
245
return -1;
246
if (ruputer_response(RCOM_DOWNLOAD, 0, &data) < 0) {
247
fprintf(stderr, "bad data");
248
return -1;
249
}
250
put_ack();
251
debug_print(&data);
252
ret = WORD(data.data);
253
if (ret != 0)
254
return ret;
255
rfi->size = DWORD(data.data + 2);
256
rfi->ctime = WORD(data.data + 6);
257
rfi->cdate = WORD(data.data + 8);
258
rfi->attr = data.data[10];
259
free(data.data);
260
rfi->name[12] = '\0';
261
if ((dat->data = (char *)malloc(rfi->size)) == NULL) {
262
fprintf(stderr, "Cannot allocate memory\n");
263
return -1;
264
}
265
dat->length = rfi->size;
266
if (xmodem_down(dat)) {
267
dat->data = NULL;
268
fprintf(stderr, "Download failed\n");
269
return -1;
270
}
271
return 0;
272
}
273
/*_____ �ե�����̾����ĤΥ��ޥ�� __________________________________________*/
274
static inline int
275
command_onefile(int command, char *dname)
276
{
277
LENDAT data;
278
int i;
279
280
i = strlen(dname) + 5;
281
if (ruputer_command(command, 0, i, dname))
282
return -1;
283
if(chk_ack())
284
return -1;
285
if (ruputer_response(command, 0, &data) < 0) {
286
fprintf(stderr, "bad data");
287
return -1;
288
}
289
put_ack();
290
debug_print(&data);
291
i = WORD(data.data);
292
free(data.data);
293
return i;
294
}
295
/*_____ MKDIR���ޥ�� _______________________________________________________*/
296
int
297
command_mkdir(char *dname)
298
{
299
return command_onefile(RCOM_MKDIR, dname);
300
}
301
/*_____ RMDIR���ޥ�� _______________________________________________________*/
302
int
303
command_rmdir(char *dname)
304
{
305
return command_onefile(RCOM_RMDIR, dname);
306
}
307
/*_____ DEL���ޥ�� _______________________________________________________*/
308
int
309
command_del(char *dname)
310
{
311
return command_onefile(RCOM_DEL, dname);
312
}
313
/*_____ CHDIR���ޥ�� _______________________________________________________*/
314
int
315
command_chdir(char *dname)
316
{
317
return command_onefile(RCOM_CHDIR, dname);
318
}
319
320
/*_____ �ե�����̾2�ĤΥ��ޥ�� _____________________________________________*/
321
static inline int
322
command_twofiles(int command, char *new, char *old)
323
{
324
LENDAT data;
325
char buf[132];
326
int i, j;
327
328
i = strlen(old) + 1;
329
WORD2(buf, i);
330
strcpy(buf + 4, old);
331
i += 4;
332
j = strlen(new) + 1;
333
WORD2(buf + 2, j);
334
strcpy(buf + i, new);
335
if (ruputer_command(command, 0, (i + j + 4), buf))
336
return -1;
337
if(chk_ack())
338
return -1;
339
if (ruputer_response(command, 0, &data) < 0) {
340
fprintf(stderr, "bad data");
341
return -1;
342
}
343
put_ack();
344
debug_print(&data);
345
i = WORD(data.data);
346
free(data.data);
347
return i;
348
}
349
/*_____ REN���ޥ�� _________________________________________________________*/
350
int
351
command_ren(char *new, char *old)
352
{
353
return command_twofiles(RCOM_REN, new, old);
354
}
355
/*_____ COPY���ޥ�� ________________________________________________________*/
356
int
357
command_copy(char *new, char *old)
358
{
359
return command_twofiles(RCOM_COPY, new, old);
360
}
361