9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] thread, utf
@ 2005-05-17 12:53 Sergey Reva
  2005-05-17 13:28 ` [9fans] Question on dial() I RATTAN
  2005-05-17 14:04 ` [9fans] thread, utf Russ Cox
  0 siblings, 2 replies; 8+ messages in thread
From: Sergey Reva @ 2005-05-17 12:53 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

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

Hello Fans,

Does I am on a right way of using thread library?

How can I convert Rune string to Utf-8 sequence? I can't find standard
function...

Sergey
-- 
http://rs-rlab.narod.ru                          mailto:rs_rlab@mail.ru

[-- Attachment #2: thrd.c --]
[-- Type: APPLICATION/OCTET-STREAM, Size: 6464 bytes --]

#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <mouse.h>
#include <keyboard.h>

enum {
	Entry=0,
	Button,
	Radio,

	MaxEntryText=50,
	MaxButText=50,

	ButtonExit=0,
	EntryBox1,
	RadioPlus,
	RadioMinus,
	RadioMul,
	RadioDiv,
	EntryBox2,
	ButtonEval,
	EntryResult,
	WidgEnd,

	OpAdd=0,
	OpSub,
	OpMul,
	OpDiv,

	AButtons=0,
	AResize,
	AEnd
};

typedef struct BUT BUT;
struct BUT {
	int id;
	int grp;
	int type;
	Rune* txt;
	int aux;
	int x;
	int y;
	int cx;
	int cy;
	void (*pclick)(BUT *b, int but);
	void (*pdraw)(BUT *b);
	void (*pkbd)(BUT *b, Rune r);
};

BUT widg[];
BUT *focus=nil;

Channel *bc, *redraw;

Rune Entry1[MaxEntryText]={L"1"};
Rune Entry2[MaxEntryText]={L"2"};
Rune Entry3[MaxEntryText]={L""};

/////////////////////////Controls//////////////////////////
void kbddup(BUT *b, Rune r)
{
}

void clickdup(BUT *b, int but)
{
}

void drawradio(BUT *b)
{
	Rectangle r;

	r.min.x=b->x+screen->r.min.x;
	r.min.y=b->y+screen->r.min.y;
	r.max.x=r.min.x+b->cy;
	r.max.y=r.min.y+b->cy;
	draw(screen,r,display->white,display->opaque,ZP);

	if (b->aux)
		draw(screen,r,display->black,display->opaque,ZP);
	else
		border(screen,r,1,display->black,ZP);

	r.max.y=r.min.y;
	r.max.x+=2;
	runestringn(screen, r.max, display->black, ZP, font, b->txt, MaxEntryText);
}

void clickradio(BUT *b, int but)
{
	int i;

	if (!(but & 1))
		return;

	if (b->aux)
		return;

	for (i=0;i<WidgEnd;i++)
		if ((widg[i].grp==b->grp) && (widg[i].type=Radio))
			widg[i].aux=0;
	b->aux=1;
	sendul(redraw,WidgEnd);
}

void drawentry(BUT *b)
{
	Rectangle r;

	r.min.x=b->x+screen->r.min.x;
	r.min.y=b->y+screen->r.min.y;
	r.max.x=r.min.x+b->cx;
	r.max.y=r.min.y+b->cy;
	draw(screen,r,display->white,display->opaque,ZP);
	border(screen,r,1,display->black,ZP);

	r.min.x+=2;
	runestringn(screen, r.min, display->black, ZP, font, b->txt, MaxEntryText);
}

void kbdentry(BUT *b, Rune r)
{
	switch(r)
	{
	case 8:
		b->aux--;
		if (b->aux<0)
			b->aux=0;
		b->txt[b->aux]=0;
		break;
	default:
		if (r>31)
		{
			b->txt[b->aux]=r;
			b->aux++;
			b->txt[b->aux]=0;
		}
		break;
	}

	sendul(redraw,b->id);
}

void drawbutton(BUT *b)
{
	Rectangle r;

	r.min.x=b->x+screen->r.min.x;
	r.min.y=b->y+screen->r.min.y;
	r.max.x=r.min.x+b->cx;
	r.max.y=r.min.y+b->cy;
	draw(screen,r,display->white,display->opaque,ZP);
	border(screen,r,1,display->black,ZP);

	r.min.x+=2;
	runestringn(screen, r.min, display->black, ZP, font, b->txt, MaxButText);
}

void clickbutton(BUT *b, int but)
{
	if (but & 1)
		sendul(bc, b->id);
}

/////////////////Main program//////////////

void graphthread(void*)
{
	ulong id;

	for(;;)
	{
		id=recvul(redraw);
		
		if (id<WidgEnd)
			widg[id].pdraw(&widg[id]);
		else
			for (id=0;id<WidgEnd;id++)
			{
				widg[id].pdraw(&widg[id]);
			}

		flushimage(display,1);
	}
}

void keyboardthread(void* v)
{
	Rune kbdc;
	Channel *c=(Channel*)v;

	for(;;)
	{
		recv(c,&kbdc);

		if ((kbdc!=Runeerror) && (focus!=nil))
			focus->pkbd(focus,kbdc);
	}
}

void mousethread(void *v)
{
	int i;
	Rectangle r;
	Mouse m;
	Channel *c=(Channel*)v;

	for(;;)
	{
		recv(c,&m);

		if (m.buttons)
		for (i=0;i<WidgEnd;i++)
		{
			r.min.x=screen->r.min.x+widg[i].x;
			r.min.y=screen->r.min.y+widg[i].y;
			r.max.x=r.min.x+widg[i].cx;
			r.max.y=r.min.y+widg[i].cy;

			if (ptinrect(m.xy,r))
			{
				focus=&widg[i];
				focus->pclick(focus, m.buttons);
				break;
			}
		}
	}
}

void runetocharcpy(char *c, Rune *r)
{
	int n=0,i;

	for (i=0;r[i]!=0;i++)
		n+=runetochar(&c[n],&r[i]);
	c[n]=0;
}

void Evaluate(Rune* result, Rune* op1, Rune* op2, int op)
{
	char uop1[MaxEntryText*2];
	char uop2[MaxEntryText*2];
	double res;

	runetocharcpy(uop1,op1);
	runetocharcpy(uop2,op2);

	switch(op)
	{
	case OpAdd:
		res=atof(uop1)+atof(uop2);
		break;
	case OpSub:
		res=atof(uop1)-atof(uop2);
		break;
	case OpMul:
		res=atof(uop1)*atof(uop2);
		break;
	case OpDiv:
		res=atof(uop1)/atof(uop2);
		break;
	}

	runesprint(result,"%f",res);
}

void threadmain(int, char**)
{
	Mousectl *mc;
	Keyboardctl *kc;
	ulong but,i;

	Alt alts[AEnd+1]={
		[AButtons] {nil,&but,CHANRCV},
		[AResize] {nil,nil,CHANRCV},
		[AEnd] {nil,nil,CHANEND}
	};

	initdraw(0,0,"Alt example");


	mc=initmouse(nil,screen);
	kc=initkeyboard(nil);

	bc=chancreate(sizeof(ulong),0);
	redraw=chancreate(sizeof(ulong),0);

	alts[AButtons].c=bc;
	alts[AResize].c=mc->resizec;

	threadcreate(graphthread, nil, 8192);
	threadcreate(mousethread, (void*)mc->c, 8192);
	threadcreate(keyboardthread, (void*)kc->c, 8192);

	send(mc->resizec,nil);

	for (;;)
	switch(alt(alts))
	{
	case AButtons:
		switch(but)
		{
		case ButtonEval:
			Evaluate(widg[EntryResult].txt,widg[EntryBox1].txt,widg[EntryBox2].txt,
				((widg[RadioPlus].aux) || (widg[RadioMinus].aux)) ? 
					((widg[RadioPlus].aux) ? OpAdd : OpSub)
				:
					((widg[RadioMul].aux) ? OpMul : OpDiv));

			sendul(redraw,EntryResult);
			break;
		case ButtonExit:
			chanfree(bc);
			chanfree(redraw);
			threadexitsall(0);
			break;
		}
		break;
	case AResize:
		if(getwindow(display, Refnone) < 0)
			sysfatal("resize failed: %r");
		
		for(i=0;i<WidgEnd;i++)
		if (i!=ButtonExit)
		{
			if (widg[i].grp!=1)
				widg[i].cx=screen->r.max.x-screen->r.min.x-4;
		}
		else
			widg[i].x=screen->r.max.x-screen->r.min.x-4-widg[i].cx;

		sendul(redraw,WidgEnd);
		break;
	}
}

BUT widg[]={
[ButtonExit]	{ButtonExit,	0,Button,		L"X",		0,	2,0,16,16,		clickbutton, drawbutton, kbddup},
[EntryBox1]	{EntryBox1,	0,Entry,		Entry1,	1,	2,18,50,16,	clickdup, drawentry, kbdentry},
[RadioPlus]	{RadioPlus,	1,Radio,		L"+",		1,	2,36,40,16,	clickradio, drawradio, kbddup},
[RadioMinus]	{RadioMinus,	1,Radio,		L"-",		0,	2,54,40,16,	clickradio, drawradio, kbddup},
[RadioMul]	{RadioMul,	1,Radio,		L"*",		0,	42,36,40,16,	clickradio, drawradio, kbddup},
[RadioDiv]	{RadioDiv,		1,Radio,		L"/",		0,	42,54,40,16,	clickradio, drawradio, kbddup},
[EntryBox2]	{EntryBox2,	0,Entry,		Entry2,	1,	2,72,50,16,	clickdup, drawentry, kbdentry},
[ButtonEval]	{ButtonEval,	0,Button,		L"=",		0,	2,90,50,16,	clickbutton, drawbutton, kbddup},
[EntryResult]	{EntryResult,	0,Entry,		Entry3,	0,	2,108,50,16,	clickdup, drawentry, kbdentry},
};

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

* [9fans] Question on dial()
  2005-05-17 12:53 [9fans] thread, utf Sergey Reva
@ 2005-05-17 13:28 ` I RATTAN
  2005-05-17 14:04   ` Russ Cox
  2005-05-17 14:04 ` [9fans] thread, utf Russ Cox
  1 sibling, 1 reply; 8+ messages in thread
From: I RATTAN @ 2005-05-17 13:28 UTC (permalink / raw)
  To: 9fans

An echo client written using dial() does not connect to
a UNIX echo server (tcp/7). The call always fails. I am
totally confused here. What is the scenario under which
announce()/dial() combo will work when server and client are
on separate machines?

-ishwar




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

* Re: [9fans] thread, utf
  2005-05-17 12:53 [9fans] thread, utf Sergey Reva
  2005-05-17 13:28 ` [9fans] Question on dial() I RATTAN
@ 2005-05-17 14:04 ` Russ Cox
  1 sibling, 0 replies; 8+ messages in thread
From: Russ Cox @ 2005-05-17 14:04 UTC (permalink / raw)
  To: Sergey Reva, Fans of the OS Plan 9 from Bell Labs

> How can I convert Rune string to Utf-8 sequence? I can't find standard
> function...

runetochar does a single character.  
snprint(buf, sizeof buf, "%S") will convert a string.

russ


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

* Re: [9fans] Question on dial()
  2005-05-17 13:28 ` [9fans] Question on dial() I RATTAN
@ 2005-05-17 14:04   ` Russ Cox
  2005-05-17 14:19     ` I RATTAN
  0 siblings, 1 reply; 8+ messages in thread
From: Russ Cox @ 2005-05-17 14:04 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> An echo client written using dial() does not connect to
> a UNIX echo server (tcp/7). The call always fails. I am
> totally confused here. What is the scenario under which
> announce()/dial() combo will work when server and client are
> on separate machines?

What does your program look like?


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

* Re: [9fans] Question on dial()
  2005-05-17 14:04   ` Russ Cox
@ 2005-05-17 14:19     ` I RATTAN
  2005-05-17 15:35       ` Robert Raschke
                         ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: I RATTAN @ 2005-05-17 14:19 UTC (permalink / raw)
  To: Russ Cox, Fans of the OS Plan 9 from Bell Labs



On Tue, 17 May 2005, Russ Cox wrote:

> What does your program look like?

Here is the client code..
-ishwar
-----
#include <u.h>
#include <libc.h>

void
main(void)
{
   char ibuf[128], obuf[33]="98765432109876543210987654321098";

   int fd, i, len;


   if((fd = dial("tcp!tigaon!7", 0, 0, 0)) < 0)
        sysfatal("dial");
   for (i = 0; i < 21; i++) {
        obuf[33-i] = 0;
        len = strlen(obuf) + 1;
        if(write(fd, obuf, len) != len)
                sysfatal("write");
        if(read(fd, ibuf, 128) > 0)
                print("Got from server: %s\n", ibuf);
    }
    close(fd);
    exits(nil);
}
---


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

* Re: [9fans] Question on dial()
  2005-05-17 14:19     ` I RATTAN
@ 2005-05-17 15:35       ` Robert Raschke
  2005-05-17 16:04       ` Christoph Lohmann
  2005-05-17 16:17       ` "Nils O. Selåsdal"
  2 siblings, 0 replies; 8+ messages in thread
From: Robert Raschke @ 2005-05-17 15:35 UTC (permalink / raw)
  To: 9fans, rattan

I don't know anything about dial or your problem, but ...

> #include <u.h>
> #include <libc.h>
> 
> void
> main(void)
> {
>    char ibuf[128], obuf[33]="98765432109876543210987654321098";
> 
>    int fd, i, len;
> 
> 
>    if((fd = dial("tcp!tigaon!7", 0, 0, 0)) < 0)
>         sysfatal("dial");
>    for (i = 0; i < 21; i++) {
>         obuf[33-i] = 0;

... this does not look good when i == 0.

Did you mean "obuf[32-i] = 0"?

Robby



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

* Re: [9fans] Question on dial()
  2005-05-17 14:19     ` I RATTAN
  2005-05-17 15:35       ` Robert Raschke
@ 2005-05-17 16:04       ` Christoph Lohmann
  2005-05-17 16:17       ` "Nils O. Selåsdal"
  2 siblings, 0 replies; 8+ messages in thread
From: Christoph Lohmann @ 2005-05-17 16:04 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Good day.

Can you resolve the hostname "tigaon" on the same machine? For an echo
server; look at dial(2), which shows one in the EXAMPLES section.

Sincerely,

Christoph

On Tue, 17 May 2005 10:19:59 -0400 (EDT)
I RATTAN <rattan@cps.cmich.edu> wrote:

> 
> 
> On Tue, 17 May 2005, Russ Cox wrote:
> 
> > What does your program look like?
> 
> Here is the client code..
> -ishwar
> -----
> #include <u.h>
> #include <libc.h>
> 
> void
> main(void)
> {
>    char ibuf[128], obuf[33]="98765432109876543210987654321098";
> 
>    int fd, i, len;
> 
> 
>    if((fd = dial("tcp!tigaon!7", 0, 0, 0)) < 0)
>         sysfatal("dial");
>    for (i = 0; i < 21; i++) {
>         obuf[33-i] = 0;
>         len = strlen(obuf) + 1;
>         if(write(fd, obuf, len) != len)
>                 sysfatal("write");
>         if(read(fd, ibuf, 128) > 0)
>                 print("Got from server: %s\n", ibuf);
>     }
>     close(fd);
>     exits(nil);
> }
> ---


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

* Re: [9fans] Question on dial()
  2005-05-17 14:19     ` I RATTAN
  2005-05-17 15:35       ` Robert Raschke
  2005-05-17 16:04       ` Christoph Lohmann
@ 2005-05-17 16:17       ` "Nils O. Selåsdal"
  2 siblings, 0 replies; 8+ messages in thread
From: "Nils O. Selåsdal" @ 2005-05-17 16:17 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

I RATTAN wrote:
> 
> On Tue, 17 May 2005, Russ Cox wrote:
> 
> 
>>What does your program look like?
> 
> 
> Here is the client code..
> -ishwar
> -----
> #include <u.h>
> #include <libc.h>
> 
> void
> main(void)
> {
>    char ibuf[128], obuf[33]="98765432109876543210987654321098";
> 
>    int fd, i, len;
> 
> 
>    if((fd = dial("tcp!tigaon!7", 0, 0, 0)) < 0)
>         sysfatal("dial");
If you do sysfatal("dial %r"); it should print
errstr as well, so you'll get some more info when it fails.


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

end of thread, other threads:[~2005-05-17 16:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-17 12:53 [9fans] thread, utf Sergey Reva
2005-05-17 13:28 ` [9fans] Question on dial() I RATTAN
2005-05-17 14:04   ` Russ Cox
2005-05-17 14:19     ` I RATTAN
2005-05-17 15:35       ` Robert Raschke
2005-05-17 16:04       ` Christoph Lohmann
2005-05-17 16:17       ` "Nils O. Selåsdal"
2005-05-17 14:04 ` [9fans] thread, utf Russ Cox

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).