drm/radeon: Finish replacing task queues by Linux work queues
[dragonfly.git] / usr.sbin / pppctl / pppctl.c
blob22ddd113774378f08943322516ada10449c27cd0
1 /*-
2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD: src/usr.sbin/pppctl/pppctl.c,v 1.21.2.2 2001/11/23 13:18:39 brian Exp $
29 #include <sys/param.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/un.h>
35 #include <netdb.h>
37 #include <sys/time.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <histedit.h>
41 #include <setjmp.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <time.h>
47 #include <unistd.h>
49 #define LINELEN 2048
50 static char Buffer[LINELEN], Command[LINELEN];
52 static int
53 usage(void)
55 fprintf(stderr, "usage: pppctl [-v] [-t n] [-p passwd] "
56 "Port|LocalSock [command[;command]...]\n");
57 fprintf(stderr, " -v tells pppctl to output all"
58 " conversation\n");
59 fprintf(stderr, " -t n specifies a timeout of n"
60 " seconds when connecting (default 2)\n");
61 fprintf(stderr, " -p passwd specifies your password\n");
62 exit(1);
65 static int TimedOut = 0;
66 static void
67 Timeout(int Sig __unused)
69 TimedOut = 1;
72 #define REC_PASSWD (1)
73 #define REC_SHOW (2)
74 #define REC_VERBOSE (4)
76 static char *passwd;
77 static char *prompt;
79 static char *
80 GetPrompt(EditLine *e __unused)
82 if (prompt == NULL)
83 prompt = "";
84 return prompt;
87 static int
88 Receive(int fd, int display)
90 char temp[sizeof(Buffer)];
91 int Result;
92 int len;
93 char *last;
95 prompt = Buffer;
96 len = 0;
97 while (Result = read(fd, Buffer+len, sizeof(Buffer)-len-1), Result != -1) {
98 if (Result == 0 && errno != EINTR) {
99 Result = -1;
100 break;
102 len += Result;
103 Buffer[len] = '\0';
104 if (len > 2 && !strcmp(Buffer+len-2, "> ")) {
105 prompt = strrchr(Buffer, '\n');
106 if (display & (REC_SHOW|REC_VERBOSE)) {
107 if (display & REC_VERBOSE)
108 last = Buffer+len-1;
109 else
110 last = prompt;
111 if (last) {
112 last++;
113 write(1, Buffer, last-Buffer);
116 prompt = prompt == NULL ? Buffer : prompt+1;
117 for (last = Buffer+len-2; last > Buffer && *last != ' '; last--)
119 if (last > Buffer+3 && !strncmp(last-3, " on", 3)) {
120 /* a password is required ! */
121 if (display & REC_PASSWD) {
122 /* password time */
123 if (!passwd)
124 passwd = getpass("Password: ");
125 sprintf(Buffer, "passwd %s\n", passwd);
126 memset(passwd, '\0', strlen(passwd));
127 if (display & REC_VERBOSE)
128 write(1, Buffer, strlen(Buffer));
129 write(fd, Buffer, strlen(Buffer));
130 memset(Buffer, '\0', strlen(Buffer));
131 return Receive(fd, display & ~REC_PASSWD);
133 Result = 1;
134 } else
135 Result = 0;
136 break;
138 if (len == sizeof Buffer - 1) {
139 int flush;
140 if ((last = strrchr(Buffer, '\n')) == NULL)
141 /* Yeuch - this is one mother of a line ! */
142 flush = sizeof Buffer / 2;
143 else
144 flush = last - Buffer + 1;
145 write(1, Buffer, flush);
146 strcpy(temp, Buffer + flush);
147 strcpy(Buffer, temp);
148 len -= flush;
152 return Result;
155 static int data = -1;
156 static jmp_buf pppdead;
158 static void
159 check_fd(int sig __unused)
161 if (data != -1) {
162 struct timeval t;
163 fd_set f;
164 static char buf[LINELEN];
165 int len;
167 FD_ZERO(&f);
168 FD_SET(data, &f);
169 t.tv_sec = t.tv_usec = 0;
170 if (select(data+1, &f, NULL, NULL, &t) > 0) {
171 len = read(data, buf, sizeof buf);
172 if (len > 0)
173 write(1, buf, len);
174 else
175 longjmp(pppdead, -1);
180 static const char *
181 smartgets(EditLine *e, int *count, int fd)
183 const char *result;
185 data = fd;
186 signal(SIGALRM, check_fd);
187 ualarm(500000, 500000);
188 result = setjmp(pppdead) ? NULL : el_gets(e, count);
189 ualarm(0,0);
190 signal(SIGALRM, SIG_DFL);
191 data = -1;
193 return result;
197 main(int argc, char **argv)
199 struct servent *s;
200 struct hostent *h;
201 struct sockaddr *sock;
202 struct sockaddr_in ifsin;
203 struct sockaddr_un ifsun;
204 int n, socksz, arg, fd, len, verbose, save_errno, hide1, hide1off, hide2;
205 unsigned TimeoutVal;
206 char *DoneWord = "x", *next, *start;
207 struct sigaction act, oact;
209 verbose = 0;
210 TimeoutVal = 2;
211 hide1 = hide1off = hide2 = 0;
213 for (arg = 1; arg < argc; arg++)
214 if (*argv[arg] == '-') {
215 for (start = argv[arg] + 1; *start; start++)
216 switch (*start) {
217 case 't':
218 TimeoutVal = (unsigned)atoi
219 (start[1] ? start + 1 : argv[++arg]);
220 start = DoneWord;
221 break;
223 case 'v':
224 verbose = REC_VERBOSE;
225 break;
227 case 'p':
228 if (start[1]) {
229 hide1 = arg;
230 hide1off = start - argv[arg];
231 passwd = start + 1;
232 } else {
233 hide1 = arg;
234 hide1off = start - argv[arg];
235 passwd = argv[++arg];
236 hide2 = arg;
238 start = DoneWord;
239 break;
241 default:
242 usage();
245 else
246 break;
249 if (argc < arg + 1)
250 usage();
252 if (hide1) {
253 char title[1024];
254 int pos, harg;
256 for (harg = pos = 0; harg < argc; harg++)
257 if (harg == 0 || harg != hide2) {
258 if (harg == 0 || harg != hide1)
259 n = snprintf(title + pos, sizeof title - pos, "%s%s",
260 harg ? " " : "", argv[harg]);
261 else if (hide1off > 1)
262 n = snprintf(title + pos, sizeof title - pos, " %.*s",
263 hide1off, argv[harg]);
264 else
265 n = 0;
266 if (n < 0 || n >= sizeof title - pos)
267 break;
268 pos += n;
270 #ifdef __DragonFly__
271 setproctitle("-%s", title);
272 #else
273 setproctitle("%s", title);
274 #endif
277 if (*argv[arg] == '/') {
278 sock = (struct sockaddr *)&ifsun;
279 socksz = sizeof ifsun;
281 memset(&ifsun, '\0', sizeof ifsun);
282 ifsun.sun_len = strlen(argv[arg]);
283 if (ifsun.sun_len > sizeof ifsun.sun_path - 1) {
284 warnx("%s: path too long", argv[arg]);
285 return 1;
287 ifsun.sun_family = AF_LOCAL;
288 strcpy(ifsun.sun_path, argv[arg]);
290 if (fd = socket(AF_LOCAL, SOCK_STREAM, 0), fd < 0) {
291 warnx("cannot create local domain socket");
292 return 2;
294 } else {
295 char *port, *host, *colon;
296 int hlen;
298 colon = strchr(argv[arg], ':');
299 if (colon) {
300 port = colon + 1;
301 *colon = '\0';
302 host = argv[arg];
303 } else {
304 port = argv[arg];
305 host = "127.0.0.1";
307 sock = (struct sockaddr *)&ifsin;
308 socksz = sizeof ifsin;
309 hlen = strlen(host);
311 memset(&ifsin, '\0', sizeof ifsin);
312 if (strspn(host, "0123456789.") == hlen) {
313 if (!inet_aton(host, &ifsin.sin_addr)) {
314 warnx("cannot translate %s", host);
315 return 1;
317 } else if ((h = gethostbyname(host)) == NULL) {
318 warnx("cannot resolve %s", host);
319 return 1;
321 else
322 ifsin.sin_addr.s_addr = *(u_long *)h->h_addr_list[0];
324 if (colon)
325 *colon = ':';
327 if (strspn(port, "0123456789") == strlen(port))
328 ifsin.sin_port = htons(atoi(port));
329 else if (s = getservbyname(port, "tcp"), !s) {
330 warnx("%s isn't a valid port or service!", port);
331 usage();
333 else
334 ifsin.sin_port = s->s_port;
336 ifsin.sin_len = sizeof(ifsin);
337 ifsin.sin_family = AF_INET;
339 if (fd = socket(AF_INET, SOCK_STREAM, 0), fd < 0) {
340 warnx("cannot create internet socket");
341 return 2;
345 TimedOut = 0;
346 if (TimeoutVal) {
347 act.sa_handler = Timeout;
348 sigemptyset(&act.sa_mask);
349 act.sa_flags = 0;
350 sigaction(SIGALRM, &act, &oact);
351 alarm(TimeoutVal);
354 if (connect(fd, sock, socksz) < 0) {
355 if (TimeoutVal) {
356 save_errno = errno;
357 alarm(0);
358 sigaction(SIGALRM, &oact, 0);
359 errno = save_errno;
361 if (TimedOut)
362 warnx("timeout: cannot connect to socket %s", argv[arg]);
363 else {
364 if (errno)
365 warn("cannot connect to socket %s", argv[arg]);
366 else
367 warnx("cannot connect to socket %s", argv[arg]);
369 close(fd);
370 return 3;
373 if (TimeoutVal) {
374 alarm(0);
375 sigaction(SIGALRM, &oact, 0);
378 len = 0;
379 Command[sizeof(Command)-1] = '\0';
380 for (arg++; arg < argc; arg++) {
381 if (len && len < sizeof(Command)-1)
382 strcpy(Command+len++, " ");
383 strncpy(Command+len, argv[arg], sizeof(Command)-len-1);
384 len += strlen(Command+len);
387 switch (Receive(fd, verbose | REC_PASSWD))
389 case 1:
390 fprintf(stderr, "Password incorrect\n");
391 break;
393 case 0:
394 if (len == 0) {
395 EditLine *edit;
396 History *hist;
397 HistEvent he;
398 const char *l, *env;
399 int size;
401 hist = history_init();
402 if ((env = getenv("EL_SIZE"))) {
403 size = atoi(env);
404 if (size < 0)
405 size = 20;
406 } else
407 size = 20;
408 history(hist, &he, H_SETSIZE, size);
409 edit = el_init("pppctl", stdin, stdout, stderr);
410 el_source(edit, NULL);
411 el_set(edit, EL_PROMPT, GetPrompt);
412 if ((env = getenv("EL_EDITOR"))) {
413 if (!strcmp(env, "vi"))
414 el_set(edit, EL_EDITOR, "vi");
415 else if (!strcmp(env, "emacs"))
416 el_set(edit, EL_EDITOR, "emacs");
418 el_set(edit, EL_SIGNAL, 1);
419 el_set(edit, EL_HIST, history, (const char *)hist);
420 while ((l = smartgets(edit, &len, fd))) {
421 if (len > 1)
422 history(hist, &he, H_ENTER, l);
423 write(fd, l, len);
424 if (Receive(fd, REC_SHOW) != 0)
425 break;
427 fprintf(stderr, "Connection closed\n");
428 el_end(edit);
429 history_end(hist);
430 } else {
431 start = Command;
432 do {
433 next = strchr(start, ';');
434 while (*start == ' ' || *start == '\t')
435 start++;
436 if (next)
437 *next = '\0';
438 strcpy(Buffer, start);
439 Buffer[sizeof(Buffer)-2] = '\0';
440 strcat(Buffer, "\n");
441 if (verbose)
442 write(1, Buffer, strlen(Buffer));
443 write(fd, Buffer, strlen(Buffer));
444 if (Receive(fd, verbose | REC_SHOW) != 0) {
445 fprintf(stderr, "Connection closed\n");
446 break;
448 if (next)
449 start = ++next;
450 } while (next && *next);
451 if (verbose)
452 puts("");
454 break;
456 default:
457 warnx("ppp is not responding");
458 break;
461 close(fd);
463 return 0;