Added an actual command to dcsh.
[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>
a24f31ec 28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include <doldaconnect/uilib.h>
33#include <doldaconnect/uimisc.h>
34#include <doldaconnect/utils.h>
35
36int verbose = 0;
37int dcfd;
38
c8817ca2
FT
39struct cmd {
40 wchar_t *name;
41 int (*handler)(int argc, wchar_t **argv);
42};
43
44int cmd_lsnodes(int argc, wchar_t **argv)
45{
46 int i;
47 struct dc_response *resp;
48 struct dc_intresp *ires;
49
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';
58 }
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);
60 dc_freeires(ires);
61 }
62 } else if(resp->code == 201) {
63 } else {
64 fprintf(stderr, "dcsh: %ls\n", resp->rlines[0].argv[0]);
65 }
66 return(0);
67}
68
69struct cmd commands[] = {
70 {L"hubs", cmd_lsnodes},
71 {NULL, NULL}
72};
73
74int run1(int argc, wchar_t **argv)
75{
76 struct cmd *c;
77
78 for(c = commands; c->handler != NULL; c++) {
79 if(!wcscmp(argv[0], c->name))
80 return(c->handler(argc, argv));
81 }
82 fprintf(stderr, "dcsh: no such command\n");
83 return(1);
84}
85
86int runchar(int argc, char **argv) {
87 int i, rv;
88 wchar_t **wargv, *buf;
89 size_t wargvsize, wargvdata;
90
91 wargv = NULL;
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++)
97 free(wargv[i]);
98 if(wargv != NULL)
99 free(wargv);
100 return(1);
101 }
102 addtobuf(wargv, buf);
103 }
104 addtobuf(wargv, NULL);
105 rv = run1(wargvdata - 1, wargv);
106 dc_freewcsarr(wargv);
107 return(rv);
108}
109
110int shell(void)
111{
112 int ret, argc;
113 wchar_t *buf, **argv;
114 char *p, cmdbuf[128];
115 int cmddata;
116 struct pollfd pfd[2];
117
118 cmddata = 0;
119 while(1) {
120 pfd[0].fd = dcfd;
121 pfd[0].events = POLLIN;
122 if(dc_wantwrite())
123 pfd[0].events |= POLLOUT;
124 pfd[1].fd = 0;
125 pfd[1].events = POLLIN;
126 pfd[1].revents = 0;
127 if(poll(pfd, 2, -1) < 0) {
128 if(errno != EINTR) {
129 perror("dcsh: poll");
130 exit(1);
131 }
132 }
133 if(((pfd[0].revents & POLLIN) && dc_handleread()) || ((pfd[0].revents & POLLOUT) && dc_handlewrite()))
134 return(1);
135 if(pfd[1].revents) {
136 ret = read(0, cmdbuf + cmddata, sizeof(cmdbuf) - cmddata);
137 if(ret < 0) {
138 fprintf(stderr, "dcsh: stdin: %s\n", strerror(errno));
139 return(1);
140 } else if(ret == 0) {
141 return(0);
142 }
143 cmddata += ret;
144 while((p = memchr(cmdbuf, '\n', cmddata)) != NULL) {
145 *(p++) = 0;
146 if((buf = icmbstowcs(cmdbuf, NULL)) == NULL) {
147 fprintf(stderr, "dcsh: could not convert command to wide chars: %s\n", strerror(errno));
148 } else {
149 argv = dc_lexsexpr(buf);
150 free(buf);
151 for(argc = 0; argv[argc] != NULL; argc++);
152 if(argc > 0)
153 run1(argc, argv);
154 dc_freewcsarr(argv);
155 }
156 memmove(cmdbuf, p, cmddata -= (p - cmdbuf));
157 }
158 }
159 }
160}
161
a24f31ec 162int main(int argc, char **argv)
163{
c8817ca2 164 int c, rv;
a24f31ec 165 char *server;
166 char *username;
167 int authless, authia;
a24f31ec 168
c8817ca2 169 setlocale(LC_ALL, "");
a24f31ec 170 server = username = NULL;
171 authless = authia = 1;
172 while((c = getopt(argc, argv, "hvIAs:u:")) != -1) {
173 switch(c) {
174 case 's':
175 server = optarg;
176 break;
177 case 'u':
178 username = optarg;
179 break;
180 case 'I':
181 authia = 0;
182 break;
183 case 'A':
184 authless = 0;
185 break;
186 case 'v':
187 verbose++;
188 break;
189 default:
c8817ca2 190 fprintf(stderr, "usage: dcsh [-AIhv] [-s SERVER] [-u USERNAME] [COMMAND [ARGS...]]\n");
a24f31ec 191 exit((c == 'h')?0:1);
192 }
193 }
194 dc_init();
195
196 if(verbose)
197 fprintf(stderr, "connecting...\n");
198 if((dcfd = dc_connectsync2(server, 1)) < 0) {
199 perror("dcsh: could not connect to server");
200 exit(1);
201 }
202 if(verbose)
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");
206 exit(1);
207 }
208 if(verbose)
209 fprintf(stderr, "done\n");
c8817ca2
FT
210 if(optind < argc)
211 rv = runchar(argc - optind, argv + optind);
212 else
213 rv = shell();
214 return(rv);
a24f31ec 215}