File: analyser.c

package info (click to toggle)
robodoc 4.99.34-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,468 kB
  • ctags: 1,009
  • sloc: ansic: 14,040; sh: 3,635; makefile: 163; perl: 155
file content (2054 lines) | stat: -rw-r--r-- 54,027 bytes parent folder | download
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
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
/* vi: ff=unix spell */
/****h* ROBODoc/Analyser
 * NAME
 *   Analyser -- Functions to scan source and collect headers
 * FUNCTION
 *   This module provides the functions to scan a sourcefile and
 *   collect all the headers.
 *
 *****
 * $Id: analyser.c,v 1.72 2007/02/21 22:24:46 gumpu Exp $
 */

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

#include "robodoc.h"
#include "globals.h"
#include "headers.h"
#include "headertypes.h"
#include "items.h"
#include "util.h"
#include "links.h"
#include "analyser.h"
#include "document.h"
#include "file.h"
#include "part.h"
#include "roboconfig.h"

#ifdef DMALLOC
#include <dmalloc.h>
#endif

static int          ToBeAdded(
    struct RB_Document *document,
    struct RB_header *header );
static int          Find_Header_Name(
    FILE *,
    struct RB_header * );
static struct RB_header *Grab_Header(
    FILE *sourcehandle,
    struct RB_Document *arg_document );
static char        *Function_Name(
    char *header_name );
static char        *Module_Name(
    char *header_name );
static int          Find_End_Marker(
    FILE *document,
    struct RB_header *new_header );
struct RB_HeaderType *AnalyseHeaderType(
    char **cur_char,
    int *is_internal );
static struct RB_HeaderType *RB_Find_Marker(
    FILE *document,
    int *is_internal,
    int reuse_previous_line );
static int          Analyse_Items(
    struct RB_header *arg_header );
static int          Is_ListItem_Start(
    char *arg_line,
    int arg_indent );

/****f* Analyser/Is_Pipe_Marker
 * NAME
 *   Is_Pipe_Marker
 * FUNCTION
 *   Check for "pipe" markers e.g. "|html ". 
 * SYNOPSIS
 */
static char        *Is_Pipe_Marker(
    char *cur_char,
    int *pipe_mode )
/*
 * RESULT
 *   Pointer to the data to be piped to document or in case no pointers
 *   are found.
 * SEE ALSO
 *   RB_Check_Pipe
 * SOURCE
 */
{
    char               *s = cur_char + 1;

    *pipe_mode = -1;
    if ( *cur_char == '|' && *s )
    {
        if ( strncmp( "html ", s, 5 ) == 0 )
        {
            *pipe_mode = HTML;
            return ( s + 5 );
        }
        else if ( strncmp( "latex ", s, 6 ) == 0 )
        {
            *pipe_mode = LATEX;
            return ( s + 6 );
        }
        else if ( strncmp( "rtf ", s, 4 ) == 0 )
        {
            *pipe_mode = RTF;
            return ( s + 4 );
        }
        else if ( strncmp( "dbxml ", s, 6 ) == 0 )
        {
            *pipe_mode = XMLDOCBOOK;
            return ( s + 6 );
        }
        else if ( strncmp( "ascii ", s, 6 ) == 0 )
        {
            *pipe_mode = ASCII;
            return ( s + 6 );
        }
    }

    return ( char * ) NULL;
}

/*****/


/****f* Analyser/Is_Tool
 * SYNOPSIS
 */
static char        *Is_Tool(
    char *cur_char,
    enum ItemLineKind *itemkind,
    int *tool_active )
/*
 * FUNCTION
 *   Checks for tool start and end markers
 * SOURCE
 */
{
    char               *s = cur_char + 1;

    if ( *cur_char == '|' && *s )
    {
        // Check if tool starts or ends
        if ( !strncmp( "tool ", s, 5 ) )
        {
            if ( *tool_active )
            {
                *itemkind = ITEM_LINE_TOOL_END;
                *tool_active = 0;
            }
            else
            {
                *itemkind = ITEM_LINE_TOOL_START;
                *tool_active = 1;
            }

            return ( s + 5 );
        }
        // Check if DOT starts or ends
        if ( !strncmp( "dot ", s, 4 ) )
        {
            if ( *tool_active )
            {
                *itemkind = ITEM_LINE_DOT_END;
                *tool_active = 0;
            }
            else
            {
                *itemkind = ITEM_LINE_DOT_START;
                *tool_active = 1;
            }

            return ( s + 4 );
        }
        // Check for DOT file includes
        else if ( !strncmp( "dotfile ", s, 8 ) && !*tool_active )
        {
            *itemkind = ITEM_LINE_DOT_FILE;
            return ( s + 8 );
        }
        // Check for exec items
        else if ( !strncmp( "exec ", s, 5 ) && !*tool_active )
        {
            *itemkind = ITEM_LINE_EXEC;
            return ( s + 5 );
        }
    }

    return NULL;
}

/*****/

/****f* Analyser/RB_Analyse_Document
 *   foo
 * FUNCTION
 *   Scan all the sourcefiles of all parts of a document for
 *   headers.  Store these headers in each part (RB_Part).
 * SYNOPSIS
 */
void RB_Analyse_Document(
    struct RB_Document *arg_document )
/*
 * INPUTS
 *   o document -- document to be analysed.
 * RESULT
 *   Each part will contain the headers that were found in the
 *   sourcefile of the part.
 * SOURCE
 */
{
    struct RB_Part     *a_part;
    struct RB_Filename *a_filename;
    FILE               *filehandle;

    for ( a_part = arg_document->parts; a_part; a_part = a_part->next )
    {
        struct RB_header   *new_header = NULL;

        a_filename = a_part->filename;
        RB_Say( "analysing %s\n", SAY_DEBUG, Get_Fullname( a_filename ) );
        RB_SetCurrentFile( Get_Fullname( a_filename ) );

        RB_Header_Lock_Reset(  );
        filehandle = RB_Open_Source( a_part );
        line_number = 0;

        for ( new_header = Grab_Header( filehandle, arg_document );
              new_header;
              new_header = Grab_Header( filehandle, arg_document ) )
        {
            if ( ToBeAdded( arg_document, new_header ) )
            {
                /* The Add is required before the 
                 * Analyse because Add sets the owner of the header
                 * which is needed for error messages.
                 */
                RB_Part_Add_Header( a_part, new_header );
                Analyse_Items( new_header );
            }
            else
            {
                RB_Free_Header( new_header );
            }
        }
        fclose( filehandle );
    }
}

/*****/

#if 0
/****f* Analyser/Check_Header_Start
 * FUNCION
 *   Sometimes a user makes a spelling mistake in the name of the first item.
 *   ROBODoc then ignores all the lines leading up to the second item,
 *   (which is the first item ROBODoc recognized).  This function
 *   checks for this condition and gives the user a warning.
 * SYNOPSIS
 */
