Buffer containing russian Unicode charecters is not cleaning after pressing Backspace

Bug #1360419 reported by Ivan Zuboff
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gnome-terminal (Ubuntu)
Fix Committed
Low
Ivan Zuboff

Bug Description

I'm writing C applications for Ubuntu and I've find out interesting bug. That's a short program showing what's wrong. It reads a line from user and writes that line.

"#include <stdio.h>

void main()
{

char buffer[1024];

for (;;) {
 fgets(buffer, sizeof buffer-1, stdin);
 printf("%s\n", buffer);
 }

}"

When I'm using a gnome-terminal with UTF-8 encoding, if I write in this program a string "Hello!", then delete it all with Backspace, than press Enter, I'll see: program have red an empty string, as expected. Then I write "123", delete it, press Enter and see: program have red an empty string, as expected.

But if I write a string on russian, for example "Привет!", delete it all and press Enter, i'll see that program had red a string "Пр". If after full deletion of "Привет!" I write a string "123", it will show "При123". And this problem is happening with any Unicode russian string, that need to be edited, even if you want to replace only one symbol: say, I'm enter "Привед", delete one character and press "т" to change it to "Привет" - than program says it red a string "Приве�т". There's no such problems if I enter strings which contain only english letters and/or numbers.

There's no such problem if I use any other encoding, which can be selected in gnome-terminal menu: KOI8R, WINDOWS1251, ISO-8859-5. I didn't noticed such problem on both XTerm an UXTerm, probably because they're don't using Unicode; there's also no such problem on LXTerminal, XTerm and UXTerm on my other machine running Lubuntu, probably by the same reason.

Thanks you very much in advance for any efforts to solve this issue.

System info:

> lsb_release -rd
Description: Ubuntu 14.04.1 LTS
Release: 14.04

> apt-cache policy gnome-terminal
gnome-terminal:
  Установлен (Installed): 3.6.2-0ubuntu1
  Кандидат (Candidate): 3.6.2-0ubuntu1
  Таблица версий (Table of versions):
 *** 3.6.2-0ubuntu1 0
        500 http://ru.archive.ubuntu.com/ubuntu/ trusty/main i386 Packages
        100 /var/lib/dpkg/status

ProblemType: Bug
DistroRelease: Ubuntu 14.04
Package: gnome-terminal 3.6.2-0ubuntu1
ProcVersionSignature: Ubuntu 3.13.0-34.60-generic 3.13.11.4
Uname: Linux 3.13.0-34-generic i686
ApportVersion: 2.14.1-0ubuntu3.3
Architecture: i386
CurrentDesktop: Unity
Date: Fri Aug 22 22:37:38 2014
ExecutablePath: /usr/bin/gnome-terminal
InstallationDate: Installed on 2014-04-19 (124 days ago)
InstallationMedia: Ubuntu 14.04 LTS "Trusty Tahr" - Release i386 (20140417)
ProcEnviron:
 LANGUAGE=ru
 PATH=(custom, no user)
 XDG_RUNTIME_DIR=<set>
 LANG=ru_RU.UTF-8
 SHELL=/bin/bash
SourcePackage: gnome-terminal
UpgradeStatus: No upgrade log present (probably fresh install)

Revision history for this message
Ivan Zuboff (anotherdiskmag) wrote :
Revision history for this message
Egmont Koblinger (egmont-gmail) wrote :

If you're using UTF-8 character set, you need to execute "stty iutf8", so that a "stty -a" reports back "iutf8".
For non-UTF-8, the command to be executed is "stty -iutf8" and accordingly "stty -a" should report "-iutf8".

Gnome-terminal sets this according to the initial character set of the terminal, but does not (could not reliably) update this if switching encoding later. I assume your profile is set to KOI8-R by default and switch to UTF-8 during runtime, is that correct?

Please try what happens if your profile is configured to have UTF-8 by default. It should work out of the box then.

Revision history for this message
Ivan Zuboff (anotherdiskmag) wrote :

Egmont, thanks a lot, man, you're made my day!

Yeah, the problem was there: in gnome-terminal "stty -a" reports "-iutf8" by default, while on XTerm, UXTerm and LXTerminal it reports "iutf8" by default. Command "stty iutf8" getting gnome-teminal back on right way!

Sadly, I don't know why this situation happens - didn't found, what encoding was set by default on the system, but on gnome-terminal settings it was set to UTF-8.

Big thank you again!

Changed in gnome-terminal (Ubuntu):
status: New → Fix Committed
Revision history for this message
Ivan Zuboff (anotherdiskmag) wrote :

For those who interested for code solution - here it is:

#include <stdio.h>
#include <stdlib.h> //contains system() function

void main()
{

char buffer[1024];

/*executing of command "stty iutf" in shell before input begins.
this command means "assume input characters are UTF-8 encoded".*/
if (system("stty iutf8") == -1) printf("system() error!\n");

for (;;) {
 fgets(buffer, sizeof buffer-1, stdin);
 printf("%s\n", buffer);
 }

}

Revision history for this message
Ivan Zuboff (anotherdiskmag) wrote :

All works fine because Egmont Koblinger kindly offered a working solution.

Changed in gnome-terminal (Ubuntu):
assignee: nobody → Ivan Zouboff (anotherdiskmag)
Revision history for this message
Egmont Koblinger (egmont-gmail) wrote :

Hi Ivan,

The feature of setting iutf8 was implemented in gnome-terminal 10 years ago and I've been happily using it ever since. There might be a bug of course, it would be nice to investigate further why it's not set for you. (At this moment I have no idea how it could be wrong for you.)

Your app is not the right place for such a workaround. There are thousands of utilities similar to yours (e.g. just type "cat" and you'll see the same problem with backspace), and dozens of ways a terminal can be configured incorrectly, it's not feasible for every utility to try to fix every possible broken setting. Any app running in a terminal should assume that the terminal is set up properly.

If you can't figure out what's wrong with your gnome-terminal, I recommend to place something like
if [ -t 0 -a "$(locale charmap)" = "UTF-8" ]; then stty iutf8; fi
in your .bashrc as a workaround, then you're done with this for all the applications, and don't pollute your utility with something that really doesn't belong there.

Revision history for this message
Ivan Zuboff (anotherdiskmag) wrote :

Thank you a lot, Egmont, of course you're 100% right with that. Massive thanks for helping with fixing in via .bashrc, too.

Changed in gnome-terminal (Ubuntu):
importance: Undecided → Low
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.