File: slrn.c

package info (click to toggle)
slrn 1.0.3+dfsg-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,412 kB
  • sloc: ansic: 39,852; sh: 3,777; perl: 467; makefile: 229; sed: 24
file content (1772 lines) | stat: -rw-r--r-- 39,119 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
/* -*- mode: C; mode: fold -*- */
/*
 This file is part of SLRN.

 Copyright (c) 1994, 1999, 2007-2016 John E. Davis <jed@jedsoft.org>
 Copyright (c) 2001-2006 Thomas Schultz <tststs@gmx.de>

 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.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "slrnfeat.h"
/*{{{ Include files */

#include <stdio.h>
#include <signal.h>
#include <string.h>

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif

#ifdef VMS
# include "vms.h"
#else
# if !defined(sun) && !defined(IBMPC_SYSTEM)
#  include <sys/ioctl.h>
# endif
# ifdef HAVE_TERMIOS_H
#  include <termios.h>
# endif
# ifdef SYSV
#  include <sys/termio.h>
#  include <sys/stream.h>
#  include <sys/ptem.h>
#  include <sys/tty.h>
# endif
# ifndef __os2__
#  include <sys/types.h>
#  include <sys/stat.h>
# endif
# ifdef __MINGW32__
#  include <process.h>
# endif
#endif /* !VMS */

#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif

#include <slang.h>
#include "jdmacros.h"

#include <errno.h>

#include "common.h"
#include "util.h"
#include "server.h"
#include "slrn.h"
#include "group.h"
#include "misc.h"
#include "startup.h"
#include "art.h"
#include "score.h"
#include "snprintf.h"
#include "charset.h"
#include "post.h"
#include "xover.h"
#include "mime.h"
#include "hooks.h"
#include "strutil.h"

#if SLRN_USE_SLTCP
# include "sltcp.h"
#endif

#if SLRN_HAS_GROUPLENS
# include "grplens.h"
#endif
#include "interp.h"
#ifdef __os2__
# define INCL_VIO
# include <os2.h>
#endif

#if defined(__NT__) || defined(__WIN32__)
# include <windows.h>
#endif

/*}}}*/

/*{{{ Global Variables */

#if SLANG_VERSION >= 20000
int Slrn_UTF8_Mode = 0;
#endif

int Slrn_TT_Initialized = 0;

/* If -1, force mouse.  If 1 the mouse will be used on in XTerm.  If 0,
 * do not use it.
 */
int Slrn_Use_Mouse;

/* Find out whether there was a warning at startup. Used for option -w0 */
int Slrn_Saw_Warning = 0;

/* We do not (yet) offer a batch mode; however, I consider it an interesting
 * idea and might implement it after version 1.0.*/
int Slrn_Batch;
int Slrn_Suspension_Ok;
int Slrn_Simulate_Graphic_Chars = 0;

char *Slrn_Newsrc_File = NULL;
Slrn_Mode_Type *Slrn_Current_Mode;

int Slrn_Default_Server_Obj = SLRN_DEFAULT_SERVER_OBJ;
int Slrn_Default_Post_Obj = SLRN_DEFAULT_POST_OBJ;

FILE *Slrn_Debug_Fp = NULL;

/* You need to call slrn_init_graphic_chars before using these */
SLwchar_Type Graphic_LTee_Char;
SLwchar_Type Graphic_UTee_Char;
SLwchar_Type Graphic_LLCorn_Char;
SLwchar_Type Graphic_HLine_Char;
SLwchar_Type Graphic_VLine_Char;
SLwchar_Type Graphic_ULCorn_Char;

int Graphic_Chars_Mode = ALT_CHAR_SET_MODE;

/*}}}*/
/*{{{ Static Variables */

static int Can_Suspend;
static volatile int Want_Suspension;
static volatile int Want_Window_Size_Change;
static void perform_suspend (int);
static int Current_Mouse_Mode;
static int Ran_Startup_Hook;

/*}}}*/
/*{{{ Static Function Declarations */

#if defined(SIGSTOP) && defined(REAL_UNIX_SYSTEM)
static int suspend_display_mode (int);
static int resume_display_mode (int, int, int);
#endif
static void init_suspend_signals (int);
static void slrn_hangup (int);
static void run_winch_functions (int, int);
/*}}}*/

/*{{{ Newsrc Locking Routines */

static void test_lock( char *file ) /*{{{*/
{
   int pid;
   FILE *fp;

   if ((fp = fopen (file, "r")) != NULL)
     {
	if (1 == fscanf (fp, "%d", &pid) )
	  {
	     if ((pid > 0)
#if !defined(IBMPC_SYSTEM)
		 && (0 == kill (pid, 0))
#endif
		 )
	       {
#if defined(IBMPC_SYSTEM)
		  slrn_exit_error (_("\
slrn: pid %d is locking the newsrc file. If you're not running another\n\
      copy of slrn, delete the file %s"),
				   pid, file);
#else
		  slrn_exit_error (_("slrn: pid %d is locking the newsrc file."), pid);
#endif
	       }
	  }
	slrn_fclose (fp);
     }
}

/*}}}*/

static int make_lock( char *file ) /*{{{*/
{
   int pid;
   FILE *fp;

#ifdef VMS
   fp = fopen (file, "w", "fop=cif");
#else
   fp = fopen (file, "w");
#endif

   if (fp == NULL) return -1;

   pid = getpid ();
   if (EOF == fprintf (fp, "%d", pid))
     {
	slrn_fclose (fp);
	return -1;
     }
   return slrn_fclose (fp);
}

/*}}}*/