static void Check_Header_Start(
    struct RB_header *arg_header,
    int first_item )
/*
 * INPUTS
 *   * arg_header -- the header to be checked.
 *   * first      -- the line on which the first item was found
 * RESULT
 *   A warning is given if the condition occured.
 * SOURCE
 */
{
    if ( first_item )
    {
        int                 i = 0;

        for ( i = 0; i < first_item; ++i )
        {
            char               *c = arg_header->lines[i];

            c = RB_Skip_Whitespace( c );
            if ( RB_Has_Remark_Marker( c ) )
            {
                c = RB_Skip_Remark_Marker( c );
                for ( ; *c; ++c )
                {
                    if ( !utf8_isspace( *c ) )
                    {
                        RB_Warning_Full( Get_Fullname
                                         ( arg_header->owner->filename ),
                                         arg_header->line_number,
                                         "Header \"%s\" contains text before the fist item, "
                                         "this might be caused by a misspelled item name.\n",
                                         arg_header->name );
                        return;
                    }
                }
            }
        }
    }
}

/*******/
#endif

/****f* Analyser/Is_Empty_Line
 * FUNCTION
 *   Check if line is empty. This assumes that 
 *   Copy_Lines_To_Item has been run on the item.
 * SYNOPSIS
 */
static int Is_Empty_Line(
    char *line )
/*
 * INPUTS
 *   * line -- the string to be analysed.
 * SOURCE
 */
{
    line = RB_Skip_Whitespace( line );
    return ( *line == '\0' );
}

/******/


/****f* Analyser/Get_Indent
 * FUNCION
 *   Determine the indentation of a line by counting
 *   the number of spaces at the begining of the line.
 * SYNOPSIS
 */
static int Get_Indent(
    char *line )
/*
 * INPUTS
 *   * line -- the line
 * RESULT
 *   The indentation.
 * SOURCE
 */
{
    int                 i;

    for ( i = 0; line[i] && utf8_isspace( line[i] ); ++i )
    {
        /* empty */
    }
    return i;
}

/*****/


/****f* Analyser/Is_ListItem_Start
 * FUNCTION
 *   Test if a line starts with something that indicates a list item.
 *   List items start with '*', '-', or 'o'.
 * SYNPOPSIS
 */
static int Is_ListItem_Start(
    char *arg_line,
    int arg_indent )
/*
 * INPUTS
 *   * line -- the line
 * RESULT
 *   * TRUE  -- it did start with one of those characters
 *   * FALSE -- it did not.
 * SOURCE
 */
{
    char               *c = arg_line;
    int                 cur_indent = Get_Indent( arg_line );

    if ( cur_indent == arg_indent )
    {
        /* TODO  Think there is a function for this */
        for ( ; *c && utf8_isspace( *c ); ++c )
        {                       /* empty */
        };

        if ( *c && ( strlen( c ) >= 3 ) )
        {
            if ( strchr( "*-o", *c ) && utf8_isspace( *( c + 1 ) ) )
            {
                return TRUE;
            }
        }
    }
    else
    {
        /* The line is indented so it must be
         * the start of a pre block  */
    }

    return FALSE;
}

/*****/


/****f* Analyser/Is_List_Item_Continuation
 * FUNCTION
 *   Is it like the second line in something like:
 *     * this is a list item
 *       that continues 
 * SYNPOPSIS
 */
static int Is_List_Item_Continuation(
    char *arg_line,
    int indent )
/*
 * INPUTS
 *   * arg_line  -- the current line
 *   * indent    -- the indent of the current item.
 * RESULT
 *   * TRUE  -- it is.
 *   * FALSE -- it is not.
 * SOURCE
 */
{
    int                 this_indent = Get_Indent( arg_line );

    return ( this_indent > indent );
}

/*****/


#if 0
/****f* Analyser/Is_End_of_List
 * FUNCTION
 *   Check... (TODO) 
 * INPUTS
 *   * arg_line --
 *   * indent  --
 * SOURCE
 */

static int Is_End_of_List(
    char *arg_line,
    int indent )
{
    int                 this_indent = Get_Indent( arg_line );

    return ( this_indent <= indent );
}

/******/
#endif


/****f* Analyser/Is_Start_List
 * FUNCTION
 *   Check... (TODO) 
 * INPUTS
 *   * arg_line --
 *   * indent  --
 * SOURCE
 */

static int Is_Start_List(
    char *arg_line,
    int indent )
{
    int                 this_indent = Get_Indent( arg_line );
    char               *c = strrchr( arg_line, ':' );

    if ( ( this_indent == indent ) && c )
    {
        for ( ++c; *c; ++c )
        {
            if ( !utf8_isspace( *c ) )
            {
                return FALSE;
            }
        }
        return TRUE;
    }
    return FALSE;
}

/*******/


/****f* Analyser/Remove_List_Char
 * FUNCTION
 *   Remove... (TODO) 
 * INPUTS
 *   * arg_item    -- the item to be analysed.
 *   * start_index -- 
 * SOURCE
 */

static void Remove_List_Char(
    struct RB_Item *arg_item,
    int start_index )
{
    char               *c = arg_item->lines[start_index]->line;

    for ( ; *c && utf8_isspace( *c ); ++c )
    {                           /* empty */
    };
    if ( *c && ( strlen( c ) >= 3 ) )
    {
        if ( strchr( "*-o", *c ) && utf8_isspace( *( c + 1 ) ) )
        {
            char               *temp = arg_item->lines[start_index]->line;

            *c = ' ';
            arg_item->lines[start_index]->line = RB_StrDup( temp + 2 );
            free( temp );
        }
    }
}

/*******/

/****f* Analyser/Analyse_ListBody
 * FUNCTION
 *   Analyse... (TODO) 
 * SYNPOPSIS
 */
static int Analyse_ListBody(
    struct RB_Item *arg_item,
    int start_index,
    int arg_indent )
/*
 * INPUTS
 *   * arg_item    -- the item to be analysed.
 *   * start_index --
 *   * arg_index   --
 * SOURCE
 */
{
    int                 i = start_index;

    for ( ; i < arg_item->no_lines; i++ )
    {
        char               *line = arg_item->lines[i]->line;

        if ( ( arg_item->lines[i]->kind == ITEM_LINE_PLAIN ) ||
             ( arg_item->lines[i]->kind == ITEM_LINE_END ) )
        {
            if ( Is_ListItem_Start( line, arg_indent ) )
            {
                arg_item->lines[i]->format |= RBILA_END_LIST_ITEM;
                arg_item->lines[i]->format |= RBILA_BEGIN_LIST_ITEM;
                Remove_List_Char( arg_item, i );
            }
            else if ( Is_List_Item_Continuation( line, arg_indent ) )
            {
                /* Nothing */
            }
            else
            {
                /* Must be the end of the list */
                arg_item->lines[i]->format |= RBILA_END_LIST_ITEM;
                arg_item->lines[i]->format |= RBILA_END_LIST;
                break;
            }
        }
    }
    return i;
}

