Plan 9 from Bell Labs’s /usr/web/sources/contrib/akumar/α/irc/irc.c

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


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

enum {
	STACK = 2048,
};

int out;

void
setwintitle(char *t)
{
	int fd;

	if((fd = open("/dev/label", OWRITE)) >= 0){
		fprint(fd, "%s", t);
		close(fd);
	}
	/* check for /dev/acme/ctl here and set inacme */
}

char*
findtarg(char *dir, char *targ)
{
	char *path, *npath;
	char *name;
	Dir *d;
	int dfd, fd;
	int n, i;

	if((dfd = open(dir, OREAD)) < 0)
		sysfatal("find target: %r");

	path = nil;

	n = dirreadall(dfd, &d);
	for(i = 0; i < n; i++){
		if(!(d[i].qid.type&QTDIR))
			continue;
		path = malloc(strlen(dir)+strlen(d[i].name)+2);
		sprint(path, "%s/%s", dir, d[i].name);
		npath = malloc(strlen(path)+2);
		sprint(npath, "%s/name", path);
		if((fd = open(npath, OREAD)) < 0)
			sysfatal("open target name: %r");
		name = malloc(strlen(targ)+1);
		if(read(fd, name, strlen(targ)) < 0)
			sysfatal("read target name: %r");
		name[strlen(targ)] = '\0';
		if(!strcmp(name, targ)){
			path = realloc(path, strlen(path)+strlen("data")+2);
			strcat(path, "/data");
			break;
		}
		free(name);
		free(path);
		path = nil;
		close(fd);
	}
	return path;
}

void
readirc(void*)
{
	char *line;
	Biobuf data;

	threadsetname("read");

	Binit(&data, out, OREAD);
	while((line = Brdstr(&data, '\n', 0)) != nil){
		print("%s", line);
		free(line);
	}
}

void
writeirc(void)
{
	char *line, *p;
	Biobuf kbd;
	int n, i;

	Binit(&kbd, 0, OREAD);
	while((line = Brdstr(&kbd, '\n', 0)) != nil){
		n = utflen(line);
		p = malloc(n);
		for(i = 0; i < n; ++i)
			p[i] = '\b';
		write(1, p, i);
		free(p);

		fprint(out, "%s", line);
		free(line);
	}
}

void
usage(void)
{
	fprint(2, "usage: %s [-m mtpt] server target\n", argv0);
	exits("usage");
}

void
threadmain(int argc, char** argv)
{
	char *mtpt, *srvr, *targ;
	char *dir, *path;

	mtpt = "/n/irc";

	ARGBEGIN{
	case 'm':
		mtpt = EARGF(usage());
		break;
	default:
		usage();
		break;
	}ARGEND

	if(argc < 2)
		usage();

	srvr = argv[0];
	targ = argv[1];

	dir = malloc(strlen(mtpt)+strlen(srvr)+2);
	sprint(dir, "%s/%s", mtpt, srvr);
	if(!(path = findtarg(dir, targ))){
		fprint(2, "%s not found\n", targ);
		exits("target not found");
	}

	if((out = open(path, ORDWR)) < 0)
		sysfatal("open read: %r");

	setwintitle(targ);

	procrfork(readirc, nil, STACK, RFNAMEG);
	writeirc();
	threadexitsall(nil);
}

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