-
Notifications
You must be signed in to change notification settings - Fork 59
/
scan.c
2350 lines (2148 loc) · 58.9 KB
/
scan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* zipl - zSeries Initial Program Loader tool
*
* Scanner for zipl.conf configuration files
*
* Copyright IBM Corp. 2001, 2017
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*
*/
static const char *VERSION_KEYWORD = "version";
/* Need ISOC99 function isblank() in ctype.h */
#ifndef __USE_ISOC99
#define __USE_ISOC99
#endif
/* Need GNU function strverscmp() in dirent.h */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/stat.h>
#include "lib/util_base.h"
#include "boot.h"
#include "error.h"
#include "misc.h"
#include "scan.h"
/* Determines which keyword may be present in which section */
enum scan_key_state scan_key_table[SCAN_SECTION_NUM][SCAN_KEYWORD_NUM] = {
/* defa dump dump imag para parm ramd segm targ prom time defa tape mv
* ult to tofs e mete file isk ent et pt out ultm dump
* rs enu
*
* targ targ targ targ targ defa kdum secu opti
* etba etty etge etbl etof ulta p re onal
* se pe omet ocks fset uto
* ry ize
*/
/* default auto */
{opt, inv, inv, inv, inv, inv, inv, inv, req, opt, opt, inv, inv, inv,
opt, opt, opt, opt, opt, opt, inv, opt, inv},
/* default menu */
{inv, inv, inv, inv, inv, inv, inv, inv, inv, inv, inv, req, inv, inv,
inv, inv, inv, inv, inv, inv, inv, opt, inv},
/* default section */
{req, inv, inv, inv, inv, inv, inv, inv, inv, inv, inv, inv, inv, inv,
inv, inv, inv, inv, inv, inv, inv, opt, inv},
/* ipl */
{inv, inv, inv, req, opt, opt, opt, inv, req, inv, inv, inv, inv, inv,
opt, opt, opt, opt, opt, inv, opt, opt, opt},
/* segment load */
{inv, inv, inv, inv, inv, inv, inv, req, req, inv, inv, inv, inv, inv,
inv, inv, inv, inv, inv, inv, inv, inv, inv},
/* part dump */
{inv, req, inv, inv, inv, inv, inv, inv, opt, inv, inv, inv, inv, inv,
inv, inv, inv, inv, inv, inv, inv, inv, inv},
/* fs dump */
{inv, inv, req, inv, opt, opt, inv, inv, req, inv, inv, inv, inv, inv,
inv, inv, inv, inv, inv, inv, inv, inv, inv},
/* ipl tape */
{inv, inv, inv, req, opt, opt, opt, inv, inv, inv, inv, inv, req, inv,
inv, inv, inv, inv, inv, inv, inv, inv, inv},
/* multi volume dump */
{inv, inv, inv, inv, inv, inv, inv, inv, inv, inv, inv, inv, inv, req,
inv, inv, inv, inv, inv, inv, inv, inv, inv}
};
/* Determines which keyword may be present in a menu section */
static enum scan_key_state scan_menu_key_table[SCAN_KEYWORD_NUM] = {
/* menu section */
opt, inv, inv, inv, inv, inv, inv, inv, req, opt, opt, inv, inv, inv,
opt, opt, opt, opt, opt, inv, inv, opt, inv
};
/* Mapping of keyword IDs to strings */
static const struct {
char* keyword;
enum scan_keyword_id id;
} keyword_list[] = {
{ "defaultmenu", scan_keyword_defaultmenu},
{ "default", scan_keyword_default },
{ "dumptofs", scan_keyword_dumptofs },
{ "dumpto", scan_keyword_dumpto },
{ "image", scan_keyword_image },
{ "mvdump", scan_keyword_mvdump },
{ "parameters", scan_keyword_parameters },
{ "parmfile", scan_keyword_parmfile },
{ "ramdisk", scan_keyword_ramdisk },
{ "segment", scan_keyword_segment },
{ "targetbase", scan_keyword_targetbase},
{ "targettype", scan_keyword_targettype},
{ "targetgeometry", scan_keyword_targetgeometry},
{ "targetblocksize", scan_keyword_targetblocksize},
{ "targetoffset", scan_keyword_targetoffset},
{ "target", scan_keyword_target},
{ "prompt", scan_keyword_prompt},
{ "timeout", scan_keyword_timeout},
{ "tape", scan_keyword_tape},
{ "kdump", scan_keyword_kdump},
{ "secure", scan_keyword_secure},
{ "optional", scan_keyword_optional},
};
/* List of keywords that are used without an assignment */
static const struct {
char* keyword;
enum scan_keyword_id id;
} keyword_only_list[] = {
{ "defaultauto", scan_keyword_defaultauto}
};
/* Retrieve name of keyword identified by ID. */
char *
scan_keyword_name(enum scan_keyword_id id)
{
unsigned int i;
for (i=0; i < ARRAY_SIZE(keyword_list); i++) {
if (id == keyword_list[i].id)
return keyword_list[i].keyword;
}
for (i=0; i < ARRAY_SIZE(keyword_only_list); i++) {
if (id == keyword_only_list[i].id)
return keyword_only_list[i].keyword;
}
return "<unknown>";
}
/* Advance the current file pointer of file buffer FILE until the current
* character is no longer a blank. Return 0 if at least one blank
* character was encountered, non-zero otherwise. */
static int
skip_blanks(struct misc_file_buffer* file)
{
int rc;
rc = -1;
for (; isblank(misc_get_char(file, 0)); file->pos++)
rc = 0;
return rc;
}
/* Advance the current file position to beginning of next line in file buffer
* FILE or to end of file. */
static void
skip_line(struct misc_file_buffer* file)
{
for (;; file->pos++) {
switch (misc_get_char(file, 0)) {
case '\n':
file->pos++;
return;
case EOF:
return;
}
}
}
/* Skip trailing blanks of line. On success, return zero and set the file
* buffer position to beginning of next line or EOF. Return non-zero if
* non-blank characters were found before end of line. */
static int
skip_trailing_blanks(struct misc_file_buffer* file)
{
int current;
for (;; file->pos++) {
current = misc_get_char(file, 0);
if (current == '\n') {
file->pos++;
return 0;
} else if (current == EOF)
return 0;
else if (!isblank(current))
return -1;
}
}
static int
scan_section_heading(struct misc_file_buffer* file, struct scan_token* token,
int line)
{
int start_pos;
int end_pos;
int current;
char* name;
for (start_pos=file->pos; misc_get_char(file, 0) != ']'; file->pos++) {
current = misc_get_char(file, 0);
switch (current) {
case EOF:
case '\n':
error_reason("Line %d: unterminated section heading",
line);
return -1;
default:
if (!(isalnum(current) || ispunct(current))) {
error_reason("Line %d: invalid character in "
"section name", line);
return -1;
}
}
}
end_pos = file->pos;
if (end_pos == start_pos) {
error_reason("Line %d: empty section name", line);
return -1;
}
file->pos++;
if (skip_trailing_blanks(file)) {
error_reason("Line %d: unexpected characters after section "
"name", line);
return -1;
}
name = (char *) misc_malloc(end_pos - start_pos + 1);
if (name == NULL)
return -1;
memcpy(name, &file->buffer[start_pos], end_pos - start_pos);
name[end_pos - start_pos] = 0;
token->id = scan_id_section_heading;
token->line = line;
token->content.section.name = name;
return 0;
}
static int
scan_menu_heading(struct misc_file_buffer* file, struct scan_token* token,
int line)
{
int start_pos;
int end_pos;
int current;
char* name;
for (start_pos=file->pos; ; file->pos++) {
current = misc_get_char(file, 0);
if ((current == EOF) || (current == '\n'))
break;
else if (isblank(current))
break;
else if (!isalnum(current)) {
error_reason("Line %d: invalid character in menu name ",
line);
return -1;
}
}
end_pos = file->pos;
if (skip_trailing_blanks(file)) {
error_reason("Line %d: blanks not allowed in menu name",
line);
return -1;
}
if (end_pos == start_pos) {
error_reason("Line %d: empty menu name", line);
return -1;
}
name = (char *) misc_malloc(end_pos - start_pos + 1);
if (name == NULL)
return -1;
memcpy(name, &file->buffer[start_pos], end_pos - start_pos);
name[end_pos - start_pos] = 0;
token->id = scan_id_menu_heading;
token->line = line;
token->content.menu.name = name;
return 0;
}
static int
scan_number(struct misc_file_buffer* file, int* number, int line)
{
int start_pos;
int old_number;
int new_number;
old_number = 0;
new_number = 0;
start_pos = file->pos;
for (; isdigit(misc_get_char(file, 0)); file->pos++) {
new_number = old_number*10 + misc_get_char(file, 0) - '0';
if (new_number < old_number) {
error_reason("Line %d: number too large", line);
return -1;
}
old_number = new_number;
}
if (file->pos == start_pos) {
error_reason("Line %d: number expected", line);
return -1;
}
*number = new_number;
return 0;
}
static int
scan_value_string(struct misc_file_buffer* file, char** value, int line)
{
int quote;
int start_pos;
int end_pos;
int last_nonspace;
int current;
char* string;
current = misc_get_char(file, 0);
if (current == '\"') {
quote = '\"';
file->pos++;
} else if (current == '\'') {
quote = '\'';
file->pos++;
} else quote = 0;
last_nonspace = -1;
for (start_pos=file->pos;; file->pos++) {
current = misc_get_char(file, 0);
if ((current == EOF) || (current == '\n')) {
break;
} else if (quote) {
if (current == quote)
break;
} else if (!isblank(current))
last_nonspace = file->pos;
}
end_pos = file->pos;
if (quote) {
if (current != quote) {
error_reason("Line %d: unterminated quotes", line);
return -1;
}
} else if (last_nonspace >= 0)
end_pos = last_nonspace + 1;
string = (char *) misc_malloc(end_pos - start_pos + 1);
if (string == NULL)
return -1;
if (end_pos > start_pos)
memcpy(string, &file->buffer[start_pos], end_pos - start_pos);
string[end_pos - start_pos] = 0;
*value = string;
if (quote)
file->pos++;
return 0;
}
static int
scan_number_assignment(struct misc_file_buffer* file, struct scan_token* token,
int line)
{
int rc;
rc = scan_number(file, &token->content.number.number, line);
if (rc)
return rc;
skip_blanks(file);
if (misc_get_char(file, 0) != '=') {
error_reason("Line %d: number expected as keyword", line);
return -1;
}
file->pos++;
skip_blanks(file);
rc = scan_value_string(file, &token->content.number.value, line);
if (rc)
return rc;
if (skip_trailing_blanks(file)) {
error_reason("Line %d: unexpected characters at end of line",
line);
return -1;
}
token->id = scan_id_number_assignment;
token->line = line;
return 0;
}
static int
match_keyword(struct misc_file_buffer* file, const char* keyword)
{
unsigned int i;
for (i=0; i<strlen(keyword); i++)
if (misc_get_char(file, i) != keyword[i])
return -1;
return 0;
}
static int
scan_keyword(struct misc_file_buffer* file, enum scan_keyword_id* id, int line)
{
unsigned int i;
for (i=0; i < ARRAY_SIZE(keyword_list); i++)
if (match_keyword(file, keyword_list[i].keyword) == 0) {
file->pos += strlen(keyword_list[i].keyword);
*id = keyword_list[i].id;
return 0;
}
error_reason("Line %d: unknown keyword", line);
return -1;
}
static int
scan_keyword_assignment(struct misc_file_buffer* file, struct scan_token* token,
int line)
{
int rc;
rc = scan_keyword(file, &token->content.keyword.keyword, line);
if (rc)
return rc;
skip_blanks(file);
if (misc_get_char(file, 0) != '=') {
error_reason("Line %d: unexpected characters after keyword",
line);
return -1;
}
file->pos++;
skip_blanks(file);
rc = scan_value_string(file, &token->content.keyword.value, line);
if (rc)
return rc;
if (skip_trailing_blanks(file)) {
error_reason("Line %d: unexpected characters at end of line",
line);
return -1;
}
token->id = scan_id_keyword_assignment;
token->line = line;
return 0;
}
static int
scan_keyword_only(struct misc_file_buffer* file, enum scan_keyword_id* id,
int line)
{
unsigned int i;
for (i=0; i < ARRAY_SIZE(keyword_only_list); i++)
if (match_keyword(file, keyword_only_list[i].keyword) == 0) {
file->pos += strlen(keyword_only_list[i].keyword);
*id = keyword_only_list[i].id;
return 0;
}
error_reason("Line %d: unknown keyword", line);
return -1;
}
static int
scan_keyword_only_statement(struct misc_file_buffer* file,
struct scan_token* token, int line)
{
int rc;
rc = scan_keyword_only(file, &token->content.keyword.keyword, line);
if (rc)
return rc;
if (skip_trailing_blanks(file)) {
error_reason("Line %d: unexpected characters at end of line",
line);
return -1;
}
token->id = scan_id_keyword_only;
token->line = line;
return 0;
}
static int
search_line_for(struct misc_file_buffer* file, int search)
{
int i;
int current;
for (i=0; ; i++) {
current = misc_get_char(file, i);
switch (current) {
case EOF:
case '\n':
return 0;
default:
if (current == search)
return 1;
}
}
}
void
scan_free(struct scan_token* array)
{
int i;
for (i=0; array[i].id != 0; i++) {
switch (array[i].id) {
case scan_id_section_heading:
if (array[i].content.section.name != NULL) {
free(array[i].content.section.name);
array[i].content.section.name = NULL;
}
break;
case scan_id_menu_heading:
if (array[i].content.menu.name != NULL) {
free(array[i].content.menu.name);
array[i].content.menu.name = NULL;
}
break;
case scan_id_keyword_assignment:
if (array[i].content.keyword.value != NULL) {
free(array[i].content.keyword.value);
array[i].content.keyword.value = NULL;
}
break;
case scan_id_number_assignment:
if (array[i].content.number.value != NULL) {
free(array[i].content.number.value);
array[i].content.number.value = NULL;
}
break;
default:
break;
}
}
free(array);
}
#define INITIAL_ARRAY_LENGTH 40
/* Scan file FILENAME for tokens. Upon success, return the number allocated
* tokens and set TOKEN to point to a NULL-terminated array of scan_tokens,
* i.e. the token id of the last token is 0. Return non-zero otherwise. */
int
scan_file(const char* filename, struct scan_token** token)
{
struct misc_file_buffer file;
struct scan_token* array;
struct scan_token* buffer;
int pos;
int size;
int current;
int rc;
int line;
rc = misc_get_file_buffer(filename, &file);
if (rc)
return rc;
size = INITIAL_ARRAY_LENGTH;
pos = 0;
array = (struct scan_token*) misc_malloc(size *
sizeof(struct scan_token));
if (array == NULL) {
misc_free_file_buffer(&file);
return -1;
}
memset(array, 0, size * sizeof(struct scan_token));
line = 1;
while ((size_t) file.pos < file.length) {
skip_blanks(&file);
current = misc_get_char(&file, 0);
switch (current) {
case '[':
file.pos++;
rc = scan_section_heading(&file, &array[pos++], line);
break;
case ':':
file.pos++;
rc = scan_menu_heading(&file, &array[pos++], line);
break;
case '#':
file.pos++;
skip_line(&file);
rc = 0;
break;
case '\n':
file.pos++;
rc = 0;
break;
case EOF:
rc = 0;
break;
default:
if (search_line_for(&file, '=')) {
if (isdigit(current))
rc = scan_number_assignment(
&file, &array[pos++], line);
else
rc = scan_keyword_assignment(
&file, &array[pos++], line);
} else {
rc = scan_keyword_only_statement(&file,
&array[pos++], line);
}
}
if (rc)
break;
line++;
/* Enlarge array if there is only one position left */
if (pos + 1 >= size) {
size *= 2;
buffer = (struct scan_token *)
misc_malloc(size *
sizeof(struct scan_token));
if (buffer == NULL) {
rc = -1;
break;
}
memset(buffer, 0, size * sizeof(struct scan_token));
memcpy(buffer, array, pos*sizeof(struct scan_token));
free(array);
array = buffer;
}
}
misc_free_file_buffer(&file);
if (rc) {
scan_free(array);
return rc;
}
*token = array;
return size;
}
static int
bls_filter_by_names(const struct dirent *ent)
{
int offset = strlen(ent->d_name) - strlen(".conf");
if (offset < 0)
return 0;
return strncmp(ent->d_name + offset, ".conf", strlen(".conf")) == 0;
}
struct version {
char *line; /* pointer to a line with version keyword */
int offset; /* offset of version value in the line */
};
/*
* Locate version in bls file represented by ENT
*/
static void get_version(const struct dirent *ent, struct version *v)
{
char *line = NULL;
size_t len = 0;
char *d_name;
FILE *stream;
ssize_t read;
memset(v, 0, sizeof(*v));
d_name = misc_make_path((char *)blsdir, (char *)ent->d_name);
if (!d_name)
return;
stream = fopen(d_name, "r");
free(d_name);
if (!stream)
return;
while ((read = getline(&line, &len, stream)) != -1) {
if (line[read - 1] == '\n') {
line[read - 1] = '\0';
read--;
}
if ((size_t)read <= strlen(VERSION_KEYWORD) + 1)
continue;
if (strcmp(VERSION_KEYWORD, line) > 0)
continue;
if (!isblank(line[strlen(VERSION_KEYWORD)]))
continue;
/* skip blanks */
v->offset = strlen(VERSION_KEYWORD) + 1;
while (v->offset < read - 1 && isblank(line[v->offset]))
v->offset++;
if (isblank(line[v->offset]))
/*
* all characters after the keyword
* are blanks. Invalid version
*/
continue;
v->line = line;
fclose(stream);
return;
}
free(line);
fclose(stream);
}
static void put_version(struct version *v)
{
free(v->line);
}
/**
* Check version in bls file represented by ENT.
* Return 1 if version is valid. Otherwise return 0
*/
static int bls_filter_by_versions(const struct dirent *ent)
{
struct version v;
if (bls_filter_by_names(ent) == 0)
return 0;
get_version(ent, &v);
if (v.line) {
put_version(&v);
return 1;
}
return 0;
}
static int
bls_sort_by_names(const struct dirent **ent_a, const struct dirent **ent_b)
{
return strverscmp((*ent_a)->d_name, (*ent_b)->d_name);
}
static int
bls_sort_by_versions(const struct dirent **ent_a, const struct dirent **ent_b)
{
struct version v1, v2;
int ret;
get_version(*ent_a, &v1);
get_version(*ent_b, &v2);
/*
* Both versions are valid.
* It is guaranteed by bls_filter_by_versions()
*/
ret = strverscmp(v1.line + v1.offset, v2.line + v2.offset);
put_version(&v1);
put_version(&v2);
return ret;
}
static int
scan_append_section_heading(struct scan_token* scan, int* index, char* name);
static int
scan_append_keyword_assignment(struct scan_token* scan, int* index,
enum scan_keyword_id id, char* value);
static int
scan_bls_field(struct misc_file_buffer *file, struct scan_token* scan,
int* index)
{
int current;
int key_start, key_end;
int val_start, val_end;
char *val;
for (key_start = file->pos; ; file->pos++) {
current = misc_get_char(file, 0);
if (isblank(current)) {
key_end = file->pos;
skip_blanks(file);
val_start = file->pos;
break;
}
if (!isalnum(current) && current != '_' && current != '-')
return -1;
}
for (; ; file->pos++) {
current = misc_get_char(file, 0);
if (current == '\n' || current == EOF)
break;
}
val_end = file->pos;
while (val_end > val_start && isblank(file->buffer[val_end - 1]))
val_end--;
file->buffer[key_end] = '\0';
file->buffer[val_end] = '\0';
if (strncmp("title", &file->buffer[key_start], key_end - key_start) == 0) {
scan_append_section_heading(scan, index, &file->buffer[val_start]);
}
if (strncmp("linux", &file->buffer[key_start], key_end - key_start) == 0) {
misc_asprintf(&val, "%s", &file->buffer[val_start]);
scan_append_keyword_assignment(scan, index, scan_keyword_image, val);
free(val);
}
if (strncmp("options", &file->buffer[key_start], key_end - key_start) == 0) {
scan_append_keyword_assignment(scan, index, scan_keyword_parameters,
&file->buffer[val_start]);
}
if (strncmp("initrd", &file->buffer[key_start], key_end - key_start) == 0) {
misc_asprintf(&val, "%s", &file->buffer[val_start]);
scan_append_keyword_assignment(scan, index, scan_keyword_ramdisk, val);
free(val);
}
return 0;
}
/**
* find a line with keyword "title" and move it to the top
*/
static int sort_bls_fields(struct misc_file_buffer *file, char *filename)
{
bool is_title = false;
size_t title_len = 0;
int nr_titles = 0;
size_t title_off;
char *title;
int current;
size_t len;
while (file->length - file->pos > 4 /* for "title" */) {
if (strncmp("title", &file->buffer[file->pos], 5) == 0) {
is_title = true;
nr_titles++;
title_off = file->pos;
}
for (len = 0;; file->pos++, len++) {
current = misc_get_char(file, 0);
if (current == '\n' || current == EOF)
break;
}
if (is_title == true)
title_len = len;
if (current == EOF)
break;
file->pos++;
}
file->pos = 0;
if (nr_titles == 0) {
error_reason("no title in %s", filename);
return -1;
}
if (nr_titles > 1) {
error_reason("more than one title in %s", filename);
return -1;
}
if (title_off == 0)
return 0;
title = misc_malloc(title_len);
if (!title)
return -1;
/*
* copy the title field w/o trailing '\n' to the temporary buffer
*/
memcpy(title, &file->buffer[title_off], title_len);
/*
* shift preceded memory region w/o trailing '\n' to the right
*/
assert(file->buffer[title_off - 1] == '\n');
memmove(&file->buffer[title_len + 1], &file->buffer[0], title_off - 1);
file->buffer[title_len] = '\n';
memcpy(&file->buffer[0], title, title_len);
free(title);
return 0;
}
/* Search scanned tokens SCAN for a section/menu heading (according to
* TYPE) of the given NAME, beginning at token OFFSET. Return the index of
* the section/menu heading on success, a negative value on error. */
int
scan_find_section(struct scan_token* scan, char* name, enum scan_id type,
int offset)
{
int i;
for (i=offset; (int) scan[i].id != 0; i++) {
if ((scan[i].id == scan_id_section_heading) &&
(type == scan_id_section_heading))
if (strcmp(scan[i].content.section.name, name) == 0)
return i;
if ((scan[i].id == scan_id_menu_heading) &&
(type == scan_id_menu_heading))
if (strcmp(scan[i].content.menu.name, name) == 0)
return i;
}
return -1;
}
/* Check whether a string contains a load address as comma separated hex
* value at end of string. */
static int
contains_address(char* string)
{
unsigned long long result;
/* Find trailing comma */
string = strrchr(string, ',');
if (string != NULL) {
/* Try to scan a hexadecimal address */
if (sscanf(string + 1, "%llx", &result) == 1) {
return 1;
}
}
return 0;
}
enum scan_section_type
scan_get_section_type(char* keyword[])
{
if (keyword[(int) scan_keyword_tape] != NULL)
return section_ipl_tape;
else if (keyword[(int) scan_keyword_image] != NULL)
return section_ipl;
else if (keyword[(int) scan_keyword_segment] != NULL)
return section_segment;
else if (keyword[(int) scan_keyword_dumpto] != NULL)
return section_dump;
else if (keyword[(int) scan_keyword_dumptofs] != NULL)
return section_dumpfs;
else if (keyword[(int) scan_keyword_mvdump] != NULL)
return section_mvdump;
else
return section_invalid;
}
enum scan_target_type
scan_get_target_type(char *type)
{
if (strcasecmp(type, "SCSI") == 0)
return target_type_scsi;
else if (strcasecmp(type, "FBA") == 0)
return target_type_fba;
else if (strcasecmp(type, "LDL") == 0)
return target_type_ldl;
else if (strcasecmp(type, "CDL") == 0)
return target_type_cdl;
return target_type_invalid;
}
/* Check section data for correctness. KEYWORD[keyword_id] defines whether
* a keyword is present, LINE[keyword_id] specifies in which line a keyword
* was found or NULL when specified on command line, NAME specifies the
* section name or NULL when specified on command line. */
int
scan_check_section_data(char* keyword[], int* line, char* name,
int section_line, enum scan_section_type* type)
{
char* main_keyword;
int i;
main_keyword = "";
/* Find out what type this section is */
if (*type == section_invalid) {
if (keyword[(int) scan_keyword_tape]) {
*type = section_ipl_tape;
main_keyword = scan_keyword_name(scan_keyword_tape);
} else if (keyword[(int) scan_keyword_image]) {
*type = section_ipl;
main_keyword = scan_keyword_name(scan_keyword_image);
} else if (keyword[(int) scan_keyword_segment]) {
*type = section_segment;
main_keyword = scan_keyword_name(scan_keyword_segment);
} else if (keyword[(int) scan_keyword_dumpto]) {
*type = section_dump;
main_keyword = scan_keyword_name(scan_keyword_dumpto);
} else if (keyword[(int) scan_keyword_dumptofs]) {
error_reason("Option dumptofs is deprecated, "
"use dumpto instead");