Clarified the dscp-tos documentation a bit.
[doldaconnect.git] / clients / tty / dcsh.c
CommitLineData
a24f31ec 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
302a2600 3 * Copyright (C) 2007 Fredrik Tolf <fredrik@dolda2000.com>
a24f31ec 4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <unistd.h>
23#include <wchar.h>
24#include <sys/poll.h>
c8817ca2 25#include <string.h>
15ee5bfb 26#include <errno.h>
c8817ca2 27#include <locale.h>
00b8bb0b 28#include <stdint.h>
a24f31ec 29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include <doldaconnect/uilib.h>
34#include <doldaconnect/uimisc.h>
35#include <doldaconnect/utils.h>
36
fa51e153 37int interactive = 0;
a24f31ec 38int verbose = 0;
39int dcfd;
40
c8817ca2
FT
41struct cmd {
42 wchar_t *name;
43 int (*handler)(int argc, wchar_t **argv);
44};
45
fa51e153
FT
46void wcslimit(wchar_t *wcs, int limit) {
47 int i;
48
49 if(wcslen(wcs) > limit) {
50 for(i = limit - 3; i < limit; i++)
51 wcs[i] = L'.';
52 wcs[i] = L'\0';
53 }
54}
55
b6e4d01d
FT
56void wcslimitr(wchar_t *wcs, int limit) {
57 int i;
58
59 if((i = wcslen(wcs)) > limit) {
60 memmove(wcs + 3, wcs + (i - limit + 3), sizeof(wchar_t) * (limit - 2));
61 for(i = 0; i < 3; i++)
62 wcs[i] = L'.';
63 }
64}
65
c8817ca2
FT
66int cmd_lsnodes(int argc, wchar_t **argv)
67{
c8817ca2
FT
68 struct dc_response *resp;
69 struct dc_intresp *ires;
70
71 resp = dc_gettaggedrespsync(dc_queuecmd(NULL, NULL, L"lsnodes", NULL));
72 if(resp->code == 200) {
fa51e153 73 if(interactive) {
b6e4d01d 74 printf("ID NET USERS S NAME\n");
fa51e153
FT
75 printf("----- ---- ------ - ----------------------------------------------------------\n");
76 }
c8817ca2 77 while((ires = dc_interpret(resp)) != NULL) {
fa51e153
FT
78 if(interactive)
79 wcslimit(ires->argv[2].val.str, 58);
b6e4d01d
FT
80 printf("%-5i %-4ls %-6i %c %ls\n", ires->argv[0].val.num, ires->argv[1].val.str, ires->argv[3].val.num, "SHED"[ires->argv[4].val.num], ires->argv[2].val.str);
81 dc_freeires(ires);
82 }
83 } else if(resp->code == 201) {
84 } else {
85 fprintf(stderr, "dcsh: %ls\n", resp->rlines[0].argv[0]);
86 return(1);
87 }
88 return(0);
89}
90
91int cmd_lsdl(int argc, wchar_t **argv)
92{
93 struct dc_response *resp;
94 struct dc_intresp *ires;
00b8bb0b 95 wchar_t *file, *p;
b6e4d01d
FT
96
97 resp = dc_gettaggedrespsync(dc_queuecmd(NULL, NULL, L"lstrans", NULL));
98 if(resp->code == 200) {
99 if(interactive) {
00b8bb0b
FT
100 printf("ID S USER PROGRESS FILE\n");
101 printf("------- - ---------- ------------- -------------------------------------------\n");
b6e4d01d
FT
102 }
103 while((ires = dc_interpret(resp)) != NULL) {
104 if(ires->argv[1].val.num == DC_TRNSD_DOWN) {
00b8bb0b
FT
105 file = ires->argv[5].val.str;
106 if((p = wcsrchr(file, L'/')) != NULL)
107 file = p + 1;
b6e4d01d 108 if(interactive) {
00b8bb0b
FT
109 wcslimit(ires->argv[4].val.str, 10);
110 wcslimit(file, 43);
b6e4d01d 111 }
00b8bb0b 112 printf("%-7i %c %-10ls %'-13ji %ls\n", ires->argv[0].val.num, "SHED"[ires->argv[2].val.num], ires->argv[4].val.str, (intmax_t)ires->argv[7].val.lnum, file);
b6e4d01d 113 }
c8817ca2
FT
114 dc_freeires(ires);
115 }
116 } else if(resp->code == 201) {
117 } else {
118 fprintf(stderr, "dcsh: %ls\n", resp->rlines[0].argv[0]);
b6e4d01d 119 return(1);
c8817ca2
FT
120 }
121 return(0);
122}
123
124struct cmd commands[] = {
125 {L"hubs", cmd_lsnodes},
b6e4d01d 126 {L"lsdl", cmd_lsdl},
c8817ca2
FT
127 {NULL, NULL}
128};
129
130int run1(int argc, wchar_t **argv)
131{
132 struct cmd *c;
133
134 for(c = commands; c->handler != NULL; c++) {
135 if(!wcscmp(argv[0], c->name))
136 return(c->handler(argc, argv));
137 }
138 fprintf(stderr, "dcsh: no such command\n");
139 return(1);
140}
141
142int runchar(int argc, char **argv) {
143 int i, rv;
144 wchar_t **wargv, *buf;
145 size_t wargvsize, wargvdata;
146
147 wargv = NULL;
148 wargvsize = wargvdata = 0;
149 for(i = 0; i < argc; i++) {
150 if((buf = icmbstowcs(argv[i], NULL)) == NULL) {
151 fprintf(stderr, "dcsh: could not convert %s to wide char: %s\n", argv[i], strerror(errno));
152 for(i = 0; i < wargvdata; i++)
153 free(wargv[i]);
154 if(wargv != NULL)
155 free(wargv);
156 return(1);
157 }
158 addtobuf(wargv, buf);
159 }
160 addtobuf(wargv, NULL);
161 rv = run1(wargvdata - 1, wargv);
162 dc_freewcsarr(wargv);
163 return(rv);
164}
165
166int shell(void)
167{
168 int ret, argc;
169 wchar_t *buf, **argv;
170 char *p, cmdbuf[128];
171 int cmddata;
172 struct pollfd pfd[2];
173
fa51e153
FT
174 if(interactive) {
175 fprintf(stderr, "dcsh> ");
176 fflush(stderr);
177 }
c8817ca2
FT
178 cmddata = 0;
179 while(1) {
180 pfd[0].fd = dcfd;
181 pfd[0].events = POLLIN;
182 if(dc_wantwrite())
183 pfd[0].events |= POLLOUT;
184 pfd[1].fd = 0;
185 pfd[1].events = POLLIN;
186 pfd[1].revents = 0;
187 if(poll(pfd, 2, -1) < 0) {
188 if(errno != EINTR) {
189 perror("dcsh: poll");
190 exit(1);
191 }
192 }
193 if(((pfd[0].revents & POLLIN) && dc_handleread()) || ((pfd[0].revents & POLLOUT) && dc_handlewrite()))
194 return(1);
195 if(pfd[1].revents) {
196 ret = read(0, cmdbuf + cmddata, sizeof(cmdbuf) - cmddata);
197 if(ret < 0) {
198 fprintf(stderr, "dcsh: stdin: %s\n", strerror(errno));
199 return(1);
200 } else if(ret == 0) {
fa51e153
FT
201 if(interactive)
202 fprintf(stderr, "\n");
c8817ca2
FT
203 return(0);
204 }
205 cmddata += ret;
206 while((p = memchr(cmdbuf, '\n', cmddata)) != NULL) {
207 *(p++) = 0;
208 if((buf = icmbstowcs(cmdbuf, NULL)) == NULL) {
209 fprintf(stderr, "dcsh: could not convert command to wide chars: %s\n", strerror(errno));
210 } else {
211 argv = dc_lexsexpr(buf);
212 free(buf);
213 for(argc = 0; argv[argc] != NULL; argc++);
214 if(argc > 0)
215 run1(argc, argv);
216 dc_freewcsarr(argv);
217 }
218 memmove(cmdbuf, p, cmddata -= (p - cmdbuf));
fa51e153
FT
219 if(interactive) {
220 fprintf(stderr, "dcsh> ");
221 fflush(stderr);
222 }
c8817ca2
FT
223 }
224 }
225 }
226}
227
a24f31ec 228int main(int argc, char **argv)
229{
c8817ca2 230 int c, rv;
a24f31ec 231 char *server;
232 char *username;
233 int authless, authia;
a24f31ec 234
c8817ca2 235 setlocale(LC_ALL, "");
a24f31ec 236 server = username = NULL;
237 authless = authia = 1;
238 while((c = getopt(argc, argv, "hvIAs:u:")) != -1) {
239 switch(c) {
240 case 's':
241 server = optarg;
242 break;
243 case 'u':
244 username = optarg;
245 break;
246 case 'I':
247 authia = 0;
248 break;
249 case 'A':
250 authless = 0;
251 break;
252 case 'v':
253 verbose++;
254 break;
255 default:
c8817ca2 256 fprintf(stderr, "usage: dcsh [-AIhv] [-s SERVER] [-u USERNAME] [COMMAND [ARGS...]]\n");
a24f31ec 257 exit((c == 'h')?0:1);
258 }
259 }
260 dc_init();
261
262 if(verbose)
263 fprintf(stderr, "connecting...\n");
264 if((dcfd = dc_connectsync2(server, 1)) < 0) {
265 perror("dcsh: could not connect to server");
266 exit(1);
267 }
268 if(verbose)
269 fprintf(stderr, "authenticating...\n");
270 if(dc_login(username, authless, authia?dc_convtty:dc_convnone, NULL) != DC_LOGIN_ERR_SUCCESS) {
271 fprintf(stderr, "dcsh: authentication unsuccessful\n");
272 exit(1);
273 }
274 if(verbose)
275 fprintf(stderr, "done\n");
fa51e153
FT
276 if(optind < argc) {
277 interactive = isatty(1);
c8817ca2 278 rv = runchar(argc - optind, argv + optind);
fa51e153
FT
279 } else {
280 interactive = isatty(0);
c8817ca2 281 rv = shell();
fa51e153 282 }
c8817ca2 283 return(rv);
a24f31ec 284}