static void lock_file (int how) /*{{{*/
{
   char name[SLRN_MAX_PATH_LEN];
   static int not_ok_to_unlock;
#if SLRN_HAS_RNLOCK
   int rnlock = 0;
   char file_rn[SLRN_MAX_PATH_LEN];
#endif

   if (Slrn_Newsrc_File == NULL) return;
   if (not_ok_to_unlock) return;

   not_ok_to_unlock = 1;

#ifdef SLRN_USE_OS2_FAT
   slrn_os2_make_fat (name, sizeof (name), Slrn_Newsrc_File, ".lck");
#else
   slrn_snprintf (name, sizeof (name), "%s-lock", Slrn_Newsrc_File);
#endif

#if SLRN_HAS_RNLOCK
   slrn_make_home_filename (".newsrc", file_rn, sizeof (file_rn));
   if (0 == strcmp (file_rn, Slrn_Newsrc_File))
     {
	rnlock = 1;
	slrn_make_home_filename (".rnlock", file_rn, sizeof (file_rn));
     }
#endif

   if (how == 1)
     {
	test_lock (name);
#if SLRN_HAS_RNLOCK
	if (rnlock) test_lock (file_rn);
#endif
	if (-1 == make_lock (name))
	  {
	     slrn_exit_error (_("Unable to create lock file %s."), name);
	  }

#if SLRN_HAS_RNLOCK
	if (rnlock && (-1 == make_lock (file_rn)))
	  {
	     slrn_delete_file (name); /* delete the "normal" lock file */
	     slrn_exit_error (_("Unable to create lock file %s."), file_rn);
	  }
#endif
     }
   else
     {
	if (-1 == slrn_delete_file (name))
	  {
	     /* slrn_exit_error ("Unable to remove lockfile %s.", file); */
	  }
#if SLRN_HAS_RNLOCK
        if (rnlock && -1 == slrn_delete_file (file_rn))
	  {
	     /* slrn_exit_error ("Unable to remove lockfile %s.", file_rn); */
	  }
#endif
     }
   not_ok_to_unlock = 0;
}

/*}}}*/

/*}}}*/

/*{{{ Signal Related Functions */

/*{{{ Low-Level signal-related utility functions */

static void init_like_signals (int argc, int *argv, /*{{{*/
			       void (*f0)(int),
			       void (*f1)(int),
			       int state)
{
#ifdef HAVE_SIGACTION
   struct sigaction sa;
#endif
   int i;

   if (state == 0)
     {
	for (i = 0; i < argc; i++)
	  SLsignal_intr (argv[i], f0);
	return;
     }

   for (i = 0; i < argc; i++)
     {
	int sig = argv[i];
	SLsignal_intr (sig, f1);

#if defined(SLRN_POSIX_SIGNALS)
	if (-1 != sigaction (sig, NULL, &sa))
	  {
	     int j;
	     for (j = 0; j < argc; j++)
	       {
		  if (j != i) sigaddset (&sa.sa_mask, argv[j]);
	       }

	     (void) sigaction (sig, &sa, NULL);
	  }
#endif
     }
}

/*}}}*/

/*}}}*/

/*{{{ Suspension signals */

#ifdef REAL_UNIX_SYSTEM
#define SUSPEND_STACK_SIZE 512
static char Suspend_Stack [SUSPEND_STACK_SIZE];
static unsigned int Suspension_Stack_Depth = 0;
static int Ok_To_Suspend = 0;
#endif

static int Suspend_Sigtstp_Suspension = 0;

void slrn_push_suspension (int ok) /*{{{*/
{
#ifdef REAL_UNIX_SYSTEM
   if (Suspension_Stack_Depth < SUSPEND_STACK_SIZE)
     {
	Suspend_Stack [Suspension_Stack_Depth] = Ok_To_Suspend;
     }
   else ok = 0;

   Suspension_Stack_Depth++;

   (void) slrn_handle_interrupts ();

   Ok_To_Suspend = ok;
#else
   (void) ok;
#endif
}
/*}}}*/
void slrn_pop_suspension (void) /*{{{*/
{
#ifdef REAL_UNIX_SYSTEM

   if (Suspension_Stack_Depth == 0)
     {
	slrn_error (_("pop_suspension: underflow!"));
	return;
     }

   Suspension_Stack_Depth--;

   if (Suspension_Stack_Depth < SUSPEND_STACK_SIZE)
     {
	Ok_To_Suspend = Suspend_Stack [Suspension_Stack_Depth];
     }
   else Ok_To_Suspend = 0;

   (void) slrn_handle_interrupts ();
#endif
}
/*}}}*/

/* This function is called by the SIGTSTP handler.  Since it operates
 * in an asynchronous fashion, care must be exercised to control when that
 * can happen.  This is accomplished via the push/pop_suspension functions.
 */
static void sig_suspend (int sig)
{
#ifdef REAL_UNIX_SYSTEM
   sig = errno;

   if (Ok_To_Suspend
       && (0 == Suspend_Sigtstp_Suspension))
     {
	perform_suspend (1);
     }
   else Want_Suspension = 1;

   init_suspend_signals (1);
   errno = sig;
#else
   (void) sig;
#endif
}

static void init_suspend_signals (int state) /*{{{*/
{
   int argv[2];
   int argc = 0;

   if (Can_Suspend == 0)
     return;

#ifdef SIGTSTP
   argv[argc++] = SIGTSTP;
#endif

#ifdef SIGTTIN
   argv[argc++] = SIGTTIN;
#endif

   init_like_signals (argc, argv, SIG_DFL, sig_suspend, state);
}

/*}}}*/

static void perform_suspend (int smg_suspend_flag) /*{{{*/
{
#if !defined(SIGSTOP) || !defined(REAL_UNIX_SYSTEM)
   (void) smg_suspend_flag;
   slrn_error (_("Not implemented."));
   Want_Suspension = 0;
#else

   int init;
   int mouse_mode = Current_Mouse_Mode;

# ifdef SLRN_POSIX_SIGNALS
   sigset_t mask;

   Want_Suspension = 0;
   if (Can_Suspend == 0)
     {
	slrn_error (_("Suspension not allowed by shell."));
	return;
     }

   sigemptyset (&mask);
   sigaddset (&mask, SIGTSTP);

   /* This function resets SIGTSTP to default */
   init = suspend_display_mode (smg_suspend_flag);

   kill (getpid (), SIGTSTP);

   /* If SIGTSTP is pending, it will be delivered now.  That's ok. */
   sigprocmask (SIG_UNBLOCK, &mask, NULL);
# else

   Want_Suspension = 0;
   if (Can_Suspend == 0)
     {
	slrn_error (_("Suspension not allowed by shell."));
	return;
     }

   init = suspend_display_mode (smg_suspend_flag);
   kill(getpid(),SIGSTOP);
# endif

   resume_display_mode (smg_suspend_flag, init, mouse_mode);
#endif				       /* !defined(SIGSTOP) || !defined(REAL_UNIX_SYSTEM) */
}

/*}}}*/

void slrn_suspend_cmd (void)
{
   perform_suspend (0);
}

static void check_for_suspension (void)
{
#ifdef SIGTSTP
   void (*f)(int);

   f = SLsignal (SIGTSTP, SIG_DFL);
   (void) SLsignal (SIGTSTP, f);

#if 0
   Can_Suspend = (f == SIG_DFL);
#else
   Can_Suspend = (f != SIG_IGN);
#endif

#else
   Can_Suspend = 0;
#endif
}

