cp: revamp directory handling
[hvf.git] / cp / nucleus / config.c
blob0834b981a71194364641832865ad7c8b5a3dc5da
1 /*
2 * (C) Copyright 2007-2011 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * This file is released under the GPLv2. See the COPYING file for more
5 * details.
6 */
8 #include <device.h>
9 #include <bdev.h>
10 #include <edf.h>
11 #include <slab.h>
12 #include <directory.h>
13 #include <ebcdic.h>
14 #include <sclp.h>
15 #include <parser.h>
16 #include <lexer.h>
18 struct sysconf sysconf;
20 static void __load_logos(struct fs *fs)
22 struct logo *logo, *tmp;
23 struct logo_rec *rec;
24 struct file *file;
25 int ret;
26 int i;
28 list_for_each_entry_safe(logo, tmp, &sysconf.logos, list) {
29 /* look up the config file */
30 file = edf_lookup(fs, logo->fn, logo->ft);
31 if (IS_ERR(file))
32 goto remove;
34 if ((file->FST.LRECL != CONFIG_LRECL) ||
35 (file->FST.RECFM != FSTDFIX))
36 goto remove;
38 /* parse each record in the config file */
39 for(i=0; i<file->FST.AIC; i++) {
40 rec = malloc(sizeof(struct logo_rec) + CONFIG_LRECL,
41 ZONE_NORMAL);
42 if (!rec)
43 goto remove;
45 ret = edf_read_rec(file, (char*) rec->data, i);
46 if (ret)
47 goto remove;
49 ebcdic2ascii(rec->data, CONFIG_LRECL);
51 list_add_tail(&rec->list, &logo->lines);
54 continue;
56 remove:
57 list_del(&logo->list);
58 free(logo);
62 static void *_realloc(void *ptr, size_t n)
64 if (!ptr)
65 return malloc(n, ZONE_NORMAL);
67 BUG();
68 return NULL;
71 static void _error(struct parser *parser, char *msg)
73 sclp_msg("config parse error: %s\n", msg);
74 BUG();
77 struct fs *load_config(u32 iplsch)
79 struct parser p;
80 struct lexer l;
81 struct device *dev;
82 struct fs *fs;
84 sclp_msg("LOADING CONFIG FROM '%*.*s' '%*.*s'\n",
85 8, 8, CONFIG_FILE_NAME, 8, 8, CONFIG_FILE_TYPE);
87 memset(&sysconf, 0, sizeof(struct sysconf));
88 INIT_LIST_HEAD(&sysconf.rdevs);
89 INIT_LIST_HEAD(&sysconf.logos);
91 /* find the real device */
92 dev = find_device_by_sch(iplsch);
93 if (IS_ERR(dev))
94 return ERR_CAST(dev);
96 /* mount the fs */
97 fs = edf_mount(dev);
98 if (IS_ERR(fs))
99 return fs;
101 /* set up the parser */
102 memset(&p, 0, sizeof(p));
103 p.lex = config_lex;
104 p.lex_data = &l;
105 p.realloc = _realloc;
106 p.free = free;
107 p.error = _error;
109 /* set up the lexer */
110 memset(&l, 0, sizeof(l));
111 l.fs = fs;
112 l.init = 0;
114 /* parse! */
115 if (config_parse(&p))
116 return ERR_PTR(-ECORRUPT);
118 load_directory(fs);
119 __load_logos(fs);
121 return fs;