EMILE/tools/emile-set-output.c

422 lines
9.5 KiB
C
Raw Normal View History

/*
*
* (c) 2004 Laurent Vivier <Laurent@Vivier.EU>
*
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
2004-12-24 23:43:44 +00:00
#include <getopt.h>
2004-12-10 00:28:35 +00:00
#include "libemile.h"
2007-08-25 21:33:57 +00:00
#include "libconfig.h"
2005-11-28 23:39:28 +00:00
static char parity[] = { 'n', 'o', 'e' };
2004-12-24 23:43:44 +00:00
enum {
ARG_NONE = 0,
ARG_HELP = 'h',
ARG_DISPLAY,
ARG_WIDTH,
ARG_HEIGHT,
ARG_DEPTH,
ARG_MODEM,
ARG_PRINTER,
ARG_BITRATE,
ARG_DATASIZE,
ARG_PARITY,
ARG_STOPBITS,
ARG_NODISPLAY,
ARG_NOMODEM,
ARG_NOPRINTER,
ARG_GESTALTID,
};
static struct option long_options[] =
{
{"help", 0, NULL, ARG_HELP },
{"display", 0, NULL, ARG_DISPLAY },
{"width", 1, NULL, ARG_WIDTH },
{"height", 1, NULL, ARG_HEIGHT },
{"depth", 1, NULL, ARG_DEPTH },
{"modem", 0, NULL, ARG_MODEM },
{"printer", 0, NULL, ARG_PRINTER },
{"bitrate", 1, NULL, ARG_BITRATE },
{"datasize", 1, NULL, ARG_DATASIZE },
{"parity", 1, NULL, ARG_PARITY },
{"stopbits", 1, NULL, ARG_STOPBITS },
{"nodisplay", 0, NULL, ARG_NODISPLAY },
{"nomodem", 0, NULL, ARG_NOMODEM },
{"noprinter", 0, NULL, ARG_NOPRINTER },
{"gestaltid", 1, NULL, ARG_GESTALTID },
{NULL, 0, NULL, 0 },
};
2005-11-28 23:39:28 +00:00
enum {
STDOUT_VGA = 1,
STDOUT_MODEM = 2,
STDOUT_PRINTER = 4,
};
static void usage(int argc, char** argv)
{
fprintf(stderr, "Usage:\n");
2008-07-21 22:44:35 +00:00
fprintf(stderr, "Usage: %s [TYPE][OPTIONS] PATH\n", argv[0]);
fprintf(stderr, "\nTYPE can be:\n");
fprintf(stderr, " --display enable display output\n");
fprintf(stderr, " --nodisplay disable display output\n");
fprintf(stderr, " --modem "
"enable modem port (0) serial output\n");
fprintf(stderr, " --nomodem "
"disable modem port (0) serial output\n");
fprintf(stderr, " --printer "
"enable printer port (1) serial output\n");
fprintf(stderr, " --noprinter "
"disable printer port (1) serial output\n");
fprintf(stderr, " --gestaltid=ID "
"force gestalt id (0 to unset)\n");
fprintf(stderr, "\n--display OPTIONS can be:\n");
fprintf(stderr, " --width=WIDTH set display width\n");
fprintf(stderr, " --height=HEIGHT set display height\n");
fprintf(stderr, " --depth=DEPTH set display depth\n");
fprintf(stderr, "\n--modem and --printer OPTIONS can be:\n");
fprintf(stderr, " --bitrate=BITRATE set bitrate\n");
fprintf(stderr, " --datasize=DATASIZE set datasize\n");
fprintf(stderr, " --parity=PARITY set parity\n"
" "
"(0 for none, 1 for odd, 2 for even)\n");
fprintf(stderr, " --stopbits=STOPBITS set stopbits\n");
fprintf(stderr, "\nNo flag displays current configuration\n");
fprintf(stderr, "\nbuild: \n%s\n", SIGNATURE);
}
2004-12-10 00:28:35 +00:00
static int display_output(char* image)
{
int drive, second, size;
2007-09-08 23:09:14 +00:00
int8_t *configuration;
2005-11-28 23:39:28 +00:00
char property[256];
2004-12-10 00:28:35 +00:00
int fd;
int ret;
fd = open(image, O_RDONLY);
if (fd == -1)
{
perror("Cannot open file");
return 2;
}
/* can work on an image or directly on second level file */
ret = emile_first_get_param(fd, &drive, &second, &size);
if (ret == EEMILE_UNKNOWN_FIRST)
{
/* should be a second level file */
ret = lseek(fd, 0, SEEK_SET);
if (ret == -1)
{
perror("Cannot go to buffer offset");
close(fd);
return 3;
}
}
2005-11-28 23:39:28 +00:00
configuration = emile_second_get_configuration(fd);
if (configuration == NULL)
{
2004-12-10 00:28:35 +00:00
perror("Cannot read header");
close(fd);
2004-12-10 00:28:35 +00:00
return 4;
}
close(fd);
if (config_get_property(configuration, "vga", property) != -1)
2005-11-28 23:39:28 +00:00
printf("Output to display enabled (%s)\n", property);
else
printf("Output to display disabled\n");
if (config_get_property(configuration, "modem", property) != -1)
2005-11-28 23:39:28 +00:00
printf("Output to serial port 0 (modem) enabled (%s)\n", property);
else
printf("Output to serial port 0 (modem) disabled\n");
if (config_get_property(configuration, "printer", property) != -1)
2005-11-28 23:39:28 +00:00
printf("Output to serial port 1 (printer) enabled (%s)\n", property);
else
printf("Output to serial port 1 (printer) disabled\n");
if (config_get_property(configuration, "gestaltID", property) != -1)
2005-11-28 23:39:28 +00:00
printf("Force Gestalt ID to %ld\n", strtol(property, NULL, 0));
2004-10-07 19:53:37 +00:00
else
printf("Gestalt ID is not modified\n");
2005-11-28 23:39:28 +00:00
free(configuration);
return 0;
}
static int set_output(char* image,
2004-12-10 00:28:35 +00:00
unsigned int enable_mask, unsigned int disable_mask,
unsigned int bitrate0, int datasize0,
int parity0, int stopbits0,
2004-12-10 00:28:35 +00:00
unsigned int bitrate1, int datasize1,
2004-10-07 19:53:37 +00:00
int parity1, int stopbits1, int gestaltid)
{
2005-12-05 19:52:48 +00:00
int drive, second, size;
int fd;
int ret;
2007-09-08 23:09:14 +00:00
int8_t *configuration;
2005-11-28 23:39:28 +00:00
char property[32];
2005-12-05 19:52:48 +00:00
int offset;
fd = open(image, O_RDWR);
if (fd == -1)
{
perror("Cannot open image file");
return 2;
}
2005-12-05 19:52:48 +00:00
/* can work on an image or directly on second level file */
ret = emile_first_get_param(fd, &drive, &second, &size);
if (ret == EEMILE_UNKNOWN_FIRST)
{
2005-12-05 19:52:48 +00:00
/* should be a second level file */
offset = lseek(fd, 0, SEEK_SET);
if (offset == -1)
{
perror("Cannot go to buffer offset");
close(fd);
return 3;
}
}
2005-12-12 22:14:52 +00:00
else
offset = lseek(fd, 0, SEEK_CUR);
2005-11-28 23:39:28 +00:00
configuration = emile_second_get_configuration(fd);
if (configuration == NULL)
{
perror("Cannot read header");
close(fd);
return 4;
}
if (disable_mask & STDOUT_VGA)
2007-08-25 21:33:57 +00:00
config_remove_property(configuration, "vga");
2005-11-28 23:39:28 +00:00
if (disable_mask & STDOUT_MODEM)
2007-08-25 21:33:57 +00:00
config_remove_property(configuration, "modem");
2005-11-28 23:39:28 +00:00
if (disable_mask & STDOUT_PRINTER)
2007-08-25 21:33:57 +00:00
config_remove_property(configuration, "printer");
2005-11-28 23:39:28 +00:00
if (enable_mask & STDOUT_VGA)
2007-08-25 21:33:57 +00:00
config_set_property(configuration, "vga", "default");
2005-11-28 23:39:28 +00:00
if (enable_mask & STDOUT_MODEM)
{
sprintf(property, "%d%c%d+%d", bitrate0, parity[parity0], datasize0, stopbits0);
2007-08-25 21:33:57 +00:00
config_set_property(configuration, "modem", property);
2005-11-28 23:39:28 +00:00
}
if (enable_mask & STDOUT_PRINTER)
{
sprintf(property, "%d%c%d+%d", bitrate1, parity[parity1], datasize1, stopbits1);
2007-08-25 21:33:57 +00:00
config_set_property(configuration, "printer", property);
2005-11-28 23:39:28 +00:00
}
if (gestaltid == 0)
2007-08-25 21:33:57 +00:00
config_remove_property(configuration, "gestaltID");
2005-11-28 23:39:28 +00:00
else if (gestaltid != -1)
{
sprintf(property, "0x%x", gestaltid);
2007-08-25 21:33:57 +00:00
config_set_property(configuration, "gestaltID", property);
2005-11-28 23:39:28 +00:00
}
2005-12-05 19:52:48 +00:00
ret = lseek(fd, offset, SEEK_SET);
2005-11-28 23:39:28 +00:00
if (ret == -1)
{
perror("Cannot go to buffer offset");
close(fd);
return 3;
}
ret = emile_second_set_configuration(fd, configuration);
2004-12-10 00:28:35 +00:00
if (ret)
{
2004-12-10 00:28:35 +00:00
perror("Cannot write header");
close(fd);
2004-12-10 00:28:35 +00:00
return 4;
}
close(fd);
2005-11-28 23:39:28 +00:00
free(configuration);
return 0;
}
int main(int argc, char** argv)
{
int ret;
2005-12-12 22:14:52 +00:00
char* image = NULL;
2004-12-24 23:43:44 +00:00
int option_index;
int c;
unsigned int enable_mask = 0;
unsigned int disable_mask = 0;
unsigned int last = 0;
int width = 0, height = 0 , depth = 0;
2005-11-28 23:39:28 +00:00
unsigned int bitrate0 = 9600, bitrate1 = 9600;
int datasize0 = 8, datasize1 = 8;
int stopbits0 = 1, stopbits1 = 1;
int parity0 = 0, parity1 = 0;
2005-09-10 00:06:08 +00:00
int gestaltid = -1;
2004-12-24 23:43:44 +00:00
while(1)
{
2004-12-24 23:43:44 +00:00
c = getopt_long(argc, argv, "h", long_options,
&option_index);
if (c == EOF)
break;
2004-12-24 23:43:44 +00:00
switch(c)
{
case ARG_HELP:
usage(argc, argv);
return 0;
case ARG_GESTALTID:
gestaltid = atol(optarg);
break;
case ARG_NODISPLAY:
disable_mask |= STDOUT_VGA;
last = 0;
break;
case ARG_NOMODEM:
2005-11-28 23:39:28 +00:00
disable_mask |= STDOUT_MODEM;
2004-12-24 23:43:44 +00:00
last = 0;
break;
case ARG_NOPRINTER:
2005-11-28 23:39:28 +00:00
disable_mask |= STDOUT_PRINTER;
2004-12-24 23:43:44 +00:00
last = 0;
break;
case ARG_DISPLAY:
enable_mask |= STDOUT_VGA;
last = STDOUT_VGA;
break;
case ARG_MODEM:
2005-11-28 23:39:28 +00:00
enable_mask |= STDOUT_MODEM;
last = STDOUT_MODEM;
2004-12-24 23:43:44 +00:00
break;
case ARG_PRINTER:
2005-11-28 23:39:28 +00:00
enable_mask |= STDOUT_PRINTER;
last = STDOUT_PRINTER;
2004-12-24 23:43:44 +00:00
break;
case ARG_WIDTH:
if (last != STDOUT_VGA)
{
fprintf(stderr, "missing --display\n");
return 1;
}
width = atol(optarg);
break;
case ARG_HEIGHT:
if (last != STDOUT_VGA)
{
fprintf(stderr, "missing --display\n");
return 1;
}
height = atol(optarg);
break;
case ARG_DEPTH:
if (last != STDOUT_VGA)
{
fprintf(stderr, "missing --display\n");
return 1;
}
depth = atol(optarg);
break;
case ARG_BITRATE:
2005-11-28 23:39:28 +00:00
if (last == STDOUT_MODEM)
2004-12-24 23:43:44 +00:00
bitrate0 = atol(optarg);
2005-11-28 23:39:28 +00:00
else if (last == STDOUT_PRINTER)
2004-12-24 23:43:44 +00:00
bitrate1 = atol(optarg);
else
{
fprintf(stderr,
"missing --modem or --printer\n");
return 1;
}
break;
case ARG_DATASIZE:
2005-11-28 23:39:28 +00:00
if (last == STDOUT_MODEM)
2004-12-24 23:43:44 +00:00
datasize0 = atol(optarg);
2005-11-28 23:39:28 +00:00
else if (last == STDOUT_PRINTER)
2004-12-24 23:43:44 +00:00
datasize1 = atol(optarg);
else
{
fprintf(stderr,
"missing --modem or --printer\n");
return 1;
}
break;
case ARG_PARITY:
2005-11-28 23:39:28 +00:00
if (last == STDOUT_MODEM)
2004-12-24 23:43:44 +00:00
parity0 = atol(optarg);
2005-11-28 23:39:28 +00:00
else if (last == STDOUT_PRINTER)
2004-12-24 23:43:44 +00:00
parity1 = atol(optarg);
else
{
fprintf(stderr,
"missing --modem or --printer\n");
return 1;
}
2004-12-24 23:43:44 +00:00
break;
case ARG_STOPBITS:
2005-11-28 23:39:28 +00:00
if (last == STDOUT_MODEM)
2004-12-24 23:43:44 +00:00
stopbits0 = atol(optarg);
2005-11-28 23:39:28 +00:00
else if (last == STDOUT_PRINTER)
2004-12-24 23:43:44 +00:00
stopbits1 = atol(optarg);
else
{
fprintf(stderr,
"missing --modem or --printer\n");
return 1;
}
}
2004-12-24 23:43:44 +00:00
}
if (optind < argc)
image = argv[optind];
if (image == NULL)
{
fprintf(stderr, "ERROR: missing image file name\n");
usage(argc, argv);
return 1;
}
2004-12-24 23:43:44 +00:00
if ( width || height || depth)
{
fprintf(stderr,
"WARNING: setting display properties is not yet implemented !\n");
}
2004-12-24 23:43:44 +00:00
if (enable_mask & disable_mask) {
fprintf(stderr, "Cannot enable and disable at same time\n");
return 2;
}
2005-09-10 00:06:08 +00:00
if ( (enable_mask == 0) && (disable_mask == 0) && (gestaltid == -1))
2004-12-24 23:43:44 +00:00
{
display_output(image);
return 0;
}
2004-12-24 23:43:44 +00:00
ret = set_output(image, enable_mask, disable_mask,
bitrate0, datasize0, parity0, stopbits0,
bitrate1, datasize1, parity1, stopbits1,
gestaltid);
return ret;
}