| 1 |
sysadm |
1.1 |
/*
|
| 2 |
|
|
* Copyright (C) 1998,1999,2000 Ross Combs (rocombs@cs.nmsu.edu)
|
| 3 |
|
|
* Copyright (C) 1999 Oleg Drokin (green@ccssu.ccssu.crimea.ua)
|
| 4 |
|
|
*
|
| 5 |
|
|
* This program is free software; you can redistribute it and/or
|
| 6 |
|
|
* modify it under the terms of the GNU General Public License
|
| 7 |
|
|
* as published by the Free Software Foundation; either version 2
|
| 8 |
|
|
* of the License, or (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 |
|
|
#include "common/setup_before.h"
|
| 20 |
|
|
#include <stdio.h>
|
| 21 |
|
|
#ifdef STDC_HEADERS
|
| 22 |
|
|
# include <stdlib.h>
|
| 23 |
|
|
#endif
|
| 24 |
|
|
#ifdef HAVE_STRING_H
|
| 25 |
|
|
# include <string.h>
|
| 26 |
|
|
#else
|
| 27 |
|
|
# ifdef HAVE_STRINGS_H
|
| 28 |
|
|
# include <strings.h>
|
| 29 |
|
|
# endif
|
| 30 |
|
|
#endif
|
| 31 |
|
|
#include "compat/strrchr.h"
|
| 32 |
|
|
#ifdef HAVE_UNISTD_H
|
| 33 |
|
|
# include <unistd.h>
|
| 34 |
|
|
#endif
|
| 35 |
|
|
#include "compat/read.h"
|
| 36 |
|
|
#ifdef HAVE_SYS_IOCTL_H
|
| 37 |
|
|
# include <sys/ioctl.h>
|
| 38 |
|
|
#endif
|
| 39 |
|
|
#ifdef HAVE_SYS_TYPES_H
|
| 40 |
|
|
# include <sys/types.h>
|
| 41 |
|
|
#endif
|
| 42 |
|
|
#ifdef HAVE_SYS_SOCKET_H
|
| 43 |
|
|
# include <sys/socket.h>
|
| 44 |
|
|
#endif
|
| 45 |
|
|
#include "compat/socket.h"
|
| 46 |
|
|
#include "compat/psock.h"
|
| 47 |
|
|
#include "common/packet.h"
|
| 48 |
|
|
#include "common/network.h"
|
| 49 |
|
|
#ifdef WIN32
|
| 50 |
|
|
# include <conio.h> /* for kbhit() and getch() */
|
| 51 |
|
|
#endif
|
| 52 |
|
|
#include "client.h"
|
| 53 |
|
|
#include "common/setup_after.h"
|
| 54 |
|
|
|
| 55 |
|
|
|
| 56 |
|
|
extern int client_blocksend_packet(int sock, t_packet const * packet)
|
| 57 |
|
|
{
|
| 58 |
|
|
unsigned int size=0;
|
| 59 |
|
|
|
| 60 |
|
|
for (;;)
|
| 61 |
|
|
switch (net_send_packet(sock,packet,&size))
|
| 62 |
|
|
{
|
| 63 |
|
|
case -1:
|
| 64 |
|
|
return -1;
|
| 65 |
|
|
|
| 66 |
|
|
case 0:
|
| 67 |
|
|
continue;
|
| 68 |
|
|
|
| 69 |
|
|
default:
|
| 70 |
|
|
return 0;
|
| 71 |
|
|
}
|
| 72 |
|
|
}
|
| 73 |
|
|
|
| 74 |
|
|
|
| 75 |
|
|
extern int client_blockrecv_packet(int sock, t_packet * packet)
|
| 76 |
|
|
{
|
| 77 |
|
|
unsigned int size=0;
|
| 78 |
|
|
|
| 79 |
|
|
for (;;)
|
| 80 |
|
|
switch (net_recv_packet(sock,packet,&size))
|
| 81 |
|
|
{
|
| 82 |
|
|
case -1:
|
| 83 |
|
|
return -1;
|
| 84 |
|
|
|
| 85 |
|
|
case 0:
|
| 86 |
|
|
continue;
|
| 87 |
|
|
|
| 88 |
|
|
default:
|
| 89 |
|
|
/* printf("Got packet len %u/%u type 0x%04x\n",size,packet_get_size(packet),packet_get_type(packet)); */
|
| 90 |
|
|
return 0;
|
| 91 |
|
|
}
|
| 92 |
|
|
}
|
| 93 |
|
|
|
| 94 |
|
|
|
| 95 |
|
|
extern int client_blockrecv_raw_packet(int sock, t_packet * packet, unsigned int len)
|
| 96 |
|
|
{
|
| 97 |
|
|
unsigned int size=0;
|
| 98 |
|
|
|
| 99 |
|
|
packet_set_size(packet,len);
|
| 100 |
|
|
for (;;)
|
| 101 |
|
|
switch (net_recv_packet(sock,packet,&size))
|
| 102 |
|
|
{
|
| 103 |
|
|
case -1:
|
| 104 |
|
|
return -1;
|
| 105 |
|
|
case 0:
|
| 106 |
|
|
continue;
|
| 107 |
|
|
case 1:
|
| 108 |
|
|
default:
|
| 109 |
|
|
return 0;
|
| 110 |
|
|
}
|
| 111 |
|
|
}
|
| 112 |
|
|
|
| 113 |
|
|
|
| 114 |
|
|
extern int client_get_termsize(int fd, unsigned int * w, unsigned int * h)
|
| 115 |
|
|
{
|
| 116 |
|
|
if (fd<0 || !w || !h)
|
| 117 |
|
|
return -1;
|
| 118 |
|
|
|
| 119 |
|
|
*w = 0;
|
| 120 |
|
|
*h = 0;
|
| 121 |
|
|
|
| 122 |
|
|
#ifdef HAVE_IOCTL
|
| 123 |
|
|
#ifdef TIOCGWINSZ
|
| 124 |
|
|
{
|
| 125 |
|
|
struct winsize ws;
|
| 126 |
|
|
|
| 127 |
|
|
if (ioctl(fd,TIOCGWINSZ,&ws)>=0)
|
| 128 |
|
|
{
|
| 129 |
|
|
*w = (unsigned int)ws.ws_col;
|
| 130 |
|
|
*h = (unsigned int)ws.ws_row;
|
| 131 |
|
|
}
|
| 132 |
|
|
}
|
| 133 |
|
|
#endif
|
| 134 |
|
|
#endif
|
| 135 |
|
|
|
| 136 |
|
|
#ifdef HAVE_GETENV
|
| 137 |
|
|
{
|
| 138 |
|
|
char const * str;
|
| 139 |
|
|
int val;
|
| 140 |
|
|
|
| 141 |
|
|
if (!*w && (str = getenv("COLUMNS")) && (val = atoi(str))>0)
|
| 142 |
|
|
*w = val;
|
| 143 |
|
|
if (!*h && (str = getenv("LINES")) && (val = atoi(str))>0)
|
| 144 |
|
|
*h = val;
|
| 145 |
|
|
}
|
| 146 |
|
|
#endif
|
| 147 |
|
|
|
| 148 |
|
|
if (!*w)
|
| 149 |
|
|
*w = DEF_SCREEN_WIDTH;
|
| 150 |
|
|
if (!*h)
|
| 151 |
|
|
*h = DEF_SCREEN_HEIGHT;
|
| 152 |
|
|
|
| 153 |
|
|
return 0;
|
| 154 |
|
|
}
|
| 155 |
|
|
|
| 156 |
|
|
|
| 157 |
|
|
/* This routine gets keyboard input. It handles printing the prompt, cursor positioning, and
|
| 158 |
|
|
text scrolling. It unfortunatly assumes that the chars read from stdin are in ASCII. */
|
| 159 |
|
|
/* visible: -1=nothing, 0=prompt only, 1=prompt and text */
|
| 160 |
|
|
extern int client_get_comm(char const * prompt, char * text, unsigned int maxlen, unsigned int * curpos, int visible, int redraw, unsigned int width)
|
| 161 |
|
|
{
|
| 162 |
|
|
char temp;
|
| 163 |
|
|
unsigned int addlen;
|
| 164 |
|
|
unsigned int i;
|
| 165 |
|
|
unsigned int count;
|
| 166 |
|
|
int beg_pos;
|
| 167 |
|
|
|
| 168 |
|
|
for (count=0; count<16; count++)
|
| 169 |
|
|
{
|
| 170 |
|
|
beg_pos = 0;
|
| 171 |
|
|
if (strlen(prompt)+1>=width)
|
| 172 |
|
|
visible = 0; /* no room to show any of the text */
|
| 173 |
|
|
else if (*curpos+strlen(prompt)+1>=width)
|
| 174 |
|
|
beg_pos = (int)(*curpos+strlen(prompt)+1-width);
|
| 175 |
|
|
|
| 176 |
|
|
if (redraw)
|
| 177 |
|
|
{
|
| 178 |
|
|
if (visible!=-1)
|
| 179 |
|
|
printf("\r%s",prompt);
|
| 180 |
|
|
if (visible==1)
|
| 181 |
|
|
printf("%s",text+beg_pos);
|
| 182 |
|
|
}
|
| 183 |
|
|
|
| 184 |
|
|
fflush(stdout);
|
| 185 |
|
|
#ifndef WIN32
|
| 186 |
|
|
addlen = read(fileno(stdin),&temp,1);
|
| 187 |
|
|
#else
|
| 188 |
|
|
temp = getch();
|
| 189 |
|
|
addlen=1;
|
| 190 |
|
|
#endif
|
| 191 |
|
|
|
| 192 |
|
|
if (addlen<1)
|
| 193 |
|
|
return addlen;
|
| 194 |
|
|
|
| 195 |
|
|
switch (temp)
|
| 196 |
|
|
{
|
| 197 |
|
|
case '\033': /* ESC */
|
| 198 |
|
|
return -1;
|
| 199 |
|
|
case '\r':
|
| 200 |
|
|
case '\n':
|
| 201 |
|
|
return 1;
|
| 202 |
|
|
case '\b': /* BS */
|
| 203 |
|
|
case '\177': /* DEL */
|
| 204 |
|
|
if (*curpos<1) /* already empty */
|
| 205 |
|
|
{
|
| 206 |
|
|
if (visible==1)
|
| 207 |
|
|
printf("\a");
|
| 208 |
|
|
continue;
|
| 209 |
|
|
}
|
| 210 |
|
|
(*curpos)--;
|
| 211 |
|
|
text[*curpos] = '\0';
|
| 212 |
|
|
if (visible==1)
|
| 213 |
|
|
{
|
| 214 |
|
|
if (beg_pos>0)
|
| 215 |
|
|
{
|
| 216 |
|
|
beg_pos--;
|
| 217 |
|
|
printf("\r%s%s",prompt,text+beg_pos);
|
| 218 |
|
|
}
|
| 219 |
|
|
else
|
| 220 |
|
|
{ printf("\b \b"); }
|
| 221 |
|
|
}
|
| 222 |
|
|
continue;
|
| 223 |
|
|
case '\024': /* ^T */
|
| 224 |
|
|
if (*curpos<2)
|
| 225 |
|
|
{
|
| 226 |
|
|
if (visible==1)
|
| 227 |
|
|
printf("\a");
|
| 228 |
|
|
continue;
|
| 229 |
|
|
}
|
| 230 |
|
|
{
|
| 231 |
|
|
char swap;
|
| 232 |
|
|
|
| 233 |
|
|
swap = text[*curpos-1];
|
| 234 |
|
|
text[*curpos-1] = text[*curpos-2];
|
| 235 |
|
|
text[*curpos-2] = swap;
|
| 236 |
|
|
|
| 237 |
|
|
if (visible==1)
|
| 238 |
|
|
{
|
| 239 |
|
|
printf("\b\b");
|
| 240 |
|
|
printf("%s",&text[*curpos-2]);
|
| 241 |
|
|
}
|
| 242 |
|
|
}
|
| 243 |
|
|
continue;
|
| 244 |
|
|
case '\027': /* ^W */
|
| 245 |
|
|
if (*curpos<1)
|
| 246 |
|
|
{
|
| 247 |
|
|
if (visible==1)
|
| 248 |
|
|
printf("\a");
|
| 249 |
|
|
continue;
|
| 250 |
|
|
}
|
| 251 |
|
|
{
|
| 252 |
|
|
char * t=strrchr(text,' ');
|
| 253 |
|
|
unsigned int t1=beg_pos,t2;
|
| 254 |
|
|
|
| 255 |
|
|
addlen = t ? t - text : 0;
|
| 256 |
|
|
text[addlen] = '\0';
|
| 257 |
|
|
beg_pos -= *curpos - addlen;
|
| 258 |
|
|
if (beg_pos < 0)
|
| 259 |
|
|
beg_pos=0;
|
| 260 |
|
|
if (visible==1)
|
| 261 |
|
|
{
|
| 262 |
|
|
if (t1 == 0)
|
| 263 |
|
|
for (i=0; i < *curpos - addlen; i++)
|
| 264 |
|
|
printf("\b \b");
|
| 265 |
|
|
else
|
| 266 |
|
|
{
|
| 267 |
|
|
/* the \r is counted in the return value, but that's ok
|
| 268 |
|
|
because the last column is always blank */
|
| 269 |
|
|
t2 = printf("\r%s%s",prompt,text+beg_pos);
|
| 270 |
|
|
if (t1 > 0 && beg_pos == 0)
|
| 271 |
|
|
{
|
| 272 |
|
|
for (i=0; i<width-t2; i++)
|
| 273 |
|
|
printf(" ");
|
| 274 |
|
|
for (i=0; i<width-t2; i++)
|
| 275 |
|
|
printf("\b");
|
| 276 |
|
|
}
|
| 277 |
|
|
}
|
| 278 |
|
|
}
|
| 279 |
|
|
}
|
| 280 |
|
|
*curpos = addlen;
|
| 281 |
|
|
continue;
|
| 282 |
|
|
case '\025': /* ^U */
|
| 283 |
|
|
if (visible==1)
|
| 284 |
|
|
{
|
| 285 |
|
|
unsigned int t2;
|
| 286 |
|
|
|
| 287 |
|
|
t2 = printf("\r%s",prompt);
|
| 288 |
|
|
for (i=0; i<width-t2; i++)
|
| 289 |
|
|
printf(" ");
|
| 290 |
|
|
printf("\r%s",prompt);
|
| 291 |
|
|
}
|
| 292 |
|
|
*curpos = 0;
|
| 293 |
|
|
text[0] = '\0';
|
| 294 |
|
|
continue;
|
| 295 |
|
|
default:
|
| 296 |
|
|
if (temp>0 && temp<32) /* unhandled control char */
|
| 297 |
|
|
{
|
| 298 |
|
|
if (visible==1)
|
| 299 |
|
|
printf("\a");
|
| 300 |
|
|
continue;
|
| 301 |
|
|
}
|
| 302 |
|
|
if ((*curpos+1)>=maxlen) /* too full */
|
| 303 |
|
|
{
|
| 304 |
|
|
if (visible==1)
|
| 305 |
|
|
printf("\a");
|
| 306 |
|
|
continue;
|
| 307 |
|
|
}
|
| 308 |
|
|
if (visible==1)
|
| 309 |
|
|
{
|
| 310 |
|
|
if (beg_pos>0 || (*curpos + strlen(prompt) + 2 > width))
|
| 311 |
|
|
{
|
| 312 |
|
|
beg_pos++;
|
| 313 |
|
|
printf("\r%s%s",prompt,text+beg_pos);
|
| 314 |
|
|
}
|
| 315 |
|
|
printf("%c",temp);
|
| 316 |
|
|
}
|
| 317 |
|
|
text[*curpos] = temp;
|
| 318 |
|
|
(*curpos)++;
|
| 319 |
|
|
text[*curpos] = '\0';
|
| 320 |
|
|
continue;
|
| 321 |
|
|
}
|
| 322 |
|
|
}
|
| 323 |
|
|
|
| 324 |
|
|
return 0;
|
| 325 |
|
|
}
|