Experimenting with utf8

This commit is contained in:
Andrew Pamment 2017-04-23 10:51:40 +10:00
parent 85cfb1dec8
commit bc588e8636
6 changed files with 84 additions and 12 deletions

View File

@ -35,7 +35,7 @@ $(CDK):
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${JSMN}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh -liconv
magimail: $(JAMLIB)
cd utils/magimail && $(MAKE) linux

View File

@ -39,7 +39,7 @@ OBJ = deps/aha/aha.o inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o do
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64} ${JSMN}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh $(MICROHTTPD)
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh -liconv $(MICROHTTPD)
magimail: $(JAMLIB)
cd utils/magimail && $(MAKE) linux

65
bbs.c
View File

@ -14,6 +14,7 @@
#include <fts.h>
#include <errno.h>
#include <sys/socket.h>
#include <iconv.h>
#include "bbs.h"
#include "lua/lua.h"
#include "lua/lualib.h"
@ -208,20 +209,82 @@ void s_printf(char *fmt, ...) {
s_putstring(buffer);
}
int should_convert_utf8() {
return conf.codepage;
}
void s_putchar(char c) {
iconv_t ic;
char *inbuf;
char *outbuf;
char *ptr1;
char *ptr2;
size_t inc;
size_t ouc;
size_t sz;
if (!should_convert_utf8()) {
if (sshBBS) {
putchar(c);
} else {
write(gSocket, &c, 1);
}
} else {
ic = iconv_open("UTF-8", "CP437");
inbuf = (char *)malloc(3);
outbuf = (char *)malloc(3);
memset(outbuf, 0, 3);
sprintf(inbuf, "%c", c);
inc = 1;
ouc = 3;
ptr1 = outbuf;
ptr2 = inbuf;
sz = iconv(ic, &inbuf, &inc, &outbuf, &ouc);
if (sshBBS) {
fprintf(stdout, "%s", ptr1);
} else {
write(gSocket, ptr1, strlen(ptr1));
}
iconv_close(ic);
free(ptr1);
free(ptr2);
}
}
void s_putstring(char *c) {
iconv_t ic;
char *inbuf;
char *outbuf;
size_t inc;
size_t ouc;
size_t sz;
char *ptr1;
char *ptr2;
if (!should_convert_utf8()) {
if (sshBBS) {
printf("%s", c);
puts(c);
} else {
write(gSocket, c, strlen(c));
}
} else {
ic = iconv_open("UTF-8", "CP437");
inc = strlen(c);
inbuf = strdup(c);
outbuf = (char *)malloc(inc * 2);
memset(outbuf, 0, inc *2);
ptr1 = outbuf;
ptr2 = inbuf;
ouc = inc * 2;
sz = iconv(ic, &inbuf, &inc, &outbuf, &ouc);
if (sshBBS) {
fprintf(stdout, "%s", ptr1);
} else {
write(gSocket, ptr1, strlen(ptr1));
}
iconv_close(ic);
free(ptr1);
free(ptr2);
}
}
void s_displayansi_p(char *file) {

1
bbs.h
View File

@ -113,6 +113,7 @@ struct ip_address_guard {
};
struct bbs_config {
int codepage;
char *bbs_name;
char *bwave_name;
char *sysop_name;

View File

@ -1,4 +1,5 @@
[main]
Codepage = cp437
Telnet Port = 2023
BBS Name = Magicka BBS
Sysop Name = sysop

7
main.c
View File

@ -479,6 +479,12 @@ static int handler(void* user, const char* section, const char* name,
conf->ipguard_tries = atoi(value);
} else if (strcasecmp(name, "root menu") == 0) {
conf->root_menu = strdup(value);
} else if (strcasecmp(name, "codepage") == 0) {
if (strcasecmp(value, "cp437") == 0) {
conf->codepage = 0;
} else if (strcasecmp(value, "utf8") == 0) {
conf->codepage = 1;
}
}
} else if (strcasecmp(section, "paths") == 0){
if (strcasecmp(name, "ansi path") == 0) {
@ -1154,6 +1160,7 @@ int main(int argc, char **argv) {
conf.ipguard_tries = 4;
conf.ipguard_timeout = 120;
conf.protocol_count = 0;
conf.codepage = 0;
// Load BBS data
if (ini_parse(argv[1], handler, &conf) <0) {