Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-12

EOF on stdin

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Morgan Lloyd

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]
Marco van de Voort
to Mark Morgan Lloyd
Show quoted text 25 lines
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.

Mark Morgan Lloyd
to Marco van de Voort

Good grief- that was quick :-)

Marco van de Voort wrote 9 lines
> 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]
Mark Morgan Lloyd
to Mark Morgan Lloyd
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]
Tomas Hajny
to Mark Morgan Lloyd: Good grief- that was quick :-) Thanks, I'll investigate. I was going to look at that but…
Mark Morgan Lloyd wrote 15 lines
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

Mark Morgan Lloyd
to Tomas Hajny
Tomas Hajny wrote 9 lines
>>> 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]