/*}}}*/

/*{{{ Hangup Signals */

void slrn_init_hangup_signals (int state) /*{{{*/
{
   int argv[3];
   int argc = 0;

#ifdef SIGHUP
   argv[argc++] = SIGHUP;
#endif
#ifdef SIGTERM
   argv[argc++] = SIGTERM;
#endif
#ifdef SIGQUIT
   argv[argc++] = SIGQUIT;
#endif

   init_like_signals (argc, argv, SIG_IGN, slrn_hangup, state);
}

/*}}}*/

/*}}}*/

#ifdef SIGWINCH
static void sig_winch_handler (int sig)
{
   if (Slrn_Batch) return;
   sig = errno;
   Want_Window_Size_Change = 1;
   SLsignal_intr (SIGWINCH, sig_winch_handler);
   errno = sig;
}
#endif

static void slrn_set_screen_size (int sig) /*{{{*/
{
   int old_r, old_c;

   old_r = SLtt_Screen_Rows;
   old_c = SLtt_Screen_Cols;

   SLtt_get_screen_size ();

   Slrn_Full_Screen_Update = 1;
   Want_Window_Size_Change = 0;

   run_winch_functions (old_r, old_c);

   if (sig)
     {
#if SLANG_VERSION < 10306
	SLsmg_reset_smg ();
	SLsmg_init_smg ();
#else
	SLsmg_reinit_smg ();
#endif
	slrn_redraw ();
     }
}

/*}}}*/

static void init_display_signals (int mode) /*{{{*/
{
   init_suspend_signals (mode);

   if (mode)
     {
	SLang_set_abort_signal (NULL);
#ifdef SIGPIPE
	SLsignal (SIGPIPE, SIG_IGN);
#endif
#ifdef SIGTTOU
	/* Allow background writes */
	SLsignal (SIGTTOU, SIG_IGN);
#endif
#ifdef SIGWINCH
	SLsignal_intr (SIGWINCH, sig_winch_handler);
#endif
     }
   else
     {
#ifdef SIGWINCH
	/* SLsignal_intr (SIGWINCH, SIG_DFL); */
#endif
     }
}

/*}}}*/

int slrn_handle_interrupts (void)
{
   if (Want_Suspension)
     {
	slrn_suspend_cmd ();
     }

   if (Want_Window_Size_Change)
     {
	slrn_set_screen_size (1);
     }

   return 0;
}

/*}}}*/

/*{{{ Screen Management and Terminal Init/Reset Functions */

int Slrn_Use_Flow_Control = 0;
static int init_tty (void) /*{{{*/
{
   if (Slrn_TT_Initialized & SLRN_TTY_INIT)
     {
	return 0;
     }

   if (Slrn_TT_Initialized == 0)
     init_display_signals (1);

   if (-1 == SLang_init_tty (7, !Slrn_Use_Flow_Control, 0))
     slrn_exit_error (_("Unable to initialize the tty"));

#ifdef REAL_UNIX_SYSTEM
   SLang_getkey_intr_hook = slrn_handle_interrupts;
#endif

#ifdef REAL_UNIX_SYSTEM
   if (Can_Suspend)
     SLtty_set_suspend_state (1);
#endif

   Slrn_TT_Initialized |= SLRN_TTY_INIT;

   return 0;
}

/*}}}*/

static int reset_tty (void) /*{{{*/
{
   if (0 == (Slrn_TT_Initialized & SLRN_TTY_INIT))
     {
	return 0;
     }

   SLang_reset_tty ();
   Slrn_TT_Initialized &= ~SLRN_TTY_INIT;

   if (Slrn_TT_Initialized == 0)
     init_display_signals (0);

   return 0;
}

/*}}}*/

static int init_smg (int use_resume, int mouse_mode) /*{{{*/
{
   if (Slrn_TT_Initialized & SLRN_SMG_INIT)
     return 0;

   slrn_enable_mouse (mouse_mode);

   if (Slrn_TT_Initialized == 0)
     init_display_signals (1);

   if (use_resume)
     {
	SLsmg_resume_smg ();
	Slrn_TT_Initialized |= SLRN_SMG_INIT;
     }
   else
     {
	slrn_set_screen_size (0);
	SLsmg_init_smg ();
	Slrn_TT_Initialized |= SLRN_SMG_INIT;

	/* We do not want the -> overlay cursor to affect the scroll. */
#if !defined(IBMPC_SYSTEM)
	SLsmg_Scroll_Hash_Border = 5;
#endif
	slrn_redraw ();
     }

   return 0;
}

/*}}}*/

static int reset_smg (int smg_suspend_flag) /*{{{*/
{
   if (0 == (Slrn_TT_Initialized & SLRN_SMG_INIT))
     return 0;

   slrn_enable_mouse (0);

   if (smg_suspend_flag)
     SLsmg_suspend_smg ();
   else
     {
	SLsmg_gotorc (SLtt_Screen_Rows - 1, 0);
	slrn_smg_refresh ();
	SLsmg_reset_smg ();
     }

   /* SLsignal_intr (SIGWINCH, SIG_DFL); */

   Slrn_TT_Initialized &= ~SLRN_SMG_INIT;

   if (Slrn_TT_Initialized == 0)
     init_display_signals (0);

   return 0;
}

/*}}}*/

#if defined(SIGSTOP) && defined(REAL_UNIX_SYSTEM)
static int suspend_display_mode (int smg_suspend_flag) /*{{{*/
{
   int mode = Slrn_TT_Initialized;

   SLsig_block_signals ();

   reset_smg (smg_suspend_flag);
   reset_tty ();

   SLsig_unblock_signals ();

   return mode;
}

/*}}}*/

static int resume_display_mode (int smg_suspend_flag, int mode, int mouse_mode) /*{{{*/
{
   SLsig_block_signals ();

   if (mode & SLRN_TTY_INIT)
     init_tty ();

   if (mode & SLRN_SMG_INIT)
     init_smg (smg_suspend_flag, mouse_mode);

   SLsig_unblock_signals ();
   return 0;
}

/*}}}*/
#endif /* SIGSTOP && REAL_UNIX_SYSTEM*/