/*******/

/****f* Analyser/Analyse_List
 * FUNCTION 
 *   Parse the item text to see if there are any lists.
 *   A list is either case I:
 *      ITEMNAME
 *         o bla bla
 *         o bla bla
 *   or case II:
 *      some text:     <-- begin of a list
 *      o bla bla      <-- list item
 *        bla bla bla  <-- continuation of list item.
 *      o bla bla      <-- list item
 *                     <-- end of a list 
 *      bla bla        <-- this can also be the end of a list.
 * SYNPOPSIS
 */
static void Analyse_List(
    struct RB_Item *arg_item,
    int indent )
/*
 * INPUTS
 *   * arg_item  -- the item to be parsed.
 *   * indent    -- the indent of this item.
 * OUTPUT
 *   * arg_item  -- the itemlines will contain formatting information 
 *                  for all the lists that were found.
 * SOURCE
 */
{
    if ( arg_item->no_lines >= 1 )
    {
        int                 i = 0;
        char               *line = arg_item->lines[i]->line;

        /* Case I */
        if ( ( arg_item->lines[i]->kind == ITEM_LINE_PLAIN ) &&
             Is_ListItem_Start( line, indent ) )
        {
            /* Case I, the is a list item right after the item name */
            arg_item->lines[i]->format |= RBILA_BEGIN_LIST;
            arg_item->lines[i]->format |= RBILA_BEGIN_LIST_ITEM;
            Remove_List_Char( arg_item, i );
            /* Now try to find the end of the list */
            i = Analyse_ListBody( arg_item, 1, indent );
        }

        /* Now search for case II cases */
        for ( ; i < arg_item->no_lines; i++ )
        {
            line = arg_item->lines[i]->line;
            if ( ( arg_item->lines[i]->kind == ITEM_LINE_PLAIN ) &&
                 Is_Start_List( line, indent ) )
            {
                ++i;
                if ( i < arg_item->no_lines )
                {
                    line = arg_item->lines[i]->line;
                    if ( ( arg_item->lines[i]->kind == ITEM_LINE_PLAIN ) &&
                         Is_ListItem_Start( line, indent ) )
                    {
                        arg_item->lines[i]->format |= RBILA_BEGIN_LIST;
                        arg_item->lines[i]->format |= RBILA_BEGIN_LIST_ITEM;
                        Remove_List_Char( arg_item, i );
                        ++i;
                        i = Analyse_ListBody( arg_item, i, indent );


                        /* One list might be immediately followed
                         * by another. In this case we have to
                         * analyse the last line again. */
                        line = arg_item->lines[i]->line;
                        if ( ( arg_item->lines[i]->kind == ITEM_LINE_PLAIN )
                             && Is_Start_List( line, indent ) )
                        {
                            --i;
                        }

                    }
                }
            }
        }
    }
}

/******/

// If (unused(this_function)) { delete(this_function) }
#if 0
/****f* Analyser/Dump_Item
 * FUNCTION
 *   Print debug information.
 */
static void Dump_Item(
    struct RB_Item *arg_item )
/*
 * INPUTS
 *   * arg_item -- the item to be pretty-printed.
 * SOURCE
 */
{
    int                 i;

    /* preformatted blocks */
    for ( i = 0; i < arg_item->no_lines; i++ )
    {
        char               *line = arg_item->lines[i]->line;
        int                 format = arg_item->lines[i]->format;

        printf( "%04d ", i );
        printf( ( format & RBILA_END_PARAGRAPH ) ? "p" : " " );
        printf( ( format & RBILA_BEGIN_PARAGRAPH ) ? "P" : " " );
        printf( ( format & RBILA_END_PRE ) ? "e" : " " );
        printf( ( format & RBILA_BEGIN_PRE ) ? "E" : " " );
        printf( ( format & RBILA_END_LIST ) ? "l" : " " );
        printf( ( format & RBILA_BEGIN_LIST ) ? "L" : " " );
        printf( ( format & RBILA_END_LIST_ITEM ) ? "i" : " " );
        printf( ( format & RBILA_BEGIN_LIST_ITEM ) ? "I" : " " );
        printf( " (%s)\n", line );
    }
}

/*******/
#endif

/****f* Analyser/Preformat_All
 * FUNCTION
 *   Process... (TODO) 
 * SYNPOPSIS
 */
static void Preformat_All(
    struct RB_Item *arg_item,
    int source )
/*
 * INPUTS
 *   * arg_item -- the item to be pre-formatted.
 *   * source   -- is it a source item ?
 * SOURCE
 */
{
    int                 i;
    int                 preformatted = FALSE;
    char               *line = NULL;

    if ( arg_item->no_lines > 0 )
    {
        i = 0;
        /* Skip any pipe stuff */
        for ( ;
              ( i < arg_item->no_lines )
              && ( arg_item->lines[i]->kind == ITEM_LINE_PIPE ); ++i )
        {
            /* Empty */
        }

        line = arg_item->lines[i]->line;
        if ( ( arg_item->lines[i]->kind == ITEM_LINE_RAW ) ||
             ( arg_item->lines[i]->kind == ITEM_LINE_PLAIN ) )
        {
            arg_item->lines[i]->format |=
                RBILA_BEGIN_PRE | ( source ? RBILA_BEGIN_SOURCE : 0 );
            preformatted = TRUE;

            for ( ++i; i < arg_item->no_lines; i++ )
            {
                if ( arg_item->lines[i]->kind == ITEM_LINE_PIPE )
                {
                    /* Temporarily end the preformatting to allow
                     * the piping to happen
                     */
                    arg_item->lines[i]->format |=
                        RBILA_END_PRE | ( source ? RBILA_END_SOURCE : 0 );
                    /* Find the end of the pipe stuff */
                    for ( ; ( i < arg_item->no_lines ) &&
                          ( arg_item->lines[i]->kind == ITEM_LINE_PIPE );
                          ++i )
                    {           /* Empty */
                    };
                    /* Every item ends with an ITEM_LINE_END, so: */
                    assert( i < arg_item->no_lines );
                    /* And re-enable preformatting */
                    arg_item->lines[i]->format |=
                        RBILA_BEGIN_PRE | ( source ? RBILA_BEGIN_SOURCE : 0 );
                }

                if ( arg_item->lines[i]->kind == ITEM_LINE_END )
                {
                    /* If the last line ends with a begin_pre remove
                     * it, otherwise a begin and end pre will be
                     * generated, in the wrong order, on the same line in the output.
                     */
                    if ( arg_item->lines[i]->format & RBILA_BEGIN_PRE ) {
                        arg_item->lines[i]->format &= ~( RBILA_BEGIN_PRE );
                    } else {
                        arg_item->lines[i]->format |= RBILA_END_PRE;
                    }
                    arg_item->lines[i]->format |= ( source ? RBILA_END_SOURCE : 0 );
                }
            }
        }
    }
}

