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>
41 int (*handler)(int argc, wchar_t **argv);
44 int cmd_lsnodes(int argc, wchar_t **argv)
47 struct dc_response *resp;
48 struct dc_intresp *ires;
50 resp = dc_gettaggedrespsync(dc_queuecmd(NULL, NULL, L"lsnodes", NULL));
51 if(resp->code == 200) {
52 printf(" ID NET USERS S NAME\n");
53 while((ires = dc_interpret(resp)) != NULL) {
54 if(wcslen(ires->argv[2].val.str) > 58) {
55 for(i = 55; i < 58; i++)
56 ires->argv[2].val.str[i] = L'.';
57 ires->argv[2].val.str[i] = L'\0';
59 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);
62 } else if(resp->code == 201) {
64 fprintf(stderr, "dcsh: %ls\n", resp->rlines[0].argv[0]);
69 struct cmd commands[] = {
70 {L"hubs", cmd_lsnodes},
74 int run1(int argc, wchar_t **argv)
78 for(c = commands; c->handler != NULL; c++) {
79 if(!wcscmp(argv[0], c->name))
80 return(c->handler(argc, argv));
82 fprintf(stderr, "dcsh: no such command\n");
86 int runchar(int argc, char **argv) {
88 wchar_t **wargv, *buf;
89 size_t wargvsize, wargvdata;
92 wargvsize = wargvdata = 0;
93 for(i = 0; i < argc; i++) {
94 if((buf = icmbstowcs(argv[i], NULL)) == NULL) {
95 fprintf(stderr, "dcsh: could not convert %s to wide char: %s\n", argv[i], strerror(errno));
96 for(i = 0; i < wargvdata; i++)
102 addtobuf(wargv, buf);
104 addtobuf(wargv, NULL);
105 rv = run1(wargvdata - 1, wargv);
106 dc_freewcsarr(wargv);
113 wchar_t *buf, **argv;
114 char *p, cmdbuf[128];
116 struct pollfd pfd[2];
121 pfd[0].events = POLLIN;
123 pfd[0].events |= POLLOUT;
125 pfd[1].events = POLLIN;
127 if(poll(pfd, 2, -1) < 0) {
129 perror("dcsh: poll");
133 if(((pfd[0].revents & POLLIN) && dc_handleread()) || ((pfd[0].revents & POLLOUT) && dc_handlewrite()))
136 ret = read(0, cmdbuf + cmddata, sizeof(cmdbuf) - cmddata);
138 fprintf(stderr, "dcsh: stdin: %s\n", strerror(errno));
140 } else if(ret == 0) {
144 while((p = memchr(cmdbuf, '\n', cmddata)) != NULL) {
146 if((buf = icmbstowcs(cmdbuf, NULL)) == NULL) {
147 fprintf(stderr, "dcsh: could not convert command to wide chars: %s\n", strerror(errno));
149 argv = dc_lexsexpr(buf);
151 for(argc = 0; argv[argc] != NULL; argc++);
156 memmove(cmdbuf, p, cmddata -= (p - cmdbuf));
162 int main(int argc, char **argv)
167 int authless, authia;
169 setlocale(LC_ALL, "");
170 server = username = NULL;
171 authless = authia = 1;
172 while((c = getopt(argc, argv, "hvIAs:u:")) != -1) {
190 fprintf(stderr, "usage: dcsh [-AIhv] [-s SERVER] [-u USERNAME] [COMMAND [ARGS...]]\n");
191 exit((c == 'h')?0:1);
197 fprintf(stderr, "connecting...\n");
198 if((dcfd = dc_connectsync2(server, 1)) < 0) {
199 perror("dcsh: could not connect to server");
203 fprintf(stderr, "authenticating...\n");
204 if(dc_login(username, authless, authia?dc_convtty:dc_convnone, NULL) != DC_LOGIN_ERR_SUCCESS) {
205 fprintf(stderr, "dcsh: authentication unsuccessful\n");
209 fprintf(stderr, "done\n");
211 rv = runchar(argc - optind, argv + optind);