void slrn_set_display_state (int state) /*{{{*/
{
   if (Slrn_Batch) return;

   SLsig_block_signals ();

   if (state & SLRN_TTY_INIT)
     init_tty ();
   else
     reset_tty ();

   if (state & SLRN_SMG_INIT)
     init_smg (0, 1);
   else
     reset_smg (0);

   SLsig_unblock_signals ();
}

/*}}}*/

void slrn_enable_mouse (int mode) /*{{{*/
{
   if (Current_Mouse_Mode == mode)
     return;

   if (Slrn_Use_Mouse)
     {
	if (-1 == SLtt_set_mouse_mode (mode, (Slrn_Use_Mouse < 0)))
	  Slrn_Use_Mouse = 0;
     }
   Current_Mouse_Mode = mode;
}

/*}}}*/

void slrn_init_graphic_chars (void) /*{{{*/
{
#ifndef IBMPC_SYSTEM
   if (SLtt_Has_Alt_Charset == 0)
     Slrn_Simulate_Graphic_Chars = 1;
#endif

   if (Slrn_Simulate_Graphic_Chars)
     {
	Graphic_LTee_Char = '+';
	Graphic_UTee_Char = '+';
	Graphic_LLCorn_Char = '`';
	Graphic_HLine_Char = '-';
	Graphic_VLine_Char = '|';
	Graphic_ULCorn_Char = '/';

	Graphic_Chars_Mode = SIMULATED_CHAR_SET_MODE;
     }
   else
     {
	Graphic_Chars_Mode = ALT_CHAR_SET_MODE;
	Graphic_LTee_Char = SLSMG_LTEE_CHAR;
	Graphic_UTee_Char = SLSMG_UTEE_CHAR;
	Graphic_LLCorn_Char = SLSMG_LLCORN_CHAR;
	Graphic_HLine_Char = SLSMG_HLINE_CHAR;
	Graphic_VLine_Char = SLSMG_VLINE_CHAR;
	Graphic_ULCorn_Char = SLSMG_ULCORN_CHAR;

	Graphic_Chars_Mode = ALT_CHAR_SET_MODE;
     }
}
/*}}}*/
/*}}}*/

static void perform_cleanup (void)
{
   slrn_set_display_state (0);

#if SLRN_HAS_GROUPLENS
   slrn_close_grouplens ();
#endif

   if (Slrn_Server_Obj != NULL)
     Slrn_Server_Obj->sv_close ();

   if (Slrn_Debug_Fp != NULL)
     {
	(void) fclose (Slrn_Debug_Fp);
	Slrn_Debug_Fp = NULL;
     }

#if SLRN_HAS_GROUPLENS
   slrn_close_grouplens ();
#endif

   if (Ran_Startup_Hook)
     (void) slrn_run_hooks (HOOK_QUIT, 0);
   (void) slrn_reset_slang ();

#if SLRN_USE_SLTCP && SLRN_HAS_NNTP_SUPPORT
   (void) sltcp_close_sltcp ();
#endif
   lock_file (0);
}

void slrn_quit (int retcode) /*{{{*/
{
   perform_cleanup ();
   if (retcode) fprintf (stderr, _("slrn: quiting on signal %d.\n"), retcode);
   exit (retcode);
}

/*}}}*/

void slrn_va_exit_error (char *fmt, va_list ap)
{
   static int trying_to_exit;

   if (trying_to_exit == 0)
     {
	trying_to_exit = 1;
	slrn_set_display_state (0);

	if (fmt != NULL)
	  {
	     fputs (_("slrn fatal error:"), stderr);
#ifdef IBMPC_SYSTEM
	     fputc ('\r', stderr);
#endif
	     fputc ('\n', stderr);
	     vfprintf (stderr, fmt, ap);
	  }
	if (Slrn_Groups_Dirty) slrn_write_newsrc (0);
	perform_cleanup ();
     }

   putc ('\n', stderr);
   exit (1);
}

void slrn_exit_error (char *fmt, ...) /*{{{*/
{
   va_list ap;
   va_start (ap, fmt);
   slrn_va_exit_error (fmt, ap);
   va_end(ap);
}

/*}}}*/

void slrn_error (char *fmt, ...) /*{{{*/
{
   va_list ap;

   va_start(ap, fmt);
   slrn_verror (fmt, ap);
   va_end (ap);
}

/*}}}*/

static void usage (char *extra, int exit_status) /*{{{*/
{
   printf (_("\
Usage: slrn [--inews] [--nntp ...] [--spool] OPTIONS\n\
-a              Use active file for getting new news.\n\
-f newsrc-file  Name of the newsrc file to use.\n\
-C[-]           [Do not] use colors.\n\
-Dname          Add 'name' to list of predefined preprocessing tokens.\n\
-d              Get new text descriptions of each group from server.\n\
                 Note: This may take a LONG time to retrieve this information.\n\
                 The resulting file can be several hundred Kilobytes!\n\
-i init-file    Name of initialization file to use (default: %s)\n\
-k              Do not process score file.\n\
-k0             Process score file but inhibit expensive scores.\n\
-m              Force XTerm mouse reporting\n\
-n              Do not check for new groups.  This usually results in\n\
                 a faster startup.\n\
-w              Wait for key before switching to full screen mode\n\
-w0             Wait for key (only when warnings / errors occur)\n\
--create        Create a newsrc file by getting list of groups from server.\n\
--debug FILE    Write debugging information to FILE\n\
--help          Print this usage.\n\
--kill-log FILE Keep a log of all killed articles in FILE\n\
--show-config   Print configuration\n\
--version       Show version and supported features\n\
\n\
NNTP mode has additional options; use \"slrn --nntp --help\" to display them.\n\
"), SLRN_USER_SLRNRC_FILENAME);

   if (extra != NULL)
     {
	printf ("\n%s\n", extra);
     }
   exit (exit_status);
}

/*}}}*/

static int parse_object_args (char *obj, char **argv, int argc) /*{{{*/
{
   int num_parsed;
   int zero_ok = 1;

   if (obj == NULL)
     {
	zero_ok = 0;
	obj = slrn_map_object_id_to_name (0, SLRN_DEFAULT_SERVER_OBJ);
     }

   num_parsed = slrn_parse_object_args (obj, argv, argc);
   if (num_parsed < 0)
     {
	if (num_parsed == -1)
	  slrn_exit_error (_("%s is not a supported option."), *argv);
	else
	  slrn_exit_error (_("%s is not supported."), obj);
     }
   if ((num_parsed == 0) && (zero_ok == 0))
     usage (NULL, 1);

   return num_parsed;
}

