diff options
Diffstat (limited to 'config/config.c')
| -rw-r--r-- | config/config.c | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/config/config.c b/config/config.c new file mode 100644 index 0000000..212d36b --- /dev/null +++ b/config/config.c @@ -0,0 +1,163 @@ +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <string.h> +#include <atari.h> +#include <conio.h> + +#include "../src/config.h" + +conf_t defaults = { + "N:TCP://irc.libera.chat:6667", + "FNChatTest", + "FujiNetChat User", + { 0xc0, 0x0c }, + { "#testari8", { 0 }, { 0 }, { 0 }, { 0 } }, + 3, + 1, + 0, + 1 +}; + +char filename[100] = "D:FNCHAT.CFG"; +char buf[100]; +char server[100]; +char port[6]; + +void print(const char *text) { + fputs(text, stdout); +} + +void prompt(const char *text, char *p) { + print(text); + print(" ["); + print(p); + print("]? "); + gets(buf); + if(buf[0]) strcpy(p, buf); +} + +char yn(const char *text) { + char c; + print(text); + print(" [Y/n]? "); + c = cgetc(); + if(c == '\n') + putchar('Y'); + else + putchar(c); + putchar('\n'); + return (c == '\n' || c == 'Y' || c == 'y'); +} + +void save(void) { + int fh, bad; + + do { + bad = 0; + + OS.shflok = 0x40; + prompt("Config File", filename); + OS.shflok = 0x00; + + if((fh = open(filename, O_WRONLY | O_CREAT)) < 0) { + print("!! I/O error (open)\n"); + bad = 1; + } else if((write(fh, conf, sizeof(conf_t))) != sizeof(conf_t)) { + print("!! I/O error (write)\n"); + bad = 1; + } + + if(fh >= 0) close(fh); + + if(bad) { + if(!yn("Retry")) break; + } + } while(bad); +} + +void load(void) { + int fh, bad = 0; + + if(!yn("Load Config")) { + bad = 1; + print("OK"); + } else { + OS.shflok = 0x40; + prompt("Config File", filename); + OS.shflok = 0x00; + + if((fh = open(filename, O_RDONLY)) < 0) { + print("Not found"); + bad = 1; + } else if((read(fh, conf, sizeof(conf_t))) != sizeof(conf_t)) { + print("Invalid"); + bad = 1; + } + + if(fh >= 0) close(fh); + } + + if(bad) { + print(", using built-in defaults\n"); + memcpy(conf, &defaults, sizeof(conf_t)); + } +} + +void make_url(void) { + conf->url[8] = '\0'; + strcat(conf->url, server); + strcat(conf->url, ":"); + strcat(conf->url, port); +} + +void main(void) { + char *p, *q; + + OS.color2 = defaults.colors[0]; + OS.color1 = defaults.colors[1]; + + cursor(1); + puts("\x7d" "FujiNetChat Setup\n"); + + load(); + + OS.color2 = conf->colors[0]; + OS.color1 = conf->colors[1]; + + q = server; + p = conf->url + 8; /* skip N:TCP:// */ + while(*p) { + if(*p == ':') break; + *q++ = *p++; + } + + *q = 0; + p++; + q = port; + strcpy(port, p); + + while(1) { + prompt("\nServer", server); + prompt("Port ", port); + prompt("Nick ", conf->nick); + // prompt_colors(); + // prompt_alert_type(); + conf->show_ping = yn("Show PING/PONG"); + conf->atract_away = yn("Set AWAY on ATRACT"); + conf->hide_motd = yn("Hide MOTD"); + + make_url(); + print("\nURL: "); + print(conf->url); + putchar('\n'); + + if(yn("Settings OK")) + break; + } + + if(yn("Save to disk")) + save(); + + print("\nClient loading..."); +} |