/******/

/*
 *     aaaaa
 *       aa
 *               </p>
 *       aa      <p>
 *     aaa
 *     aaaa
 *
 *
 */

/****f* Analyser/Analyse_Preformatted
 * FUNCTION
 *   Analyse preformatted text ... (TODO) 
 * SYNPOPSIS
 */
static void Analyse_Preformatted(
    struct RB_Item *arg_item,
    int indent )
/*
 * INPUTS
 *   * arg_item -- the item to be analysed.
 *   * indent   -- 
 * SOURCE
 */
{
    int                 i;
    int                 in_list = FALSE;
    int                 new_indent = -1;
    int                 preformatted = FALSE;
    char               *line = NULL;

    /* preformatted blocks */
    if ( arg_item->no_lines > 0 )
    {
        i = 0;
        /* Skip any pipe stuff */
        for ( ;
              ( i < arg_item->no_lines )
              && ( arg_item->lines[i]->kind == ITEM_LINE_PIPE ); ++i )
        {
            /* Empty */
        }

        line = arg_item->lines[i]->line;

        if ( ( !in_list )
             && ( arg_item->lines[i]->format & RBILA_BEGIN_LIST ) )
        {
            in_list = TRUE;
        }
        if ( ( in_list ) && ( arg_item->lines[i]->format & RBILA_END_LIST ) )
        {
            in_list = FALSE;
        }

        for ( ++i; i < arg_item->no_lines; i++ )
        {
            if ( arg_item->lines[i]->kind == ITEM_LINE_PIPE )
            {
                if ( preformatted )
                {
                    arg_item->lines[i]->format |= RBILA_END_PRE;
                }
                for ( ; ( i < arg_item->no_lines ) &&
                      ( arg_item->lines[i]->kind == ITEM_LINE_PIPE ); ++i )
                {               /* Empty */
                };
                /* Every item ends with an ITEM_LINE_END, so: */
                assert( i < arg_item->no_lines );
                if ( preformatted )
                {
                    arg_item->lines[i]->format |= RBILA_BEGIN_PRE;
                }
            }

            line = arg_item->lines[i]->line;
            new_indent = Get_Indent( line );

            if ( ( !in_list )
                 && ( arg_item->lines[i]->format & RBILA_BEGIN_LIST ) )
            {
                in_list = TRUE;
            }
            if ( ( in_list )
                 && ( arg_item->lines[i]->format & RBILA_END_LIST ) )
            {
                in_list = FALSE;
            }

            if ( !in_list )
            {
                if ( ( new_indent > indent ) && !preformatted )
                {
                    preformatted = TRUE;
                    arg_item->lines[i]->format |= RBILA_BEGIN_PRE;
                }
                else if ( ( new_indent <= indent ) && preformatted )
                {
                    preformatted = FALSE;
                    arg_item->lines[i]->format |= RBILA_END_PRE;
                }
                else
                {
                    /* An empty line */
                }
            }
            else
            {
                /* We are in a list, do nothing */
            }
        }
    }
}

/******/

/****f* Analyser/Analyse_Paragraphs
 * FUNCTION
 *   Analyse paragraphs... (TODO) 
 * SYNPOPSIS
 */
static void Analyse_Paragraphs(
    struct RB_Item *arg_item )
/*
 * INPUTS
 *   * arg_item -- the item to be analysed.
 * SOURCE
 */
{
    int                 i;
    int                 in_par = FALSE;
    int                 in_list = FALSE;
    int                 in_pre = FALSE;
    int                 is_empty = FALSE;
    int                 prev_is_empty = FALSE;

    for ( i = 0;
          ( i < arg_item->no_lines )
          && ( arg_item->lines[i]->kind == ITEM_LINE_PIPE ); ++i )
    {
        /* Empty */
    }
    assert( i < arg_item->no_lines );

    if ( ( arg_item->lines[i]->format == 0 ) )
    {
        arg_item->lines[i]->format |= RBILA_BEGIN_PARAGRAPH;
        in_par = TRUE;
    }
    for ( ; i < arg_item->no_lines; i++ )
    {
        char               *line = arg_item->lines[i]->line;

        prev_is_empty = is_empty;
        is_empty = Is_Empty_Line( line );
        if ( arg_item->lines[i]->format & RBILA_BEGIN_LIST )
        {
            in_list = TRUE;
        }
        if ( arg_item->lines[i]->format & RBILA_BEGIN_PRE )
        {
            in_pre = TRUE;
        }
        if ( arg_item->lines[i]->format & RBILA_END_LIST )
        {
            in_list = FALSE;
        }
        if ( arg_item->lines[i]->format & RBILA_END_PRE )
        {
            in_pre = FALSE;
        }
        if ( in_par )
        {
            if ( ( arg_item->lines[i]->format & RBILA_BEGIN_LIST ) ||
                 ( arg_item->lines[i]->format & RBILA_BEGIN_PRE ) ||
                 is_empty )
            {
                in_par = FALSE;
                arg_item->lines[i]->format |= RBILA_END_PARAGRAPH;
            }
        }
        else
        {
            if ( ( arg_item->lines[i]->format & RBILA_END_LIST ) ||
                 ( arg_item->lines[i]->format & RBILA_END_PRE ) ||
                 ( !is_empty && prev_is_empty && !in_list && !in_pre ) )
            {
                in_par = TRUE;
                arg_item->lines[i]->format |= RBILA_BEGIN_PARAGRAPH;
            }
        }
    }
    if ( in_par )
    {
        arg_item->lines[arg_item->no_lines - 1]->format |=
            RBILA_END_PARAGRAPH;
    }
}

/******/


/****f* Analyser/Analyse_Indentation
 * FUNCTION
 *  Figure out how text is indented. 
 * SYNPOPSIS
 */
static int Analyse_Indentation(
    struct RB_Item *arg_item )