/*}}}*/

static void read_score_file (void)
{
   char file[SLRN_MAX_PATH_LEN];

   if (Slrn_Score_File == NULL)
     return;

   slrn_make_home_filename (Slrn_Score_File, file, sizeof (file));

   if (1 != slrn_file_exists (file))
     {
	slrn_message_now (_("*** Warning: Score file %s does not exist"), file);
	Slrn_Saw_Warning = 1;
	return;
     }

   if (-1 == slrn_read_score_file (file))
     {
	slrn_exit_error (_("Error processing score file %s."), file);
     }
}

static int main_init_and_parse_args (int argc, char **argv) /*{{{*/
{
   char *hlp_file;
   unsigned int i;
   int create_flag = 0;
   int no_new_groups = 0;
   int no_score_file = 0;
   int use_color = 0;
   int use_mouse = 0;
   int dsc_flag = 0;
   int use_active = 0;
   int wait_for_key = 0;
   FILE *fp;
   char file [SLRN_MAX_PATH_LEN];
   char *init_file = NULL;
   char *kill_logfile = NULL;
   int print_config = 0;

#if defined(HAVE_SETLOCALE) && defined(LC_ALL)
   (void) setlocale(LC_ALL, "");
#endif
#ifdef ENABLE_NLS
   bindtextdomain(NLS_PACKAGE_NAME, NLS_LOCALEDIR);
   textdomain (NLS_PACKAGE_NAME);
#endif

   check_for_suspension ();
#ifdef __unix__
   (void) umask (077);
#endif

#if 0
   if (NULL != getenv ("AUTOSUBSCRIBE"))
     Slrn_Unsubscribe_New_Groups = 0;
   if (NULL != getenv ("AUTOUNSUBSCRIBE"))
     Slrn_Unsubscribe_New_Groups = 1;
#endif

   for (i = 1; i < (unsigned int) argc; i++)
     {
	char *argv_i = argv[i];

	if ((argv_i[0] == '-') && (argv_i[1] == '-'))
	  {
	     int status;

	     argv_i += 2;

	     status = slrn_parse_object_args (argv_i, NULL, 0);
	     if (status != -1)
	       i += parse_object_args (argv_i, argv + (i + 1), argc - (i + 1));
#if 0
	     else if (!strcmp ("batch", argv_i)) Slrn_Batch = 1;
#endif
	     else if (!strcmp ("create", argv_i)) create_flag = 1;
	     else if (!strcmp ("version", argv_i))
	       {
		  slrn_show_version (stdout);
		  exit (0);
	       }
	     else if (!strcmp ("show-config", argv_i))
	       print_config = 1;
	     else if ((!strcmp ("kill-log", argv_i)) &&
		      (i + 1 < (unsigned int) argc))
	       kill_logfile = argv[++i];
             else if ((!strcmp ("debug", argv_i)) &&
                      (i + 1 < (unsigned int) argc))
               {
		  if (Slrn_Debug_Fp != NULL)
		    fclose (Slrn_Debug_Fp);
                  Slrn_Debug_Fp = fopen (argv[++i], "w");
                  if (Slrn_Debug_Fp == NULL)
                    slrn_exit_error (_("Unable to open %s for debugging."), argv[i]);
                  setbuf (Slrn_Debug_Fp, (char *) NULL);
               }
	     else if (!strcmp ("help", argv_i))
	       usage (NULL, 0);
	     else usage (NULL, 1);
	  }
	else if (!strcmp ("-create", argv_i)) create_flag = 1;
	else if (!strcmp ("-C", argv_i)) use_color = 1;
	else if (!strcmp ("-C-", argv_i)) use_color = -1;
	else if (!strcmp ("-a", argv_i)) use_active = 1;
	else if (!strcmp ("-n", argv_i)) no_new_groups = 1;
	else if (!strcmp ("-d", argv_i)) dsc_flag = 1;
	else if (!strcmp ("-m", argv_i)) use_mouse = 1;
	else if (!strcmp ("-k", argv_i)) no_score_file = 1;
	else if (!strcmp ("-k0", argv_i))
	  Slrn_Perform_Scoring &= ~SLRN_EXPENSIVE_SCORING;
	else if (!strcmp ("-w", argv_i))
	  wait_for_key = 1;
	else if (!strcmp ("-w0", argv_i))
	  wait_for_key = 2;
	else if (!strncmp ("-D", argv_i, 2) && (argv_i[2] != 0))
	  {
	     if (-1 == SLdefine_for_ifdef (argv_i + 2))
	       {
		  slrn_exit_error (_("Unable to add preprocessor name %s."),
				   argv_i + 2);
	       }
	  }
	else if (i + 1 < (unsigned int) argc)
	  {
	     if (!strcmp ("-f", argv_i)) Slrn_Newsrc_File = argv[++i];
	     else if (!strcmp ("-i", argv_i)) init_file = argv[++i];
	     else
	       {
		  i += parse_object_args (NULL, argv + i, argc - i);
		  i -= 1;
	       }
	  }
	else
	  {
	     i += parse_object_args (NULL, argv + i, argc - i);
	     i -= 1;
	  }
     }

   fprintf (stdout, "slrn %s\n", Slrn_Version_String);

   if (dsc_flag && create_flag)
     {
	usage (_("The -d and --create flags must not be specified together."), 1);
     }

   if (Slrn_Batch == 0)
     {
	Slrn_UTF8_Mode = SLutf8_enable (-1);
	SLtt_get_terminfo ();
	if (use_color == 1) SLtt_Use_Ansi_Colors = 1;
	else if (use_color == -1) SLtt_Use_Ansi_Colors = 0;
     }

#if SLRN_USE_SLTCP && SLRN_HAS_NNTP_SUPPORT
   SLtcp_Verror_Hook = slrn_verror;
   /* This is necessary to ensure that gethostname works on Win32 */
   if (-1 == sltcp_open_sltcp ())
     slrn_exit_error (_("Error initializing SLtcp interface"));
#endif

   if (-1 == slrn_init_slang ())
     slrn_exit_error (_("Error initializing S-Lang interpreter.\n"));

   slrn_startup_initialize ();

   /* We need to get user info first, because the request file in true offline
    * reading mode is chosen based on login name */
   slrn_get_user_info ();

   /* The next function call will also define slang preprocessing tokens
    * for the appropriate objects.  For that reason, it is called after
    * startup initialize.
    */
   if (-1 == slrn_init_objects ())
     {
	slrn_exit_error (_("Error configuring server objects."));
     }

   slrn_init_hangup_signals (1);

#ifdef VMS
   slrn_snprintf (file, sizeof (file), "%s%s", SLRN_CONF_DIR, "slrn.rc");
#else
   slrn_snprintf (file, sizeof (file), "%s/%s", SLRN_CONF_DIR, "slrn.rc");
#endif

   /* Make sure terminal is initialized before setting colors.  The
    * SLtt_get_terminfo call above fixed that.
    */
   slrn_read_startup_file (file);      /* global file for all users */

   if (init_file == NULL)
     {
	slrn_make_home_filename (SLRN_USER_SLRNRC_FILENAME, file, sizeof (file));
	init_file = file;
     }

   if ((-1 == slrn_read_startup_file (init_file)) && (init_file != file))
     {
	slrn_exit_error (_("\
Could not read specified config file %s\n"), init_file);
     }

   if (Slrn_Saw_Obsolete)
     {
	slrn_message (_("\n! Your configuration file contains obsolete commands or function names that\n"
		      "! will not be supported by future versions of this program.\n"
		      "! If you have Perl installed, you can use the script slrnrc-conv to change\n"
		      "! your configuration accordingly.  It can be found in the source distribution\n"
		      "! or retrieved from <URL:http://slrn.sourceforge.net/data/>.\n"));
	Slrn_Saw_Warning = 1;
     }

   if (Slrn_Server_Id == 0) Slrn_Server_Id = Slrn_Default_Server_Obj;
   if (Slrn_Post_Id == 0) Slrn_Post_Id = Slrn_Default_Post_Obj;
   if (no_new_groups) Slrn_Check_New_Groups = 0;

   slrn_prepare_charset();

   if (print_config)
     {
	(void) putc ('\n', stdout);
	slrn_show_version (stdout);
	slrn_print_config (stdout);
	slrn_quit (0);
     }

#ifdef SIGINT
   if (Slrn_TT_Initialized == 0)
     SLsignal_intr (SIGINT, SIG_DFL);
#endif

   if ((-1 == slrn_select_server_object (Slrn_Server_Id))
       || (-1 == slrn_select_post_object (Slrn_Post_Id)))
     {
	slrn_exit_error (_("Unable to select server/post object."));
     }

#if !defined(IBMPC_SYSTEM)
   /* Allow blink characters if in mono */
   if (SLtt_Use_Ansi_Colors == 0)
     SLtt_Blink_Mode = 1;
#endif

   /* Now that we have read in the startup file, check to see if the user
    * has a username and a usable hostname.  Without those, we are not
    * starting up.
    */
   if ((NULL == Slrn_User_Info.hostname)
       || (0 == slrn_is_fqdn (Slrn_User_Info.hostname)))
     {
	slrn_exit_error (_("\
Unable to find a valid hostname for constructing your e-mail address.\n\
You probably want to specify a hostname in your %s file.\n\
Please see the \"slrn reference manual\" for full details.\n"),
			 SLRN_USER_SLRNRC_FILENAME);
     }
   if ((NULL == Slrn_User_Info.username)
       || (0 == *Slrn_User_Info.username)
       || (NULL != slrn_strbyte (Slrn_User_Info.username, '@')))
     {
	slrn_exit_error (_("\
Unable to find your user name.  This means that a valid 'From' header line\n\
cannot be constructed.  Try setting the USER environment variable.\n"));
     }

   if ((NULL == Slrn_User_Info.posting_host)
       && Slrn_Generate_Message_Id)
     {
	slrn_stderr_strcat (_("\
***Warning: Unable to find a unique fully-qualified host name."), "\n", _("\
            slrn will not generate any Message-IDs."), "\n", _("\
            Please note that the \"hostname\" setting does not affect this;"), "\n", _("\
            see the \"slrn reference manual\" for details."), "\n", NULL);
	Slrn_Saw_Warning = 1;
     }

   if (no_score_file == 0) read_score_file ();

   if (NULL == (hlp_file = getenv ("SLRNHELP")))
     {
	hlp_file = file;
#ifdef VMS
	slrn_snprintf (file, sizeof (file), "%s%s", SLRN_CONF_DIR, "help.txt");
#else
	slrn_snprintf (file, sizeof (file), "%s/%s", SLRN_CONF_DIR, "help.txt");
#endif
     }

   slrn_parse_helpfile (hlp_file);

   if ((Slrn_Newsrc_File == NULL)
       && ((Slrn_Newsrc_File = slrn_map_file_to_host (Slrn_Server_Obj->sv_name)) == NULL))
     {
#if defined(VMS) || defined(IBMPC_SYSTEM)
	Slrn_Newsrc_File = "jnews.rc";
#else
	Slrn_Newsrc_File = ".jnewsrc";
#endif
	slrn_make_home_filename (Slrn_Newsrc_File, file, sizeof (file));
	Slrn_Newsrc_File = slrn_safe_strmalloc (file);
     }

   slrn_message_now (_("Using newsrc file %s for server %s."),
		     Slrn_Newsrc_File, Slrn_Server_Obj->sv_name);

   if (use_active) Slrn_List_Active_File = 1;
   if (use_mouse) Slrn_Use_Mouse = -1; /* -1 forces it. */
   if (use_color == 1) SLtt_Use_Ansi_Colors = 1;
   else if (use_color == -1) SLtt_Use_Ansi_Colors = 0;

   if (dsc_flag)
     {
	if (Slrn_Server_Obj->sv_initialize () != 0)
	  {
	     slrn_exit_error (_("Unable to initialize server."));
	  }
	slrn_get_group_descriptions ();
	Slrn_Server_Obj->sv_close ();
	exit (0);
     }

   if (create_flag == 0)
     {
	/* Check to see if the .newsrc file exists -- I should use the access
	 * system call but for now, do it this way.
	 */
	if (NULL == (fp = fopen (Slrn_Newsrc_File, "r")))
	  {
#if 0 /* now disabled in group.c */
	     slrn_error (_("Unable to open %s.  I will try .newsrc."), Slrn_Newsrc_File);
	     if (NULL == (fp = slrn_open_home_file (".newsrc", "r", file,
						    sizeof (file), 0)))
	       {
#endif
		  slrn_exit_error (_("\nUnable to open %s.\n\
If you want to create %s, add command line options:\n\
   -f %s --create\n"), Slrn_Newsrc_File, Slrn_Newsrc_File, Slrn_Newsrc_File);
#if 0
	       }
#endif
	  }
	slrn_fclose (fp);
     }

   /* make sure we have an entry for the server */
   slrn_add_to_server_list (Slrn_Server_Obj->sv_name, NULL, NULL, NULL);

   if (kill_logfile != NULL)
     {
	Slrn_Kill_Log_FP = fopen(kill_logfile, "a");
	if (Slrn_Kill_Log_FP == NULL)
	  slrn_error (_("Unable to open %s for logging."), kill_logfile);
     }

   lock_file (1);

   (void) slrn_run_hooks (HOOK_STARTUP, 0);
   Ran_Startup_Hook = 1;

#if SLRN_HAS_GROUPLENS
   if (Slrn_Server_Id != SLRN_SERVER_ID_NNTP)
     Slrn_Use_Group_Lens = 0;

   if (Slrn_Use_Group_Lens && (Slrn_Batch == 0))
     {
	slrn_message (_("Initializing GroupLens"));
	if (-1 == slrn_init_grouplens ())
	  {
	     fprintf (stderr, _("GroupLens disabled.\n"));
	     Slrn_Use_Group_Lens = 0;
	     Slrn_Saw_Warning = 1;
	  }
     }
#endif

   if (Slrn_Server_Obj->sv_initialize () != 0)
     {
	slrn_exit_error (_("Failed to initialize server."));
     }

   if (Slrn_Server_Obj->sv_has_xover == 1)
     {
	if (0 == (Slrn_Server_Obj->sv_has_xover = slrn_read_overview_fmt ()))
	  slrn_message (_("OVERVIEW.FMT not RFC 2980 compliant -- XOVER support disabled."));
     }

   if (Slrn_Check_New_Groups || create_flag)
     {
	slrn_get_new_groups (create_flag);
     }

   slrn_read_newsrc (create_flag);
   slrn_read_group_descriptions ();

   if (wait_for_key && ((wait_for_key == 1) || Slrn_Saw_Warning))
     {
	slrn_message (_("* Press Ctrl-C to quit, any other key to continue.\n"));
	slrn_set_display_state (SLRN_TTY_INIT);
	if ('\003' == SLang_getkey ())
	  slrn_exit_error (_("Exit on user request."));
     }
   else
     putc ('\n', stdout);

   slrn_set_display_state (SLRN_SMG_INIT | SLRN_TTY_INIT);

#if defined(__unix__) && !defined(IBMPC_SYSTEM)
   if (Slrn_Autobaud) SLtt_Baud_Rate = SLang_TT_Baud_Rate;
#endif

   return 0;
}

