How to use control-D character?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use control-D character?

ali hagigat
My question is about as, GNU assembler, Version 2.14 and I have copied
the related extract from the manual:
-----------------------------------------
1.5 Input Files
If you give 'as' no file names it attempts to read one input file from
the as standard input,
which is normally your terminal. You may have to type ctl-D to tell as
there is no more
program to assemble.
----------------------------------------

How can i use <ctl-D> key combination? Will it be typed at the end of
'as' command line?
When i type:
as <ctl-D>
'as' will wait for user to enter some lines of instruction!!

If I use 'as' alone and then enter some assembly lines , how can i
terminate the input terminal text and make 'as' assemble the lines I
have written?

Thank you to read my message.
Reply | Threaded
Open this post in threaded view
|

Re: How to use control-D character?

Ian Lance Taylor
ali hagigat <[hidden email]> writes:

> My question is about as, GNU assembler, Version 2.14 and I have copied
> the related extract from the manual:
> -----------------------------------------
> 1.5 Input Files
> If you give 'as' no file names it attempts to read one input file from
> the as standard input,
> which is normally your terminal. You may have to type ctl-D to tell as
> there is no more
> program to assemble.
> ----------------------------------------
>
> How can i use <ctl-D> key combination? Will it be typed at the end of
> 'as' command line?
> When i type:
> as <ctl-D>
> 'as' will wait for user to enter some lines of instruction!!
>
> If I use 'as' alone and then enter some assembly lines , how can i
> terminate the input terminal text and make 'as' assemble the lines I
> have written?

http://en.wikipedia.org/wiki/End-of-file

In other words, type

as
your input file
^D

This assumes that you are using Unix or GNU/Linux; you didn't say.

Ian
Reply | Threaded
Open this post in threaded view
|

Re: How to use control-D character?

ali hagigat
Thank you to reply to my message. Ian, I am using Linux, Fedora 12.
I typed 'as' as you wrote:
root> as
Then I wrote the name of my input file:
root> asm1.s
Then i pressed <ENTER> and then <ctl-D> and nothing happned!
For the second time I pressed  <ctl-D> and now I have the following
error message:
{standard input}: Assembler messages:
{standard input}:1: Error: no such instruction: `asm1.s'

I think you meant writing some assembly instructions before <CTL-D>.
But when I type:
root> as  <ENTER>
root> mov  $0, %eax  <ENTER>
root> <CAPS LOCK> <CTL-D>
nothing happens!! but if i press <CTL-D> for two more times, it will
exit from as and returns back to my shell prompt!
So what happened? Did it assemble the line? Why this <CTL-D> is
useful, how it is used?

Thank you to read my message.

On Tue, Oct 12, 2010 at 4:47 PM, Ian Lance Taylor <[hidden email]> wrote:

> http://en.wikipedia.org/wiki/End-of-file
>
> In other words, type
>
> as
> your input file
> ^D
>
> This assumes that you are using Unix or GNU/Linux; you didn't say.
>
> Ian
>
Reply | Threaded
Open this post in threaded view
|

Re: How to use control-D character?

Ian Lance Taylor
ali hagigat <[hidden email]> writes:

> Thank you to reply to my message. Ian, I am using Linux, Fedora 12.
> I typed 'as' as you wrote:
> root> as
> Then I wrote the name of my input file:
> root> asm1.s
> Then i pressed <ENTER> and then <ctl-D> and nothing happned!
> For the second time I pressed  <ctl-D> and now I have the following
> error message:
> {standard input}: Assembler messages:
> {standard input}:1: Error: no such instruction: `asm1.s'
>
> I think you meant writing some assembly instructions before <CTL-D>.
> But when I type:
> root> as  <ENTER>
> root> mov  $0, %eax  <ENTER>
> root> <CAPS LOCK> <CTL-D>
> nothing happens!! but if i press <CTL-D> for two more times, it will
> exit from as and returns back to my shell prompt!
> So what happened? Did it assemble the line? Why this <CTL-D> is
> useful, how it is used?

As noted in http://en.wikipedia.org/wiki/End-of-file , ^D is the
standard Unix end-of-file indicator from the terminal.  You don't need
to use the caps lock or the shift key; pressing the control key and the
D key simultaneously is sufficient.

The fact that you have to type ^D more than once seems like a bug in the
GNU assembler.  Please consider reporting it at
http://sourceware.org/bugzilla/ .

And, yes, when you do type it more than once, and the assembler exits,
then it has assembled the line you typed.  You will find the results in
the file "a.out".

Ian
Reply | Threaded
Open this post in threaded view
|

Re: How to use control-D character?

