9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Help with interrupting fgetc()
@ 2015-06-27 20:30 Nils M Holm
  2015-06-27 20:58 ` Charles Forsyth
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Nils M Holm @ 2015-06-27 20:30 UTC (permalink / raw)
  To: Fans of the Plan 9 OS


I'm trying to get the DEL key to cause an error in an interpreter I'm
writing. On Unix, I just catch SIGINT and set an error flag that causes
the interpreter to return to the REPL. On Plan9, fgetc() seems to return
EOF after catching an "interrupted" note.

To make a long story short, I expected the following code to echo
characters typed and print "oopsie" when DEL is pressed. It should
keep echoing after printing "oopsie", but it just exits instead.
What am I missing?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#include <u.h>
#include <libc.h>
#include <stdio.h>

void n(void *x, char *s) {
	if (!strcmp(s, "interrupt")) {
		print("oopsie!\n");
		noted(NCONT);
	}
	else {
		noted(NDFLT);
	}
}

main() {
	int	c;

	notify(n);
	c = fgetc(stdin);
	while (c != EOF) {
		fputc(c, stdout);
		c = fgetc(stdin);
	}
	return 0;
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

--
Nils M Holm  < n m h @ t 3 x . o r g >  www.t3x.org



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Help with interrupting fgetc()
  2015-06-27 20:30 [9fans] Help with interrupting fgetc() Nils M Holm
@ 2015-06-27 20:58 ` Charles Forsyth
  2015-06-27 21:19   ` Nils M Holm
  2015-06-27 21:12 ` Charles Forsyth
  2015-06-28  1:29 ` erik quanstrom
  2 siblings, 1 reply; 7+ messages in thread
From: Charles Forsyth @ 2015-06-27 20:58 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

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

On 27 June 2015 at 21:30, Nils M Holm <nmh@t3x.org> wrote:

> It should
> keep echoing after printing "oopsie", but it just exits instead.
> What am I missing?
>

if interrupted, fgetc returns EOF because the underlying read system call
returns -1 (with error string "interrupted").
System calls are interrupted by a note (see notify(2)). Your loop will
therefore stop and exit.

Also, see /sys/doc/comp.ms for some other details of the Plan 9 C
environment. For instance, main is
      void main(int, char**);
and you need an explicit call to exits [sic] at the end of main, not a
return 0 (or you'll get an error status "main"
returned to the shell).

[-- Attachment #2: Type: text/html, Size: 1251 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Help with interrupting fgetc()
  2015-06-27 20:30 [9fans] Help with interrupting fgetc() Nils M Holm
  2015-06-27 20:58 ` Charles Forsyth
@ 2015-06-27 21:12 ` Charles Forsyth
  2015-06-27 21:21   ` Nils M Holm
  2015-06-28  1:29 ` erik quanstrom
  2 siblings, 1 reply; 7+ messages in thread
From: Charles Forsyth @ 2015-06-27 21:12 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

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

On 27 June 2015 at 21:30, Nils M Holm <nmh@t3x.org> wrote:

>         if (!strcmp(s, "interrupt")) {
>

A minor stylistic point: Plan 9 code almost invariably uses if(strcmp(...)
== 0), and for pointers if(p == nil) instead of if(!p).
if(!!...) is never used.

[-- Attachment #2: Type: text/html, Size: 648 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Help with interrupting fgetc()
  2015-06-27 20:58 ` Charles Forsyth
@ 2015-06-27 21:19   ` Nils M Holm
  0 siblings, 0 replies; 7+ messages in thread
From: Nils M Holm @ 2015-06-27 21:19 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 2015-06-27T21:58:47+0100, Charles Forsyth wrote:
> if interrupted, fgetc returns EOF because the underlying read system call
> returns -1 (with error string "interrupted").
> System calls are interrupted by a note (see notify(2)). Your loop will
> therefore stop and exit.
>
> Also, see /sys/doc/comp.ms for some other details of the Plan 9 C
> environment. For instance, main is
>       void main(int, char**);
> and you need an explicit call to exits [sic] at the end of main, not a
> return 0 (or you'll get an error status "main"
> returned to the shell).

Thanks! I updated my code accordingly, and it works fine now. It seems
like a clearerr(stdin) is necessary to restore the original behavior of
fgetc() after an interrupted system call. Would the below code be
acceptable?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#include <u.h>
#include <libc.h>
#include <stdio.h>

volatile int intr = 0;

void n(void *x, char *s) {
	if (!strcmp(s, "interrupt")) {
		print("oopsie!\n");
		intr = 1;
		noted(NCONT);
	}
	else {
		noted(NDFLT);
	}
}

void main(int argc, char **argv) {
	int	c;

	notify(n);
	c = fgetc(stdin);
	while (c != EOF || intr) {
		if (intr)
			clearerr(stdin);
		else
			fputc(c, stdout);
		intr = 0;
		c = fgetc(stdin);
	}
	exits(NULL);
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

--
Nils M Holm  < n m h @ t 3 x . o r g >  www.t3x.org



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Help with interrupting fgetc()
  2015-06-27 21:12 ` Charles Forsyth