/*}}}*/

/*{{{ Main Loop and Key Processing Functions */
#define MAX_MODE_STACK_LEN 15
/* Allow one extra because the top one is the current mode */
static Slrn_Mode_Type *Mode_Stack [MAX_MODE_STACK_LEN + 1];
static unsigned int Mode_Stack_Depth;

void slrn_push_mode (Slrn_Mode_Type *mode)
{
   if (Mode_Stack_Depth == MAX_MODE_STACK_LEN)
     slrn_exit_error (_("Internal Error: Mode_Stack overflow"));

   Mode_Stack[Mode_Stack_Depth] = Slrn_Current_Mode;
   Mode_Stack_Depth++;

   Mode_Stack[Mode_Stack_Depth] = Slrn_Current_Mode = mode;

   Slrn_Full_Screen_Update = 1;
   if ((mode != NULL) && (mode->enter_mode_hook != NULL))
     (*mode->enter_mode_hook) ();
}

void slrn_pop_mode (void)
{
   if (Mode_Stack_Depth == 0)
     slrn_exit_error (_("Internal Error: Mode_Stack underflow"));

   Mode_Stack [Mode_Stack_Depth] = NULL;   /* null current mode */
   Mode_Stack_Depth--;
   Slrn_Current_Mode = Mode_Stack[Mode_Stack_Depth];

   Slrn_Full_Screen_Update = 1;
   if ((Slrn_Current_Mode != NULL) && (Slrn_Current_Mode->enter_mode_hook != NULL))
     (*Slrn_Current_Mode->enter_mode_hook) ();
}

