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 int cmd_lsnodes(int argc, wchar_t **argv)
57 struct dc_response *resp;
58 struct dc_intresp *ires;
60 resp = dc_gettaggedrespsync(dc_queuecmd(NULL, NULL, L"lsnodes", NULL));
61 if(resp->code == 200) {
63 printf(" ID NET USERS S NAME\n");
64 printf("----- ---- ------ - ----------------------------------------------------------\n");
66 while((ires = dc_interpret(resp)) != NULL) {
68 wcslimit(ires->argv[2].val.str, 58);
69 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);
72 } else if(resp->code == 201) {
74 fprintf(stderr, "dcsh: %ls\n", resp->rlines[0].argv[0]);
79 struct cmd commands[] = {
80 {L"hubs", cmd_lsnodes},
84 int run1(int argc, wchar_t **argv)
88 for(c = commands; c->handler != NULL; c++) {
89 if(!wcscmp(argv[0], c->name))
90 return(c->handler(argc, argv));
92 fprintf(stderr, "dcsh: no such command\n");
96 int runchar(int argc, char **argv) {
98 wchar_t **wargv, *buf;
99 size_t wargvsize, wargvdata;
102 wargvsize = wargvdata = 0;
103 for(i = 0; i < argc; i++) {
104 if((buf = icmbstowcs(argv[i], NULL)) == NULL) {
105 fprintf(stderr, "dcsh: could not convert %s to wide char: %s\n", argv[i], strerror(errno));
106 for(i = 0; i < wargvdata; i++)
112 addtobuf(wargv, buf);
114 addtobuf(wargv, NULL);
115 rv = run1(wargvdata - 1, wargv);
116 dc_freewcsarr(wargv);
123 wchar_t *buf, **argv;
124 char *p, cmdbuf[128];
126 struct pollfd pfd[2];
129 fprintf(stderr, "dcsh> ");
135 pfd[0].events = POLLIN;
137 pfd[0].events |= POLLOUT;
139 pfd[1].events = POLLIN;
141 if(poll(pfd, 2, -1) < 0) {
143 perror("dcsh: poll");
147 if(((pfd[0].revents & POLLIN) && dc_handleread()) || ((pfd[0].revents & POLLOUT) && dc_handlewrite()))
150 ret = read(0, cmdbuf + cmddata, sizeof(cmdbuf) - cmddata);
152 fprintf(stderr, "dcsh: stdin: %s\n", strerror(errno));
154 } else if(ret == 0) {
156 fprintf(stderr, "\n");
160 while((p = memchr(cmdbuf, '\n', cmddata)) != NULL) {
162 if((buf = icmbstowcs(cmdbuf, NULL)) == NULL) {
163 fprintf(stderr, "dcsh: could not convert command to wide chars: %s\n", strerror(errno));
165 argv = dc_lexsexpr(buf);
167 for(argc = 0; argv[argc] != NULL; argc++);
172 memmove(cmdbuf, p, cmddata -= (p - cmdbuf));
174 fprintf(stderr, "dcsh> ");
182 int main(int argc, char **argv)
187 int authless, authia;
189 setlocale(LC_ALL, "");
190 server = username = NULL;
191 authless = authia = 1;
192 while((c = getopt(argc, argv, "hvIAs:u:")) != -1) {
210 fprintf(stderr, "usage: dcsh [-AIhv] [-s SERVER] [-u USERNAME] [COMMAND [ARGS...]]\n");
211 exit((c == 'h')?0:1);
217 fprintf(stderr, "connecting...\n");
218 if((dcfd = dc_connectsync2(server, 1)) < 0) {
219 perror("dcsh: could not connect to server");
223 fprintf(stderr, "authenticating...\n");
224 if(dc_login(username, authless, authia?dc_convtty:dc_convnone, NULL) != DC_LOGIN_ERR_SUCCESS) {
225 fprintf(stderr, "dcsh: authentication unsuccessful\n");
229 fprintf(stderr, "done\n");
231 interactive = isatty(1);
232 rv = runchar(argc - optind, argv + optind);
234 interactive = isatty(0);