b26c33e48fd254382cb2609f900c3f1352e5fc33
[rrq/maintain_lilo.git] / src / identify.c
1 /* identify.c  -  Translate label names to kernel paths */
2
3 /* 
4 Copyright 1992-1998 Werner Almesberger.
5 Copyright 1999-2004 John Coffman.
6 All rights reserved.
7
8 Licensed under the terms contained in the file 'COPYING' in the 
9 source directory.
10
11 */
12
13
14 #define _GNU_SOURCE
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <string.h>
19
20 #include "lilo.h"
21 #include "common.h"
22 #include "cfg.h"
23
24 char *identify;
25 static char *opt;
26 static char *first, *dflt;
27 static int idefault;
28
29
30 static void do_identify(char *var,char type)
31 {
32     char *label,*path,*alias,*initrd,*keytab,*appstr,*dtem,*addappstr;
33     char *rootstr;
34     int root,image, ramdisk, kt, append;
35
36 #if 1
37     image = !!strchr(opt,'i');
38     ramdisk = !!strchr(opt,'r');
39     kt = !!strchr(opt,'k');
40     append = !!strchr(opt,'a');
41     root = !!strchr(opt,'R');
42     if (opt && !image && !ramdisk && !kt && !append
43         && !idefault && !root) exit(1);
44 /*    if (!opt) image = 1; */
45 #else
46     image = ramdisk = 1;
47     printf("do_identify:  opt=\"%s\"\n", opt);
48 #endif
49     
50     label = strrchr(path = cfg_get_strg(cf_identify,var),'/');
51     if (label) label++;
52     if (cfg_get_strg(cf_all,"label")) label = cfg_get_strg(cf_all,"label");
53     else if (!label) label = path;
54
55     if (!first) first = stralloc(label);
56     
57     alias = cfg_get_strg(cf_all,"alias");
58     dtem = cfg_get_strg(cf_options,"default");
59
60     if (verbose>=2) printf("identify: dtem=%s  label=%s\n", dtem, label);
61 #ifdef LCF_IGNORECASE
62     if (dtem && (!strcasecmp(label,dtem) || (alias && !strcasecmp(alias,dtem)))) {
63 #else
64     if (dtem && (!strcmp(label,dtem) || (alias && !strcmp(alias,dtem)))) {
65 #endif
66         if (verbose>=2) printf("setting  dflt\n");
67         dflt = dtem;
68     }
69
70     initrd = cfg_get_strg(cf_kernel,"initrd");
71     if (!initrd) initrd = cfg_get_strg(cf_options,"initrd");
72     keytab = cfg_get_strg(cf_options,"keytable");
73     if (!keytab) keytab="us.ktl";
74     appstr = cfg_get_strg(cf_kernel,"append");
75     if (!appstr) appstr = cfg_get_strg(cf_options,"append");
76     addappstr = cfg_get_strg(cf_kernel,"addappend");
77     rootstr = cfg_get_strg(cf_kernel,"root");
78     if (!rootstr) rootstr = cfg_get_strg(cf_options,"root");
79
80 #ifdef LCF_IGNORECASE
81     if (!strcasecmp(label,identify) || (alias && !strcasecmp(alias,identify))) {
82 #else
83     if (!strcmp(label,identify) || (alias && !strcmp(alias,identify))) {
84 #endif
85         if (image) printf("%s\n",path);
86         if (ramdisk) printf("%s\n",initrd?initrd:"No initial ramdisk specified");
87         if (kt) printf("%s\n",keytab);
88         if (append) {
89             if (!appstr && !addappstr)
90                 printf("No append= was specified\n");
91             else if ((appstr && !addappstr) || (!appstr && addappstr))
92                 printf("%s\n", appstr?appstr:addappstr);
93             else printf("%s %s\n", appstr, addappstr);
94         }
95         if (root) printf("%s\n",rootstr?rootstr:"No root specified");
96         if (idefault) printf("%s\n", dflt ? dflt : first);
97         exit(0);
98     }
99 }
100
101
102 void id_image(void)
103 {
104     cfg_init(cf_image);
105     (void) cfg_parse(cf_image);
106     do_identify("image",'i');
107     cfg_init(cf_identify);
108 }
109
110
111 void id_other(void)
112 {
113     cfg_init(cf_other);
114     cfg_init(cf_kernel);
115     curr_drv_map = curr_prt_map = 0;
116     (void) cfg_parse(cf_other);
117     cfg_init(cf_identify);
118 }
119
120
121 void identify_image(char *label,char *options)
122 {
123     identify = label;
124     opt = options;
125     if (verbose>=2) printf("identify_image: id='%s' opt='%s'\n", label, options);
126     idefault = !!strchr(opt,'D');
127     if (idefault) identify = "";
128     cfg_init(cf_identify);
129     if (cfg_parse(cf_identify)) cfg_error("Syntax error");
130     if (idefault && first) {
131         printf("%s\n", dflt ? dflt : first);
132         exit(0);
133     }
134     die("No image found for \"%s\"",label);
135 }