2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2007 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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
32 #include <doldaconnect/uilib.h>
33 #include <doldaconnect/uimisc.h>
34 #include <doldaconnect/utils.h>
42 int (*handler)(int argc, wchar_t **argv);
45 void wcslimit(wchar_t *wcs, int limit) {
48 if(wcslen(wcs) > limit) {
49 for(i = limit - 3; i < limit; i++)
55 void wcslimitr(wchar_t *wcs, int limit) {
58 if((i = wcslen(wcs)) > limit) {
59 memmove(wcs + 3, wcs + (i - limit + 3), sizeof(wchar_t) * (limit - 2));
60 for(i = 0; i < 3; i++)
65 int cmd_lsnodes(int argc, wchar_t **argv)
67 struct dc_response *resp;
68 struct dc_intresp *ires;
70 resp = dc_gettaggedrespsync(dc_queuecmd(NULL, NULL, L"lsnodes", NULL));
71 if(resp->code == 200) {
73 printf("ID NET USERS S NAME\n");
74 printf("----- ---- ------ - ----------------------------------------------------------\n");
76 while((ires = dc_interpret(resp)) != NULL) {
78 wcslimit(ires->argv[2].val.str, 58);
79 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);
82 } else if(resp->code == 201) {
84 fprintf(stderr, "dcsh: %ls\n", resp->rlines[0].argv[0]);
90 int cmd_lsdl(int argc, wchar_t **argv)
92 struct dc_response *resp;
93 struct dc_intresp *ires;
95 resp = dc_gettaggedrespsync(dc_queuecmd(NULL, NULL, L"lstrans", NULL));
96 if(resp->code == 200) {
98 printf("ID S USER FILE\n");
99 printf("------- - --------------- ----------------------------------------------------\n");
101 while((ires = dc_interpret(resp)) != NULL) {
102 if(ires->argv[1].val.num == DC_TRNSD_DOWN) {
104 wcslimit(ires->argv[4].val.str, 15);
105 wcslimitr(ires->argv[5].val.str, 52);
107 printf("%-7i %c %-15ls %ls\n", ires->argv[0].val.num, "SHED"[ires->argv[2].val.num], ires->argv[4].val.str, ires->argv[5].val.str);
111 } else if(resp->code == 201) {
113 fprintf(stderr, "dcsh: %ls\n", resp->rlines[0].argv[0]);
119 struct cmd commands[] = {
120 {L"hubs", cmd_lsnodes},
125 int run1(int argc, wchar_t **argv)
129 for(c = commands; c->handler != NULL; c++) {
130 if(!wcscmp(argv[0], c->name))
131 return(c->handler(argc, argv));
133 fprintf(stderr, "dcsh: no such command\n");
137 int runchar(int argc, char **argv) {
139 wchar_t **wargv, *buf;
140 size_t wargvsize, wargvdata;
143 wargvsize = wargvdata = 0;
144 for(i = 0; i < argc; i++) {
145 if((buf = icmbstowcs(argv[i], NULL)) == NULL) {
146 fprintf(stderr, "dcsh: could not convert %s to wide char: %s\n", argv[i], strerror(errno));
147 for(i = 0; i < wargvdata; i++)
153 addtobuf(wargv, buf);
155 addtobuf(wargv, NULL);
156 rv = run1(wargvdata - 1, wargv);
157 dc_freewcsarr(wargv);
164 wchar_t *buf, **argv;
165 char *p, cmdbuf[128];
167 struct pollfd pfd[2];
170 fprintf(stderr, "dcsh> ");
176 pfd[0].events = POLLIN;
178 pfd[0].events |= POLLOUT;
180 pfd[1].events = POLLIN;
182 if(poll(pfd, 2, -1) < 0) {
184 perror("dcsh: poll");
188 if(((pfd[0].revents & POLLIN) && dc_handleread()) || ((pfd[0].revents & POLLOUT) && dc_handlewrite()))
191 ret = read(0, cmdbuf + cmddata, sizeof(cmdbuf) - cmddata);
193 fprintf(stderr, "dcsh: stdin: %s\n", strerror(errno));
195 } else if(ret == 0) {
197 fprintf(stderr, "\n");
201 while((p = memchr(cmdbuf, '\n', cmddata)) != NULL) {
203 if((buf = icmbstowcs(cmdbuf, NULL)) == NULL) {
204 fprintf(stderr, "dcsh: could not convert command to wide chars: %s\n", strerror(errno));
206 argv = dc_lexsexpr(buf);
208 for(argc = 0; argv[argc] != NULL; argc++);
213 memmove(cmdbuf, p, cmddata -= (p - cmdbuf));
215 fprintf(stderr, "dcsh> ");
223 int main(int argc, char **argv)
228 int authless, authia;
230 setlocale(LC_ALL, "");
231 server = username = NULL;
232 authless = authia = 1;
233 while((c = getopt(argc, argv, "hvIAs:u:")) != -1) {
251 fprintf(stderr, "usage: dcsh [-AIhv] [-s SERVER] [-u USERNAME] [COMMAND [ARGS...]]\n");
252 exit((c == 'h')?0:1);
258 fprintf(stderr, "connecting...\n");
259 if((dcfd = dc_connectsync2(server, 1)) < 0) {
260 perror("dcsh: could not connect to server");
264 fprintf(stderr, "authenticating...\n");
265 if(dc_login(username, authless, authia?dc_convtty:dc_convnone, NULL) != DC_LOGIN_ERR_SUCCESS) {
266 fprintf(stderr, "dcsh: authentication unsuccessful\n");
270 fprintf(stderr, "done\n");
272 interactive = isatty(1);
273 rv = runchar(argc - optind, argv + optind);
275 interactive = isatty(0);