6

I am trying to use ansi-term in emacs (configured to tcsh shell). I see some issues with newline characters being displayed. If I try the following from the terminal (ansi-term), I get the correct output:

myterm > echo "Line1"; echo "Line2"; echo "Line3";    
Line1
Line2
Line3
myterm >

But if I try putting the same lines in a shell script and try to execute the script from ansi-term, I get a wrong output

Script: (test)

#!/usr/bin/env tcsh
echo "Line1"; echo "Line2"; echo "Line3";

Running the script (test):

myterm > ./test
Line1
     Line2
          Line3
               myterm >

Note: /usr/bin/env tcsh does point to the correct shell (its the same shell that I used while invoking ansi-term). Also executing the script from gnome-terminal also displays the correct output. I have also tried setting the following variables but it did not solve my issues:

(set-terminal-coding-system 'utf-8-unix)
(setq default-process-coding-system '((utf-8-unix . utf-8-unix)))
Pulimon
  • 1,776
  • 4
  • 31
  • 45
  • most likely \r & \r\n difference. can't reproduce here, so can't help more. – iced Apr 27 '16 at 12:48
  • the output of `infocmp` might help to generate some answers here – Niall Cosgrove May 02 '16 at 14:33
  • Try use `printf` instead of `echo` and specify \r\n explicitly at the end of each string – sqr163 May 03 '16 at 23:59
  • @Ivan : Using printf with \r\n will give me the required output in this case. But the problem is that I can't control the outputs of the tools I use. The prints from those tools have the same issue. The above code is the simplest example which replicates the issue. – Pulimon May 04 '16 at 06:36
  • @user6170930: I'm not sure how to use infocmp. Could you give me some more details? – Pulimon May 04 '16 at 06:36
  • The [infocmp](http://linuxcommand.org/man_pages/infocmp1.html) utility, executed without any options, will produce the terminfo description of the currently defined terminal in a form suitable for editing and recompilation with the tic utility. I was curious whether a `cr=^J` in your eterm-color terminfo description might be the problem, but when I changed mine in an attempt to replicate your problem ansi-term didn't change its behaviour. so now I'm as confused as yourself. – Niall Cosgrove May 04 '16 at 12:01
  • ok. I think your problem is the `opost` option to `stty`. I can emulate your problem by setting `stty -opost` which tells my terminal not to post-process output data. So `stty +opost` might be what you need. – Niall Cosgrove May 04 '16 at 13:52
  • oops: i should have said `stty opost` not `stty +opost` – Niall Cosgrove May 04 '16 at 14:06
  • @user6170930 I tried that but it still didn't work. my ssty - a output is as follows: speed 38400 baud; rows 50; columns 93; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ; eol2 = ; swtch = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; – Pulimon May 04 '16 at 20:25
  • -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany imaxbel -iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab3 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke – Pulimon May 04 '16 at 20:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111048/discussion-between-user6170930-and-pulimon). – Niall Cosgrove May 04 '16 at 20:29
  • moved this to chat because SO is complaining about too many comments. – Niall Cosgrove May 04 '16 at 20:44

1 Answers1

15

If you set stty onlcr in your script, you will get the behaviour you require.
Translating the command into english one might say:
set the tty to output newline as carriage-return and newline.

This is a workaround of course, because this option should be set by default. I can see from the output of stty -a that you gave in your comments that it is set in the tcsh that runs in your ansi-term. I suspect one possible reason why ansi-term and your shell script behave differently is due to the following lines in term.el

    (apply 'start-process name buffer
       "/bin/sh" "-c"
       (format "stty -nl echo rows %d columns %d sane 2>/dev/null; 
                if [ $1 = .. ]; then shift; fi; exec \"$@\""
               term-height term-width)
       ".."
       command switches)))

The stty command in the above actually sets onlcr twice, since
the compound option -nl translates to icrnl -inlcr -igncr onlcr -ocrnl -onlret
and the sane option translates to
cread -ignbrk brkint -inlcr -igncr icrnl -iutf8 -ixoff -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

Another possible cause: for non-login shells tcsh will only read /etc/csh.cshrc and either ~/.tcshrc or ~/.cshrc when it starts up, but for login shells it reads a number of other files including /etc/csh.login ~/.history or the value of $histfile - You should consult the man page for full details including the exact order in which it reads things.

Niall Cosgrove
  • 1,273
  • 1
  • 15
  • 24
  • 3
    @[pulimon](http://stackoverflow.com/users/990772/pulimon) I did a major re-write of the answer since my first go was a little terse. There is a discussion about it on [meta](http://meta.stackoverflow.com/questions/322563/editing-my-answer-that-has-been-upvoted-accepted-and-has-bounty-paid) – Niall Cosgrove May 07 '16 at 15:13