print local, remote filenames only if remapped or non-prompting,
[unix-history] / usr / src / usr.bin / telnet / utilities.c
CommitLineData
897ce52e
KB
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b36fc510
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
897ce52e
KB
16 */
17
18#ifndef lint
e3f3aa94 19static char sccsid[] = "@(#)utilities.c 1.11 (Berkeley) %G%";
897ce52e
KB
20#endif /* not lint */
21
788caa15
GM
22#define TELOPTS
23#include <arpa/telnet.h>
115a5494 24#include <sys/types.h>
788caa15
GM
25
26#include <ctype.h>
27
2dde2af0
GM
28#include "general.h"
29
6d0564c5
GM
30#include "fdset.h"
31
115a5494
GM
32#include "ring.h"
33
cf9305fa
GM
34#include "defines.h"
35
788caa15
GM
36#include "externs.h"
37
38FILE *NetTrace = 0; /* Not in bss, since needs to stay */
39
40/*
41 * upcase()
42 *
43 * Upcase (in place) the argument.
44 */
45
46void
47upcase(argument)
48register char *argument;
49{
50 register int c;
51
52 while ((c = *argument) != 0) {
53 if (islower(c)) {
54 *argument = toupper(c);
55 }
56 argument++;
57 }
58}
59
60/*
61 * SetSockOpt()
62 *
63 * Compensate for differences in 4.2 and 4.3 systems.
64 */
65
66int
67SetSockOpt(fd, level, option, yesno)
68int
69 fd,
70 level,
71 option,
72 yesno;
73{
74#ifndef NOT43
75 return setsockopt(fd, level, option,
76 (char *)&yesno, sizeof yesno);
77#else /* NOT43 */
78 if (yesno == 0) { /* Can't do that in 4.2! */
79 fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",
80 option);
81 return -1;
82 }
83 return setsockopt(fd, level, option, 0, 0);
84#endif /* NOT43 */
85}
86\f
87/*
88 * The following are routines used to print out debugging information.
89 */
90
91
92void
93Dump(direction, buffer, length)
94char direction;
95char *buffer;
96int length;
97{
98# define BYTES_PER_LINE 32
99# define min(x,y) ((x<y)? x:y)
100 char *pThis;
101 int offset;
102
103 offset = 0;
104
105 while (length) {
106 /* print one line */
107 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
108 pThis = buffer;
109 buffer = buffer+min(length, BYTES_PER_LINE);
110 while (pThis < buffer) {
111 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
112 pThis++;
113 }
114 fprintf(NetTrace, "\n");
115 length -= BYTES_PER_LINE;
116 offset += BYTES_PER_LINE;
117 if (length < 0) {
e3f3aa94 118 fflush(NetTrace);
788caa15
GM
119 return;
120 }
121 /* find next unique line */
122 }
e3f3aa94 123 fflush(NetTrace);
788caa15
GM
124}
125
126
127/*VARARGS*/
128void
129printoption(direction, fmt, option, what)
130 char *direction, *fmt;
131 int option, what;
132{
133 if (!showoptions)
134 return;
135 fprintf(NetTrace, "%s ", direction+1);
136 if (fmt == doopt)
137 fmt = "do";
138 else if (fmt == dont)
139 fmt = "dont";
140 else if (fmt == will)
141 fmt = "will";
142 else if (fmt == wont)
143 fmt = "wont";
144 else
145 fmt = "???";
146 if (option < (sizeof telopts/sizeof telopts[0]))
147 fprintf(NetTrace, "%s %s", fmt, telopts[option]);
148 else
149 fprintf(NetTrace, "%s %d", fmt, option);
150 if (*direction == '<') {
151 fprintf(NetTrace, "\r\n");
152 return;
153 }
154 fprintf(NetTrace, " (%s)\r\n", what ? "reply" : "don't reply");
155}
156
157void
158printsub(direction, pointer, length)
159char *direction, /* "<" or ">" */
160 *pointer; /* where suboption data sits */
161int length; /* length of suboption data */
162{
163 if (showoptions) {
164 fprintf(NetTrace, "%s suboption ",
165 (direction[0] == '<')? "Received":"Sent");
166 switch (pointer[0]) {
167 case TELOPT_TTYPE:
168 fprintf(NetTrace, "Terminal type ");
169 switch (pointer[1]) {
170 case TELQUAL_IS:
171 {
448f9c06 172 char tmpbuf[SUBBUFSIZE];
788caa15
GM
173 int minlen = min(length, sizeof tmpbuf);
174
175 memcpy(tmpbuf, pointer+2, minlen);
176 tmpbuf[minlen-1] = 0;
177 fprintf(NetTrace, "is %s.\n", tmpbuf);
178 }
179 break;
180 case TELQUAL_SEND:
181 fprintf(NetTrace, "- request to send.\n");
182 break;
183 default:
184 fprintf(NetTrace,
80a47e22
GM
185 "- unknown qualifier %d (0x%x).\n",
186 pointer[1], pointer[1]);
788caa15
GM
187 }
188 break;
189 default:
190 fprintf(NetTrace, "Unknown option %d (0x%x)\n",
191 pointer[0], pointer[0]);
192 }
193 }
194}
cf9305fa
GM
195
196/* EmptyTerminal - called to make sure that the terminal buffer is empty.
197 * Note that we consider the buffer to run all the
198 * way to the kernel (thus the select).
199 */
200
201void
202EmptyTerminal()
203{
204#if defined(unix)
205 fd_set o;
206
207 FD_ZERO(&o);
208#endif /* defined(unix) */
209
210 if (TTYBYTES() == 0) {
211#if defined(unix)
212 FD_SET(tout, &o);
213 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
214 (struct timeval *) 0); /* wait for TTLOWAT */
215#endif /* defined(unix) */
216 } else {
217 while (TTYBYTES()) {
218 ttyflush(0);
219#if defined(unix)
220 FD_SET(tout, &o);
221 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
222 (struct timeval *) 0); /* wait for TTLOWAT */
223#endif /* defined(unix) */
224 }
225 }
226}
227
228void
229SetForExit()
230{
231 setconnmode();
232#if defined(TN3270)
233 if (In3270) {
234 Finish3270();
235 }
6da40c06
GM
236#else /* defined(TN3270) */
237 do {
238 telrcv(); /* Process any incoming data */
239 EmptyTerminal();
240 } while (ring_full_count(&netiring)); /* While there is any */
cf9305fa
GM
241#endif /* defined(TN3270) */
242 setcommandmode();
243 fflush(stdout);
244 fflush(stderr);
245#if defined(TN3270)
246 if (In3270) {
247 StopScreen(1);
248 }
249#endif /* defined(TN3270) */
250 setconnmode();
251 EmptyTerminal(); /* Flush the path to the tty */
252 setcommandmode();
253}
254
255void
256Exit(returnCode)
257int returnCode;
258{
259 SetForExit();
260 exit(returnCode);
261}
262
263void
264ExitString(string, returnCode)
265char *string;
266int returnCode;
267{
268 SetForExit();
269 fwrite(string, 1, strlen(string), stderr);
270 exit(returnCode);
271}
272
273#if defined(MSDOS)
274void
275ExitPerror(string, returnCode)
276char *string;
277int returnCode;
278{
279 SetForExit();
280 perror(string);
281 exit(returnCode);
282}
283#endif /* defined(MSDOS) */