Maciej W. Rozycki
On Wed, 13 Oct 2010, Ian Lance Taylor wrote:

> The fact that you have to type ^D more than once seems like a bug in the
> GNU assembler.  Please consider reporting it at
> http://sourceware.org/bugzilla/ .

 We have two problems on Linux apparently:

1. input_file_open() uses ungetc(3) without checking for EOF.

2. glibc does not handle the EOF condition in the context of ungetc(3)
   correctly when input is connected to the terminal.

 The issue #1 above is obvious and only applies to EOF seen right at the
beginning.  I'll elaborate on #2.  We make this sequence of calls on
input:

getc()
ungetc()
fread()
fread()
fread()
...

The call to getc(3) collects a line of input with read(2) (that is
retained) and returns the first character.  The call to ungetc(3) pushes
the character back to the head of the line.  The call to fread(3) collects
another line of input (that is retained) with read(2) and returns the line
previously collected by getc(3).  The next call to fread(3) collects
another line of input (that is retained) with read(2) and returns the line
collected by the previous call to fread(3), etc., etc...

 Now if at any point read(2) returns EOF, then glibc records the fact (see
libio/fileops.c and look for "fp->_flags |= _IO_EOF_SEEN" assignments,
such as in _IO_file_xsgetn() that is used by fread(3)), but obviously it
still has to return the line buffered by the previous call to fread(3).  
The EOF condition is expected to be triggered by the next call to fread(3)
(or, to be exact, once all the buffered characters have been consumed, up
to which point fread(3) should refrain from making further calls to
read(2)), but that doesn't happen unless the associated read(2) call
returns another EOF, because glibc actually never checks whether the
_IO_EOF_SEEN flag is set.

 The ISO C99 standard seems a bit imprecise about the interaction between
ungetc(3) and fread(3), which matters especially in the context of
line-mode devices such as terminals.  The above is my interpretation of
how these calls should behave only, that is guaranteed the glibc
maintainers will disagree with, as usually.

 Fixing input_file_open() looks moderately easy, although will only
address a corner case.  Frankly I find the way #NO_APP is handled in
input_file_open() rather hackish.

 Fixing glibc is the actual problem, but strictly speaking outside the
scope of binutils, although we may recognise some interest.  Perhaps the
best choice would simply be avoiding the grey area and rewriting the
handling of #NO_APP in a more civilised way.

  Maciej
Reply | Threaded
Open this post in threaded view
|

Re: How to use control-D character?

Nick Clifton
Hi Maciej,

> 1. input_file_open() uses ungetc(3) without checking for EOF.
>
> 2. glibc does not handle the EOF condition in the context of ungetc(3)
>     correctly when input is connected to the terminal.
>
>   The issue #1 above is obvious and only applies to EOF seen right at the
> beginning.  I'll elaborate on #2.  We make this sequence of calls on
> input:
>
> getc()
> ungetc()
> fread()
> fread()
> fread()
> ...
>
> The call to getc(3) collects a line of input with read(2) (that is
> retained) and returns the first character.  The call to ungetc(3) pushes
> the character back to the head of the line.  The call to fread(3) collects
> another line of input (that is retained) with read(2) and returns the line
> previously collected by getc(3).  The next call to fread(3) collects
> another line of input (that is retained) with read(2) and returns the line
> collected by the previous call to fread(3), etc., etc...
>
>   Now if at any point read(2) returns EOF, then glibc records the fact (see
> libio/fileops.c and look for "fp->_flags |= _IO_EOF_SEEN" assignments,
> such as in _IO_file_xsgetn() that is used by fread(3)), but obviously it
> still has to return the line buffered by the previous call to fread(3).
> The EOF condition is expected to be triggered by the next call to fread(3)
> (or, to be exact, once all the buffered characters have been consumed, up
> to which point fread(3) should refrain from making further calls to
> read(2)), but that doesn't happen unless the associated read(2) call
> returns another EOF, because glibc actually never checks whether the
> _IO_EOF_SEEN flag is set.
>
>   The ISO C99 standard seems a bit imprecise about the interaction between
> ungetc(3) and fread(3), which matters especially in the context of
> line-mode devices such as terminals.  The above is my interpretation of
> how these calls should behave only, that is guaranteed the glibc
> maintainers will disagree with, as usual.
Imagining that I am Uli for a second, it occurs to me that he will
defend glibc by saying that we ought to be calling feof() to check for
the end of file condition before calling fread().

So what do you think of this patch, which appears to take care of both
points (1) and (2), although it does not attempt to tidy up the #NO_APP
processing ?

Cheers
   Nick

input-file.c.patch (1K) Download Attachment