/*
 * INPUTS
 *   * arg_item -- the item to be analysed.
 * SOURCE
 */
{
    int                 i;
    int                 indent = -1;

    assert( arg_item->no_lines > 0 );

    for ( i = 0; i < arg_item->no_lines; ++i )
    {
        if ( arg_item->lines[i]->kind == ITEM_LINE_PLAIN )
        {
            char               *line = arg_item->lines[i]->line;

            if ( Is_Empty_Line( line ) )
            {
                /* Empty */
                indent = 0;
            }
            else
            {
                indent = Get_Indent( line );
                break;
            }
        }
    }
    assert( indent >= 0 );
    return indent;
}

/******/

/****f* Analyser/Analyse_Item_Format
 * FUNCTION
 *   Try to determine the formatting of an item.
 *   An empty line generates a new paragraph
 *   Things that are indented more that the rest of the text
 *   are preformatted.
 *   Things that start with a '*' or '-' create list items
 *   unless they are indented more that the rest of the text.
 * SYNPOPSIS
 */
static void Analyse_Item_Format(
    struct RB_Item *arg_item )
/*
 * INPUTS
 *   * arg_item -- the item to be analysed.
 * SOURCE
 */
{
    // If item is not empty
    if ( arg_item->no_lines )
    {
        // If it is a SOURCE item
        if ( Works_Like_SourceItem( arg_item->type ) )
        {
            // Preformat it like a SOURCE item
            Preformat_All( arg_item, TRUE );
        }
        // Check if we have to analyse this item
        else if ( ( course_of_action.do_nopre
                    || Is_Format_Item( arg_item->type ) )
                  && !Is_Preformatted_Item( arg_item->type ) )
        {
            // analyse item
            int                 indent = Analyse_Indentation( arg_item );

            Analyse_List( arg_item, indent );
            Analyse_Preformatted( arg_item, indent );
            Analyse_Paragraphs( arg_item );
        }
        // If none of above, preformat item
        else
        {
            // Preformat it
            Preformat_All( arg_item, FALSE );
        }
    }
    // Item is empty
    else
    {
        // Do nothing
    }
}

/*****/



static int Trim_Empty_Item_Begin_Lines(
    struct RB_header *arg_header,
    struct RB_Item *arg_item,
    int current_index )
{

    char               *c;

    if ( Works_Like_SourceItem( arg_item->type ) )
    {
        /* We skip the first line after the source item, if
         * it an remark end marker -- such as '*)'
         */
        c = arg_header->lines[current_index];
        if ( RB_Is_Remark_End_Marker( c ) )
        {
            c = RB_Skip_Remark_End_Marker( c );
            c = RB_Skip_Whitespace( c );
            if ( *c != '\0' )
            {
                c = arg_header->lines[current_index];
                RB_Warning( "text following a remark end marker:\n%s\n", c );
            }
            ++current_index;
        }
    }

    if ( current_index > arg_item->end_index )
    {
        /* item is empty */
    }
    else
    {
        do
        {
            c = arg_header->lines[current_index];
            c = RB_Skip_Whitespace( c );
            if ( RB_Has_Remark_Marker( c ) )
            {
                c = RB_Skip_Remark_Marker( c );
            }
            c = RB_Skip_Whitespace( c );
            if ( *c == '\0' )
            {
                ++current_index;
            }
        }
        while ( ( *c == '\0' ) && ( current_index < arg_item->end_index ) );
    }

    return current_index;
}



static int Trim_Empty_Item_End_Lines(
    struct RB_header *arg_header,
    struct RB_Item *arg_item,
    int current_index )
{
    char               *c;

    if ( Works_Like_SourceItem( arg_item->type ) )
    {
        c = arg_header->lines[current_index];
        if ( RB_Is_Remark_Begin_Marker( c ) )
        {
            c = RB_Skip_Remark_Begin_Marker( c );
            c = RB_Skip_Whitespace( c );
            if ( *c != '\0' )
            {
                c = arg_header->lines[current_index];
                RB_Warning( "text following a remark begin marker:\n%s\n",
                            c );
            }
            --current_index;
        }
    }

    do
    {
        c = arg_header->lines[current_index];
        c = RB_Skip_Whitespace( c );
        if ( RB_Has_Remark_Marker( c ) )
        {
            c = RB_Skip_Remark_Marker( c );
        }
        c = RB_Skip_Whitespace( c );
        if ( *c == '\0' )
        {
            --current_index;
        }
    }
    while ( ( *c == '\0' ) && ( current_index > arg_item->begin_index ) );

    return current_index;
}



static void Trim_Empty_Item_Lines(
    struct RB_header *arg_header,
    struct RB_Item *arg_item )
{
    arg_item->no_lines = arg_item->end_index - arg_item->begin_index + 1;
    if ( arg_item->no_lines <= 1 )
    {
        /* item is empty */
        arg_item->no_lines = 0;
    }
    else
    {
        int                 current_index;

        /* trim all empty lines at the begin of an item */

        /* we skip the first line because that contains the item name.
         */
        current_index = arg_item->begin_index + 1;
        current_index =
            Trim_Empty_Item_Begin_Lines( arg_header, arg_item,
                                         current_index );

        /* Is there anything left? */
        if ( current_index <= arg_item->end_index )
        {
            arg_item->begin_index = current_index;

            /* trim all the empty lines at the end of an item */
            current_index = arg_item->end_index;
            current_index =
                Trim_Empty_Item_End_Lines( arg_header, arg_item,
                                           current_index );
            if ( current_index >= arg_item->begin_index )
            {
                arg_item->end_index = current_index;
                arg_item->no_lines =
                    arg_item->end_index - arg_item->begin_index + 1;
            }
            else
            {
                /* item is empty */
                arg_item->no_lines = 0;
            }
        }
        else
        {
            /* item is empty */
            arg_item->no_lines = 0;
        }
    }
}




/* TODO This routine is way too long */

static void Copy_Lines_To_Item(
    struct RB_header *arg_header,
    struct RB_Item *arg_item )
{
#if 0
    printf( "%d\n%d\n%s\n%s\n",
            arg_item->begin_index,
            arg_item->end_index,
            arg_header->lines[arg_item->begin_index],
            arg_header->lines[arg_item->end_index] );
#endif

    Trim_Empty_Item_Lines( arg_header, arg_item );