@ 2015-06-27 21:21   ` Nils M Holm
  0 siblings, 0 replies; 7+ messages in thread
From: Nils M Holm @ 2015-06-27 21:21 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 2015-06-27T22:12:09+0100, Charles Forsyth wrote:
> On 27 June 2015 at 21:30, Nils M Holm <nmh@t3x.org> wrote:
>
> >         if (!strcmp(s, "interrupt")) {
> >
>
> A minor stylistic point: Plan 9 code almost invariably uses if(strcmp(...)
> == 0), and for pointers if(p == nil) instead of if(!p).
> if(!!...) is never used.

Thanks, appreciated!

--
Nils M Holm  < n m h @ t 3 x . o r g >  www.t3x.org



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Help with interrupting fgetc()
  2015-06-27 20:30 [9fans] Help with interrupting fgetc() Nils M Holm
  2015-06-27 20:58 ` Charles Forsyth
  2015-06-27 21:12 ` Charles Forsyth
@ 2015-06-28  1:29 ` erik quanstrom
  2015-06-28  7:29   ` Nils M Holm
  2 siblings, 1 reply; 7+ messages in thread
From: erik quanstrom @ 2015-06-28  1:29 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

two additional points.  I the style for note matching is strstr matching because the exact string can't be counted on.  for example details may be added.  also the bio(2) library and peint(2) areusually used instead of stdio.

- erik


On Jun 27, 2015 1:30 PM, Nils M Holm <nmh@t3x.org> wrote:
>
>
> I'm trying to get the DEL key to cause an error in an interpreter I'm 
> writing. On Unix, I just catch SIGINT and set an error flag that causes 
> the interpreter to return to the REPL. On Plan9, fgetc() seems to return 
> EOF after catching an "interrupted" note. 
>
> To make a long story short, I expected the following code to echo 
> characters typed and print "oopsie" when DEL is pressed. It should 
> keep echoing after printing "oopsie", but it just exits instead. 
> What am I missing? 
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> #include <u.h> 
> #include <libc.h> 
> #include <stdio.h> 
>
> void n(void *x, char *s) { 
> if (!strcmp(s, "interrupt")) { 
> print("oopsie!\n"); 
> noted(NCONT); 
> } 
> else { 
> noted(NDFLT); 
> } 
> } 
>
> main() { 
> int c; 
>
> notify(n); 
> c = fgetc(stdin); 
> while (c != EOF) { 
> fputc(c, stdout); 
> c = fgetc(stdin); 
> } 
> return 0; 
> } 
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
>
> -- 
> Nils M Holm  < n m h @ t 3 x . o r g >  www.t3x.org 
>
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [9fans] Help with interrupting fgetc()
  2015-06-28  1:29 ` erik quanstrom
@ 2015-06-28  7:29   ` Nils M Holm
  0 siblings, 0 replies; 7+ messages in thread
From: Nils M Holm @ 2015-06-28  7:29 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 2015-06-27T18:29:12-0700, erik quanstrom wrote:
> two additional points.  I the style for note matching is strstr
> matching because the exact string can't be counted on. [...]

Good to know!

> also the bio(2) library and peint(2) areusually
> used instead of stdio.

Yes, I know about bio(2). I'm currently working on a program that
is supposed to run on both Unix and Plan 9, this is why I'm using
stdio.

--
Nils M Holm  < n m h @ t 3 x . o r g >  www.t3x.org



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-06-28  7:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-27 20:30 [9fans] Help with interrupting fgetc() Nils M Holm
2015-06-27 20:58 ` Charles Forsyth
2015-06-27 21:19   ` Nils M Holm
2015-06-27 21:12 ` Charles Forsyth
2015-06-27 21:21   ` Nils M Holm
2015-06-28  1:29 ` erik quanstrom
2015-06-28  7:29   ` Nils M Holm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).