aboutsummaryrefslogtreecommitdiff
path: root/ext2load/conffile.c
blob: 6eb475cbd2ed421ee6404c439af7dcf81d4116b7 (plain)
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
/*
 * Copyright 2001-2004 Guido Guenther <agx@sigxcpu.org>
 *
 * load arcboots configuration file and process the arguments
 *
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <ext2_fs.h>
#include <ext2fs.h>
#include "arcboot.h"

#define _PARM_LIMIT	32

static	char	*carray[_PARM_LIMIT+4]; /* 0 is the name,
					   1 an initrd,
					   2 the bootfile,
					   X is OSLoadOptions 
					   X+1 ... _PARM_LIMIT are options given on the
					   command line */

char** GetConfig(char* config, char* name)
{
	char *t, *start, *end;
	int		i;

	/* Loop on lines */
	while(*config != 0x0) {

		start=config;

		while(*config != 0xa && *config != 0x0) {
			/* Delete comments */
			if (*config == '#') 
				*config=0x0;
			config++;
		}

		/* Did we stop at the end of a line ? */
		if (*config == 0xa) {
			/* Terminate Line */
			*config=0x0;
			config++;
		}		

		/* Skip leading spaces and tabs */
		while(*start == ' ' || *start == '\t') 
			start++;

		/* If the start of a line is the end - Next line */
		if (*start == 0x0)
			continue;

		/* get the end pointer */
		end=&start[strlen(start)-1];

		/* Delete spaces and tabs at the end of a line */
		while(*end == ' ' || *end == '\t')
			*end--=0x0;

		if (strncmp("label=",start,6) == 0) {
			/* If we found the right profile or want the first */
			if (carray[0])
				if (((strcmp(carray[0], name) == 0) && (strcmp(name, carray[0]) == 0))) {
					return carray;
				}
			/* Reset initrd & image & append */
			carray[1]=carray[2]=carray[3]=0;
			carray[0]=&start[6];
		} else if (strncmp("image=",start,6) == 0) {
			carray[2]=&start[6];
		} else if (strncmp("initrd=",start,7) == 0) {
			carray[1]=&start[7];
		} else if (strncmp("append=",start,7) == 0) {
			t=&start[7];
			/* Does append start with " */
			if (*t == '"') {
				t++;
				/* If so - append starts +1 */
				carray[3]=t;
				/* Search ending quote */
				while(*t != '"' && *t != 0x0)
					t++;
				/* And delete */
				if (*t == '"') 
					*t=0x0;
			} else 
				carray[3]=&start[7];
			t=carray[3];
			i=4;
			while(i<_PARM_LIMIT && *t != 0x0) {
				t++;

				if (*t == ' ' || *t == '\t') {
					*t++=0x0;
					if (*t != 0x0)
						carray[i++]=t;
				}
			}
		}
	}
	if (carray[0])
		if ((name == NULL) ||
			(strcmp(carray[0], name) == 0)) {
			return carray;
		}
	/* Found nothing appropriate: */
	return NULL;
}

char** ReadConfFile(char** partition, const char *filename, char* label)
{
	ext2_file_t file;
	unsigned size, num_read;
	errcode_t status;
	char *conf_file;

	if(!OpenFile( *partition, filename, &file ))
		return 0;

	size = ext2fs_file_get_size(file);
	conf_file = malloc(size);
	if( !conf_file ) {
		printf("Can't read configuration file - not enough memory\n\r");
		return 0;
	}
	status = ext2fs_file_read(file,(char*) conf_file, size, &num_read);
	if( status ) {
		print_ext2fs_error(status);
		return 0;
	}
	if( size != num_read ) {
		printf("Wanted: %u, got %u bytes of configuration file\n\r", size, num_read);
		return 0;
	}
	return GetConfig(conf_file, label);
}