void slrn_update_screen (void)
{
   Slrn_Mode_Type *mode;
   unsigned int i, imax;

   if (Slrn_Batch) return;

   slrn_push_suspension (0);
   imax = Mode_Stack_Depth + 1;	       /* include current mode */
   for (i = 0; i < imax; i++)
     {
	Slrn_Full_Screen_Update = 1;
	mode = Mode_Stack [i];
	if ((mode != NULL)
	    && (mode->redraw_fun != NULL))
	  (*mode->redraw_fun) ();
     }
   slrn_smg_refresh ();
   slrn_pop_suspension ();
}

static void run_winch_functions (int old_r, int old_c)
{
   Slrn_Mode_Type *mode;
   unsigned int i, imax;

   imax = Mode_Stack_Depth + 1;	       /* include current */
   for (i = 0; i < imax; i++)
     {
	mode = Mode_Stack [i];
	if ((mode != NULL)
	    && (mode->sigwinch_fun != NULL))
	  (*mode->sigwinch_fun) (old_r, old_c);
     }

   if (SLang_get_error() == 0)
     slrn_run_hooks (HOOK_RESIZE_SCREEN, 0);
}

static void slrn_hangup (int sig) /*{{{*/
{
   Slrn_Mode_Type *mode;
   unsigned int i;

   slrn_init_hangup_signals (0);

   i = Mode_Stack_Depth + 1;	       /* include current */
   while (i != 0)
     {
	i--;
	mode = Mode_Stack [i];
	if ((mode != NULL)
	    && (mode->hangup_fun != NULL))
	  (*mode->hangup_fun) (sig);
     }

   slrn_write_newsrc (0);
   slrn_quit (sig);
}

/*}}}*/

void slrn_call_command (char *cmd) /*{{{*/
{
   SLKeymap_Function_Type *list;

   if ((Slrn_Current_Mode == NULL)
       || (Slrn_Current_Mode->keymap == NULL))
     list = NULL;
   else
     list = Slrn_Current_Mode->keymap->functions;

   while ((list != NULL) && (list->name != NULL))
     {
	if (0 == strcmp (cmd, list->name))
	  {
	     (void) (*list->f) ();
	     /* sync the line number to avoid surprises in subsequent calls
	      * that might change the article window (not the article itself) */
	     if (Slrn_Current_Mode->mode == SLRN_ARTICLE_MODE)
	       slrn_art_sync_article (Slrn_Current_Article);
	     return;
	  }
	list++;
     }

   slrn_error (_("call: %s not in current keymap."), cmd);
}