    if ( arg_item->no_lines > 0 )
    {
        int                 i = 0;
        int                 j = 0;
        struct RB_Item_Line *itemline = NULL;
        int                 tool_active = 0;    // Shows wether we are inside a tool body

        /* Allocate enough memory for all the lines, plus one
         * extra line
         */
        ++arg_item->no_lines;
        arg_item->lines =
            calloc( arg_item->no_lines, sizeof( struct RB_Item_Line * ) );
        if ( !arg_item->lines )
        {
            RB_Panic( "Out of memory! %s\n", "Copy_Lines_To_Item" );
        }

        /* And create an RB_Item_Line for each of them, and add
         * those to the RB_Item
         */
        for ( i = 0; i < arg_item->no_lines - 1; ++i )
        {
            char               *c =
                arg_header->lines[arg_item->begin_index + i];
            /* TODO should be a Create_ItemLine() */
            itemline = malloc( sizeof( struct RB_Item_Line ) );
            if ( !itemline )
            {
                RB_Panic( "Out of memory! %s (2)\n", "Copy_Lines_To_Item" );
            }

            c = ExpandTab( c );
            c = RB_Skip_Whitespace( c );
            // Lines with remark marker
            if ( RB_Has_Remark_Marker( c )
                 && !Works_Like_SourceItem( arg_item->type ) )
            {
                char               *c2, *c3;
                int                 pipe_mode;
                enum ItemLineKind   item_kind;

                c = RB_Skip_Remark_Marker( c );
                c2 = RB_Skip_Whitespace( c );
                if ( *c2 )
                {
                    // Check wether a tool starts or ends
                    if ( ( c3 = Is_Tool( c2, &item_kind, &tool_active ) ) )
                    {
                        itemline->kind = item_kind;
                        c = c3;
                    }
                    // If we have an active tool, copy the body lines
                    else if ( tool_active )
                    {
                        itemline->kind = ITEM_LINE_TOOL_BODY;
                        c++;    // Skip space after the remark marker
                    }
                    // Check for pipes
                    else if ( ( c3 = Is_Pipe_Marker( c2, &pipe_mode ) ) )
                    {
                        itemline->kind = ITEM_LINE_PIPE;
                        itemline->pipe_mode = pipe_mode;
                        c = c3;
                    }
                    // Plain Items ...
                    else
                    {
                        itemline->kind = ITEM_LINE_PLAIN;
                    }
                }
                // Empty lines with remark markers and active tool
                else if ( tool_active )
                {
                    itemline->kind = ITEM_LINE_TOOL_BODY;
                }
                // Plain empty lines with remark markers...
                else
                {
                    itemline->kind = ITEM_LINE_PLAIN;
                }
            }
            else
            {
                itemline->kind = ITEM_LINE_RAW;
                /* The is raw code, so we do not want to have the
                 * whitespace stripped of
                 */
                c = arg_header->lines[arg_item->begin_index + i];
                c = ExpandTab( c );
            }

            if ( ( !Works_Like_SourceItem( arg_item->type ) &&
                   ( itemline->kind != ITEM_LINE_RAW ) ) ||
                 Works_Like_SourceItem( arg_item->type ) )
            {
                itemline->line = RB_StrDup( c );
                itemline->format = 0;
                arg_item->lines[j] = itemline;
                ++j;
            }
            else
            {
                /* We dump the RAW item lines if we are not in a
                 * source item.
                 */
                free( itemline );
            }
        }

        if ( j > 0 )
        {
            /* And one empty line to mark the end of an item and
             * to be able to store some additional formatting actions
             */
            itemline = malloc( sizeof( struct RB_Item_Line ) );
            if ( !itemline )
            {
                RB_Panic( "Out of memory! %s (3)\n", "Copy_Lines_To_Item" );
            }

            itemline->kind = ITEM_LINE_END;
            itemline->line = RB_StrDup( "" );
            itemline->format = 0;
            arg_item->lines[j] = itemline;

            /* Store the real number of lines we copied */
            assert( arg_item->no_lines >= ( j + 1 ) );
            arg_item->no_lines = j + 1;
        }
        else
        {
            arg_item->no_lines = 0;
            free( arg_item->lines );
            arg_item->lines = NULL;
        }
    }
    else
    {
        arg_item->no_lines = 0;
        arg_item->lines = NULL;
    }
}


/****f* Analyser/RB_Analyse_Items
 * FUNCTION
 *   Locate the items in the header and create RB_Item structures for
 *   them.
 * SYNPOPSIS
 */
static int Analyse_Items(
    struct RB_header *arg_header )
/*
 * SOURCE
 */
{
    int                 line_nr;
    enum ItemType       item_type = NO_ITEM;
    struct RB_Item     *new_item;
    struct RB_Item     *cur_item;

    RB_Item_Lock_Reset(  );

    /* find the first item */
    for ( line_nr = 0; line_nr < arg_header->no_lines; ++line_nr )
    {
        item_type = RB_Is_ItemName( arg_header->lines[line_nr] );
        if ( item_type != NO_ITEM )
        {
            break;
        }
    }

    /* and all the others */
    while ( ( item_type != NO_ITEM ) && ( line_nr < arg_header->no_lines ) )
    {
        new_item = RB_Create_Item( item_type );
        new_item->begin_index = line_nr;

        /* Add the item to the end of the list of items. */
        if ( arg_header->items )
        {
            for ( cur_item = arg_header->items; cur_item->next;
                  cur_item = cur_item->next )
            {
                /* Empty */
            }
            cur_item->next = new_item;
        }
        else
        {
            arg_header->items = new_item;
        }
        /* Find the next item */
        for ( ++line_nr; line_nr < arg_header->no_lines; ++line_nr )
        {
            item_type = RB_Is_ItemName( arg_header->lines[line_nr] );
            if ( item_type != NO_ITEM )
            {
                break;
            }
        }

        /* This points to the last line in the item */
        new_item->end_index = line_nr - 1;

        assert( new_item->end_index >= new_item->begin_index );

        /* Now analyse and copy the lines */
        Copy_Lines_To_Item( arg_header, new_item );
        Analyse_Item_Format( new_item );
        /* Handy for debugging wiki formatting 
         *   Dump_Item( new_item );
         */
    }

    return 0;
}

/******/



/****f* Analyser/ToBeAdded
 * FUNCTION
 *   Test whether or not a header needs to be added to the
 *   list of headers. This implements the options 
 *      --internal 
 *   and
 *      --internalonly
 * SYNPOPSIS
 */
static int ToBeAdded(
    struct RB_Document *document,
    struct RB_header *header )
/*
 * INPUTS
 *   o document  -- a document (to determine the options)
 *   o header    -- a header
 * RESULT
 *   TRUE  -- Add header
 *   FALSE -- Don't add header
 * SOURCE
 */
{
    int                 add = FALSE;

    if ( header->is_internal )
    {
        if ( ( document->actions.do_include_internal ) ||
             ( document->actions.do_internal_only ) )
        {
            add = TRUE;
        }
        else
        {
            add = FALSE;
        }
    }
    else
    {
        if ( document->actions.do_internal_only )
        {
            add = FALSE;
        }
        else
        {
            add = TRUE;
        }
    }
    return add;
}

/******/



/****f* Analyser/Grab_Header
 * FUNCTION
 *   Grab a header from a source file, that is scan a source file
 *   until the start of a header is found.  Then search for the end
 *   of a header and store all the lines in between.
 * SYNPOPSIS
 */
