|
From: Russ D. <ru...@us...> - 2001-08-31 14:06:14
|
Update of /cvsroot/blob/blob/utils/mkparamblock
In directory usw-pr-cvs1:/tmp/cvs-serv6792
Added Files:
Makefile mkparamblock.c sample.config
Log Message:
param block generation
--- NEW FILE Makefile ---
# I don't really know how to get autoconf, etc, to build something with HOSTCC, so here
# is a standin Makefile
all: mkparamblock
mkparamblock: mkparamblock.o
gcc mkparamblock.o -o mkparamblock
mkparamblock.o: mkparamblock.c
gcc -Wall -g mkparamblock.c -c -o mkparamblock.o -I../../include/
clean:
-rm *.o mkparamblock
--- NEW FILE mkparamblock.c ---
/* just some basic, very unfancy code for now */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "param_block.h"
#include "serial.h"
struct config_keyword {
char keyword[40];
int (*handler)(char *line, FILE *fp);
};
int convert_baud(char *arg)
{
eBauds retval;
if(strcmp(arg, "1200") == 0) {
retval = baud1k2;
} else if(strcmp(arg, "1k2") == 0) {
retval = baud1k2;
} else if(strcmp(arg, "9600") == 0) {
retval = baud9k6;
} else if(strcmp(arg, "9k6") == 0) {
retval = baud9k6;
} else if(strcmp(arg, "19200") == 0) {
retval = baud19k2;
} else if(strcmp(arg, "19k2") == 0) {
retval = baud19k2;
} else if(strcmp(arg, "38400") == 0) {
retval = baud38k4;
} else if(strcmp(arg, "38k4") == 0) {
retval = baud38k4;
} else if(strcmp(arg, "57600") == 0) {
retval = baud57k6;
} else if(strcmp(arg, "57k6") == 0) {
retval = baud57k6;
} else if(strcmp(arg, "115200") == 0) {
retval = baud115k2;
} else if(strcmp(arg, "115k2") == 0) {
retval = baud115k2;
} else if(strcmp(arg, "230400") == 0) {
retval = baud230k4;
} else if(strcmp(arg, "230k4") == 0) {
retval = baud230k4;
} else return -1;
return (int) retval;
}
int read_yn(char *line)
{
char *arg;
if (!(arg = strtok(line, " \t"))) return -1;
if (!strcasecmp("yes", arg) || !strcmp("1", arg) || !strcasecmp("true", arg))
return 1;
else if (!strcasecmp("no", arg) || !strcmp("0", arg) || !strcasecmp("false", arg))
return 0;
return -1;
}
int tack_ramdisk(char *line, FILE *fp)
{
struct ptag tag;
tag.hdr.ptag = PTAG_RAMDISK;
tag.hdr.size = ptag_size(ptag_ramdisk);
if ((tag.u.ramdisk.flags = read_yn(line)) == -1) return -1;
fwrite(&tag, ptag_size(ptag_ramdisk), 4, fp);
return 0;
}
int tack_bootdelay(char *line, FILE *fp)
{
struct ptag tag;
tag.hdr.ptag = PTAG_BOOTDELAY;
tag.hdr.size = ptag_size(ptag_bootdelay);
if ((tag.u.bootdelay.delay = strtoul(line, 0, 0)) <= 0) return -1;
fwrite(&tag, ptag_size(ptag_bootdelay), 4, fp);
return 0;
}
int tack_cmdline(char *line, FILE *fp)
{
struct ptag tag;
int zero = 0;
tag.hdr.ptag = PTAG_CMDLINE;
tag.hdr.size = (sizeof(tag.hdr) + strlen(line) + 1 + 4) >> 2;
fwrite(&tag, sizeof(tag.hdr), 1, fp);
fwrite(line, strlen(line) + 1, 1, fp);
fwrite(&zero, 1, (4 - (strlen(line) + 1)) % 4, fp);
return 0;
}
int tack_baud(char *line, FILE *fp)
{
struct ptag tag;
char *arg;
int ret;
tag.hdr.ptag = PTAG_BAUD;
tag.hdr.size = ptag_size(ptag_baud);
if (!(arg = strtok(line, "\t =,"))) return -1;
if ((ret = convert_baud(arg)) < 0) return -1;
tag.u.baud.terminal = ret;
if (!(arg = strtok(NULL, "\t ,"))) return -1;
if ((ret = convert_baud(arg)) < 0) return -1;
tag.u.baud.download = ret;
fwrite(&tag, ptag_size(ptag_baud), 4, fp);
return 0;
}
void tack_core(FILE *fp)
{
struct ptag tag;
tag.hdr.ptag = PTAG_CORE;
tag.hdr.size = ptag_size(ptag_core);
fwrite(&tag, ptag_size(ptag_core), 4, fp);
}
void tack_none(FILE *fp)
{
struct ptag tag;
tag.hdr.ptag = PTAG_NONE;
tag.hdr.size = 0;
fwrite(&tag, sizeof(tag.hdr), 1, fp);
}
static struct config_keyword keywords[] = {
/* keyword handler */
{"ramdisk", tack_ramdisk},
{"bootdelay", tack_bootdelay},
{"cmdline", tack_cmdline},
{"baud", tack_baud},
};
int main(int argc, char *argv[])
{
FILE *in, *out;
char buffer[256], *token, *line;
int i, line_num = 0;
if (argc < 2) {
printf("Usage: mkparamblock <configfile> <outfile>\n\n");
return 0;
}
if (!(in = fopen(argv[1], "r"))) {
perror("could not open config file");
return 0;
}
if (!(out = fopen(argv[2], "w"))) {
perror("could not open output file");
return 0;
}
tack_core(out);
while (fgets(buffer, 256, in)) {
line_num++;
/* clear trailing newline */
if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
/* clear trailing comments */
if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
/* find token */
token = buffer + strspn(buffer, " \t");
if (*token == '\0') continue;
/* find line */
line = token + strcspn(token, " \t=");
if (*line == '\0') continue;
*line = '\0';
line++;
line = line + strspn(line, " \t=");
if (*line == '\0') continue;
for (i = 0; strlen(keywords[i].keyword); i++)
if (!strcasecmp(token, keywords[i].keyword))
if (keywords[i].handler(line, out) < 0)
printf("Error %s args on line %d\n", token, line_num);
}
tack_none(out);
fclose(in);
fclose(out);
return 0;
}
--- NEW FILE sample.config ---
# Sample paramater block configuration file
ramdisk no # default is yes
bootdelay 1 # default is 10
cmdline console=ttySA0,9600 console=tty1 root=/dev/mtdblock2 init=/linuxrc
baud 9600, 115k2 # these are the defaults
|