/*}}}*/

int slrn_getkey (void)
{
   static char buf[32];
   static unsigned int buf_len;
   static int timeout_active;

   int ch;

   if (SLang_Key_TimeOut_Flag == 0)
     {
	timeout_active = 0;
	buf_len = 0;
     }
   else	if ((timeout_active || (0 == SLang_input_pending (10)))
	    && (buf_len + 2 < sizeof (buf)))
     {
	int r, c;

	buf[buf_len] = '-';
	buf[buf_len + 1] = 0;

	slrn_push_suspension (0);
	r = SLsmg_get_row (); c = SLsmg_get_column ();
	slrn_message ("%s", buf);
	SLsmg_gotorc (r, c);
	slrn_smg_refresh ();
	slrn_pop_suspension ();
	timeout_active = 1;
     }

   while (1)
     {
#if defined(REAL_UNIX_SYSTEM) && (SLANG_VERSION < 20202)
	int ttyfd = SLang_TT_Read_FD;
#endif
	int e;
	errno = 0;

	ch = SLang_getkey ();
	if (ch != SLANG_GETKEY_ERROR)
	  break;
	e = errno;

#if defined(REAL_UNIX_SYSTEM) && (SLANG_VERSION < 20202)
	if (ttyfd != SLang_TT_Read_FD)
	  continue;
#endif

	slrn_exit_error ("%s: errno=%d [%s]", _("SLang_getkey failed"),
			 e, SLerrno_strerror (e));
     }

   if (buf_len + 4 < sizeof (buf))
     {
	if (ch == 0)
	  {
	     /* Need to handle NULL character. */
	     buf[buf_len++] = '^';
	     buf[buf_len] = '@';
	  }
	else if (ch == 27)
	  {
	     buf[buf_len++] = 'E';
	     buf[buf_len++] = 'S';
	     buf[buf_len++] = 'C';
	     buf[buf_len] = ' ';
	  }
	else buf[buf_len] = (char) ch;
     }
   buf_len++;

   return ch;
}

void slrn_do_keymap_key (SLKeyMap_List_Type *map) /*{{{*/
{
   SLang_Key_Type *key;
   static SLKeyMap_List_Type *last_map;
   static SLang_Key_Type *last_key;

   Suspend_Sigtstp_Suspension = 1;
   key = SLang_do_key (map, slrn_getkey);
   Suspend_Sigtstp_Suspension = 0;

   if (Slrn_Message_Present || SLang_get_error())
     {
	if (SLang_get_error()) SLang_restart (0);
	slrn_clear_message ();
     }
   SLKeyBoard_Quit = 0;
   SLang_set_error (0);

   if ((key == NULL) || (key->type == 0))
     {
	SLtt_beep ();
	return;
     }

   if (key->type == SLKEY_F_INTRINSIC)
     {
	if ((map == last_map) && (key->f.f == (FVOID_STAR) slrn_repeat_last_key))
	  key = last_key;

	/* set now to avoid problems with recursive call */
	last_key = key;
	last_map = map;

	if (key->type == SLKEY_F_INTRINSIC)
	  {
	     (((void (*)(void))(key->f.f)) ());
	     return;
	  }
     }

   /* Otherwise we have interpreted key. */
   last_key = key;
   last_map = map;

   Slrn_Full_Screen_Update = 1;

   if ((*key->f.s == '.')
       || !SLang_execute_function (key->f.s))
     SLang_load_string(key->f.s);
}

/*}}}*/

void slrn_set_prefix_argument (int rep) /*{{{*/
{
   static int repeat;

   repeat = rep;
   Slrn_Prefix_Arg_Ptr = &repeat;
}

/*}}}*/

void slrn_digit_arg (void) /*{{{*/
{
   char buf[20];
   unsigned char key;
   unsigned int i;

   i = 0;
   buf[i++] = (char) SLang_Last_Key_Char;

   SLang_Key_TimeOut_Flag = 1;

   while (1)
     {
	buf[i] = 0;
	key = (unsigned char) slrn_getkey ();
	if ((key < '0') || (key > '9')) break;
	if (i + 1 < sizeof (buf))
	  {
	     buf[i] = (char) key;
	     i++;
	  }
     }

   SLang_Key_TimeOut_Flag = 0;
   slrn_set_prefix_argument (atoi (buf));

   SLang_ungetkey (key);
   if ((Slrn_Current_Mode != NULL)
       && (Slrn_Current_Mode->keymap != NULL))
     slrn_do_keymap_key (Slrn_Current_Mode->keymap);

   Slrn_Prefix_Arg_Ptr = NULL;
}

/*}}}*/

void slrn_repeat_last_key (void) /*{{{*/
{
   SLtt_beep ();
}

/*}}}*/

int main (int argc, char **argv) /*{{{*/
{
   if (-1 == main_init_and_parse_args (argc, argv))
     return 1;

   if (-1 == slrn_select_group_mode ())
     return 1;

   slrn_push_suspension (1);

   (void) slrn_run_hooks (HOOK_GROUP_MODE_STARTUP, 0);

   if (Slrn_Batch) return 1;

   if (SLang_get_error ())
     {
	SLang_restart (1);	       /* prints any queued messages */
	slrn_exit_error (_("An error was encountered during initialization."));
     }

   while (Slrn_Current_Mode != NULL)
     {
	if (SLKeyBoard_Quit)
	  {
	     SLKeyBoard_Quit = 0;
	     slrn_error (_("Quit!"));
	  }

	(void) slrn_handle_interrupts ();

	if (SLang_get_error() || !SLang_input_pending(0))
	  {
	     slrn_update_screen ();
	  }

	slrn_do_keymap_key (Slrn_Current_Mode->keymap);
     }

   return 1;
}

/*}}}*/

/*}}}*/

int slrn_posix_system (char *cmd, int reset) /*{{{*/
{
   int ret;
   int init_mode = Slrn_TT_Initialized;

   if (reset) slrn_set_display_state (0);
   ret = SLsystem (cmd);
   if (reset) slrn_set_display_state (init_mode);
   Slrn_Full_Screen_Update = 1;
   return ret;
}

/*}}}*/