@@ -64,10 +64,11 @@ static void termFormatError(lua_State *L, DWORD errorCode, const char* prefix) {
6464static int pusherror (lua_State * L , const char * info )
6565{
6666 lua_pushnil (L );
67- if (info == NULL )
67+ if (info == NULL ) {
6868 lua_pushstring (L , strerror (errno ));
69- else
69+ } else {
7070 lua_pushfstring (L , "%s: %s" , info , strerror (errno ));
71+ }
7172 lua_pushinteger (L , errno );
7273 return 3 ;
7374}
@@ -458,8 +459,7 @@ static int lst_getconsoleflags(lua_State *L)
458459
459460 if (GetConsoleMode (console_handle , & console_mode ) == 0 )
460461 {
461- lua_pushnil (L );
462- lua_pushliteral (L , "failed to get console mode" );
462+ termFormatError (L , GetLastError (), "failed to get console mode" );
463463 return 2 ;
464464 }
465465#else
@@ -970,8 +970,7 @@ static int lst_readkey(lua_State *L) {
970970 // printf("utf8_buffer_len: %d\n", utf8_buffer_len);
971971 utf8_buffer_index = 0 ;
972972 if (utf8_buffer_len <= 0 ) {
973- lua_pushnil (L );
974- lua_pushliteral (L , "UTF-8 conversion error" );
973+ termFormatError (L , GetLastError (), "UTF-8 conversion error" );
975974 return 2 ;
976975 }
977976
@@ -1208,12 +1207,18 @@ int lst_utf8swidth(lua_State *L) {
12081207Gets the current console code page (Windows).
12091208@function getconsolecp
12101209@treturn[1] int the current code page (always 65001 on Posix systems)
1210+ @treturn[2] nil
1211+ @treturn[2] string error message
12111212@within Terminal_UTF-8
12121213*/
12131214static int lst_getconsolecp (lua_State * L ) {
12141215 unsigned int cp = 65001 ;
12151216#ifdef _WIN32
12161217 cp = GetConsoleCP ();
1218+ if (cp == 0 ) {
1219+ termFormatError (L , GetLastError (), "failed to get console code page" );
1220+ return 2 ;
1221+ }
12171222#endif
12181223 lua_pushinteger (L , cp );
12191224 return 1 ;
@@ -1226,17 +1231,21 @@ Sets the current console code page (Windows).
12261231@function setconsolecp
12271232@tparam int cp the code page to set, use `system.CODEPAGE_UTF8` (65001) for UTF-8
12281233@treturn[1] bool `true` on success (always `true` on Posix systems)
1234+ @treturn[2] nil
1235+ @treturn[2] string error message
12291236@within Terminal_UTF-8
12301237*/
12311238static int lst_setconsolecp (lua_State * L ) {
12321239 unsigned int cp = (unsigned int )luaL_checkinteger (L , 1 );
1233- int success = TRUE;
12341240#ifdef _WIN32
1235- SetConsoleCP (cp );
1241+ if (!SetConsoleCP (cp )) {
1242+ termFormatError (L , GetLastError (), "failed to set console code page" );
1243+ return 2 ;
1244+ }
12361245#else
12371246 (void )cp ;
12381247#endif
1239- lua_pushboolean (L , success );
1248+ lua_pushboolean (L , 1 );
12401249 return 1 ;
12411250}
12421251
@@ -1246,12 +1255,18 @@ static int lst_setconsolecp(lua_State *L) {
12461255Gets the current console output code page (Windows).
12471256@function getconsoleoutputcp
12481257@treturn[1] int the current code page (always 65001 on Posix systems)
1258+ @treturn[2] nil
1259+ @treturn[2] string error message
12491260@within Terminal_UTF-8
12501261*/
12511262static int lst_getconsoleoutputcp (lua_State * L ) {
12521263 unsigned int cp = 65001 ;
12531264#ifdef _WIN32
12541265 cp = GetConsoleOutputCP ();
1266+ if (cp == 0 ) {
1267+ termFormatError (L , GetLastError (), "failed to get console output code page" );
1268+ return 2 ;
1269+ }
12551270#endif
12561271 lua_pushinteger (L , cp );
12571272 return 1 ;
@@ -1264,17 +1279,21 @@ Sets the current console output code page (Windows).
12641279@function setconsoleoutputcp
12651280@tparam int cp the code page to set, use `system.CODEPAGE_UTF8` (65001) for UTF-8
12661281@treturn[1] bool `true` on success (always `true` on Posix systems)
1282+ @treturn[2] nil
1283+ @treturn[2] string error message
12671284@within Terminal_UTF-8
12681285*/
12691286static int lst_setconsoleoutputcp (lua_State * L ) {
12701287 unsigned int cp = (unsigned int )luaL_checkinteger (L , 1 );
1271- int success = TRUE;
12721288#ifdef _WIN32
1273- SetConsoleOutputCP (cp );
1289+ if (!SetConsoleOutputCP (cp )) {
1290+ termFormatError (L , GetLastError (), "failed to set console output code page" );
1291+ return 2 ;
1292+ }
12741293#else
12751294 (void )cp ;
12761295#endif
1277- lua_pushboolean (L , success );
1296+ lua_pushboolean (L , 1 );
12781297 return 1 ;
12791298}
12801299
0 commit comments