static struct RB_header *Grab_Header(
    FILE *sourcehandle,
    struct RB_Document *arg_document )
/*
 * INPUTS
 *   o sourcehandle -- an opened source file.
 * OUTPUT
 *   o sourcehandle -- will point to the line following the end marker.
 * RESULT
 *   0 if no header was found, or a pointer to a new header otherwise.
 * SOURCE
 */
{
    struct RB_header   *new_header = NULL;
    int                 is_internal = 0;
    struct RB_HeaderType *header_type = NULL;
    int                 good_header = FALSE;
    int                 reuse = FALSE;

    do
    {
        good_header = FALSE;
        header_type = RB_Find_Marker( sourcehandle, &is_internal, reuse );
        reuse = FALSE;
        if ( header_type )
        {
            struct RB_header   *duplicate_header = NULL;
            long                previous_line = 0;

            new_header = RB_Alloc_Header(  );
            new_header->htype = header_type;
            new_header->is_internal = is_internal;

            if ( Find_Header_Name( sourcehandle, new_header ) )
            {
                new_header->line_number = line_number;
                RB_Say( "found header [line %5d]: \"%s\"\n", SAY_DEBUG,
                        line_number, new_header->name );
                duplicate_header =
                    RB_Document_Check_For_Duplicate( arg_document,
                                                     new_header );
                if ( duplicate_header )
                {
                    /* Duplicate headers do not crash the program so
                     * we accept them.  But we do warn the user.
                     */
                    RB_Warning
                        ( "A header with the name \"%s\" already exists.\n  See %s(%d)\n",
                          new_header->name,
                          Get_Fullname( duplicate_header->owner->filename ),
                          duplicate_header->line_number );
                }

                if ( ( new_header->function_name =
                       Function_Name( new_header->name ) ) == NULL )
                {
                    RB_Warning( "Can't determine the \"function\" name.\n" );
                    RB_Free_Header( new_header );
                    new_header = NULL;
                }
                else
                {
                    if ( ( new_header->module_name =
                           Module_Name( new_header->name ) ) == NULL )
                    {
                        RB_Warning
                            ( "Can't determine the \"module\" name.\n" );
                        RB_Free_Header( new_header );
                        new_header = NULL;
                    }
                    else
                    {
                        previous_line = line_number;
                        if ( Find_End_Marker( sourcehandle, new_header ) ==
                             0 )
                        {
                            RB_Warning
                                ( "found header on line %d with name \"%s\"\n"
                                  "  but I can't find the end marker\n",
                                  previous_line, new_header->name );
                            /* Reuse the current line while finding the next
                             * Marking using RB_Find_Marker()
                             */
                            reuse = TRUE;
                            RB_Free_Header( new_header );
                            new_header = NULL;
                        }
                        else
                        {
                            RB_Say( "found end header [line %5d]:\n",
                                    SAY_DEBUG, line_number );
                            /* Good header found, we can stop */
                            good_header = TRUE;
                        }
                    }
                }
            }
            else
            {
                RB_Warning( "found header marker but no name\n" );
                RB_Free_Header( new_header );
                new_header = NULL;
            }
        }
        else
        {
            /* end of the file */
            good_header = TRUE;
        }
    }
    while ( !good_header );
    return new_header;
}

/*******/



/****f* Analyser/Module_Name
 * FUNCTION
 *   Get the module name from the header name.  The header name will be
 *   something like
 *
 *     module/functionname.
 *
 * SYNPOPSIS
 */
static char        *Module_Name(
    char *header_name )
/*
 * INPUTS
 *   o header_name -- a pointer to a nul terminated string.
 * RESULT
 *   Pointer to the modulename.  You're responsible for freeing it.
 * SEE ALSO
 *   Function_Name()
 * SOURCE
 */
{
    char               *cur_char;
    char                c;
    char               *name = NULL;

    assert( header_name );

    for ( cur_char = header_name; *cur_char && *cur_char != '/'; ++cur_char );
    if ( *cur_char )
    {
        c = *cur_char;
        *cur_char = '\0';
        name = RB_StrDup( header_name );
        *cur_char = c;
    }
    return name;
}

/******/



/****f* Analyser/Function_Name
 * FUNCTION
 *   A header name is consists of two parts. The module name and
 *   the function name. This returns a pointer to the function name.
 *   The name "function name" is a bit obsolete. It is really the name
 *   of any of objects that can be documented; classes, methods,
 *   variables, functions, projects, etc.
 * SYNOPSIS
 */
static char        *Function_Name(
    char *header_name )
/*
 * SOURCE
 */
{
    char               *cur_char;
    char               *name;

    name = NULL;
    if ( ( cur_char = header_name ) != NULL )
    {
        for ( ; *cur_char != '\0'; ++cur_char )
        {
            if ( '/' == *cur_char )
            {
                ++cur_char;
                if ( *cur_char )
                {
                    name = cur_char;
                    break;
                }
            }
        }
    }
    if ( name )
    {
        return RB_StrDup( name );
    }
    else
    {
        return ( name );
    }
}

/*** Function_Name ***/


/****f* Analyser/RB_Find_Marker
 * NAME
 *   RB_Find_Marker -- Search for header marker in document.
 * FUNCTION
 *   Read document file line by line, and search each line for 
 *   any of the headers defined in the array  header_markers (OR
 *   if using the -rh switch, robo_head)
 * SYNOPSIS
 */
static struct RB_HeaderType *RB_Find_Marker(
    FILE *document,
    int *is_internal,
    int reuse_previous_line )
/*
 * INPUTS
 *   document - pointer to the file to be searched.
 *   the gobal buffer line_buffer.
 * OUTPUT
 *   o document will point to the line after the line with 
 *     the header marker.
 *   o is_internal will be TRUE if the header is an internal
 *     header.
 * RESULT
 *   o header type
 * BUGS
 *   Bad use of feof(), fgets().
 * SEE ALSO
 *   Find_End_Marker
 * SOURCE
 */
{
    int                 found;
    char               *cur_char;
    struct RB_HeaderType *header_type = 0;

    cur_char = NULL;
    found = FALSE;
    while ( !feof( document ) && !found )
    {
        if ( reuse_previous_line )
        {
            /* reuse line in the line_buffer */
            reuse_previous_line = FALSE;
        }
        else
        {
            RB_FreeLineBuffer(  );
            myLine = RB_ReadWholeLine( document, line_buffer, &readChars );
        }
        if ( !feof( document ) )
        {
            line_number++;
            found = RB_Is_Begin_Marker( myLine, &cur_char );
            if ( found )
            {
                header_type = AnalyseHeaderType( &cur_char, is_internal );
                RB_Say( "found header marker of type %s\n", SAY_DEBUG,
                        header_type->indexName );
            }
        }
    }

    return header_type;
}

