Update config.c

This commit is contained in:
Jasmine Iwanek
2022-07-21 21:44:55 -04:00
parent c430fbe84c
commit 02040ca052

View File

@@ -90,7 +90,8 @@ typedef struct {
wchar_t wdata[512];
} entry_t;
#define list_add(new, head) { \
#define list_add(new, head) \
{ \
list_t *next = head; \
\
while (next->next != NULL) \
@@ -100,7 +101,8 @@ typedef struct {
(new)->next = NULL; \
}
#define list_delete(old, head) { \
#define list_delete(old, head) \
{ \
list_t *next = head; \
\
while ((next)->next != old) { \
@@ -112,18 +114,15 @@ typedef struct {
(head)->next = (old)->next; \
}
static list_t config_head;
/* TODO: Backwards compatibility, get rid of this when enough time has passed. */
static int backwards_compat = 0;
static int backwards_compat2 = 0;
#ifdef ENABLE_CONFIG_LOG
int config_do_log = ENABLE_CONFIG_LOG;
static void
config_log(const char *fmt, ...)
{
@@ -139,7 +138,6 @@ config_log(const char *fmt, ...)
# define config_log(fmt, ...)
#endif
static section_t *
find_section(char *name)
{
@@ -160,14 +158,12 @@ find_section(char *name)
return (NULL);
}
void *
config_find_section(char *name)
{
return (void *) find_section(name);
}
void
config_rename_section(void *priv, char *name)
{
@@ -177,7 +173,6 @@ config_rename_section(void *priv, char *name)
memcpy(sec->name, name, MIN(128, strlen(name) + 1));
}
static entry_t *
find_entry(section_t *section, char *name)
{
@@ -195,7 +190,6 @@ find_entry(section_t *section, char *name)
return (NULL);
}
static int
entries_num(section_t *section)
{
@@ -205,7 +199,8 @@ entries_num(section_t *section)
ent = (entry_t *) section->entry_head.next;
while (ent != NULL) {
if (strlen(ent->name) > 0) i++;
if (strlen(ent->name) > 0)
i++;
ent = (entry_t *) ent->list.next;
}
@@ -213,14 +208,14 @@ entries_num(section_t *section)
return (i);
}
static void
delete_section_if_empty(char *head)
{
section_t *section;
section = find_section(head);
if (section == NULL) return;
if (section == NULL)
return;
if (entries_num(section) == 0) {
list_delete(&section->list, &config_head);
@@ -228,7 +223,6 @@ delete_section_if_empty(char *head)
}
}
static section_t *
create_section(char *name)
{
@@ -241,7 +235,6 @@ create_section(char *name)
return (ns);
}
static entry_t *
create_entry(section_t *section, char *name)
{
@@ -254,7 +247,6 @@ create_entry(section_t *section, char *name)
return (ne);
}
#if 0
static void
config_free(void)
@@ -291,10 +283,10 @@ config_detect_bom(char *fn)
#else
f = plat_fopen(fn, "rt, ccs=UTF-8");
#endif
if (f == NULL) return(0);
if (f == NULL)
return (0);
fread(bom, 1, 3, f);
if (bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF)
{
if (bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF) {
fclose(f);
return 1;
}
@@ -308,17 +300,21 @@ static wchar_t*
config_fgetws(wchar_t *str, int count, FILE *stream)
{
int i = 0;
if (feof(stream)) return NULL;
if (feof(stream))
return NULL;
for (i = 0; i < count; i++) {
wint_t curChar = fgetwc(stream);
if (curChar == WEOF) {
if (i + 1 < count) str[i + 1] = 0;
if (i + 1 < count)
str[i + 1] = 0;
return feof(stream) ? str : NULL;
}
str[i] = curChar;
if (curChar == '\n') break;
if (curChar == '\n')
break;
}
if (i + 1 < count) str[i + 1] = 0;
if (i + 1 < count)
str[i + 1] = 0;
return str;
}
#endif
@@ -340,7 +336,8 @@ config_read(char *fn)
#else
f = plat_fopen(fn, "rt, ccs=UTF-8");
#endif
if (f == NULL) return(0);
if (f == NULL)
return (0);
sec = malloc(sizeof(section_t));
memset(sec, 0x00, sizeof(section_t));
@@ -356,13 +353,16 @@ config_read(char *fn)
#else
fgetws(buff, sizeof_w(buff), f);
#endif
if (feof(f)) break;
if (feof(f))
break;
/* Make sure there are no stray newlines or hard-returns in there. */
if (wcslen(buff) > 0)
if (buff[wcslen(buff)-1] == L'\n') buff[wcslen(buff)-1] = L'\0';
if (buff[wcslen(buff) - 1] == L'\n')
buff[wcslen(buff) - 1] = L'\0';
if (wcslen(buff) > 0)
if (buff[wcslen(buff)-1] == L'\r') buff[wcslen(buff)-1] = L'\0';
if (buff[wcslen(buff) - 1] == L'\r')
buff[wcslen(buff) - 1] = L'\0';
/* Skip any leading whitespace. */
c = 0;
@@ -370,10 +370,12 @@ config_read(char *fn)
c++;
/* Skip empty lines. */
if (buff[c] == L'\0') continue;
if (buff[c] == L'\0')
continue;
/* Skip lines that (only) have a comment. */
if ((buff[c] == L'#') || (buff[c] == L';')) continue;
if ((buff[c] == L'#') || (buff[c] == L';'))
continue;
if (buff[c] == L'[') { /*Section*/
c++;
@@ -383,7 +385,8 @@ config_read(char *fn)
sname[d] = L'\0';
/* Is the section name properly terminated? */
if (buff[c] != L']') continue;
if (buff[c] != L']')
continue;
/* Create a new section and insert it. */
ns = malloc(sizeof(section_t));
@@ -403,14 +406,16 @@ config_read(char *fn)
ename[d] = L'\0';
/* Skip incomplete lines. */
if (buff[c] == L'\0') continue;
if (buff[c] == L'\0')
continue;
/* Look for =, skip whitespace. */
while ((buff[c] == L'=' || buff[c] == L' ') && buff[c])
c++;
/* Skip incomplete lines. */
if (buff[c] == L'\0') continue;
if (buff[c] == L'\0')
continue;
/* This is where the value part starts. */
d = c;
@@ -440,7 +445,6 @@ config_read(char *fn)
return (1);
}
/*
* Write the in-memory configuration to disk.
* This is a public function, because the Settings UI
@@ -460,7 +464,8 @@ config_write(char *fn)
#else
f = plat_fopen(fn, "wt, ccs=UTF-8");
#endif
if (f == NULL) return;
if (f == NULL)
return;
sec = (section_t *) config_head.next;
while (sec != NULL) {
@@ -495,7 +500,6 @@ config_write(char *fn)
(void) fclose(f);
}
#if NOT_USED
static void
config_new(void)
@@ -511,7 +515,6 @@ config_new(void)
}
#endif
/* Load "General" section. */
static void
load_general(void)
@@ -611,7 +614,6 @@ load_general(void)
strncpy(video_shader, config_get_string(cat, "video_gl_shader", ""), sizeof(video_shader));
}
/* Load "Machine" section. */
static void
load_machine(void)
@@ -791,7 +793,8 @@ load_machine(void)
if (cpu_legacy_table[c].machine) {
/* Determine the amount of CPU entries on the table. */
i = -1;
while (cpu_legacy_table[c].tables[legacy_mfg][++i].family);
while (cpu_legacy_table[c].tables[legacy_mfg][++i].family)
;
/* If the CPU ID is out of bounds, reset to the last known ID. */
if (legacy_cpu >= i)
@@ -874,11 +877,9 @@ load_machine(void)
if (p != NULL) {
if (!strcmp(p, "disabled"))
time_sync = TIME_SYNC_DISABLED;
else
if (!strcmp(p, "local"))
else if (!strcmp(p, "local"))
time_sync = TIME_SYNC_ENABLED;
else
if (!strcmp(p, "utc") || !strcmp(p, "gmt"))
else if (!strcmp(p, "utc") || !strcmp(p, "gmt"))
time_sync = TIME_SYNC_ENABLED | TIME_SYNC_UTC;
else
time_sync = TIME_SYNC_ENABLED;
@@ -890,7 +891,6 @@ load_machine(void)
config_delete_var(cat, "enable_sync");
}
/* Load "Video" section. */
static void
load_video(void)
@@ -927,11 +927,11 @@ load_video(void)
xga_enabled = !!config_get_int(cat, "xga", 0);
show_second_monitors = !!config_get_int(cat, "show_second_monitors", 1);
p = config_get_string(cat, "gfxcard_2", NULL);
if (!p) p = "none";
if (!p)
p = "none";
gfxcard_2 = video_get_video_from_internal_name(p);
}
static void
load_monitor(int monitor_index)
{
@@ -945,8 +945,10 @@ load_monitor(int monitor_index)
config_delete_var("General", "window_coordinates");
}
snprintf(monitor_config_name, sizeof(monitor_config_name), "Monitor #%i", monitor_index + 1);
if (!ptr) ptr = config_get_string(monitor_config_name, "window_coordinates", "0, 0, 0, 0");
if (window_remember || (vid_resize & 2)) sscanf(ptr, "%i, %i, %i, %i",
if (!ptr)
ptr = config_get_string(monitor_config_name, "window_coordinates", "0, 0, 0, 0");
if (window_remember || (vid_resize & 2))
sscanf(ptr, "%i, %i, %i, %i",
&monitor_settings[monitor_index].mon_window_x, &monitor_settings[monitor_index].mon_window_y,
&monitor_settings[monitor_index].mon_window_w, &monitor_settings[monitor_index].mon_window_h);
}
@@ -961,15 +963,15 @@ save_monitor(int monitor_index)
if (!(monitor_settings[monitor_index].mon_window_x == 0
&& monitor_settings[monitor_index].mon_window_y == 0
&& monitor_settings[monitor_index].mon_window_w == 0
&& monitor_settings[monitor_index].mon_window_h == 0) && (window_remember || (vid_resize & 2))) {
&& monitor_settings[monitor_index].mon_window_h == 0)
&& (window_remember || (vid_resize & 2))) {
snprintf(saved_coordinates, sizeof(saved_coordinates), "%i, %i, %i, %i", monitor_settings[monitor_index].mon_window_x, monitor_settings[monitor_index].mon_window_y,
monitor_settings[monitor_index].mon_window_w, monitor_settings[monitor_index].mon_window_h);
config_set_string(monitor_config_name, "window_coordinates", saved_coordinates);
} else
config_delete_var(monitor_config_name, "window_coordinates");
}
else config_delete_var(monitor_config_name, "window_coordinates");
}
/* Load "Input Devices" section. */
static void
@@ -1061,7 +1063,6 @@ load_input_devices(void)
}
}
/* Load "Sound" section. */
static void
load_sound(void)
@@ -1109,7 +1110,6 @@ load_sound(void)
sound_is_float = 0;
}
/* Load "Network" section. */
static void
load_network(void)
@@ -1121,8 +1121,7 @@ load_network(void)
if (p != NULL) {
if (!strcmp(p, "pcap") || !strcmp(p, "1"))
network_type = NET_TYPE_PCAP;
else
if (!strcmp(p, "slirp") || !strcmp(p, "2"))
else if (!strcmp(p, "slirp") || !strcmp(p, "2"))
network_type = NET_TYPE_SLIRP;
else
network_type = NET_TYPE_NONE;
@@ -1158,7 +1157,6 @@ load_network(void)
network_card = 0;
}
/* Load "Ports" section. */
static void
load_ports(void)
@@ -1197,7 +1195,6 @@ load_ports(void)
config_delete_var(cat, "lpt_enabled");
}
/* Load "Storage Controllers" section. */
static void
load_storage_controllers(void)
@@ -1312,7 +1309,6 @@ load_storage_controllers(void)
}
}
/* Load "Hard Disks" section. */
static void
load_hard_disks(void)
@@ -1512,7 +1508,6 @@ load_hard_disks(void)
}
}
/* TODO: Backwards compatibility, get rid of this when enough time has passed. */
/* Load "Floppy Drives" section. */
static void
@@ -1575,7 +1570,6 @@ load_floppy_drives(void)
delete_section_if_empty(cat);
}
/* Load "Floppy and CD-ROM Drives" section. */
static void
load_floppy_and_cdrom_drives(void)
@@ -1746,8 +1740,7 @@ load_floppy_and_cdrom_drives(void)
if (cdrom[c].host_drive && (cdrom[c].host_drive != 200))
cdrom[c].host_drive = 0;
if ((cdrom[c].host_drive == 0x200) &&
(strlen(cdrom[c].image_path) == 0))
if ((cdrom[c].host_drive == 0x200) && (strlen(cdrom[c].image_path) == 0))
cdrom[c].host_drive = 0;
/* If the CD-ROM is disabled, delete all its variables. */
@@ -1773,7 +1766,6 @@ load_floppy_and_cdrom_drives(void)
}
}
/* Load "Other Removable Devices" section. */
static void
load_other_removable_devices(void)
@@ -1860,8 +1852,7 @@ load_other_removable_devices(void)
if (cdrom[c].host_drive && (cdrom[c].host_drive != 200))
cdrom[c].host_drive = 0;
if ((cdrom[c].host_drive == 0x200) &&
(strlen(cdrom[c].image_path) == 0))
if ((cdrom[c].host_drive == 0x200) && (strlen(cdrom[c].image_path) == 0))
cdrom[c].host_drive = 0;
}
}
@@ -2051,7 +2042,6 @@ load_other_removable_devices(void)
}
}
/* Load "Other Peripherals" section. */
static void
load_other_peripherals(void)
@@ -2127,7 +2117,6 @@ load_other_peripherals(void)
isartc_type = isartc_get_from_internal_name(p);
}
/* Load the specified or a default configuration file. */
void
config_load(void)
@@ -2229,7 +2218,6 @@ config_load(void)
video_copy = (video_grayscale || invert_display) ? video_transform_copy : memcpy;
}
/* Save "General" section. */
static void
save_general(void)
@@ -2396,7 +2384,6 @@ save_general(void)
delete_section_if_empty(cat);
}
/* Save "Machine" section. */
static void
save_machine(void)
@@ -2440,12 +2427,10 @@ save_machine(void)
/* Match the family name, speed and multiplier. */
if (!strcmp(cpu_f->internal_name, legacy_table_entry->family)) {
if ((legacy_table_entry->rspeed == cpu_f->cpus[cpu].rspeed) &&
(legacy_table_entry->multi == cpu_f->cpus[cpu].multi)) { /* exact speed/multiplier match */
if ((legacy_table_entry->rspeed == cpu_f->cpus[cpu].rspeed) && (legacy_table_entry->multi == cpu_f->cpus[cpu].multi)) { /* exact speed/multiplier match */
legacy_cpu = i;
break;
} else if ((legacy_table_entry->rspeed >= cpu_f->cpus[cpu].rspeed) &&
(closest_legacy_cpu == -1)) { /* closest speed match */
} else if ((legacy_table_entry->rspeed >= cpu_f->cpus[cpu].rspeed) && (closest_legacy_cpu == -1)) { /* closest speed match */
closest_legacy_cpu = i;
}
}
@@ -2497,7 +2482,6 @@ save_machine(void)
delete_section_if_empty(cat);
}
/* Save "Video" section. */
static void
save_video(void)
@@ -2535,7 +2519,6 @@ save_video(void)
delete_section_if_empty(cat);
}
/* Save "Input Devices" section. */
static void
save_input_devices(void)
@@ -2594,7 +2577,6 @@ save_input_devices(void)
delete_section_if_empty(cat);
}
/* Save "Sound" section. */
static void
save_sound(void)
@@ -2644,7 +2626,6 @@ save_sound(void)
delete_section_if_empty(cat);
}
/* Save "Network" section. */
static void
save_network(void)
@@ -2676,7 +2657,6 @@ save_network(void)
delete_section_if_empty(cat);
}
/* Save "Ports" section. */
static void
save_ports(void)
@@ -2727,7 +2707,6 @@ save_ports(void)
delete_section_if_empty(cat);
}
/* Save "Storage Controllers" section. */
static void
save_storage_controllers(void)
@@ -2818,7 +2797,6 @@ save_storage_controllers(void)
}
}
/* Save "Other Peripherals" section. */
static void
save_other_peripherals(void)
@@ -2855,7 +2833,6 @@ save_other_peripherals(void)
delete_section_if_empty(cat);
}
/* Save "Hard Disks" section. */
static void
save_hard_disks(void)
@@ -2922,8 +2899,7 @@ save_hard_disks(void)
config_set_string(cat, temp, &hdd[c].fn[strlen(usr_path)]);
else
config_set_string(cat, temp, hdd[c].fn);
}
else
} else
config_delete_var(cat, temp);
sprintf(temp, "hdd_%02i_speed", c + 1);
@@ -2931,13 +2907,11 @@ save_hard_disks(void)
config_delete_var(cat, temp);
else
config_set_string(cat, temp, hdd_preset_get_internal_name(hdd[c].speed_preset));
}
delete_section_if_empty(cat);
}
/* Save "Floppy Drives" section. */
static void
save_floppy_and_cdrom_drives(void)
@@ -3031,8 +3005,7 @@ save_floppy_and_cdrom_drives(void)
}
sprintf(temp, "cdrom_%02i_image_path", c + 1);
if ((cdrom[c].bus_type == 0) ||
(strlen(cdrom[c].image_path) == 0)) {
if ((cdrom[c].bus_type == 0) || (strlen(cdrom[c].image_path) == 0)) {
config_delete_var(cat, temp);
} else {
config_set_string(cat, temp, cdrom[c].image_path);
@@ -3042,7 +3015,6 @@ save_floppy_and_cdrom_drives(void)
delete_section_if_empty(cat);
}
/* Save "Other Removable Devices" section. */
static void
save_other_removable_devices(void)
@@ -3083,8 +3055,7 @@ save_other_removable_devices(void)
}
sprintf(temp, "zip_%02i_image_path", c + 1);
if ((zip_drives[c].bus_type == 0) ||
(strlen(zip_drives[c].image_path) == 0)) {
if ((zip_drives[c].bus_type == 0) || (strlen(zip_drives[c].image_path) == 0)) {
config_delete_var(cat, temp);
} else {
config_set_string(cat, temp, zip_drives[c].image_path);
@@ -3123,8 +3094,7 @@ save_other_removable_devices(void)
}
sprintf(temp, "mo_%02i_image_path", c + 1);
if ((mo_drives[c].bus_type == 0) ||
(strlen(mo_drives[c].image_path) == 0)) {
if ((mo_drives[c].bus_type == 0) || (strlen(mo_drives[c].image_path) == 0)) {
config_delete_var(cat, temp);
} else {
config_set_string(cat, temp, mo_drives[c].image_path);
@@ -3134,7 +3104,6 @@ save_other_removable_devices(void)
delete_section_if_empty(cat);
}
void
config_save(void)
{
@@ -3158,7 +3127,6 @@ config_save(void)
config_write(cfg_path);
}
void
config_dump(void)
{
@@ -3182,7 +3150,6 @@ config_dump(void)
}
}
void
config_delete_var(char *head, char *name)
{
@@ -3190,7 +3157,8 @@ config_delete_var(char *head, char *name)
entry_t *entry;
section = find_section(head);
if (section == NULL) return;
if (section == NULL)
return;
entry = find_entry(section, name);
if (entry != NULL) {
@@ -3199,7 +3167,6 @@ config_delete_var(char *head, char *name)
}
}
int
config_get_int(char *head, char *name, int def)
{
@@ -3220,7 +3187,6 @@ config_get_int(char *head, char *name, int def)
return (value);
}
double
config_get_double(char *head, char *name, double def)
{
@@ -3241,7 +3207,6 @@ config_get_double(char *head, char *name, double def)
return (value);
}
int
config_get_hex16(char *head, char *name, int def)
{
@@ -3262,7 +3227,6 @@ config_get_hex16(char *head, char *name, int def)
return (value);
}
int
config_get_hex20(char *head, char *name, int def)
{
@@ -3283,7 +3247,6 @@ config_get_hex20(char *head, char *name, int def)
return (value);
}
int
config_get_mac(char *head, char *name, int def)
{
@@ -3304,7 +3267,6 @@ config_get_mac(char *head, char *name, int def)
return ((val0 << 16) + (val1 << 8) + val2);
}
char *
config_get_string(char *head, char *name, char *def)
{
@@ -3322,7 +3284,6 @@ config_get_string(char *head, char *name, char *def)
return (entry->data);
}
wchar_t *
config_get_wstring(char *head, char *name, wchar_t *def)
{
@@ -3340,7 +3301,6 @@ config_get_wstring(char *head, char *name, wchar_t *def)
return (entry->wdata);
}
void
config_set_int(char *head, char *name, int val)
{
@@ -3359,7 +3319,6 @@ config_set_int(char *head, char *name, int val)
mbstowcs(ent->wdata, ent->data, 512);
}
void
config_set_double(char *head, char *name, double val)
{
@@ -3378,7 +3337,6 @@ config_set_double(char *head, char *name, double val)
mbstowcs(ent->wdata, ent->data, 512);
}
void
config_set_hex16(char *head, char *name, int val)
{
@@ -3397,7 +3355,6 @@ config_set_hex16(char *head, char *name, int val)
mbstowcs(ent->wdata, ent->data, sizeof_w(ent->wdata));
}
void
config_set_hex20(char *head, char *name, int val)
{
@@ -3416,7 +3373,6 @@ config_set_hex20(char *head, char *name, int val)
mbstowcs(ent->wdata, ent->data, sizeof_w(ent->wdata));
}
void
config_set_mac(char *head, char *name, int val)
{
@@ -3436,7 +3392,6 @@ config_set_mac(char *head, char *name, int val)
mbstowcs(ent->wdata, ent->data, 512);
}
void
config_set_string(char *head, char *name, char *val)
{
@@ -3462,7 +3417,6 @@ config_set_string(char *head, char *name, char *val)
#endif
}
void
config_set_wstring(char *head, char *name, wchar_t *val)
{