summaryrefslogtreecommitdiffstats
path: root/loader/devices.c
blob: d5cfcf6e460ec0652438fda92d600291a971ec98 (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
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
#include <alloca.h>
#include <ctype.h>
#include <newt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "devices.h"
#include "../isys/isys.h"
#include "lang.h"
#include "loader.h"
#include "modules.h"

static int getModuleArgs(struct moduleInfo * mod, char *** argPtr) {
    struct newtWinEntry * entries;
    int i;
    int numArgs;
    char ** values;
    char * chptr, * end;
    int misc = -1;
    char ** args;
    int argc;
    int rc;

    entries = alloca(sizeof(*entries) * (mod->numArgs + 2));
    values = alloca(sizeof(*values) * (mod->numArgs + 2));

    for (i = 0; i < mod->numArgs; i++) {
    	entries[i].text = mod->args[i].description;
	if (mod->args[i].arg) {
	    values[i] = malloc(strlen(mod->args[i].arg) + 2);
	    strcpy(values[i], mod->args[i].arg);
	    strcat(values[i], "=");
	} else {
	    values[i] = NULL;
	}
	entries[i].value = values + i;
	entries[i].flags = NEWT_FLAG_SCROLL;
    }

    numArgs = i;

    if (!(mod->flags & MI_FLAG_NOMISCARGS)) {
    	values[i] = NULL;
    	entries[i].text = _("Miscellaneous");
    	entries[i].value = values + i;
	entries[i].flags = NEWT_FLAG_SCROLL;
	misc = i;
	i++;
    }

    entries[i].text = (void *) entries[i].value = NULL;

    rc = newtWinEntries(_("Module Parameters"), _("This module can take "
		    "parameters which affects its operation. If you don't "
		    "know what parameters to supply, just skip this "
		    "screen by pressing the \"Ok\" button now."),
		    40, 5, 15, 20, entries, _("Ok"), _("Back"), NULL);

    if (rc == 2) {
        for (i = 0; i < numArgs; i++)
	    if (values[i]) free(values[i]);
	return LOADER_BACK;
    }

    /* we keep args big enough for the args we know about, plus a NULL */

    args = malloc(sizeof(*args) * (numArgs + 1));
    argc = 0;

    for (i = 0; i < numArgs; i++) {
    	if (values[i] && *values[i]) {
	    chptr = values[i] + strlen(values[i]) - 1;
	    while (isspace(*chptr)) chptr--;
	    if (*chptr != '=')
		args[argc++] = values[i];
	}
    }

    if (misc >= 0 && values[misc]) {
    	chptr = values[misc];
	i = 1;
	while (*chptr) {
	    if (isspace(*chptr)) i++;
	    chptr++;
	}

	args = realloc(args, sizeof(*args) * (argc + i + 1));
	chptr = values[misc];
	while (*chptr) {
	    while (*chptr && isspace(*chptr)) chptr++;
	    if (!*chptr) break;

	    end = chptr;
	    while (!isspace(*end) && *end) end++;
	    args[argc] = malloc(end - chptr + 1);
	    memcpy(args[argc], chptr, end - chptr);
	    args[argc][end - chptr] = '\0';
	    argc++;
	    chptr = end;
	}

	free(values[misc]);
    }

    args[argc] = NULL;
    *argPtr = args;

    return 0;
}

static int pickModule(moduleInfoSet modInfo, enum driverMajor type,
		      moduleList modLoaded, struct moduleInfo * suggestion,
		      struct moduleInfo ** modp, int * specifyParams) {
    int i;
    newtComponent form, text, listbox, answer, checkbox, ok, back;
    newtGrid buttons, grid, subgrid;
    char specifyParameters = *specifyParams ? '*' : ' ';

    text = newtTextboxReflowed(-1, -1, _("Which driver should I try?"),
				20, 0, 10, 0);
    listbox = newtListbox(-1, -1, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);

    for (i = 0; i < modInfo->numModules; i++) {
	if (modInfo->moduleList[i].major == type && 
	    !mlModuleInList(modInfo->moduleList[i].moduleName, modLoaded)) {
	    newtListboxAppendEntry(listbox, modInfo->moduleList[i].description,
				   (void *) i);
	    if (modp && (modInfo->moduleList + i) == *modp)
		newtListboxSetCurrentByKey(listbox, (void *) i);
	}
    }

    buttons = newtButtonBar(_("Ok"), &ok, _("Back"), &back, NULL);
    checkbox = newtCheckbox(-1, -1, _("Specify module parameters"),
			    specifyParameters, NULL, &specifyParameters);
    subgrid = newtGridVStacked(NEWT_GRID_COMPONENT, listbox,
			       NEWT_GRID_COMPONENT, checkbox, NULL);
    grid = newtGridBasicWindow(text, subgrid, buttons);

    newtGridWrappedWindow(grid, _("Devices"));

    form = newtForm(NULL, NULL, 0);
    newtGridAddComponentsToForm(grid, form, 1);

    answer = newtRunForm(form);
    newtPopWindow();

    i = (int) newtListboxGetCurrent(listbox);

    newtGridFree(grid, 1);
    newtFormDestroy(form);

    if (answer == back) return LOADER_BACK;
    
    *specifyParams = (specifyParameters != ' ');
    *modp = modInfo->moduleList + i;

    return 0;
}

int devDeviceMenu(enum driverMajor type, moduleInfoSet modInfo, 
		  moduleList modLoaded, moduleDeps modDeps, int flags,
		  char ** moduleName) {
    struct moduleInfo * mod = NULL;
    enum { S_MODULE, S_ARGS, S_DONE } stage = S_MODULE;
    int rc;
    char ** args = NULL, ** arg;
    int specifyArgs = 0;

    while (stage != S_DONE) {
    	switch (stage) {
	  case S_MODULE:
	    if ((rc = pickModule(modInfo, type, modLoaded, mod, &mod, 
				 &specifyArgs)))
		return LOADER_BACK;
	    stage = S_ARGS;
	    break;

	  case S_ARGS:
	    if (specifyArgs) {
		rc = getModuleArgs(mod, &args);
		if (rc) {
		    stage = S_MODULE;
		    break;
		}
	    }
	    stage = S_DONE;
	    break;

	  case S_DONE:
	}
    }

    if (mod->major == DRIVER_SCSI) scsiWindow(mod->moduleName);
    rc = mlLoadModule(mod->moduleName, modLoaded, modDeps, args,
		      FL_TESTING(flags));
    if (mod->major == DRIVER_SCSI) newtPopWindow();

    if (args) {
	for (arg = args; *arg; arg++)
	    free(*arg);
	free(args);
    }

    if (!rc && moduleName)
        *moduleName = mod->moduleName;
    
    return rc;
}