Plan 9 from Bell Labs’s /usr/web/sources/contrib/pdt/sky/cmd/skywatch/ops.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <bio.h>
#include <thread.h>
#include <draw.h>

#include "debug.h"
#include "lex.h"
#include "sky.h"
#include "gfx.h"

#include "dat.h"
#include "fns.h"

Cmdreg* cmdreg;

/* commands */
Tok opquit(Tok *argv, int argc);
Tok opload(Tok *argv, int argc);

/* functions */
Tok opadd(Tok *argv, int argc);
Tok opmult(Tok *argv, int argc);
Tok ophms(Tok *argv, int argc);
Tok opdegms(Tok *argv, int argc);

void
opinit(void)
{
	int i;
	Cmd *c, clist[] = {
		{L"insert", 1, {Tidentifier,}, &cfginsert},
		{L"get", 1, {Tidentifier,}, &cfgget},
		{L"set", 2, {Tidentifier, Tdecimal,}, &cfgset},
		{L"set", 2, {Tidentifier, Tstr,}, &cfgset},
		{L"draw", 0, {0,}, &opdraw},
		{L"resize", 0, {0,}, &opresize},
		{L"bye", 0, {0,}, &opquit},
		{L"load", 0, {0,}, &opload},

		{L"add", 2, {Tdecimal, Tdecimal,}, &opadd},
		{L"mult", 2, {Tdecimal, Tdecimal,}, &opmult},
		{L"hms", 3, {Tdecimal, Tdecimal, Tdecimal}, &ophms},
		{L"degms", 3, {Tdecimal, Tdecimal, Tdecimal}, &opdegms},
	};

	if(cfginit() < 0)
		sysfatal("cfginit");

	if((cmdreg = mkcmdreg(0)) == nil)
		sysfatal("mkcmdreg: out of memory");

	for(i=0, c=clist; i<nelem(clist); i++, c++)
		if(cmdregister(cmdreg, c) < 0)
			sysfatal("cmdregister: can't register %S", c->str);
}

Tok
opquit(Tok *argv, int argc)
{
	Tok t = {Tempty, L"success (hopefully)"};

	fprint(2, "cleaning up...\n");
	cmdterm(cmdreg);
	cfgterm();

	cleanup();

	threadexitsall(nil);

	return t; /* never reached */
}

Tok
opdraw(Tok *argv, int argc)
{
	Tok te[] = {
		{Tempty, L"success"},
		{Terror, L"invalid arguments"},
	};
	struct Skynode *head;
	Hzloc hz;
	Planar p;
	Point pt;
	RGBi c;
	int size;

	if(getwindow(display, Refnone) < 0)
		sysfatal("can't reattach to window");
	draw(screen, screen->r, display->black, nil, ZP);

	head = cfg->head;

	if(argc != 0) return te[1];

	/* the tail node is always pre-allocated but unset */
	while(head->next){
		hz = eq2hz(head->eq, cfg->obs.g, cfg->jd, 0, 0);
		if(cfg->atm) hz.z += saemundsson(&cfg->obs, hz.z);
		p = hz2pxl(&cfg->camera, hz);
		pt.x = (p.x + cfg->camera.wpx/2)/cfg->samplerate;
		pt.y = (p.y + cfg->camera.hpx/2)/cfg->samplerate;
		if(ptinrect(pt, screen->r)){
			bvmag2rgbsz(head->bv, head->vmag, &c.r, &c.g, &c.b, &size);
			DBPRINT(10, "cfg.c:cfgdraw marker %d,%d c(%d,%d,%d) s%d L\"%S\"\n", pt.x, pt.y, c.r, c.g, c.b, size, head->str);
			marker(screen, pt, c, size, Mdisc, head->str);
		}
		head=head->next;
	}

	flushimage(display, 0);

	return te[0];
}

Tok
opresize(Tok *argv, int argc)
{
	Tok t = {Tempty, L"success (hopefully)"};

	if(getwindow(display, Refnone) < 0)
		sysfatal("can't reattach to window");

	resize(cfg->camera.wpx/cfg->samplerate, cfg->camera.hpx/cfg->samplerate);

	return t;
}

Tok
opload(Tok *argv, int argc)
{
	Tok te[] = {
		{Tempty, L"success"},
		{Terror, L"invalid arguments"},
		{Terror, L"i/o error"},
	};
	int fd;
	char b[Ltoksize+1];

	/* XXX */
	fprint(2, "not implemented\n");
	return te[1];

	if(argc != 2) return te[1];
	if(argv->typ != Tidentifier) return te[1];
	if(runestrncmp(argv->str, L"bsc5p", Ltoksize+1)) return te[1];
	argv++;
	if(utf8enc(argv->str, b, Ltoksize) < 0) return te[1];
	b[Ltoksize] = '\0';
	if((fd = open(b, OREAD)) < 0) return te[2];

	/* TODO */

	close(fd);
	return te[0];
}

Tok
opadd(Tok *argv, int argc)
{
	Tok t = {Terror, "invalid arguments"};
	double r;

	if(argc < 2) return t;

	r = 0;
	while(argc--){
		if(argv->typ != Tdecimal) return t;
		r += runeatof(argv++->str);
	}
	t.typ = Tdecimal;
	runesnprint(t.str, Ltoksize+1, "%f", r);
	return t;
}

Tok
opmult(Tok *argv, int argc)
{
	Tok t = {Terror, "invalid arguments"};
	double r;

	if(argc < 2) return t;

	r = 1;
	while(argc--){
		if(argv->typ != Tdecimal) return t;
		r *= runeatof(argv++->str);
	}
	t.typ = Tdecimal;
	runesnprint(t.str, Ltoksize+1, "%f", r);
	return t;
}

Tok
ophms(Tok *argv, int argc)
{
	Tok t = {Terror, "invalid arguments"};
	double a[3], *ap;

	if(argc != 3) return t;

	ap = a;
	while(argc--){
		if(argv->typ != Tdecimal) return t;
		*ap++ = runeatof(argv++->str);
	}
	t.typ = Tdecimal;
	runesnprint(t.str, Ltoksize+1, "%f", hms2rad(a[0], a[1], a[2]));
	return t;
}

Tok
opdegms(Tok *argv, int argc)
{
	Tok t = {Terror, "invalid arguments"};
	double a[3], *ap;

	if(argc != 3) return t;

	ap = a;
	while(argc--){
		if(argv->typ != Tdecimal) return t;
		*ap++ = runeatof(argv++->str);
	}
	t.typ = Tdecimal;
	runesnprint(t.str, Ltoksize+1, "%f", degms2rad(a[0], a[1], a[2]));
	return t;
}


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].