-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcu.c
2105 lines (1819 loc) · 50.7 KB
/
cu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* cu.c
Call up a remote system.
Copyright (C) 1992, 1993, 1994, 1995, 2002 Ian Lance Taylor
This file is part of the Taylor UUCP package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
The author of the program may be contacted at [email protected].
*/
#include "uucp.h"
#if USE_RCS_ID
const char cu_rcsid[] = "$Id$";
#endif
#include "cu.h"
#include "uudefs.h"
#include "uuconf.h"
#include "conn.h"
#include "prot.h"
#include "system.h"
#include "sysdep.h"
#include "getopt.h"
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
/* Here are the user settable variables. The user is permitted to
change these while running the program, using ~s. */
/* The escape character used to introduce a special command. The
escape character is the first character of this string. */
const char *zCuvar_escape = "~";
/* Whether to delay for a second before printing the host name after
seeing an escape character. */
boolean fCuvar_delay = TRUE;
/* The input characters which finish a line. The escape character is
only recognized following one of these characters. The default is
carriage return, ^U, ^C, ^O, ^D, ^S, ^Q, ^R, which I got from the
Ultrix /etc/remote file. */
const char *zCuvar_eol = "\r\025\003\017\004\023\021\022";
/* Whether to transfer binary data (nonprintable characters other than
newline and tab) when sending a file. If this is FALSE, then
newline is changed to carriage return. */
boolean fCuvar_binary = FALSE;
/* A prefix string to use before sending a binary character from a
file; this is only used if fCuvar_binary is TRUE. The default is
^V. */
const char *zCuvar_binary_prefix = "\026";
/* Whether to check for echoes of characters sent when sending a file.
This is ignored if fCuvar_binary is TRUE. */
boolean fCuvar_echocheck = FALSE;
/* A character to look for after each newline is sent when sending a
file. The character is the first character in this string, except
that a '\0' means that no echo check is done. */
const char *zCuvar_echonl = "\r";
/* The timeout to use when looking for an character. */
int cCuvar_timeout = 30;
/* The character to use to kill a line if an echo check fails. The
first character in this string is sent. The default is ^U. */
const char *zCuvar_kill = "\025";
/* The number of times to try resending a line if the echo check keeps
failing. */
int cCuvar_resend = 10;
/* The string to send at the end of a file sent with ~>. The default
is ^D. */
const char *zCuvar_eofwrite = "\004";
/* The string to look for to finish a file received with ~<. For tip
this is a collection of single characters, but I don't want to do
that because it means that there are characters which cannot be
received. The default is a guess at a typical shell prompt. */
const char *zCuvar_eofread = "$";
/* Whether to provide verbose information when sending or receiving a
file. */
boolean fCuvar_verbose = TRUE;
/* The table used to give a value to a variable, and to print all the
variable values. */
static const struct uuconf_cmdtab asCuvars[] =
{
{ "escape", UUCONF_CMDTABTYPE_STRING, (pointer) &zCuvar_escape, NULL },
{ "delay", UUCONF_CMDTABTYPE_BOOLEAN, (pointer) &fCuvar_delay, NULL },
{ "eol", UUCONF_CMDTABTYPE_STRING, (pointer) &zCuvar_eol, NULL },
{ "binary", UUCONF_CMDTABTYPE_BOOLEAN, (pointer) &fCuvar_binary, NULL },
{ "binary-prefix", UUCONF_CMDTABTYPE_STRING,
(pointer) &zCuvar_binary_prefix, NULL },
{ "echocheck", UUCONF_CMDTABTYPE_BOOLEAN,
(pointer) &fCuvar_echocheck, NULL },
{ "echonl", UUCONF_CMDTABTYPE_STRING, (pointer) &zCuvar_echonl, NULL },
{ "timeout", UUCONF_CMDTABTYPE_INT, (pointer) &cCuvar_timeout, NULL },
{ "kill", UUCONF_CMDTABTYPE_STRING, (pointer) &zCuvar_kill, NULL },
{ "resend", UUCONF_CMDTABTYPE_INT, (pointer) &cCuvar_resend, NULL },
{ "eofwrite", UUCONF_CMDTABTYPE_STRING, (pointer) &zCuvar_eofwrite, NULL },
{ "eofread", UUCONF_CMDTABTYPE_STRING, (pointer) &zCuvar_eofread, NULL },
{ "verbose", UUCONF_CMDTABTYPE_BOOLEAN, (pointer) &fCuvar_verbose, NULL },
{ NULL, 0, NULL, NULL}
};
/* The string printed at the initial connect. */
#if ANSI_C
#define ZCONNMSG "\aConnected."
#else
#define ZCONNMSG "Connected."
#endif
/* The string printed when disconnecting. */
#if ANSI_C
#define ZDISMSG "\aDisconnected."
#else
#define ZDISMSG "Disconnected."
#endif
/* Local variables. */
/* The string we print when the user is once again connected to the
port after transferring a file or taking some other action. */
static const char abCuconnected[]
#if ANSI_C
= "\a[connected]";
#else
= "[connected]";
#endif
/* Global uuconf pointer. */
static pointer pCuuuconf;
/* Connection. */
static struct sconnection *qCuconn;
/* Whether to close the connection. */
static boolean fCuclose_conn;
/* Dialer used to dial out. */
static struct uuconf_dialer *qCudialer;
/* Whether we need to restore the terminal. */
static boolean fCurestore_terminal;
/* Whether we are doing local echoing. */
static boolean fCulocalecho;
/* Whether we need to call fsysdep_cu_finish. */
static boolean fCustarted;
/* Whether ZCONNMSG has been printed yet. */
static boolean fCuconnprinted = FALSE;
/* A structure used to pass information to icuport_lock. */
struct sconninfo
{
boolean fmatched;
boolean flocked;
boolean fdirect;
struct sconnection *qconn;
const char *zline;
};
/* Local functions. */
static void ucuusage P((void));
static void ucuhelp P((void));
static void ucuabort P((void));
static void uculog_start P((void));
static void uculog_end P((void));
static int icuport_lock P((struct uuconf_port *qport, pointer pinfo));
static boolean fcudo_cmd P((pointer puuconf, struct sconnection *qconn,
int bcmd));
static boolean fcuset_var P((pointer puuconf, char *zline));
static int icuunrecogvar P((pointer puuconf, int argc, char **argv,
pointer pvar, pointer pinfo));
static int icuunrecogfn P((pointer puuconf, int argc, char **argv,
pointer pvar, pointer pinfo));
static void uculist_vars P((void));
static void uculist_fns P((const char *zescape));
static boolean fcudo_subcmd P((pointer puuconf, struct sconnection *qconn,
char *zline));
static boolean fcusend_buf P((struct sconnection *qconn, const char *zbuf,
size_t cbuf));
#define ucuputs(zline) \
do { if (! fsysdep_terminal_puts (zline)) ucuabort (); } while (0)
/* Long getopt options. */
static const struct option asCulongopts[] =
{
{ "phone", required_argument, NULL, 'c' },
{ "escape", required_argument, NULL, 'E' },
{ "parity", required_argument, NULL, 2 },
{ "halfduplex", no_argument, NULL, 'h' },
{ "prompt", no_argument, NULL, 'n' },
{ "line", required_argument, NULL, 'l' },
{ "port", required_argument, NULL, 'p' },
{ "speed", required_argument, NULL, 's' },
{ "baud", required_argument, NULL, 's' },
{ "mapcr", no_argument, NULL, 't' },
{ "nostop", no_argument, NULL, 3 },
{ "system", required_argument, NULL, 'z' },
{ "config", required_argument, NULL, 'I' },
{ "debug", required_argument, NULL, 'x' },
{ "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 1 },
{ NULL, 0, NULL, 0 }
};
int
main (int argc, char **argv)
{
/* -c: phone number. */
char *zphone = NULL;
/* -e: even parity. */
boolean feven = FALSE;
/* -l: line. */
char *zline = NULL;
/* -n: prompt for phone number. */
boolean fprompt = FALSE;
/* -o: odd parity. */
boolean fodd = FALSE;
/* -p: port name. */
const char *zport = NULL;
/* -s: speed. */
long ibaud = 0L;
/* -z: system. */
const char *zsystem = NULL;
/* --nostop: turn off XON/XOFF. */
enum txonxoffsetting txonxoff = XONXOFF_ON;
/* -I: configuration file name. */
const char *zconfig = NULL;
int iopt;
pointer puuconf;
int iuuconf;
const char *zlocalname;
int i;
struct uuconf_system ssys;
const struct uuconf_system *qsys = NULL;
boolean flooped;
struct uuconf_port sport;
struct sconnection sconn;
struct sconninfo sinfo;
long ihighbaud;
struct uuconf_dialer sdialer;
struct uuconf_dialer *qdialer;
char bcmd;
zProgram = argv[0];
/* We want to accept -# as a speed. It's easiest to look through
the arguments, replace -# with -s#, and let getopt handle it. */
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-'
&& isdigit (BUCHAR (argv[i][1])))
{
size_t clen;
char *z;
clen = strlen (argv[i]);
z = zbufalc (clen + 2);
z[0] = '-';
z[1] = 's';
memcpy (z + 2, argv[i] + 1, clen);
argv[i] = z;
}
}
while ((iopt = getopt_long (argc, argv, "a:c:deE:hnI:l:op:s:tvx:z:",
asCulongopts, (int *) NULL)) != EOF)
{
switch (iopt)
{
case 'c':
/* Phone number. */
zphone = optarg;
break;
case 'd':
/* Set debugging level to maximum. */
#if DEBUG > 1
iDebug = DEBUG_MAX;
#endif
break;
case 'e':
/* Even parity. */
feven = TRUE;
break;
case 'E':
/* Escape character. */
zCuvar_escape = optarg;
break;
case 'h':
/* Local echo. */
fCulocalecho = TRUE;
break;
case 'n':
/* Prompt for phone number. */
fprompt = TRUE;
break;
case 'l':
/* Line name. */
zline = optarg;
break;
case 'o':
/* Odd parity. */
fodd = TRUE;
break;
case 'p':
case 'a':
/* Port name (-a is for compatibility). */
zport = optarg;
break;
case 's':
/* Speed. */
ibaud = strtol (optarg, (char **) NULL, 10);
break;
case 'z':
/* System name. */
zsystem = optarg;
break;
case 'I':
/* Configuration file name. */
if (fsysdep_other_config (optarg))
zconfig = optarg;
break;
case 'x':
#if DEBUG > 1
/* Set debugging level. */
iDebug |= idebug_parse (optarg);
#endif
break;
case 'v':
/* Print version and exit. */
printf ("cu (Taylor UUCP) %s\n", VERSION);
printf ("Copyright (C) 1991, 92, 93, 94, 1995, 2002 Ian Lance Taylor\n");
printf ("This program is free software; you may redistribute it under the terms of\n");
printf ("the GNU General Public LIcense. This program has ABSOLUTELY NO WARRANTY.\n");
exit (EXIT_SUCCESS);
/*NOTREACHED*/
case 2:
/* --parity. */
if (strncmp (optarg, "even", strlen (optarg)) == 0)
feven = TRUE;
else if (strncmp (optarg, "odd", strlen (optarg)) == 0)
fodd = TRUE;
else if (strncmp (optarg, "none", strlen (optarg)) == 0)
{
feven = TRUE;
fodd = TRUE;
}
else
{
fprintf (stderr, "%s: --parity requires even, odd or none\n",
zProgram);
ucuusage ();
}
break;
case 3:
/* --nostop. */
txonxoff = XONXOFF_OFF;
break;
case 1:
/* --help. */
ucuhelp ();
exit (EXIT_SUCCESS);
/*NOTREACHED*/
case 0:
/* Long option found and flag set. */
break;
default:
ucuusage ();
/*NOTREACHED*/
}
}
/* There can be one more argument, which is either a system name, a
phone number, or "dir". We decide which it is based on the first
character. To call a UUCP system whose name begins with a digit,
or one which is named "dir", you must use -z. */
if (optind != argc)
{
if (optind != argc - 1
|| zsystem != NULL
|| zphone != NULL)
{
fprintf (stderr, "%s: too many arguments\n", zProgram);
ucuusage ();
}
if (strcmp (argv[optind], "dir") != 0)
{
if (isdigit (BUCHAR (argv[optind][0])))
zphone = argv[optind];
else
zsystem = argv[optind];
}
}
/* If the user doesn't give a system, port, line or speed, then
there's no basis on which to select a port. */
if (zsystem == NULL
&& zport == NULL
&& zline == NULL
&& ibaud == 0L)
{
fprintf (stderr, "%s: must specify system, line, port or speed\n",
zProgram);
ucuusage ();
}
if (fprompt)
{
size_t cphone;
printf ("Phone number: ");
(void) fflush (stdout);
zphone = NULL;
cphone = 0;
if (getline (&zphone, &cphone, stdin) <= 0
|| *zphone == '\0')
{
fprintf (stderr, "%s: no phone number entered\n", zProgram);
exit (EXIT_FAILURE);
}
}
iuuconf = uuconf_init (&puuconf, "cu", zconfig);
if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
pCuuuconf = puuconf;
#if DEBUG > 1
{
const char *zdebug;
iuuconf = uuconf_debuglevel (puuconf, &zdebug);
if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
if (zdebug != NULL)
iDebug |= idebug_parse (zdebug);
}
#endif
usysdep_initialize (puuconf, INIT_NOCHDIR | INIT_SUID);
iuuconf = uuconf_localname (puuconf, &zlocalname);
if (iuuconf == UUCONF_NOT_FOUND)
{
zlocalname = zsysdep_localname ();
if (zlocalname == NULL)
exit (EXIT_FAILURE);
}
else if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
ulog_fatal_fn (ucuabort);
pfLstart = uculog_start;
pfLend = uculog_end;
#ifdef SIGINT
usysdep_signal (SIGINT);
#endif
#ifdef SIGHUP
usysdep_signal (SIGHUP);
#endif
#ifdef SIGQUIT
usysdep_signal (SIGQUIT);
#endif
#ifdef SIGTERM
usysdep_signal (SIGTERM);
#endif
#ifdef SIGPIPE
usysdep_signal (SIGPIPE);
#endif
if (zsystem != NULL)
{
iuuconf = uuconf_system_info (puuconf, zsystem, &ssys);
if (iuuconf != UUCONF_SUCCESS)
{
if (iuuconf != UUCONF_NOT_FOUND)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
ulog (LOG_FATAL, "%s: System not found", zsystem);
}
qsys = &ssys;
}
/* This loop is used if a system is specified. It loops over the
various alternates until it finds one for which the dial
succeeds. This is an ugly spaghetti construction, and it should
be broken up into different functions someday. */
flooped = FALSE;
while (TRUE)
{
enum tparitysetting tparity;
enum tstripsetting tstrip;
long iusebaud;
/* The uuconf_find_port function only selects directly on a port
name and a speed. To select based on the line name, we use a
function. If we can't find any defined port, and the user
specified a line name but did not specify a port name or a
system or a phone number, then we fake a direct port with
that line name (we don't fake a port if a system or phone
number were given because if we fake a port we have no way to
place a call; perhaps we should automatically look up a
particular dialer). This permits users to say cu -lttyd0
without having to put ttyd0 in the ports file, provided they
have read and write access to the port. */
sinfo.fmatched = FALSE;
sinfo.flocked = FALSE;
sinfo.fdirect = qsys == NULL && zphone == NULL;
sinfo.qconn = &sconn;
sinfo.zline = zline;
if (zport != NULL || zline != NULL || ibaud != 0L)
{
iuuconf = uuconf_find_port (puuconf, zport, ibaud, 0L,
icuport_lock, (pointer) &sinfo,
&sport);
if (iuuconf != UUCONF_SUCCESS)
{
if (iuuconf != UUCONF_NOT_FOUND)
{
if (sinfo.flocked)
{
(void) fconn_unlock (&sconn);
uconn_free (&sconn);
}
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
}
if (zline == NULL
|| zport != NULL
|| zphone != NULL
|| qsys != NULL)
{
if (sinfo.fmatched)
ulog (LOG_FATAL, "All matching ports in use");
else
ulog (LOG_FATAL, "No matching ports");
}
sport.uuconf_zname = zline;
sport.uuconf_ttype = UUCONF_PORTTYPE_DIRECT;
sport.uuconf_zprotocols = NULL;
sport.uuconf_qproto_params = NULL;
sport.uuconf_ireliable = 0;
sport.uuconf_zlockname = NULL;
sport.uuconf_palloc = NULL;
sport.uuconf_u.uuconf_sdirect.uuconf_zdevice = NULL;
sport.uuconf_u.uuconf_sdirect.uuconf_ibaud = ibaud;
if (! fconn_init (&sport, &sconn, UUCONF_PORTTYPE_UNKNOWN))
ucuabort ();
if (! fconn_lock (&sconn, FALSE, TRUE))
ulog (LOG_FATAL, "%s: Line in use", zline);
qCuconn = &sconn;
/* Check user access after locking the port, because on
some systems shared lines affect the ownership and
permissions. In such a case ``Line in use'' is more
clear than ``Permission denied.'' */
if (! fsysdep_port_access (&sport))
ulog (LOG_FATAL, "%s: Permission denied", zline);
}
iusebaud = ibaud;
ihighbaud = 0L;
}
else
{
for (; qsys != NULL; qsys = qsys->uuconf_qalternate)
{
if (! qsys->uuconf_fcall)
continue;
if (qsys->uuconf_qport != NULL)
{
if (fconn_init (qsys->uuconf_qport, &sconn,
UUCONF_PORTTYPE_UNKNOWN))
{
if (fconn_lock (&sconn, FALSE, FALSE))
{
qCuconn = &sconn;
break;
}
uconn_free (&sconn);
}
}
else
{
sinfo.fmatched = FALSE;
sinfo.flocked = FALSE;
sinfo.qconn = &sconn;
iuuconf = uuconf_find_port (puuconf, qsys->uuconf_zport,
qsys->uuconf_ibaud,
qsys->uuconf_ihighbaud,
icuport_lock,
(pointer) &sinfo,
&sport);
if (iuuconf == UUCONF_SUCCESS)
break;
if (iuuconf != UUCONF_NOT_FOUND)
{
if (sinfo.flocked)
{
(void) fconn_unlock (&sconn);
uconn_free (&sconn);
}
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
}
}
}
if (qsys == NULL)
{
const char *zrem;
if (flooped)
zrem = "remaining ";
else
zrem = "";
if (sinfo.fmatched)
ulog (LOG_FATAL, "%s: All %smatching ports in use",
zsystem, zrem);
else
ulog (LOG_FATAL, "%s: No %smatching ports", zsystem, zrem);
}
iusebaud = qsys->uuconf_ibaud;
ihighbaud = qsys->uuconf_ihighbaud;
}
/* Here we have locked a connection to use. */
if (! fconn_open (&sconn, iusebaud, ihighbaud, FALSE, sinfo.fdirect))
ucuabort ();
fCuclose_conn = TRUE;
if (FGOT_SIGNAL ())
ucuabort ();
/* Set up the connection. */
if (fodd && feven)
{
tparity = PARITYSETTING_NONE;
tstrip = STRIPSETTING_SEVENBITS;
}
else if (fodd)
{
tparity = PARITYSETTING_ODD;
tstrip = STRIPSETTING_SEVENBITS;
}
else if (feven)
{
tparity = PARITYSETTING_EVEN;
tstrip = STRIPSETTING_SEVENBITS;
}
else
{
tparity = PARITYSETTING_DEFAULT;
tstrip = STRIPSETTING_DEFAULT;
}
if (! fconn_set (&sconn, tparity, tstrip, txonxoff))
ucuabort ();
if (qsys != NULL)
zphone = qsys->uuconf_zphone;
if (qsys != NULL || zphone != NULL)
{
enum tdialerfound tdialer;
if (! fconn_dial (&sconn, puuconf, qsys, zphone, &sdialer,
&tdialer))
{
if (zport != NULL
|| zline != NULL
|| ibaud != 0L
|| qsys == NULL)
ucuabort ();
qsys = qsys->uuconf_qalternate;
if (qsys == NULL)
ulog (LOG_FATAL, "%s: No remaining alternates", zsystem);
fCuclose_conn = FALSE;
(void) fconn_close (&sconn, pCuuuconf, qCudialer, FALSE);
qCuconn = NULL;
(void) fconn_unlock (&sconn);
uconn_free (&sconn);
/* Loop around and try another alternate. */
flooped = TRUE;
continue;
}
if (tdialer == DIALERFOUND_FALSE)
qdialer = NULL;
else
qdialer = &sdialer;
}
else
{
/* If no system or phone number was specified, we connect
directly to the modem. We only permit this if the user
has access to the port, since it permits various
shenanigans such as reprogramming the automatic
callbacks. */
if (! fsysdep_port_access (sconn.qport))
ulog (LOG_FATAL, "Access to port denied");
qdialer = NULL;
if (! fconn_carrier (&sconn, FALSE))
ulog (LOG_FATAL, "Can't turn off carrier");
}
break;
}
qCudialer = qdialer;
if (FGOT_SIGNAL ())
ucuabort ();
/* Here we have connected, and can start the main cu protocol. The
program spends most of its time in system dependent code, and
only comes out when a special command is received from the
terminal. */
printf ("%s\n", ZCONNMSG);
fCuconnprinted = TRUE;
if (! fsysdep_terminal_raw (fCulocalecho))
ucuabort ();
fCurestore_terminal = TRUE;
if (! fsysdep_cu_init (&sconn))
ucuabort ();
fCustarted = TRUE;
while (fsysdep_cu (&sconn, &bcmd, zlocalname))
if (! fcudo_cmd (puuconf, &sconn, bcmd))
break;
fCustarted = FALSE;
if (! fsysdep_cu_finish ())
ucuabort ();
fCurestore_terminal = FALSE;
(void) fsysdep_terminal_restore ();
(void) fconn_close (&sconn, puuconf, qdialer, TRUE);
(void) fconn_unlock (&sconn);
uconn_free (&sconn);
if (fCuconnprinted)
printf ("\n%s\n", ZDISMSG);
ulog_close ();
usysdep_exit (TRUE);
/* Avoid errors about not returning a value. */
return 0;
}
/* Print a usage message and die. */
static void
ucuusage (void)
{
fprintf (stderr, "Usage: %s [options] [system or phone-number]\n",
zProgram);
fprintf (stderr, "Use %s --help for help\n", zProgram);
exit (EXIT_FAILURE);
}
/* Print a help message. */
static void
ucuhelp (void)
{
printf ("Taylor UUCP %s, copyright (C) 1991, 92, 93, 94, 1995, 2002 Ian Lance Taylor\n",
VERSION);
printf ("Usage: %s [options] [system or phone-number]\n", zProgram);
printf (" -a,-p,--port port: Use named port\n");
printf (" -l,--line line: Use named device (e.g. tty0)\n");
printf (" -s,--speed,--baud speed, -#: Use given speed\n");
printf (" -c,--phone phone: Phone number to call\n");
printf (" -z,--system system: System to call\n");
printf (" -e: Set even parity\n");
printf (" -o: Set odd parity\n");
printf (" --parity={odd,even}: Set parity\n");
printf (" -E,--escape char: Set escape character\n");
printf (" -h,--halfduplex: Echo locally\n");
printf (" --nostop: Turn off XON/XOFF handling\n");
printf (" -t,--mapcr: Map carriage return to carriage return/linefeed\n");
printf (" -n,--prompt: Prompt for phone number\n");
printf (" -d: Set maximum debugging level\n");
printf (" -x,--debug debug: Set debugging type\n");
#if HAVE_TAYLOR_CONFIG
printf (" -I,--config file: Set configuration file to use\n");
#endif /* HAVE_TAYLOR_CONFIG */
printf (" -v,--version: Print version and exit\n");
printf (" --help: Print help and exit\n");
printf ("Report bugs to [email protected]\n");
}
/* This function is called when a fatal error occurs. */
static void
ucuabort (void)
{
if (fCustarted)
{
fCustarted = FALSE;
(void) fsysdep_cu_finish ();
}
if (fCurestore_terminal)
{
fCurestore_terminal = FALSE;
(void) fsysdep_terminal_restore ();
}
if (qCuconn != NULL)
{
struct sconnection *qconn;
if (fCuclose_conn)
{
fCuclose_conn = FALSE;
(void) fconn_close (qCuconn, pCuuuconf, qCudialer, FALSE);
}
qconn = qCuconn;
qCuconn = NULL;
(void) fconn_unlock (qconn);
uconn_free (qconn);
}
ulog_close ();
if (fCuconnprinted)
printf ("\n%s\n", ZDISMSG);
usysdep_exit (FALSE);
}
/* This variable is just used to communicate between uculog_start and
uculog_end. */
static boolean fCulog_restore;
/* This function is called by ulog before it output anything. We use
it to restore the terminal, if necessary. ulog is only called for
errors or debugging in cu, so it's not too costly to do this. If
we didn't do it, then at least on Unix each line would leave the
cursor in the same column rather than wrapping back to the start,
since CRMOD will not be on. */
static void
uculog_start (void)
{
if (! fCurestore_terminal)
fCulog_restore = FALSE;
else
{
fCulog_restore = TRUE;
fCurestore_terminal = FALSE;
if (! fsysdep_terminal_restore ())
ucuabort ();
}
}
/* This function is called by ulog after everything is output. It
sets the terminal back, if necessary. */
static void
uculog_end (void)
{
if (fCulog_restore)
{
if (! fsysdep_terminal_raw (fCulocalecho))
ucuabort ();
fCurestore_terminal = TRUE;
}
}
/* Check to see if this port has the desired line, to handle the -l
option. If it does, or if no line was specified, set up a
connection and lock it. */
static int
icuport_lock (struct uuconf_port *qport, pointer pinfo)
{
struct sconninfo *q = (struct sconninfo *) pinfo;
if (q->zline != NULL
&& ! fsysdep_port_is_line (qport, q->zline))
return UUCONF_NOT_FOUND;
q->fmatched = TRUE;
if (! fconn_init (qport, q->qconn, UUCONF_PORTTYPE_UNKNOWN))
return UUCONF_NOT_FOUND;
else if (! fconn_lock (q->qconn, FALSE, q->fdirect))
{
uconn_free (q->qconn);
return UUCONF_NOT_FOUND;
}
else
{
qCuconn = q->qconn;
q->flocked = TRUE;
return UUCONF_SUCCESS;
}
}
/* Execute a cu escape command. Return TRUE if the connection should
continue, or FALSE if the connection should be terminated. */
static boolean
fcudo_cmd (pointer puuconf, struct sconnection *qconn, int bcmd)
{
char *zline;
char *z;
char abescape[5];
boolean fret;
size_t clen;
char abbuf[100];
/* Some commands take a string up to the next newline character. */
switch (bcmd)
{
default:
zline = NULL;
break;
case '!':
case '$':
case '|':
case '+':
case '%':
case 'c':
case '>':
case '<':
case 'p':
case 't':
case 's':
{
zline = zsysdep_terminal_line ((const char *) NULL);
if (zline == NULL)
ucuabort ();
zline[strcspn (zline, "\n")] = '\0';
}
break;
}
switch (bcmd)
{