EOF on stdin

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

EOF on stdin

Mark Morgan Lloyd-5
I'm trying to distinguish between a case where there is no parameter on the
command line and where there is piped input. The code below works under Win-32
but under Linux Eof() waits for input:

   IF ParamCount = 0 THEN BEGIN (* Case (1) or (2) *)
     debug('No parameters (case 1 or 2)');
     TRY
       AssignFile(input, ''); (* Standard input *)
{$IOCHECKS OFF }
       Reset(input); // Returns zero
       TRY
        IF Eof(input) THEN BEGIN (* Case (1) *)
           debug('At EOF (case 1)');
{$IOCHECKS ON }

// Nothing on command line.

        END ELSE BEGIN (* Case (2) *)
           debug('Not at EOF (case 2)');

// Got piped input.

If I start it under linux by piping an eof (i.e. echo -n |) it works.

I'm sure this is something that's been looked at before- how do I get it to work?

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: EOF on stdin

Marco van de Voort
> I'm trying to distinguish between a case where there is no parameter on the
> command line and where there is piped input. The code below works under Win-32
> but under Linux Eof() waits for input:
>
>    IF ParamCount = 0 THEN BEGIN (* Case (1) or (2) *)
>      debug('No parameters (case 1 or 2)');
>      TRY
>        AssignFile(input, ''); (* Standard input *)
> {$IOCHECKS OFF }
>        Reset(input); // Returns zero
>        TRY
> IF Eof(input) THEN BEGIN (* Case (1) *)
>            debug('At EOF (case 1)');
> {$IOCHECKS ON }
>
> // Nothing on command line.
>
> END ELSE BEGIN (* Case (2) *)
>            debug('Not at EOF (case 2)');
>
> // Got piped input.
>
> If I start it under linux by piping an eof (i.e. echo -n |) it works.
>
> I'm sure this is something that's been looked at before- how do I get it to work?

For unix one can simply use termio.isatty(input) I think.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: EOF on stdin

Mark Morgan Lloyd-5
Good grief- that was quick :-)

Marco van de Voort wrote:
>> I'm trying to distinguish between a case where there is no parameter on the
>> command line and where there is piped input. The code below works under Win-32
>> but under Linux Eof() waits for input:

>> If I start it under linux by piping an eof (i.e. echo -n |) it works.
>>
>> I'm sure this is something that's been looked at before- how do I get it to work?
>
> For unix one can simply use termio.isatty(input) I think.

Thanks, I'll investigate. I was going to look at that but you've saved me a
/lot/ of work identifying the correct function name :-)

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: EOF on stdin

Mark Morgan Lloyd-5
> Marco van de Voort wrote:

>> For unix one can simply use termio.isatty(input) I think.

I can confirm that that works.

Another implementation difference that I noticed between Win-32 and Linux was
that FileExists() on Win-32 excludes directories while on Linux it matches
them. Workaround was trivial in that case.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: EOF on stdin

Tomas Hajny
In reply to this post by Mark Morgan Lloyd-5
On 8 Dec 07, at 13:20, Mark Morgan Lloyd wrote:

> Good grief- that was quick :-)
>
> Marco van de Voort wrote:
> >> I'm trying to distinguish between a case where there is no parameter on the
> >> command line and where there is piped input. The code below works under Win-32
> >> but under Linux Eof() waits for input:
>
> >> If I start it under linux by piping an eof (i.e. echo -n |) it works.
> >>
> >> I'm sure this is something that's been looked at before- how do I get it to work?
> >
> > For unix one can simply use termio.isatty(input) I think.
>
> Thanks, I'll investigate. I was going to look at that but you've saved me a
> /lot/ of work identifying the correct function name :-)

Note that you could use function Do_IsDevice
provided in system unit for this (if Input is
redirected/piped, it is not a device, otherwise
it uses the console device). However, I suspect
that this function is only used internally and
there's no way of directly accessing it outside
the System unit. Nevertheless, you should be able
to find proper implementation for all FPC
supported platforms by looking at implementation
of this function.

Tomas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: EOF on stdin

Mark Morgan Lloyd-5
Tomas Hajny wrote:

>>>> I'm trying to distinguish between a case where there is no parameter on the
>>>> command line and where there is piped input.

>>> For unix one can simply use termio.isatty(input) I think.

> Note that you could use function Do_IsDevice
> provided in system unit for this (if Input is
> redirected/piped, it is not a device, otherwise
> it uses the console device).

Thanks Tomas, noted in case I revisit this.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal