Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Writing a line based filter that exits when no input

View Parsed - Show only message text

Path: g2news2.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!backlog2.nntp.dca.giganews.com!nntp.posted.internetamerica!news.posted.internetamerica.POSTED!not-for-mail
NNTP-Posting-Date: Sat, 04 Jul 2009 02:32:49 -0500
Newsgroups: comp.unix.programmer
From: gordonb.cf...@burditt.org (Gordon Burditt)
Subject: Re: Writing a line based filter that exits when no input
References: <pan.2009.07.04.06.09.58@tznvy.pbz>
X-Newsreader: trn 4.0-test76 (Apr 2, 2001)
Originator: gor...@hammy.burditt.org (Gordon Burditt)
Message-ID: <yKSdnXzQe8Y8mNLXnZ2dnUVZ_hidnZ2d@posted.internetamerica>
Date: Sat, 04 Jul 2009 02:32:49 -0500
Lines: 35
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 69.149.70.7
X-Trace: sv3-oflb++J0Ec2YNXLPkpcjAKZE2zS/Nrdjqx5uCnSOdVdk1qz3WoTpSfv+9XF0MCbGop9c0YY8V4/1gu+!rNiQxAGt5ZvS5cW8Mh++JrmPCTRJDdQy6gcZM3LT4hljAEU7XZC+28NyWHDoDSFflQEdsaJD5RXj!jRo79OrYMMKIN7x/sjg06+lAOO0byVaFEcC3H3Krqvw=
X-Complaints-To: abuse@airmail.net
X-DMCA-Complaints-To: ab...@airmail.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.39
X-Original-Bytes: 2298

>Am writing a line-based  filter a la sed or tr. What I  would like it to
>do, however, is if there is no input at start-up, it should present some
>usage information and exit.

What is *NO INPUT*?  If stdin is closed (redirection <&- or 0<&-
), you'll get an error when you try to read from stdin.  It is
unlikely that anyone will do this.  Chances are most commands will
not react cleanly to being invoked with stdin closed if they
expect to use it.

Reading from the user's terminal is not "no input".  If you want
to detect if stdin is connected to the user's terminal or has been
redirected to another terminal, isatty() can detect this.

>E.g., 
>
>$ MyLineFilter < TextFile # OK
>
>$ Some Command | MyLineFilter # OK
>
>$ MyLineFilter
>MyLineFilter: Did not find any text to process.  Exiting.
>
>$ echo $?
>1
>
>$ MyLineFilter Command Line Args # Undecided what to do here ...
>
>
>Would appreciate some ideas on how to implement this.
>
>Currently, am writing the usage information to stderr (unconditionally), 
>but the executable waits for input until it finds EOF.



Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google