cleanups, add manual page
[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
b36fc510 19static char sccsid[] = "@(#)utilities.c 1.7 (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
115a5494
GM
30#include "ring.h"
31
788caa15
GM
32#include "externs.h"
33
34FILE *NetTrace = 0; /* Not in bss, since needs to stay */
35
36/*
37 * upcase()
38 *
39 * Upcase (in place) the argument.
40 */
41
42void
43upcase(argument)
44register char *argument;
45{
46 register int c;
47
48 while ((c = *argument) != 0) {
49 if (islower(c)) {
50 *argument = toupper(c);
51 }
52 argument++;
53 }
54}
55
56/*
57 * SetSockOpt()
58 *
59 * Compensate for differences in 4.2 and 4.3 systems.
60 */
61
62int
63SetSockOpt(fd, level, option, yesno)
64int
65 fd,
66 level,
67 option,
68 yesno;
69{
70#ifndef NOT43
71 return setsockopt(fd, level, option,
72 (char *)&yesno, sizeof yesno);
73#else /* NOT43 */
74 if (yesno == 0) { /* Can't do that in 4.2! */
75 fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",
76 option);
77 return -1;
78 }
79 return setsockopt(fd, level, option, 0, 0);
80#endif /* NOT43 */
81}
82\f
83/*
84 * The following are routines used to print out debugging information.
85 */
86
87
88void
89Dump(direction, buffer, length)
90char direction;
91char *buffer;
92int length;
93{
94# define BYTES_PER_LINE 32
95# define min(x,y) ((x<y)? x:y)
96 char *pThis;
97 int offset;
98
99 offset = 0;
100
101 while (length) {
102 /* print one line */
103 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
104 pThis = buffer;
105 buffer = buffer+min(length, BYTES_PER_LINE);
106 while (pThis < buffer) {
107 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
108 pThis++;
109 }
110 fprintf(NetTrace, "\n");
111 length -= BYTES_PER_LINE;
112 offset += BYTES_PER_LINE;
113 if (length < 0) {
114 return;
115 }
116 /* find next unique line */
117 }
118}
119
120
121/*VARARGS*/
122void
123printoption(direction, fmt, option, what)
124 char *direction, *fmt;
125 int option, what;
126{
127 if (!showoptions)
128 return;
129 fprintf(NetTrace, "%s ", direction+1);
130 if (fmt == doopt)
131 fmt = "do";
132 else if (fmt == dont)
133 fmt = "dont";
134 else if (fmt == will)
135 fmt = "will";
136 else if (fmt == wont)
137 fmt = "wont";
138 else
139 fmt = "???";
140 if (option < (sizeof telopts/sizeof telopts[0]))
141 fprintf(NetTrace, "%s %s", fmt, telopts[option]);
142 else
143 fprintf(NetTrace, "%s %d", fmt, option);
144 if (*direction == '<') {
145 fprintf(NetTrace, "\r\n");
146 return;
147 }
148 fprintf(NetTrace, " (%s)\r\n", what ? "reply" : "don't reply");
149}
150
151void
152printsub(direction, pointer, length)
153char *direction, /* "<" or ">" */
154 *pointer; /* where suboption data sits */
155int length; /* length of suboption data */
156{
157 if (showoptions) {
158 fprintf(NetTrace, "%s suboption ",
159 (direction[0] == '<')? "Received":"Sent");
160 switch (pointer[0]) {
161 case TELOPT_TTYPE:
162 fprintf(NetTrace, "Terminal type ");
163 switch (pointer[1]) {
164 case TELQUAL_IS:
165 {
448f9c06 166 char tmpbuf[SUBBUFSIZE];
788caa15
GM
167 int minlen = min(length, sizeof tmpbuf);
168
169 memcpy(tmpbuf, pointer+2, minlen);
170 tmpbuf[minlen-1] = 0;
171 fprintf(NetTrace, "is %s.\n", tmpbuf);
172 }
173 break;
174 case TELQUAL_SEND:
175 fprintf(NetTrace, "- request to send.\n");
176 break;
177 default:
178 fprintf(NetTrace,
80a47e22
GM
179 "- unknown qualifier %d (0x%x).\n",
180 pointer[1], pointer[1]);
788caa15
GM
181 }
182 break;
183 default:
184 fprintf(NetTrace, "Unknown option %d (0x%x)\n",
185 pointer[0], pointer[0]);
186 }
187 }
188}