/******** END RB_Find_Marker ******/


/****f* Analyser/AnalyseHeaderType
 * FUNCTION
 *   Determine the type of the header.
 * SYNOPSIS
 */
struct RB_HeaderType *AnalyseHeaderType(
    char **cur_char,
    int *is_internal )
/*
 * INPUTS
 *   o cur_char -- pointer to the header type character
 * OUTPUT
 *   o is_internal -- indicates if it is an internal header or not.*
 *   o cur_char -- points to the header type character
 * RESULT
 *   o pointer to a RB_HeaderType
 * SOURCE
 */
{
    struct RB_HeaderType *headertype = 0;

    *is_internal = RB_IsInternalHeader( **cur_char );

    if ( *is_internal )
    {
        /* Skip the character */
        ++( *cur_char );
    }
    headertype = RB_FindHeaderType( **cur_char );
    if ( !headertype )
    {
        RB_Panic( "Undefined headertype (%c)\n", **cur_char );
    }

    return headertype;
}

/*******/



/****f* Analyser/Find_End_Marker
 * FUNCTION
 *   Scan and store all lines from a source file until
 *   an end marker is found.
 * SYNOPSIS
 */
static int Find_End_Marker(
    FILE *document,
    struct RB_header *new_header )
/*
 * INPUTS
 *   o document -- a pointer to an opened source file.
 * OUTPUT
 *   o new_header -- the lines of source code will be added
 *                   here.
 * RESULT
 *   o TRUE  -- an end marker was found.
 *   o FALSE -- no end marker was found while scanning the
 *              source file.
 * SOURCE
 */
{
    int                 found = FALSE;
    unsigned int        no_lines = 0;
    unsigned int        max_no_lines = 10;
    char              **lines = NULL;
    char              **new_lines = NULL;
    char               *dummy;

    lines = ( char ** ) calloc( max_no_lines, sizeof( char * ) );
    if ( !lines )
    {
        RB_Panic( "Out of memory! %s()\n", "Find_End_Marker" );
    }

    while ( !feof( document ) )
    {
        RB_FreeLineBuffer(  );
        myLine = RB_ReadWholeLine( document, line_buffer, &readChars );
        ++line_number;          /* global linecounter, koessi */
        if ( RB_Is_Begin_Marker( myLine, &dummy ) )
        {
            /* Bad... found a begin marker but was expecting to
               find an end marker.  Panic... */
            found = FALSE;
            return found;
        }
        else if ( RB_Is_End_Marker( myLine ) )
        {
            RB_Say( "Found end marker \"%s\"", SAY_DEBUG, myLine );
            found = TRUE;
            break;
        }
        else
        {
            unsigned int        n;
            char               *line;

            line = RB_StrDup( myLine );
            n = strlen( line );
            assert( n > 0 );
            assert( line[n - 1] == '\n' );
            /* Strip CR */
            line[n - 1] = '\0';
            lines[no_lines] = line;
            ++no_lines;
            if ( no_lines == max_no_lines )
            {
                max_no_lines *= 2;
                new_lines = realloc( lines, max_no_lines * sizeof( char * ) );

                if ( !new_lines )
                {
                    RB_Panic( "Out of memory! %s()\n", "Find_End_Marker" );
                }

                lines = new_lines;
            }
        }
    }

    new_header->no_lines = no_lines;
    new_header->lines = lines;

    return found;
}


/******/


/* TODO Documentation */
static void Remove_Trailing_Asterics(
    char *line )
{
    int                 i = strlen( line ) - 1;

    for ( ; ( i > 0 ) && utf8_isspace( line[i] ); i-- )
    {
        /* Empty */
    }
    for ( ; ( i > 0 ) && ( line[i] == '*' ); i-- )
    {
        line[i] = ' ';
    }
}


/****if* Utilities/RB_WordWithSpacesLen
 * SYNOPSIS
 */
static int RB_WordWithSpacesLen(
    char *str )
/*
 * FUNCTION
 *   get the amount of bytes until next separator character or ignore character
 *   or end of line
 * INPUTS
 *   char *str      -- the line
 * RESULT
 *   int -- length of the next word or 0
 * SEE ALSO
 *   RB_Find_Header_Name()
 * SOURCE
 */
{
    int                 len;
    char                c;

    for ( len = 0; ( ( c = *str ) != '\0' ) && ( c != '\n' ); ++str, ++len )
    {
        // Look for header truncating characters
        if ( Find_Parameter_Char( &( configuration.header_separate_chars ),
                                  c ) != NULL
             ||
             Find_Parameter_Char( &( configuration.header_ignore_chars ),
                                  c ) != NULL )
        {
            // and exit loop if any found
            break;
        }
    }
    /* Don't count any of the trailing spaces. */
    if ( len )
    {
        --str;
        for ( ; utf8_isspace( *str ); --len, --str )
        {
            /* empty */
        }
    }
    return ( len );
}

/*** RB_WordWithSpacesLen ***/


/* TODO Documentation */
static int Find_Header_Name(
    FILE *fh,
    struct RB_header *hdr )
{
    char               *cur_char = myLine;
    char              **names = NULL;
    int                 num = 0;

    Remove_Trailing_Asterics( cur_char );
    skip_while( *cur_char != '*' );
    skip_while( !utf8_isspace( *cur_char ) );
    skip_while( utf8_isspace( *cur_char ) );
    while ( *cur_char )
    {
        int                 length = RB_WordWithSpacesLen( cur_char );

        if ( length == 0 )
            break;
        names = realloc( names, ( ++num ) * sizeof *names );

        if ( !names )
        {
            RB_Panic( "Out of memory! %s()\n", "Find_Header_Name" );
        }

        names[num - 1] = RB_StrDupLen( cur_char, length );
        /* printf("%c adding name = %s\n", num > 1 ? ' ' : '*', names[ num - 1 ] ); */
        cur_char += length;
        if ( Find_Parameter_Char( &( configuration.header_separate_chars ),
                                  *cur_char ) )
        {
            for ( cur_char++; utf8_isspace( *cur_char ); cur_char++ );
            /* End of line reach, but comma encountered, more headernames follow on next line */
            if ( *cur_char == 0 )
            {
                /* Skip comment */
                RB_FreeLineBuffer(  );
                myLine = RB_ReadWholeLine( fh, line_buffer, &readChars );
                line_number++;
                for ( cur_char = myLine; *cur_char && !utf8_isalpha( *cur_char );
                      cur_char++ );
            }
        }
    }
    hdr->names = names;
    hdr->no_names = num;
    hdr->name = num ? names[0] : NULL;
    return num;
}

/*****  Find_Header_Name  *****/