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
|
/* install-info -- create Info directory entry(ies) for an Info file.
Copyright (C) 1996 Free Software Foundation, Inc.
$Id: install-info.c,v 1.12 1996/10/03 23:13:36 karl Exp $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#define INSTALL_INFO_VERSION_STRING "GNU install-info (Texinfo 3.9) 1.2"
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <sys/types.h>
/* Get O_RDONLY. */
#ifdef HAVE_SYS_FCNTL_H
#include <sys/fcntl.h>
#else
#include <fcntl.h>
#endif /* !HAVE_SYS_FCNTL_H */
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
/* Name this program was invoked with. */
char *progname;
char *readfile ();
struct line_data *findlines ();
char *my_strerror ();
void fatal ();
void insert_entry_here ();
int compare_section_names ();
struct spec_entry;
/* Data structures. */
/* Record info about a single line from a file
as read into core. */
struct line_data
{
/* The start of the line. */
char *start;
/* The number of characters in the line,
excluding the terminating newline. */
int size;
/* Vector containing pointers to the entries to add before this line.
The vector is null-terminated. */
struct spec_entry **add_entries_before;
/* 1 means output any needed new sections before this line. */
int add_sections_before;
/* 1 means don't output this line. */
int delete;
};
/* This is used for a list of the specified menu section names
in which entries should be added. */
struct spec_section
{
struct spec_section *next;
char *name;
/* 1 means we have not yet found an existing section with this name
in the dir file--so we will need to add a new section. */
int missing;
};
/* This is used for a list of the entries specified to be added. */
struct spec_entry
{
struct spec_entry *next;
char *text;
};
/* This is used for a list of nodes found by parsing the dir file. */
struct node
{
struct node *next;
/* The node name. */
char *name;
/* The line number of the line where the node starts.
This is the line that contains control-underscore. */
int start_line;
/* The line number of the line where the node ends,
which is the end of the file or where the next line starts. */
int end_line;
/* Start of first line in this node's menu
(the line after the * Menu: line). */
char *menu_start;
/* The start of the chain of sections in this node's menu. */
struct menu_section *sections;
/* The last menu section in the chain. */
struct menu_section *last_section;
};
/* This is used for a list of sections found in a node's menu.
Each struct node has such a list in the sections field. */
struct menu_section
{
struct menu_section *next;
char *name;
/* Line number of start of section. */
int start_line;
/* Line number of end of section. */
int end_line;
};
/* Memory allocation and string operations. */
/* Like malloc but get fatal error if memory is exhausted. */
void *
xmalloc (size)
unsigned int size;
{
extern void *malloc ();
void *result = malloc (size);
if (result == NULL)
fatal ("virtual memory exhausted", 0);
return result;
}
/* Like malloc but get fatal error if memory is exhausted. */
void *
xrealloc (obj, size)
void *obj;
unsigned int size;
{
extern void *realloc ();
void *result = realloc (obj, size);
if (result == NULL)
fatal ("virtual memory exhausted", 0);
return result;
}
/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
char *
concat (s1, s2, s3)
char *s1, *s2, *s3;
{
int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
strcpy (result, s1);
strcpy (result + len1, s2);
strcpy (result + len1 + len2, s3);
*(result + len1 + len2 + len3) = 0;
return result;
}
/* Return a string containing SIZE characters
copied from starting at STRING. */
char *
copy_string (string, size)
char *string;
int size;
{
int i;
char *copy = (char *) xmalloc (size + 1);
for (i = 0; i < size; i++)
copy[i] = string[i];
copy[size] = 0;
return copy;
}
/* Error message functions. */
/* Print error message. `s1' is printf control string, `s2' is arg for it. */
/* VARARGS1 */
void
error (s1, s2, s3)
char *s1, *s2, *s3;
{
fprintf (stderr, "%s: ", progname);
fprintf (stderr, s1, s2, s3);
fprintf (stderr, "\n");
}
/* VARARGS1 */
void
warning (s1, s2, s3)
char *s1, *s2, *s3;
{
fprintf (stderr, "%s: Warning: ", progname);
fprintf (stderr, s1, s2, s3);
fprintf (stderr, "\n");
}
/* Print error message and exit. */
void
fatal (s1, s2, s3)
char *s1, *s2, *s3;
{
error (s1, s2, s3);
exit (1);
}
/* Print fatal error message based on errno, with file name NAME. */
void
pfatal_with_name (name)
char *name;
{
char *s = concat ("", my_strerror (errno), " for %s");
fatal (s, name);
}
/* Given the full text of a menu entry, null terminated,
return just the menu item name (copied). */
char *
extract_menu_item_name (item_text)
char *item_text;
{
char *p;
if (*item_text == '*')
item_text++;
while (*item_text == ' ')
item_text++;
p = item_text;
while (*p && *p != ':') p++;
return copy_string (item_text, p - item_text);
}
/* Given the full text of a menu entry, terminated by null or newline,
return just the menu item file (copied). */
char *
extract_menu_file_name (item_text)
char *item_text;
{
char *p = item_text;
/* If we have text that looks like * ITEM: (FILE)NODE...,
extract just FILE. Otherwise return "(none)". */
if (*p == '*')
p++;
while (*p == ' ')
p++;
/* Skip to and past the colon. */
while (*p && *p != '\n' && *p != ':') p++;
if (*p == ':') p++;
/* Skip past the open-paren. */
while (1)
{
if (*p == '(')
break;
else if (*p == ' ' || *p == '\t')
p++;
else
return "(none)";
}
p++;
item_text = p;
/* File name ends just before the close-paren. */
while (*p && *p != '\n' && *p != ')') p++;
if (*p != ')')
return "(none)";
return copy_string (item_text, p - item_text);
}
void
suggest_asking_for_help ()
{
fprintf (stderr, "\tTry `%s --help' for a complete list of options.\n",
progname);
exit (1);
}
void
print_help ()
{
printf ("%s [OPTION]... [INFO-FILE [DIR-FILE]]\n\
Install INFO-FILE in the Info directory file DIR-FILE.\n\
\n\
Options:\n\
--delete Delete existing entries in INFO-FILE;\n\
don't insert any new entries.\n\
--dir-file=NAME Specify file name of Info directory file.\n\
This is equivalent to using the DIR-FILE argument.\n\
--entry=TEXT Insert TEXT as an Info directory entry.\n\
TEXT should have the form of an Info menu item line\n\
plus zero or more extra lines starting with whitespace.\n\
If you specify more than one entry, they are all added.\n\
If you don't specify any entries, they are determined\n\
from information in the Info file itself.\n\
--help Display this help and exit.\n\
--info-file=FILE Specify Info file to install in the directory.\n\
This is equivalent to using the INFO-FILE argument.\n\
--info-dir=DIR Same as --dir-file=DIR/dir.\n\
--item=TEXT Same as --entry TEXT.\n\
An Info directory entry is actually a menu item.\n\
--quiet Suppress warnings.\n\
--remove Same as --delete.\n\
--section=SEC Put this file's entries in section SEC of the directory.\n\
If you specify more than one section, all the entries\n\
are added in each of the sections.\n\
If you don't specify any sections, they are determined\n\
from information in the Info file itself.\n\
--version Display version information and exit.\n\
\n\
Email bug reports to bug-texinfo@prep.ai.mit.edu.\n\
", progname);
}
/* Convert an errno value into a string describing the error.
We define this function here rather than using strerror
because not all systems have strerror. */
char *
my_strerror (errnum)
int errnum;
{
extern char *sys_errlist[];
extern int sys_nerr;
if (errnum >= 0 && errnum < sys_nerr)
return sys_errlist[errnum];
return (char *) "Unknown error";
}
/* This table defines all the long-named options, says whether they
use an argument, and maps them into equivalent single-letter options. */
struct option longopts[] =
{
{ "delete", no_argument, NULL, 'r' },
{ "dir-file", required_argument, NULL, 'd' },
{ "entry", required_argument, NULL, 'e' },
{ "help", no_argument, NULL, 'h' },
{ "info-dir", required_argument, NULL, 'D' },
{ "info-file", required_argument, NULL, 'i' },
{ "item", required_argument, NULL, 'e' },
{ "quiet", no_argument, NULL, 'q' },
{ "remove", no_argument, NULL, 'r' },
{ "section", required_argument, NULL, 's' },
{ "version", no_argument, NULL, 'V' },
{ 0 }
};
main (argc, argv)
int argc;
char **argv;
{
char *infile = 0, *dirfile = 0;
char *infile_sans_info;
unsigned infilelen_sans_info;
FILE *output;
/* Record the text of the Info file, as a sequence of characters
and as a sequence of lines. */
char *input_data;
int input_size;
struct line_data *input_lines;
int input_nlines;
/* Record here the specified section names and directory entries. */
struct spec_section *input_sections = NULL;
struct spec_entry *entries_to_add = NULL;
int n_entries_to_add = 0;
/* Record the old text of the dir file, as plain characters,
as lines, and as nodes. */
char *dir_data;
int dir_size;
int dir_nlines;
struct line_data *dir_lines;
struct node *dir_nodes;
/* Nonzero means --delete was specified (just delete existing entries). */
int delete_flag = 0;
int something_deleted = 0;
/* Nonzero means -q was specified. */
int quiet_flag = 0;
int node_header_flag;
int prefix_length;
int i;
progname = argv[0];
while (1)
{
int opt = getopt_long (argc, argv, "i:d:e:s:hHr", longopts, 0);
if (opt == EOF)
break;
switch (opt)
{
case 0:
/* If getopt returns 0, then it has already processed a
long-named option. We should do nothing. */
break;
case 1:
abort ();
case 'd':
if (dirfile)
{
fprintf (stderr, "%s: Specify the Info directory only once.\n",
progname);
suggest_asking_for_help ();
}
dirfile = optarg;
break;
case 'D':
if (dirfile)
{
fprintf (stderr, "%s: Specify the Info directory only once.\n",
progname);
suggest_asking_for_help ();
}
dirfile = concat (optarg, "", "/dir");
break;
case 'e':
{
struct spec_entry *next
= (struct spec_entry *) xmalloc (sizeof (struct spec_entry));
if (! (*optarg != 0 && optarg[strlen (optarg) - 1] == '\n'))
optarg = concat (optarg, "\n", "");
next->text = optarg;
next->next = entries_to_add;
entries_to_add = next;
n_entries_to_add++;
}
break;
case 'h':
case 'H':
print_help ();
exit (0);
case 'i':
if (infile)
{
fprintf (stderr, "%s: Specify the Info file only once.\n",
progname);
suggest_asking_for_help ();
}
infile = optarg;
break;
case 'q':
quiet_flag = 1;
break;
case 'r':
delete_flag = 1;
break;
case 's':
{
struct spec_section *next
= (struct spec_section *) xmalloc (sizeof (struct spec_section));
next->name = optarg;
next->next = input_sections;
next->missing = 1;
input_sections = next;
}
break;
case 'V':
puts (INSTALL_INFO_VERSION_STRING);
puts ("Copyright (C) 1996 Free Software Foundation, Inc.\n\
There is NO warranty. You may redistribute this software\n\
under the terms of the GNU General Public License.\n\
For more information about these matters, see the files named COPYING.");
exit (0);
default:
suggest_asking_for_help ();
}
}
/* Interpret the non-option arguments as file names. */
for (; optind < argc; ++optind)
{
if (infile == 0)
infile = argv[optind];
else if (dirfile == 0)
dirfile = argv[optind];
else
error ("excess command line argument `%s'", argv[optind]);
}
if (!infile)
fatal ("No input file specified");
if (!dirfile)
fatal ("No dir file specified");
/* Read the Info file and parse it into lines. */
input_data = readfile (infile, &input_size);
input_lines = findlines (input_data, input_size, &input_nlines);
/* Parse the input file to find the section names it specifies. */
if (input_sections == 0)
{
prefix_length = strlen ("INFO-DIR-SECTION ");
for (i = 0; i < input_nlines; i++)
{
if (!strncmp ("INFO-DIR-SECTION ", input_lines[i].start,
prefix_length))
{
struct spec_section *next
= (struct spec_section *) xmalloc (sizeof (struct spec_section));
next->name = copy_string (input_lines[i].start + prefix_length,
input_lines[i].size - prefix_length);
next->next = input_sections;
next->missing = 1;
input_sections = next;
}
}
}
/* Default to section "Miscellaneous" if no sections specified. */
if (input_sections == 0)
{
input_sections
= (struct spec_section *) xmalloc (sizeof (struct spec_section));
input_sections->name = "Miscellaneous";
input_sections->next = 0;
input_sections->missing = 1;
}
/* Now find the directory entries specified in the file
and put them on entries_to_add. But not if entries
were specified explicitly with command options. */
if (entries_to_add == 0)
{
char *start_of_this_entry = 0;
for (i = 0; i < input_nlines; i++)
{
if (!strncmp ("START-INFO-DIR-ENTRY", input_lines[i].start,
input_lines[i].size)
&& sizeof ("START-INFO-DIR-ENTRY") - 1 == input_lines[i].size)
{
if (start_of_this_entry != 0)
fatal ("START-INFO-DIR-ENTRY without matching END-INFO-DIR-ENTRY");
start_of_this_entry = input_lines[i + 1].start;
}
if (!strncmp ("END-INFO-DIR-ENTRY", input_lines[i].start,
input_lines[i].size)
&& sizeof ("END-INFO-DIR-ENTRY") - 1 == input_lines[i].size)
{
if (start_of_this_entry != 0)
{
struct spec_entry *next
= (struct spec_entry *) xmalloc (sizeof (struct spec_entry));
next->text = copy_string (start_of_this_entry,
input_lines[i].start - start_of_this_entry);
next->next = entries_to_add;
entries_to_add = next;
n_entries_to_add++;
start_of_this_entry = 0;
}
else
fatal ("END-INFO-DIR-ENTRY without matching START-INFO-DIR-ENTRY");
}
}
if (start_of_this_entry != 0)
fatal ("START-INFO-DIR-ENTRY without matching END-INFO-DIR-ENTRY");
}
if (!delete_flag)
if (entries_to_add == 0)
fatal ("no info dir entry in `%s'", infile);
/* Now read in the Info dir file. */
dir_data = readfile (dirfile, &dir_size);
dir_lines = findlines (dir_data, dir_size, &dir_nlines);
/* We will be comparing the entries in the dir file against the
current filename, so need to strip off any directory prefix and any
.info suffix. */
{
unsigned basename_len;
extern char *strrchr ();
char *infile_basename = strrchr (infile, '/');
if (infile_basename)
infile_basename++;
else
infile_basename = infile;
basename_len = strlen (infile_basename);
infile_sans_info
= (strlen (infile_basename) > 5
&& strcmp (infile_basename + basename_len - 5, ".info") == 0)
? copy_string (infile_basename, basename_len - 5)
: infile_basename;
infilelen_sans_info = strlen (infile_sans_info);
}
/* Parse the dir file. Find all the nodes, and their menus,
and the sections of their menus. */
dir_nodes = 0;
node_header_flag = 0;
for (i = 0; i < dir_nlines; i++)
{
/* Parse node header lines. */
if (node_header_flag)
{
int j, end;
for (j = 0; j < dir_lines[i].size; j++)
/* Find the node name and store it in the `struct node'. */
if (!strncmp ("Node:", dir_lines[i].start + j, 5))
{
char *line = dir_lines[i].start;
/* Find the start of the node name. */
j += 5;
while (line[j] == ' ' || line[j] == '\t')
j++;
/* Find the end of the node name. */
end = j;
while (line[end] != 0 && line[end] != ',' && line[end] != '\n'
&& line[end] != '\t')
end++;
dir_nodes->name = copy_string (line + j, end - j);
}
node_header_flag = 0;
}
/* Notice the start of a node. */
if (*dir_lines[i].start == 037)
{
struct node *next
= (struct node *) xmalloc (sizeof (struct node));
next->next = dir_nodes;
next->name = NULL;
next->start_line = i;
next->end_line = 0;
next->menu_start = NULL;
next->sections = NULL;
next->last_section = NULL;
if (dir_nodes != 0)
dir_nodes->end_line = i;
/* Fill in the end of the last menu section
of the previous node. */
if (dir_nodes != 0 && dir_nodes->last_section != 0)
dir_nodes->last_section->end_line = i;
dir_nodes = next;
/* The following line is the header of this node;
parse it. */
node_header_flag = 1;
}
/* Notice the lines that start menus. */
if (dir_nodes != 0
&& !strncmp ("* Menu:", dir_lines[i].start, 7))
dir_nodes->menu_start = dir_lines[i + 1].start;
/* Notice sections in menus. */
if (dir_nodes != 0
&& dir_nodes->menu_start != 0
&& *dir_lines[i].start != '\n'
&& *dir_lines[i].start != '*'
&& *dir_lines[i].start != ' '
&& *dir_lines[i].start != '\t')
{
/* Add this menu section to the node's list.
This list grows in forward order. */
struct menu_section *next
= (struct menu_section *) xmalloc (sizeof (struct menu_section));
next->start_line = i + 1;
next->next = 0;
next->end_line = 0;
next->name = copy_string (dir_lines[i].start, dir_lines[i].size);
if (dir_nodes->sections)
{
dir_nodes->last_section->next = next;
dir_nodes->last_section->end_line = i;
}
else
dir_nodes->sections = next;
dir_nodes->last_section = next;
}
/* Check for an existing entry that should be deleted.
Delete all entries which specify this file name. */
if (*dir_lines[i].start == '*')
{
char *p = dir_lines[i].start;
while (*p != 0 && *p != ':')
p++;
p++;
while (*p == ' ') p++;
if (*p == '(')
{
p++;
if ((dir_lines[i].size
> (p - dir_lines[i].start + infilelen_sans_info))
&& !strncmp (p, infile_sans_info, infilelen_sans_info)
&& p[infilelen_sans_info] == ')')
dir_lines[i].delete = 1;
}
}
/* Treat lines that start with whitespace
as continuations; if we are deleting an entry,
delete all its continuations as well. */
else if (i > 0
&& (*dir_lines[i].start == ' '
|| *dir_lines[i].start == '\t'))
{
dir_lines[i].delete = dir_lines[i - 1].delete;
something_deleted = 1;
}
}
/* Finish the info about the end of the last node. */
if (dir_nodes != 0)
{
dir_nodes->end_line = dir_nlines;
if (dir_nodes->last_section != 0)
dir_nodes->last_section->end_line = dir_nlines;
}
/* Decide where to add the new entries (unless --delete was used).
Find the menu sections to add them in.
In each section, find the proper alphabetical place to add
each of the entries. */
if (!delete_flag)
{
struct node *node;
struct menu_section *section;
struct spec_section *spec;
for (node = dir_nodes; node; node = node->next)
for (section = node->sections; section; section = section->next)
{
for (i = section->end_line; i > section->start_line; i--)
if (dir_lines[i - 1].size != 0)
break;
section->end_line = i;
for (spec = input_sections; spec; spec = spec->next)
if (!strcmp (spec->name, section->name))
break;
if (spec)
{
int add_at_line = section->end_line;
struct spec_entry *entry;
/* Say we have found at least one section with this name,
so we need not add such a section. */
spec->missing = 0;
/* For each entry, find the right place in this section
to add it. */
for (entry = entries_to_add; entry; entry = entry->next)
{
int textlen = strlen (entry->text);
/* Subtract one because dir_lines is zero-based,
but the `end_line' and `start_line' members are
one-based. */
for (i = section->end_line - 1;
i >= section->start_line - 1; i--)
{
/* If an entry exists with the same name,
and was not marked for deletion
(which means it is for some other file),
we are in trouble. */
if (dir_lines[i].start[0] == '*'
&& menu_line_equal (entry->text, textlen,
dir_lines[i].start,
dir_lines[i].size)
&& !dir_lines[i].delete)
fatal ("menu item `%s' already exists, for file `%s'",
extract_menu_item_name (entry->text),
extract_menu_file_name (dir_lines[i].start));
if (dir_lines[i].start[0] == '*'
&& menu_line_lessp (entry->text, textlen,
dir_lines[i].start,
dir_lines[i].size))
add_at_line = i;
}
insert_entry_here (entry, add_at_line,
dir_lines, n_entries_to_add);
}
}
}
/* Mark the end of the Top node as the place to add any
new sections that are needed. */
for (node = dir_nodes; node; node = node->next)
if (node->name && strcmp (node->name, "Top") == 0)
dir_lines[node->end_line].add_sections_before = 1;
}
if (delete_flag && !something_deleted && !quiet_flag)
warning ("no entries found for `%s'; nothing deleted", infile);
/* Output the old dir file, interpolating the new sections
and/or new entries where appropriate. */
output = fopen (dirfile, "w");
if (!output)
{
perror (dirfile);
exit (1);
}
for (i = 0; i <= dir_nlines; i++)
{
int j;
/* If we decided to output some new entries before this line,
output them now. */
if (dir_lines[i].add_entries_before)
for (j = 0; j < n_entries_to_add; j++)
{
struct spec_entry *this = dir_lines[i].add_entries_before[j];
if (this == 0)
break;
fputs (this->text, output);
}
/* If we decided to add some sections here
because there are no such sections in the file,
output them now. */
if (dir_lines[i].add_sections_before)
{
struct spec_section *spec;
struct spec_section **sections;
int n_sections = 0;
/* Count the sections and allocate a vector for all of them. */
for (spec = input_sections; spec; spec = spec->next)
n_sections++;
sections = ((struct spec_section **)
xmalloc (n_sections * sizeof (struct spec_section *)));
/* Fill the vector SECTIONS with pointers to all the sections,
and sort them. */
j = 0;
for (spec = input_sections; spec; spec = spec->next)
sections[j++] = spec;
qsort (sections, n_sections, sizeof (struct spec_section *),
compare_section_names);
/* Generate the new sections in alphabetical order.
In each new section, output all of our entries. */
for (j = 0; j < n_sections; j++)
{
spec = sections[j];
if (spec->missing)
{
struct spec_entry *entry;
putc ('\n', output);
fputs (spec->name, output);
putc ('\n', output);
for (entry = entries_to_add; entry; entry = entry->next)
fputs (entry->text, output);
}
}
free (sections);
}
/* Output the original dir lines unless marked for deletion. */
if (i < dir_nlines && !dir_lines[i].delete)
{
fwrite (dir_lines[i].start, 1, dir_lines[i].size, output);
putc ('\n', output);
}
}
fclose (output);
exit (0);
}
/* Read all of file FILNAME into memory
and return the address of the data.
Store the size into SIZEP.
If there is trouble, do a fatal error. */
char *
readfile (filename, sizep)
char *filename;
int *sizep;
{
int data_size = 1024;
char *data = (char *) xmalloc (data_size);
int filled = 0;
int nread = 0;
int desc = open (filename, O_RDONLY);
if (desc < 0)
pfatal_with_name (filename);
while (1)
{
nread = read (desc, data + filled, data_size - filled);
if (nread < 0)
pfatal_with_name (filename);
if (nread == 0)
break;
filled += nread;
if (filled == data_size)
{
data_size *= 2;
data = (char *) xrealloc (data, data_size);
}
}
*sizep = filled;
return data;
}
/* Divide the text at DATA (of SIZE bytes) into lines.
Return a vector of struct line_data describing the lines.
Store the length of that vector into *NLINESP. */
struct line_data *
findlines (data, size, nlinesp)
char *data;
int size;
int *nlinesp;
{
struct line_data *lines;
int lines_allocated = 512;
int filled = 0;
int i = 0;
int lineflag;
lines = (struct line_data *) xmalloc (lines_allocated * sizeof (struct line_data));
lineflag = 1;
for (i = 0; i < size; i++)
{
if (lineflag)
{
if (filled == lines_allocated)
{
lines_allocated *= 2;
lines = (struct line_data *) xrealloc (lines, lines_allocated * sizeof (struct line_data));
}
lines[filled].start = &data[i];
lines[filled].add_entries_before = 0;
lines[filled].add_sections_before = 0;
lines[filled].delete = 0;
if (filled > 0)
lines[filled - 1].size
= lines[filled].start - lines[filled - 1].start - 1;
filled++;
}
lineflag = (data[i] == '\n');
}
if (filled > 0)
lines[filled - 1].size = &data[i] - lines[filled - 1].start - lineflag;
/* Do not leave garbage in the last element. */
lines[filled].start = NULL;
lines[filled].add_entries_before = NULL;
lines[filled].add_sections_before = 0;
lines[filled].delete = 0;
lines[filled].size = 0;
*nlinesp = filled;
return lines;
}
/* Compare the menu item names in LINE1 (line length LEN1)
and LINE2 (line length LEN2). Return 1 if the item name
in LINE1 is less, 0 otherwise. */
int
menu_line_lessp (line1, len1, line2, len2)
char *line1;
int len1;
char *line2;
int len2;
{
int minlen = (len1 < len2 ? len1 : len2);
int i;
for (i = 0; i < minlen; i++)
{
/* If one item name is a prefix of the other,
the former one is less. */
if (line1[i] == ':' && line2[i] != ':')
return 1;
if (line2[i] == ':' && line1[i] != ':')
return 0;
/* If they both continue and differ, one is less. */
if (line1[i] < line2[i])
return 1;
if (line1[i] > line2[i])
return 0;
}
/* With a properly formatted dir file,
we can only get here if the item names are equal. */
return 0;
}
/* Compare the menu item names in LINE1 (line length LEN1)
and LINE2 (line length LEN2). Return 1 if the item names are equal,
0 otherwise. */
int
menu_line_equal (line1, len1, line2, len2)
char *line1;
int len1;
char *line2;
int len2;
{
int minlen = (len1 < len2 ? len1 : len2);
int i;
for (i = 0; i < minlen; i++)
{
/* If both item names end here, they are equal. */
if (line1[i] == ':' && line2[i] == ':')
return 1;
/* If they both continue and differ, one is less. */
if (line1[i] != line2[i])
return 0;
}
/* With a properly formatted dir file,
we can only get here if the item names are equal. */
return 1;
}
/* This is the comparison function for qsort
for a vector of pointers to struct spec_section.
Compare the section names. */
int
compare_section_names (sec1, sec2)
struct spec_section **sec1, **sec2;
{
char *name1 = (*sec1)->name;
char *name2 = (*sec2)->name;
return strcmp (name1, name2);
}
/* Insert ENTRY into the add_entries_before vector
for line number LINE_NUMBER of the dir file.
DIR_LINES and N_ENTRIES carry information from like-named variables
in main. */
void
insert_entry_here (entry, line_number, dir_lines, n_entries)
struct spec_entry *entry;
int line_number;
struct line_data *dir_lines;
int n_entries;
{
int i;
if (dir_lines[line_number].add_entries_before == 0)
{
dir_lines[line_number].add_entries_before
= (struct spec_entry **) xmalloc (n_entries * sizeof (struct spec_entry *));
for (i = 0; i < n_entries; i++)
dir_lines[line_number].add_entries_before[i] = 0;
}
for (i = 0; i < n_entries; i++)
if (dir_lines[line_number].add_entries_before[i] == 0)
break;
if (i == n_entries)
abort ();
dir_lines[line_number].add_entries_before[i] = entry;
}
|