Index: src/client.mm ================================================================== --- src/client.mm +++ src/client.mm @@ -164,11 +164,11 @@ disconnect(0, !disconnecting); } string ctext; void -toserver(char *text) +toserver(const char *text) { conoutf(@"%s:\f %s", player1->name, text); strn0cpy(ctext, text, 80); } Index: src/command.mm ================================================================== --- src/command.mm +++ src/command.mm @@ -509,36 +509,47 @@ { completesize = 0; } void -complete(char *s) -{ - if (*s != '/') { - string t; - strcpy_s(t, s); - strcpy_s(s, "/"); - strcat_s(s, t); - } - if (!s[1]) - return; - if (!completesize) { - completesize = (int)strlen(s) - 1; - completeidx = 0; - } - __block int idx = 0; - [idents enumerateKeysAndObjectsUsingBlock:^( - OFString *name, Ident *ident, bool *stop) { - if (strncmp(ident.name.UTF8String, s + 1, completesize) == 0 && - idx++ == completeidx) { - strcpy_s(s, "/"); - strcat_s(s, ident.name.UTF8String); - } - }]; - completeidx++; - if (completeidx >= idx) - completeidx = 0; +complete(OFString *s_) +{ + @autoreleasepool { + std::unique_ptr copy(strdup(s_.UTF8String)); + char *s = copy.get(); + + if (*s != '/') { + string t; + strcpy_s(t, s); + strcpy_s(s, "/"); + strcat_s(s, t); + } + + if (!s[1]) + return; + + if (!completesize) { + completesize = strlen(s) - 1; + completeidx = 0; + } + + __block int idx = 0; + [idents enumerateKeysAndObjectsUsingBlock:^( + OFString *name, Ident *ident, bool *stop) { + if (strncmp(ident.name.UTF8String, s + 1, + completesize) == 0 && + idx++ == completeidx) { + strcpy_s(s, "/"); + strcat_s(s, ident.name.UTF8String); + } + }]; + + completeidx++; + + if (completeidx >= idx) + completeidx = 0; + } } bool execfile(OFString *cfgfile) { Index: src/console.mm ================================================================== --- src/console.mm +++ src/console.mm @@ -16,11 +16,11 @@ const int ndraw = 5; const int WORDWRAP = 80; int conskip = 0; bool saycommandon = false; -string commandbuf; +static OFMutableString *commandbuf; void setconskip(int n) { conskip += n; @@ -126,22 +126,25 @@ conoutf(@"unknown key \"%@\"", key); } COMMANDN(bind, bindkey, ARG_2STR) void -saycommand(char *init) // turns input to the command line on or off +saycommand(const char *init) // turns input to the command line on or off { saycommandon = (init != NULL); if (saycommandon) SDL_StartTextInput(); else SDL_StopTextInput(); + if (!editmode) Cube.sharedInstance.repeatsKeys = saycommandon; + if (!init) init = ""; - strcpy_s(commandbuf, init); + + commandbuf = [[OFMutableString alloc] initWithUTF8String:init]; } COMMAND(saycommand, ARG_VARI) void mapmsg(OFString *s) @@ -153,16 +156,17 @@ COMMAND(mapmsg, ARG_1STR) void pasteconsole() { - char *cb = SDL_GetClipboardText(); - strcat_s(commandbuf, cb); + @autoreleasepool { + [commandbuf appendString:@(SDL_GetClipboardText())]; + } } static OFMutableArray *vhistory; -int histpos = 0; +static int histpos = 0; void history(int n) { static bool rec = false; @@ -187,35 +191,28 @@ case SDLK_RETURN: break; case SDLK_BACKSPACE: case SDLK_LEFT: { - for (int i = 0; commandbuf[i]; i++) - if (!commandbuf[i + 1]) - commandbuf[i] = 0; + [commandbuf + deleteCharactersInRange: + OFMakeRange(commandbuf.length - 1, 1)]; + resetcomplete(); break; } case SDLK_UP: - if (histpos) { - @autoreleasepool { - strcpy_s(commandbuf, - vhistory[--histpos] - .UTF8String); - } - } + if (histpos) + commandbuf = + [vhistory[--histpos] mutableCopy]; break; case SDLK_DOWN: - if (histpos < vhistory.count) { - @autoreleasepool { - strcpy_s(commandbuf, - vhistory[histpos++] - .UTF8String); - } - } + if (histpos < vhistory.count) + commandbuf = + [vhistory[histpos++] mutableCopy]; break; case SDLK_TAB: complete(commandbuf); break; @@ -223,44 +220,44 @@ case SDLK_v: if (SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL)) { pasteconsole(); return; - }; + } default: resetcomplete(); - if (cooked) { - char add[] = {(char)cooked, 0}; - strcat_s(commandbuf, add); - } + if (cooked) + [commandbuf appendFormat:@"%c", cooked]; } } else { if (code == SDLK_RETURN) { - if (commandbuf[0]) { + if (commandbuf.length > 0) { @autoreleasepool { - OFString *cmdbuf = - @(commandbuf); - if (vhistory == nil) vhistory = [[OFMutableArray alloc] init]; if (vhistory.count == 0 || ![vhistory.lastObject - isEqual:cmdbuf]) { + isEqual:commandbuf]) { // cap this? [vhistory - addObject:cmdbuf]; + addObject: + [commandbuf + copy]]; } } histpos = vhistory.count; - if (commandbuf[0] == '/') - execute(commandbuf, true); - else - toserver(commandbuf); + if ([commandbuf hasPrefix:@"/"]) { + std::unique_ptr copy( + strdup( + commandbuf.UTF8String)); + execute(copy.get(), true); + } else + toserver(commandbuf.UTF8String); } saycommand(NULL); } else if (code == SDLK_ESCAPE) { saycommand(NULL); } @@ -279,11 +276,11 @@ } } } } -char * +OFString * getcurcommand() { return saycommandon ? commandbuf : NULL; } Index: src/protos.h ================================================================== --- src/protos.h +++ src/protos.h @@ -9,20 +9,20 @@ extern bool addcommand(OFString *name, void (*fun)(), int narg); extern int execute(char *p, bool down = true); extern void exec(OFString *cfgfile); extern bool execfile(OFString *cfgfile); extern void resetcomplete(); -extern void complete(char *s); +extern void complete(OFString *s); extern void alias(OFString *name, OFString *action); extern OFString *getalias(OFString *name); extern void writecfg(); // console extern void keypress(int code, bool isdown, int cooked); extern void renderconsole(); extern void conoutf(OFConstantString *format, ...); -extern char *getcurcommand(); +extern OFString *getcurcommand(); extern void writebinds(OFStream *stream); // init extern void enqueueInit(void (^init)(void)); extern void processInitQueue(void); @@ -69,11 +69,11 @@ // client extern void localservertoclient(uchar *buf, int len); extern void connects(OFString *servername); extern void disconnect(int onlyclean = 0, int async = 0); -extern void toserver(char *text); +extern void toserver(const char *text); extern void addmsg(int rel, int num, int type, ...); extern bool multiplayer(); extern bool allowedittoggle(); extern void sendpackettoserv(void *packet); extern void gets2c(); Index: src/renderextras.mm ================================================================== --- src/renderextras.mm +++ src/renderextras.mm @@ -364,15 +364,15 @@ } glEnable(GL_TEXTURE_2D); @autoreleasepool { - char *command = getcurcommand(); + OFString *command = getcurcommand(); char *player = playerincrosshair(); if (command) - draw_textf(@"> %s_", 20, 1570, 2, command); + draw_textf(@"> %@_", 20, 1570, 2, command); else if (closeent[0]) draw_text(@(closeent), 20, 1570, 2); else if (player) draw_text(@(player), 20, 1570, 2); }