home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 February
/
CHIP_2_98.iso
/
misc
/
src
/
install
/
.#kickstart.c.1.4
< prev
next >
Wrap
Text File
|
1997-09-18
|
3KB
|
154 lines
#include <alloca.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <newt.h>
#include <popt.h>
#include <unistd.h>
#include "install.h"
#include "kickstart.h"
struct ksCommandNames {
int code;
char * name;
} ;
struct ksCommand {
int code, argc;
char ** argv;
};
struct ksCommandNames ksTable[] = {
{ KS_CMD_NFS, "nfs" },
{ KS_CMD_INSTALL, "install" },
{ KS_CMD_UPGRADE, "upgrade" },
{ KS_CMD_ROOTPW, "rootpw" },
#if defined(__i386__)
{ KS_CMD_LILO, "lilo" },
#else if defined(__sparc__)
{ KS_CMD_LILO, "silo" },
#endif
{ KS_CMD_PART, "part" },
{ KS_CMD_CLEARPART, "clearpart" },
{ KS_CMD_KEYBOARD, "keyboard" },
{ KS_CMD_MOUSE, "mouse" },
{ KS_CMD_TIMEZONE, "timezone" },
{ KS_CMD_NONE, NULL }
};
struct ksCommand * commands = NULL;
int numCommands = 0;
int ksReadCommands(char * cmdFile) {
int fd;
char * buf;
struct stat sb;
char * start, * end;
char oldch;
int line = 0;
char ** argv;
int argc;
struct ksCommandNames * cmd;
int numAlloced = 5;
if ((fd = open(cmdFile, O_RDONLY)) < 0) {
newtWinMessage("Kickstart Error", "Ok",
"Error opening: kickstart file %s: %s", cmdFile,
strerror(errno));
return INST_ERROR;
}
fstat(fd, &sb);
buf = alloca(sb.st_size + 1);
if (read(fd, buf, sb.st_size) != sb.st_size) {
newtWinMessage("Kickstart Error", "Ok",
"Error reading contents of kickstart file %s: %s",
cmdFile, strerror(errno));
close(fd);
return INST_ERROR;
}
close(fd);
buf[sb.st_size] = '\0';
commands = malloc(sizeof(*commands) * numAlloced);
start = buf;
while (*start) {
line++;
if (!(end = strchr(start, '\n')))
end = start + strlen(start);
oldch = *end;
*end = '\0';
while (*start && isspace(*start)) start++;
if (*start && *start != '#') {
if (poptParseArgvString(start, &argc, &argv) || !argc) {
newtWinMessage("Kickstart Error", "Ok",
"Error on line %d of kickstart file %s.",
argv[0], line, cmdFile);
} else {
for (cmd = ksTable; cmd->name; cmd++)
if (!strcmp(cmd->name, argv[0])) break;
if (!cmd->name) {
newtWinMessage("Kickstart Error", "Ok",
"Unknown command %s on line %d of "
"kickstart file %s.", argv[0], line,
cmdFile);
} else {
if (numCommands == numAlloced) {
numAlloced += 5;
commands = realloc(commands,
sizeof(*commands) * numAlloced);
}
commands[numCommands].code = cmd->code;
commands[numCommands].argc = argc;
commands[numCommands].argv = argv;
numCommands++;
}
}
}
if (oldch)
start = end + 1;
else
start = end;
}
return 0;
}
int ksGetCommand(int cmd, char ** last, int * argc, char *** argv) {
int i = 0;
if (last) {
for (i = 0; i < numCommands; i++) {
if (commands[i].argv == last) break;
}
i++;
}
while (i < numCommands) {
if (commands[i].code == cmd) {
if (argv) *argv = commands[i].argv;
if (argc) *argc = commands[i].argc;
return 0;
}
i++;
}
return 1;
}