ALT Linux Team development discussions
 help / color / mirror / Atom feed
From: Alexey Tourbin <at@altlinux.ru>
To: devel@lists.altlinux.org
Subject: [devel] watch(1)
Date: Thu, 14 Sep 2006 09:23:55 +0400
Message-ID: <20060914052355.GF17693@localhost.localdomain> (raw)
In-Reply-To: <20060914005144.GZ17693@localhost.localdomain>

[-- Attachment #1: Type: text/plain, Size: 1877 bytes --]

On Thu, Sep 14, 2006 at 04:51:44AM +0400, Alexey Tourbin wrote:
>     Implement the following behaviour:
>     
>     1) execute a program also when a key is pressed;
>     2) but if that key is 'q' then exit instead.
>     
>     This makes watch look more like an interactive program.
> 
> diff --git a/procps/watch.c b/procps/watch.c
> index 18ded5e..4abad74 100644
> --- a/procps/watch.c
> +++ b/procps/watch.c
> @@ -20,6 +20,7 @@ #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
>  #include <sys/ioctl.h>
> +#include <sys/select.h>
>  #include <time.h>
>  #include <unistd.h>
>  #include <locale.h>
> @@ -227,6 +228,7 @@ main(int argc, char *argv[])
>  	nonl();
>  	noecho();
>  	cbreak();
> +	nodelay(stdscr, TRUE);
>  
>  	for (;;) {
>  		time_t t = time(NULL);
> @@ -236,6 +238,10 @@ main(int argc, char *argv[])
>  		FILE *p;
>  		int x, y;
>  		int oldeolseen = 1;
> +		int key;
> +
> +		if ((key = getch()) == 'q') 
> +			do_exit(0);
>  
>  		if (screen_size_changed) {
>  			get_terminal_size();
> @@ -314,7 +320,16 @@ main(int argc, char *argv[])
>  
>  		first_screen = 0;
>  		refresh();
> -		usleep(interval * 1000000);
> +
> +		/* wait until tiemout or until a key press */
> +		{
> +			fd_set set;
> +			int fd = fileno(stdin);
> +			struct timeval timeout = { interval, 0 };
> +			FD_ZERO(&set);
> +			FD_SET(fd, &set);
> +			select(fd + 1, &set, NULL, NULL, &timeout);
> +		}
>  	}
>  
>  	endwin();
> $


В общем затея оказалась небеспроблемная -- select может отвалить или
вернуть раньше времени по самым разным причинам, а входить в
безудержный цикл последнее дело.  Можно конечно проверять на isatty(0),
но мне эта идея почему-то не очень нравится.  Короче окончательный
вариант в git.alt:/people/at/packages/procps.git несколько лучше, чем
этот и несколько последующих.

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

  parent reply	other threads:[~2006-09-14  5:23 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-14  0:51 [devel] procps.git Alexey Tourbin
2006-09-14  1:06 ` Dmitry V. Levin
2006-09-14  1:15   ` Alexey Tourbin
2006-09-14  1:26     ` Dmitry V. Levin
2006-09-14  5:51       ` Kirill Maslinsky
2006-09-14 12:23         ` Dmitry V. Levin
2006-09-14 12:56           ` Anton Farygin
2006-09-14 13:08             ` Dmitry V. Levin
2006-09-14 13:10               ` Anton Farygin
2006-09-14 19:34   ` Michael Shigorin
2006-09-14  1:32 ` Alexey Tourbin
2006-09-14  1:32   ` Alexey Tourbin
2006-09-14  5:23 ` Alexey Tourbin [this message]
2006-09-14 13:38   ` [devel] watch(1) Dmitry V. Levin
2006-09-14  5:40 ` [devel] procps.git Alexey Tourbin
2006-09-14 13:43   ` Dmitry V. Levin
2006-09-14 14:05     ` Alexey Tourbin
2006-09-14 14:21       ` [devel] [jt] procps.git Dmitry V. Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060914052355.GF17693@localhost.localdomain \
    --to=at@altlinux.ru \
    --cc=devel@lists.altlinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

ALT Linux Team development discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/devel/0 devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 devel devel/ http://lore.altlinux.org/devel \
		devel@altlinux.org devel@altlinux.ru devel@lists.altlinux.org devel@lists.altlinux.ru devel@linux.iplabs.ru mandrake-russian@linuxteam.iplabs.ru sisyphus@linuxteam.iplabs.ru
	public-inbox-index devel

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git