예제 #1
0
void
link(char *e)
{
	char *s, *o;

	s = o = pos;
	while(s < epos){
		if(e != nil && s >= e)
			break;
		if(*s == 0 || strchr("<>[] \t\r\n", *s) != nil)
			break;
		s++;
	}
	if(s-4 >= o)
	if(cistrncmp(s-4, ".png", 4)
	&& cistrncmp(s-4, ".jpg", 4)
	&& cistrncmp(s-4, ".gif", 4)){
		string("<a href=\"");
		escape(s);
		string("\">");
		eatspace();
		if(e != nil && pos < e)
			ebody(e);
		else {
			pos = o;
			escape(s);
		}
		string("</a>");
	} else {
		string("<img src=\"");
		escape(s);
		string("\">");
	}
}
예제 #2
0
char*
fixdate(char *s)
{
	char *f[10], *m, *t, *wd, tmp[40];
	int d, i, j, nf, hh, mm;

	nf = tokenize(s, f, nelem(f));

	wd = nil;
	d = 0;
	m = nil;
	t = nil;
	for(i=0; i<nf; i++){
		for(j=0; j<7; j++)
			if(cistrncmp(day[j], f[i], 3)==0)
				wd = day[j];
		for(j=0; j<12; j++)
			if(cistrncmp(mon[j], f[i], 3)==0)
				m = mon[j];
		j = atoi(f[i]);
		if(1 <= j && j <= 31 && d != 0)
			d = j;
		if(strchr(f[i], ':'))
			t = f[i];
	}

	if(d==0 || wd==nil || m==nil || t==nil)
		return nil;

	hh = strtol(t, 0, 10);
	mm = strtol(strchr(t, ':')+1, 0, 10);
	sprint(tmp, "%s %d %s %d:%.2d", wd, d, m, hh, mm);
	return estrdup(tmp);
}
예제 #3
0
파일: http.c 프로젝트: bhanug/harvey
void
wwwauthenticate(HttpState *hs, char *line)
{
	char cred[64], *user, *pass, *realm, *s, *spec, *name;
	Fmt fmt;
	UserPasswd *up;

	spec = nil;
	up = nil;
	cred[0] = 0;
	hs->autherror[0] = 0;
	if(cistrncmp(line, "basic ", 6) != 0){
		werrstr("unknown auth: %s", line);
		goto error;
	}
	line += 6;
	if(cistrncmp(line, "realm=", 6) != 0){
		werrstr("missing realm: %s", line);
		goto error;
	}
	line += 6;
	user = hs->c->url->user;
	pass = hs->c->url->passwd;
	if(user==nil || pass==nil){
		realm = unquote(line, &line);
		fmtstrinit(&fmt);
		name = servername(hs->netaddr);
		fmtprint(&fmt, "proto=pass service=http server=%q realm=%q", name, realm);
		free(name);
		if(hs->c->url->user)
			fmtprint(&fmt, " user=%q", hs->c->url->user);
		spec = fmtstrflush(&fmt);
		if(spec == nil)
			goto error;
		if((up = auth_getuserpasswd(nil, "%s", spec)) == nil)
			goto error;
		user = up->user;
		pass = up->passwd;
	}
	if((s = smprint("%s:%s", user, pass)) == nil)
		goto error;
	free(up);
	enc64(cred, sizeof(cred), (uint8_t*)s, strlen(s));
	memset(s, 0, strlen(s));
	free(s);
	hs->credentials = smprint("Basic %s", cred);
	if(hs->credentials == nil)
		goto error;
	return;

error:
	free(up);
	free(spec);
	snprint(hs->autherror, sizeof hs->autherror, "%r");
	fprint(2, "%s: Authentication failed: %r\n", argv0);
}
예제 #4
0
void
replywindow(Article *m)
{
	Biobuf *b;
	char *p, *ep, *q, tmp[40];
	int fd, copy;
	Article *reply;

	sprint(tmp, "%d/article", m->n);
	p = estrstrdup(dir, tmp);
	if((fd = open(p, OREAD)) < 0){
		free(p);	
		return;
	}
	free(p);

	reply = newpost();
	winopenbody(reply->w, OWRITE);
	b = emalloc(sizeof(*b));
	Binit(b, fd, OREAD);
	copy = 0;
	while(p = Brdline(b, '\n')){
		if(Blinelen(b)==1)
			break;
		ep = p+Blinelen(b);
		if(!isspace(*p)){
			copy = 0;
			if(cistrncmp(p, "newsgroups:", 11)==0){
				for(q=p+11; *q!='\n'; q++)
					if(*q==',')
						*q = ' ';
				copy = 1;
			}else if(cistrncmp(p, "subject:", 8)==0){
				if(!strstr(p, " Re:") && !strstr(p, " RE:") && !strstr(p, " re:")){
					p = skip(p, "subject:");
					ep[-1] = '\0';
					Bprint(reply->w->body, "Subject: Re: %s\n", p);
				}else
					copy = 1;
			}else if(cistrncmp(p, "message-id:", 11)==0){
				Bprint(reply->w->body, "References: ");
				p += 11;
				copy = 1;
			}
		}
		if(copy)
			Bwrite(reply->w->body, p, ep-p);
	}
	Bterm(b);
	close(fd);
	free(b);
	Bprint(reply->w->body, "\n");
	winclean(reply->w);
	winselect(reply->w, "$", 0);
}
예제 #5
0
/*
 *  Check for a matching MIME type/subtype. If either type or subtype is
 *  NULL, then it won't be checked. If both are NULL, then TRUE is always
 *  returned.
 *
 *  Args:
 *   mimePtr   [IN] Pointer to the emsMIMEtype structure
 *   mime_type [IN] The major MIME type to check
 *   sub_type  [IN] The MIME subtype to check
 *  
 *  Returns: Boolean (TRUE if the MIME type matches, FALSE if not)
 */
int match_mime_type(emsMIMEtypeP mimePtr, const char *mime_type, const char *sub_type)
{
	if (!mimePtr)
		return (FALSE);

	return (
			((mime_type == NULL) || (cistrncmp(mimePtr->type, mime_type, strlen(mime_type)) == 0))
			&&
			((sub_type == NULL) || (cistrncmp(mimePtr->subType, sub_type, strlen(sub_type)) == 0))
		   );
}
예제 #6
0
파일: http.c 프로젝트: bhanug/harvey
static int
httpheaders(HttpState *hs)
{
	char buf[2048];
	char *p;
	int i, n;

	for(;;){
		n = getheader(hs, buf, sizeof(buf));
		if(n < 0)
			return -1;
		if(n == 0)
			return 0;
		//	print("http header: '%.*s'\n", n, buf);
		for(i = 0; i < nelem(hdrtab); i++){
			n = strlen(hdrtab[i].name);
			if(cistrncmp(buf, hdrtab[i].name, n) == 0){
				/* skip field name and leading white */
				p = buf + n;
				while(*p == ' ' || *p == '\t')
					p++;
				(*hdrtab[i].fn)(hs, p);
				break;
			}
		}
	}
}
예제 #7
0
int
match(char *s)
{
	int n;

	n = strlen(s);
	if(pos+n > epos)
		return 0;
	return cistrncmp(pos, s, n) == 0;
}
예제 #8
0
// Finds header and REMOVES that header line from the full header
char *rfc822_remove_header(char *pFullHeader, const char *pLinePrefix)
{
	const char *kNewline = "\r\n";
	const unsigned int kNewlineLen = (2);
	const unsigned int nPreLen = strlen(pLinePrefix);

	char *pStart, *pEnd, *pBuf, *pPos, *pRetBuf;

	if (nPreLen < 1)
		return (NULL);

	pStart = (char *) pFullHeader;

	/* Find first 'line' which matches prefix */
	while ((pStart) && (cistrncmp(pStart, pLinePrefix, nPreLen) != 0))
		if ((pStart = strstr(pStart, kNewline)) != 0)
			pStart += kNewlineLen;

	if (!pStart)
		return (NULL); /* Not found */

	/* Find the end of this header line */
	for (pEnd = strstr(pStart, kNewline); (pEnd != NULL); pEnd = strstr(pEnd, kNewline))
	{
		pPos = pEnd + kNewlineLen;

		/* Does header line continue on next line? */
		if ((*pPos == ' ') || (*pPos == '\t'))
			pEnd = pPos;
		else
			break;
	}

	/* If we ran off the end of the string, then the end is the last char in the string */
	if (pEnd == NULL)
		pEnd = strchr(pStart, '\0');
	
	pBuf = pRetBuf = (char *) malloc(pEnd - pStart + 1); /* Max length of output string */

	for (pPos = pStart; pPos < pEnd; pPos++)
	{
		if (strncmp(pPos, kNewline, kNewlineLen) == 0) /* We're at a newline */
			pPos += kNewlineLen; /* Skip over newline */

		*pBuf++ = *pPos;
	}

	*pBuf = '\0';

	// Now squish the header string down by copying over the requested header
	while ((*pStart++ = *pEnd++) != '\0')
		/* copy */ ;

	return (pRetBuf);
}
예제 #9
0
파일: parsereq.c 프로젝트: CoryXie/nix-os
static Strings
parseuri(HConnect *c, char *uri)
{
	Strings ss;
	char *urihost, *p;

	urihost = nil;
	ss.s1 = ss.s2 = nil;
	if(uri[0] != '/')
		if(cistrncmp(uri, "http://", 7) == 0)
			uri += 5;		/* skip http: */
		else if (cistrncmp(uri, "https://", 8) == 0)
			uri += 6;		/* skip https: */
		else
			return ss;

	/*
	 * anything starting with // is a host name or number
	 * hostnames consists of letters, digits, - and .
	 * for now, just ignore any port given
	 */
	if(uri[0] == '/' && uri[1] == '/'){
		urihost = uri + 2;
		p = strchr(urihost, '/');
		if(p == nil)
			uri = hstrdup(c, "/");
		else{
			uri = hstrdup(c, p);
			*p = '\0';
		}
		p = strchr(urihost, ':');
		if(p != nil)
			*p = '\0';
	}

	if(uri[0] != '/' || uri[1] == '/')
		return ss;

	ss.s1 = uri;
	ss.s2 = hlower(urihost);
	return ss;
}
예제 #10
0
파일: html.c 프로젝트: 99years/plan9
/*
 * Somewhat of a hack.  Not a full parse, just looks for strings in the beginning
 * of the document (cistrstr only looks at first somewhat bytes).
 */
int
charset(char *s)
{
	char *meta, *emeta, *charset;

	if(defcharset == 0)
		defcharset = ISO_8859_1;
	meta = cistrstr(s, "<meta");
	if(meta == nil)
		return defcharset;
	for(emeta=meta; *emeta!='>' && *emeta!='\0'; emeta++)
		;
	charset = cistrstr(s, "charset=");
	if(charset == nil)
		return defcharset;
	charset += 8;
	if(*charset == '"')
		charset++;
	if(cistrncmp(charset, "utf-8", 5) || cistrncmp(charset, "utf8", 4))
		return UTF_8;
	return defcharset;
}
예제 #11
0
static void
ne2000pnp(Ether* edev)
{
	int i, id;
	Pcidev *p;
	Ctlr *ctlr;

	/*
	 * Make a list of all ethernet controllers
	 * if not already done.
	 */
	if(ctlrhead == nil){
		p = nil;
		while(p = pcimatch(p, 0, 0)){
			if(p->ccrb != 0x02 || p->ccru != 0)
				continue;
			ctlr = malloc(sizeof(Ctlr));
			if(ctlr == nil)
				error(Enomem);
			ctlr->pcidev = p;

			if(ctlrhead != nil)
				ctlrtail->next = ctlr;
			else
				ctlrhead = ctlr;
			ctlrtail = ctlr;
		}
	}

	/*
	 * Is it a card with an unrecognised vid+did?
	 * Normally a search is made through all the found controllers
	 * for one which matches any of the known vid+did pairs.
	 * If a vid+did pair is specified a search is made for that
	 * specific controller only.
	 */
	id = 0;
	for(i = 0; i < edev->nopt; i++){
		if(cistrncmp(edev->opt[i], "id=", 3) == 0)
			id = strtol(&edev->opt[i][3], nil, 0);
	}

	if(id != 0)
		ne2000match(edev, id);
	else for(i = 0; ne2000pci[i].name; i++){
		if(ne2000match(edev, ne2000pci[i].id) != nil)
			break;
	}
}
예제 #12
0
char*
look(char *s, char *e)
{
	char *p;
	int n;

	if(e == nil)
		e = epos;
	n = strlen(s);
	e -= n;
	for(p = pos; p <= e; p++)
		if(cistrncmp(p, s, n) == 0)
			return p;
	return nil;
}
예제 #13
0
파일: mesg.c 프로젝트: Requaos/harvey
/*
 * Find plumb port, knowing type is text, given file name (by extension)
 */
int
plumbportbysuffix(char *file)
{
	char *suf;
	int i, nsuf, nfile;

	nfile = strlen(file);
	for(i=0; ports[i].type!=nil; i++){
		suf = ports[i].suffix;
		nsuf = strlen(suf);
		if(nfile > nsuf)
			if(cistrncmp(file+nfile-nsuf, suf, nsuf) == 0)
				return i;
	}
	return 0;
}
예제 #14
0
파일: mesg.c 프로젝트: Requaos/harvey
void
printheader(char *dir, Biobuf *b, char **okheaders)
{
	char *s;
	char *lines[100];
	int i, j, n;

	s = readfile(dir, "header", nil);
	if(s == nil)
		return;
	n = getfields(s, lines, nelem(lines), 0, "\n");
	for(i=0; i<n; i++)
		for(j=0; okheaders[j]; j++)
			if(cistrncmp(lines[i], okheaders[j], strlen(okheaders[j])) == 0)
				Bprint(b, "%s\n", lines[i]);
	free(s);
}
예제 #15
0
파일: cistrstr.c 프로젝트: 00001/plan9port
char*
cistrstr(char *s, char *sub)
{
	int c, csub, n;

	csub = *sub;
	if(csub == '\0')
		return s;
	if(csub >= 'A' && csub <= 'Z')
		csub -= 'A' - 'a';
	sub++;
	n = strlen(sub);
	for(; c = *s; s++){
		if(c >= 'A' && c <= 'Z')
			c -= 'A' - 'a';
		if(c == csub && cistrncmp(s+1, sub, n) == 0)
			return s;
	}
	return nil;
}
예제 #16
0
char*
findfrom(void)
{
	char *p, *u;
	Biobuf *b;

	u = getuser();
	if(u==nil)
		return "glenda";

	p = estrstrstrdup("/usr/", u, "/lib/newsfrom");
	b = Bopen(p, OREAD);
	free(p);
	if(b){
		p = Brdline(b, '\n');
		if(p){
			p[Blinelen(b)-1] = '\0';
			p = estrdup(p);
			Bterm(b);
			return p;
		}
		Bterm(b);
	}

	p = estrstrstrdup("/mail/box/", u, "/headers");
	b = Bopen(p, OREAD);
	free(p);
	if(b){
		while(p = Brdline(b, '\n')){
			p[Blinelen(b)-1] = '\0';
			if(cistrncmp(p, "from:", 5)==0){
				p = estrdup(skip(p, "from:"));
				Bterm(b);
				return p;
			}
		}
		Bterm(b);
	}

	return u;
}
예제 #17
0
파일: page.c 프로젝트: carriercomm/legacy
char *
getfilter(Rune *r, int x, int y)
{
	char buf[128];
	int i;

	snprint(buf, sizeof(buf), "%S", r);
	for(i=0; filtertab[i].mime!=nil; i++)
		if(cistrncmp(buf, filtertab[i].mime, strlen(filtertab[i].mime)) == 0)
			break;

	if(filtertab[i].filter == nil)
		return nil;

	if(x==0 && y==0)
		return smprint("%s", filtertab[i].filter);
	if(x!=0 && y!=0)
		return smprint("%s | resample -x %d -y %d", filtertab[i].filter, x, y);
	if(x != 0)
		return smprint("%s | resample -x %d", filtertab[i].filter, x);
	/* y != 0 */
	return smprint("%s | resample -y %d", filtertab[i].filter, y);
}
예제 #18
0
int
w_option(Ctlr* ctlr, char* buf, long n)
{
	char *p;
	int i, r;
	Cmdbuf *cb;

	r = 0;

	cb = parsecmd(buf, n);
	if(cb->nf < 2)
		r = -1;
	else if(cistrcmp(cb->f[0], "essid") == 0){
		if(cistrcmp(cb->f[1],"default") == 0)
			p = "";
		else
			p = cb->f[1];
		if(ctlr->ptype == WPTypeAdHoc){
			memset(ctlr->netname, 0, sizeof(ctlr->netname));
			strncpy(ctlr->netname, p, WNameLen-1);
		}
		else{
			memset(ctlr->wantname, 0, sizeof(ctlr->wantname));
			strncpy(ctlr->wantname, p, WNameLen-1);
		}
	}
	else if(cistrcmp(cb->f[0], "station") == 0){
		memset(ctlr->nodename, 0, sizeof(ctlr->nodename));
		strncpy(ctlr->nodename, cb->f[1], WNameLen-1);
	}
	else if(cistrcmp(cb->f[0], "channel") == 0){
		if((i = atoi(cb->f[1])) >= 1 && i <= 16)
			ctlr->chan = i;
		else
			r = -1;
	}
	else if(cistrcmp(cb->f[0], "mode") == 0){
		if(cistrcmp(cb->f[1], "managed") == 0)
			ctlr->ptype = WPTypeManaged;
		else if(cistrcmp(cb->f[1], "wds") == 0)
			ctlr->ptype = WPTypeWDS;
		else if(cistrcmp(cb->f[1], "adhoc") == 0)
			ctlr->ptype = WPTypeAdHoc;
		else if((i = atoi(cb->f[1])) >= 0 && i <= 3)
			ctlr->ptype = i;
		else
			r = -1;
	}
	else if(cistrcmp(cb->f[0], "ibss") == 0){
		if(cistrcmp(cb->f[1], "on") == 0)
			ctlr->createibss = 1;
		else
			ctlr->createibss = 0;
	}
	else if(cistrcmp(cb->f[0], "crypt") == 0){
		if(cistrcmp(cb->f[1], "off") == 0)
			ctlr->crypt = 0;
		else if(cistrcmp(cb->f[1], "on") == 0 && ctlr->hascrypt)
			ctlr->crypt = 1;
		else
			r = -1;
	}
	else if(cistrcmp(cb->f[0], "clear") == 0){
		if(cistrcmp(cb->f[1], "on") == 0)
			ctlr->xclear = 0;
		else if(cistrcmp(cb->f[1], "off") == 0 && ctlr->hascrypt)
			ctlr->xclear = 1;
		else
			r = -1;
	}
	else if(cistrncmp(cb->f[0], "key", 3) == 0){
		if((i = atoi(cb->f[0]+3)) >= 1 && i <= WNKeys){
			ctlr->txkey = i-1;
			if(parsekey(&ctlr->keys.keys[ctlr->txkey], cb->f[1]))
				r = -1;
		}
		else
			r = -1;
	}
	else if(cistrcmp(cb->f[0], "txkey") == 0){
		if((i = atoi(cb->f[1])) >= 1 && i <= WNKeys)
			ctlr->txkey = i-1;
		else
			r = -1;
	}
	else if(cistrcmp(cb->f[0], "pm") == 0){
		if(cistrcmp(cb->f[1], "off") == 0)
			ctlr->pmena = 0;
		else if(cistrcmp(cb->f[1], "on") == 0){
			ctlr->pmena = 1;
			if(cb->nf == 3){
				i = atoi(cb->f[2]);
				// check range here? what are the units?
				ctlr->pmwait = i;
			}
		}
		else
			r = -1;
	}
	else
		r = -2;
	free(cb);

	return r;
}
예제 #19
0
/*
 *  Create an emsMIMEtype structure to hold MIME information. Structure is
 *  initialized to values provided in RFC822 content-type header line. This
 *  includes all parameter name-value pairs.
 *   
 *  NOTE: The user of this function is responsible for calling
 *        free_mime_type() on returned structure.
 *
 *  Args:
 *   content_type [IN] The RFC822 content-type string to parse
 *
 *  Returns: Pointer to the created emsMIMEtype structure, NULL if error.
 */
emsMIMEtypeP parse_make_mime_type(const char *content_type)
{
	const char *kPrefixStr = "Content-Type:";
	const unsigned int kPrefixStrLen = strlen(kPrefixStr);

	if (cistrncmp(content_type, kPrefixStr, kPrefixStrLen) != 0)
		return NULL;

	char *cp = (char *) content_type + kPrefixStrLen;
	char *mime_type = NULL, *mime_subtype = NULL;

	mime_type = rfc822_extract_token(&cp);

	if ((strlen(mime_type) > 0) && ((*cp++) == '/'))
	{
		mime_subtype = rfc822_extract_token(&cp);

		if (strlen(mime_subtype) > 0)
		{
			// We have a type/subtype, so create emsMIMEtype structure
			emsMIMEtypeP mimePtr = make_mime_type(mime_type, mime_subtype, NULL);

			safefree(mime_type);
			safefree(mime_subtype);

			if (mimePtr)
			{
				char *name = NULL, *value = NULL;

				do {

					if ((*cp++) != ';') // Skip semi-colon
						break;
				
					if (!(name = rfc822_extract_token(&cp)))
						break;

					if ((*cp++) != '=') // Skip equals
						break;

					if (!(value = rfc822_extract_token(&cp)))
						break;

					if ((strlen(name) > 0) && (strlen(value) > 0))
						add_mime_parameter(mimePtr, name, value);

					safefree(name);
					safefree(value);
				} while (*cp);

				safefree(name);
				safefree(value);

				return mimePtr;
			}
		}
	}

	// If we get here then something went wrong
	// Do complete cleanup

	safefree(mime_type);
	safefree(mime_subtype);

	return (NULL); // ERROR
}
예제 #20
0
파일: page.c 프로젝트: carriercomm/legacy
static
void
pageloadproc(void *v)
{
	Page *p;
	char buf[BUFSIZE], *s;
	int32_t n, l;
	int fd, i, ctype;

	threadsetname("pageloadproc");
	rfork(RFFDG);

	p = v;
	addrefresh(p, "opening: %S...", p->url->src.r);
	fd = urlopen(p->url);
	if(fd < 0){
		addrefresh(p, "%S: %r", p->url->src.r);
    Err:
		p->loading = FALSE;
		return;
	}
	if(runestrlen(p->url->ctype.r) == 0) /* assume .html when headers don't say anyting */
		goto Html;

	snprint(buf, sizeof(buf), "%S", p->url->ctype.r);
	for(i=0; mimetab[i]!=nil; i++)
		if(cistrncmp(buf, mimetab[i], strlen(mimetab[i])) == 0)
			break;

	if(mimetab[i]){
    Html:
		ctype = TextHtml;
	}else if(cistrncmp(buf, "text/", 5) == 0)
		ctype = TextPlain;
	else{
		close(fd);
		addrefresh(p, "%S: unsupported mime type: '%S'", p->url->act.r, p->url->ctype.r);
		goto Err;
	}
	addrefresh(p, "loading: %S...", p->url->src.r);
	s = nil;
	l = 0;
	while((n=read(fd, buf, sizeof(buf))) > 0){
		if(p->aborting){
			if(s){
				free(s);
				s = nil;
			}
			break;
		}
		s = erealloc(s, l+n+1);
		memmove(s+l, buf, n);
		l += n;
		s[l] = '\0';
	}
	close(fd);
	n = l;
	if(s){
		s = convert(p->url->ctype, s, &n);
		p->items = parsehtml((uint8_t *)s, n, p->url->act.r, ctype,
				     UTF_8, &p->doc);
		free(s);
		fixtext(p);
		if(ctype==TextHtml && p->aborting==FALSE){
			p->changed = TRUE;
			addrefresh(p, "");
			if(p->doc->doctitle){
				p->title.r = erunestrdup(p->doc->doctitle);
				p->title.nr = runestrlen(p->title.r);
			}
			p->loading = XXX;
			if(p->doc->kidinfo)
				loadchilds(p, p->doc->kidinfo);
			else if(p->doc->images)
				loadimages(p);
		}
	}
	p->changed = TRUE;
	p->loading = FALSE;
	addrefresh(p, "");
}
예제 #21
0
char *
tcs(char *cs, char *s, long *np)
{
	Channel *sync;
	Exec *e;
	Rune r;
	long i, n;
	void **a;
	uchar *us;
	char buf[BUFSIZE], cmd[50];
	char *t, *u;
	int p[2], q[2];


	if(s==nil || *s=='\0' || *np==0){
		werrstr("tcs failed: no data");
		return s;
	}

	if(cs == nil){
		werrstr("tcs failed: no charset");
		return s;
	}

	if(cistrncmp(cs, "utf-8", 5)==0 || cistrncmp(cs, "utf8", 4)==0)
		return s;

	for(i=0; tcstab[i].mime!=nil; i++)
		if(cistrncmp(cs, tcstab[i].mime, strlen(tcstab[i].mime)) == 0)
			break;

	if(tcstab[i].mime == nil){
		fprint(2, "abaco: charset: %s not supported\n", cs);
		goto latin1;
	}
	if(cistrcmp(tcstab[i].tcs, "8859-1")==0 || cistrcmp(tcstab[i].tcs, "ascii")==0){
latin1:
		n = 0;
		for(us=(uchar*)s; *us; us++)
			n += runelen(*us);
		n++;
		t = emalloc(n);
		for(us=(uchar*)s, u=t; *us; us++){
			if(*us>=Winstart && *us<=Winend)
				*u++ = winchars[*us-Winstart];
			else{
				r = *us;
				u += runetochar(u, &r);
			}
		}
		*u = 0;
		free(s);
		return t;
	}

	if(pipe(p)<0 || pipe(q)<0)
		error("can't create pipe");

	sync = chancreate(sizeof(ulong), 0);
	if(sync == nil)
		error("can't create channel");

	snprint(cmd, sizeof cmd, "tcs -f %s", tcstab[i].tcs);
	e = emalloc(sizeof(Exec));
	e->p[0] = p[0];
	e->p[1] = p[1];
	e->q[0] = q[0];
	e->q[1] = q[1];
	e->cmd = cmd;
	e->sync = sync;
	proccreate(execproc, e, STACK);
	recvul(sync);
	chanfree(sync);
	close(p[0]);
	close(q[1]);

	/* in case tcs fails */
	t = s;
	sync = chancreate(sizeof(ulong), 0);
	if(sync == nil)
		error("can't create channel");

	a = emalloc(4*sizeof(void *));
	a[0] = sync;
	a[1] = (void *)p[1];
	a[2] = s;
	a[3] = (void *)*np;
	proccreate(writeproc, a, STACK);

	s = nil;
	while((n = read(q[0], buf, sizeof(buf))) > 0){
		s = erealloc(s, i+n+1);
		memmove(s+i, buf, n);
		i += n;
		s[i] = '\0';
	}
	n = recvul(sync);
	if(n != *np)
		fprint(2, "tcs: did not write %ld; wrote %uld\n", *np, n);

	*np = i;
	chanfree(sync);
	close(q[0]);

	if(s == nil){
		fprint(2, "tcs failed: can't convert charset=%s to %s\n", cs, tcstab[i].tcs);
		return t;
	}
	free(t);

	return s;
}
예제 #22
0
void
mesgpost(Article *m)
{
	Biobuf *b;
	char *p, *ep;
	int isfirst, ishdr, havegroup, havefrom;

	p = estrstrdup(dir, "post");
	if((b = Bopen(p, OWRITE)) == nil){
		fprint(2, "cannot open %s: %r\n", p);
		free(p);
		return;
	}
	free(p);

	winopenbody(m->w, OREAD);
	ishdr = 1;
	isfirst = 1;
	havegroup = havefrom = 0;
	while(p = Brdline(m->w->body, '\n')){
		ep = p+Blinelen(m->w->body);
		if(ishdr && p+1==ep){
			if(!havegroup)
				Bprint(b, "Newsgroups: %s\n", group);
			if(!havefrom)
				Bprint(b, "From: %s\n", from);
			ishdr = 0;
		}
		if(ishdr){
			ep[-1] = '\0';
			if(isfirst && strchr(p, ':')==0){	/* group list */
				commas(p, ep);
				Bprint(b, "newsgroups: %s\n", p);
				havegroup = 1;
				isfirst = 0;
				continue;
			}
			if(cistrncmp(p, "newsgroup:", 10)==0){
				commas(skip(p, "newsgroup:"), ep);
				Bprint(b, "newsgroups: %s\n", skip(p, "newsgroup:"));
				havegroup = 1;
				continue;
			}
			if(cistrncmp(p, "newsgroups:", 11)==0){
				commas(skip(p, "newsgroups:"), ep);
				Bprint(b, "newsgroups: %s\n", skip(p, "newsgroups:"));
				havegroup = 1;
				continue;
			}
			if(cistrncmp(p, "from:", 5)==0)
				havefrom = 1;
			ep[-1] = '\n';
		}
		Bwrite(b, p, ep-p);
	}
	winclosebody(m->w);
	Bflush(b);
	if(write(Bfildes(b), "", 0) == 0)
		winclean(m->w);
	else
		fprint(2, "post: %r\n");
	Bterm(b);
}
예제 #23
0
파일: parts.c 프로젝트: Earnestly/plan9
/*
 * To facilitate booting from CDs, we create a partition for
 * the FAT filesystem image embedded in a bootable CD.
 */
static int
part9660(PSDunit *unit)
{
	ulong a, n, i, j;
	uchar drecsz;
	uchar *p;
	uchar buf[Maxsec];
	Drec *rootdrec, *drec;
	Voldesc *v;
	static char stdid[] = "CD001\x01";

	if(unit->secsize == 0)
		unit->secsize = Cdsec;
	if(unit->secsize != Cdsec)
		return -1;

	if(psdread(unit, &unit->part[0], buf, Cdsec, VOLDESC*Cdsec) < 0)
		return -1;
	if(buf[0] != PrimaryIso ||
	    memcmp((char*)buf+1, stdid, sizeof stdid - 1) != 0)
		return -1;

	v = (Voldesc *)buf;
	rootdrec = (Drec *)v->z.desc.rootdir;
	assert(rootdrec);
	p = rootdrec->addr;
	a = p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
	p = rootdrec->size;
	n = p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
//	print("part9660: read %uld %uld\n", n, a);	/* debugging */

	if(n < Cdsec){
		print("warning: bad boot file size %ld in iso directory", n);
		n = Cdsec;
	}

	drec = nil;
	for(j = 0; j*Cdsec < n; j++){
		if(psdread(unit, &unit->part[0], buf, Cdsec, (a + j)*Cdsec) < 0)
			return -1;
		for(i = 0; i + j*Cdsec <= n && i < Cdsec; i += drecsz){
			drec = (Drec *)&buf[i];
			drecsz = drec->reclen;
			if(drecsz == 0 || drecsz + i > Cdsec)
				break;
			if(cistrncmp("bootdisk.img", (char *)drec->name, 12) == 0)
				goto Found;
		}
	}
Found:
	if(j*Cdsec >= n || drec == nil)
		return -1;

	p = drec->addr;
	a = p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
	p = drec->size;
	n = p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);

	print("found partition %s!9fat; %lud+%lud\n", unit->name, a, n);
	n /= Cdsec;
	psdaddpart(unit, "9fat", a, a+n);
	return 0;
}
예제 #24
0
파일: devi82365.c 프로젝트: CoryXie/NxM
static I82365*
i82365probe(int x, int d, int dev)
{
	uchar c, id;
	I82365 *cp;
	ISAConf isa;
	int i, nslot;

	outb(x, Rid + (dev<<7));
	id = inb(d);
	if((id & 0xf0) != 0x80)
		return 0;		/* not a memory & I/O card */
	if((id & 0x0f) == 0x00)
		return 0;		/* no revision number, not possible */

	cp = xalloc(sizeof(I82365));
	cp->xreg = x;
	cp->dreg = d;
	cp->dev = dev;
	cp->type = Ti82365;
	cp->nslot = 2;

	switch(id){
	case 0x82:
	case 0x83:
	case 0x84:
		/* could be a cirrus */
		outb(x, Rchipinfo + (dev<<7));
		outb(d, 0);
		c = inb(d);
		if((c & 0xc0) != 0xc0)
			break;
		c = inb(d);
		if((c & 0xc0) != 0x00)
			break;
		if(c & 0x20){
			cp->type = Tpd6720;
		} else {
			cp->type = Tpd6710;
			cp->nslot = 1;
		}

		/* low power mode */
		outb(x, Rmisc2 + (dev<<7));
		c = inb(d);
		outb(d, c & ~Flowpow);
		break;
	}

	/* if it's not a Cirrus, it could be a Vadem... */
	if(cp->type == Ti82365){
		/* unlock the Vadem extended regs */
		outb(x, 0x0E + (dev<<7));
		outb(x, 0x37 + (dev<<7));

		/* make the id register show the Vadem id */
		outb(x, 0x3A + (dev<<7));
		c = inb(d);
		outb(d, c|0xC0);
		outb(x, Rid + (dev<<7));
		c = inb(d);
		if(c & 0x08)
			cp->type = Tvg46x;

		/* go back to Intel compatible id */
		outb(x, 0x3A + (dev<<7));
		c = inb(d);
		outb(d, c & ~0xC0);
	}

	memset(&isa, 0, sizeof(ISAConf));
	if(isaconfig("pcmcia", ncontroller, &isa) && isa.irq)
		cp->irq = isa.irq;
	else
		cp->irq = IrqPCMCIA;

	for(i = 0; i < isa.nopt; i++){
		if(cistrncmp(isa.opt[i], "nslot=", 6))
			continue;
		nslot = strtol(&isa.opt[i][6], nil, 0);
		if(nslot > 0 && nslot <= 2)
			cp->nslot = nslot;
	}

	controller[ncontroller++] = cp;
	return cp;
}
예제 #25
0
static int
reset(Ether* ether)
{
	ushort buf[16];
	ulong port;
	Dp8390 *ctlr;
	int i, slot;
	uchar ea[Eaddrlen], sum, x;
	Ec2t *ec2t, tmpec2t;

	/*
	 * Set up the software configuration.
	 * Use defaults for port, irq, mem and size
	 * if not specified.
	 * The manual says 16KB memory, the box
	 * says 32KB. The manual seems to be correct.
	 */
	if(ether->port == 0)
		ether->port = 0x300;
	if(ether->irq == 0)
		ether->irq = 9;
	if(ether->mem == 0)
		ether->mem = 0x4000;
	if(ether->size == 0)
		ether->size = 16*1024;
	port = ether->port;

	if(ioalloc(ether->port, 0x20, 0, "ec2t") < 0)
		return -1;
	slot = -1;
	for(ec2t = ec2tpcmcia; ec2t->name != nil; ec2t++){
		if((slot = pcmspecial(ec2t->name, ether)) >= 0)
			break;
	}
	if(ec2t->name == nil){
		ec2t = &tmpec2t;
		ec2t->name = nil;
		ec2t->iochecksum = 0;
		for(i = 0; i < ether->nopt; i++){
			if(cistrncmp(ether->opt[i], "id=", 3) == 0){
				ec2t->name = &ether->opt[i][3];
				slot = pcmspecial(ec2t->name, ether);
			}
			else if(cistrncmp(ether->opt[i], "iochecksum", 10) == 0)
				ec2t->iochecksum = 1;
		}
	}
	ctlr = malloc(sizeof(Dp8390));
	if(ctlr == nil || slot < 0){
		iofree(port);
		free(ctlr);
		return -1;
	}
	ether->ctlr = ctlr;
	ctlr->width = 2;
	ctlr->ram = 0;

	ctlr->port = port;
	ctlr->data = port+Data;

	ctlr->tstart = HOWMANY(ether->mem, Dp8390BufSz);
	ctlr->pstart = ctlr->tstart + HOWMANY(sizeof(Etherpkt), Dp8390BufSz);
	ctlr->pstop = ctlr->tstart + HOWMANY(ether->size, Dp8390BufSz);

	ctlr->dummyrr = 0;
	for(i = 0; i < ether->nopt; i++){
		if(cistrcmp(ether->opt[i], "nodummyrr") == 0)
			ctlr->dummyrr = 0;
		else if(cistrncmp(ether->opt[i], "dummyrr=", 8) == 0)
			ctlr->dummyrr = strtol(&ether->opt[i][8], nil, 0);
	}

	/*
	 * Reset the board. This is done by doing a read
	 * followed by a write to the Reset address.
	 */
	buf[0] = inb(port+Reset);
	delay(2);
	outb(port+Reset, buf[0]);
	delay(2);

	/*
	 * Init the (possible) chip, then use the (possible)
	 * chip to read the (possible) PROM for ethernet address
	 * and a marker byte.
	 * Could just look at the DP8390 command register after
	 * initialisation has been tried, but that wouldn't be
	 * enough, there are other ethernet boards which could
	 * match.
	 */
	dp8390reset(ether);
	sum = 0;
	if(ec2t->iochecksum){
		/*
		 * These cards have the ethernet address in I/O space.
		 * There's a checksum over 8 bytes which sums to 0xFF.
		 */
		for(i = 0; i < 8; i++){
			x = inb(port+0x14+i);
			sum += x;
			buf[i] = (x<<8)|x;
		}
	}
	else{
		memset(buf, 0, sizeof(buf));
		dp8390read(ctlr, buf, 0, sizeof(buf));
		if((buf[0x0E] & 0xFF) == 0x57 && (buf[0x0F] & 0xFF) == 0x57)
			sum = 0xFF;
	}
	if(sum != 0xFF){
		pcmspecialclose(slot);
		iofree(ether->port);
		free(ether->ctlr);
		return -1;
	}

	/*
	 * Stupid machine. Shorts were asked for,
	 * shorts were delivered, although the PROM is a byte array.
	 * Set the ethernet address.
	 */
	memset(ea, 0, Eaddrlen);
	if(memcmp(ea, ether->ea, Eaddrlen) == 0){
		for(i = 0; i < sizeof(ether->ea); i++)
			ether->ea[i] = buf[i];
	}
	dp8390setea(ether);

	return 0;
}
예제 #26
0
int
fillmesgwindow(int fd, Article *m)
{
	Biobuf *b;
	char *p, tmp[40];
	int i, inhdr, copy, xfd;
	Window *w;

	xfd = -1;
	if(fd == -1){
		sprint(tmp, "%d/article", m->n);
		p = estrstrdup(dir, tmp);
		if((xfd = open(p, OREAD)) < 0){
			free(p);	
			return 0;
		}
		free(p);
		fd = xfd;
	}

	w = m->w;
	if(w->data < 0)
		w->data = winopenfile(w, "data");
	if(winsetaddr(w, ",", 0))
		write(w->data, "", 0);

	winopenbody(m->w, OWRITE);
	b = emalloc(sizeof(*b));
	Binit(b, fd, OREAD);

	inhdr = 1;
	copy = 1;
	while(p = Brdline(b, '\n')){
		if(Blinelen(b)==1)
			inhdr = 0, copy=1;
		if(inhdr && !isspace(p[0])){
			copy = 1;
			if(!m->headers){
				if(cistrncmp(p, "from:", 5)==0){
					p[Blinelen(b)-1] = '\0';
					p = fixfrom(skip(p, "from:"));
					Bprint(m->w->body, "From: %s\n", p);
					free(p);
					copy = 0;
					continue;
				}
				for(i=0; i<nelem(skipheader); i++)
					if(cistrncmp(p, skipheader[i], strlen(skipheader[i]))==0)
						copy=0;
			}
		}
		if(copy)
			Bwrite(m->w->body, p, Blinelen(b));
	}
	Bterm(b);
	free(b);
	winclean(m->w);
	if(xfd != -1)
		close(xfd);
	return 1;
}
예제 #27
0
파일: msgtok.c 프로젝트: 00001/plan9port
void
main(int argc, char **argv)
{
	int i, hdr, n, eof, off;
	Dreprog *re[3];
	int m[3];
	char *p, *ep, *tag;
	Biobuf bout, bin;
	char msg[1024+1];
	char buf[1024];

	refile = unsharp(refile);
	buildre(re);
	ARGBEGIN{
	case 'D':
		debug = 1;
		break;
	case 'n':
		maxtoklen = atoi(EARGF(usage()));
		break;
	case 'r':
		refile = EARGF(usage());
		break;
	default:
		usage();
	}ARGEND;

	if(argc > 1)
		usage();
	if(argc == 1){
		close(0);
		if(open(argv[0], OREAD) < 0)
			sysfatal("open %s: %r", argv[0]);
	}

	tag = nil;
	Binit(&bin, 0, OREAD);
	Binit(&bout, 1, OWRITE);
	ep = msg;
	p = msg;
	eof = 0;
	off = 0;
	hdr = 1;
	for(;;){
		/* replenish buffer */
		if(ep - p < 512 && !eof){
			if(p > msg + 1){
				n = ep - p;
				memmove(msg, p-1, ep-(p-1));
				off += (p-1) - msg;
				p = msg+1;
				ep = p + n;
			}
			n = Bread(&bin, ep, msg+(sizeof msg - 1)- ep);
			if(n < 0)
				sysfatal("read error: %r");
			if(n == 0)
				eof = 1;
			ep += n;
			*ep = 0;
		}
		if(p >= ep)
			break;

		if(*p == 0){
			p++;
			continue;
		}

		if(hdr && p[-1]=='\n'){
			if(p[0]=='\n')
				hdr = 0;
			else if(cistrncmp(p-1, "\nfrom:", 6) == 0)
				tag = "From*";
			else if(cistrncmp(p-1, "\nto:", 4) == 0)
				tag = "To*";
			else if(cistrncmp(p-1, "\nsubject:", 9) == 0)
				tag = "Subject*";
			else if(cistrncmp(p-1, "\nreturn-path:", 13) == 0)
				tag = "Return-Path*";
			else
				tag = nil;
		}
		m[0] = dregexec(re[0], p, p==msg || p[-1]=='\n');
		m[1] = dregexec(re[1], p, p==msg || p[-1]=='\n');
		m[2] = dregexec(re[2], p, p==msg || p[-1]=='\n');

		n = m[0];
		if(n < m[1])
			n = m[1];
		if(n < m[2])
			n = m[2];
		if(n <= 0){
fprint(2, "«%s» %.2ux", p, p[0]);
			sysfatal("no regexps matched at %ld", off + (p-msg));
		}

		if(m[0] >= m[1] && m[0] >= m[2]){
			/* "From " marks start of new message */
			Bprint(&bout, "*From*\n");
			n = m[0];
			hdr = 1;
		}else if(m[2] > 1){
			/* ignore */
			n = m[2];
		}else if(m[1] >= m[0] && m[1] >= m[2] && m[1] > 2 && m[1] <= maxtoklen){
			/* keyword */
			/* should do UTF-aware lowercasing, too much bother */
/*
			for(i=0; i<n; i++)
				if('A' <= p[i] && p[i] <= 'Z')
					p[i] += 'a' - 'A';
*/
			if(tag){
				i = strlen(tag);	
				memmove(buf, tag, i);
				memmove(buf+i, p, m[1]);
				buf[i+m[1]] = 0;
			}else{
				memmove(buf, p, m[1]);
				buf[m[1]] = 0;
			}
			Bprint(&bout, "%s\n", buf);
			while(trim(buf) >= 0)
				Bprint(&bout, "stem*%s\n", buf);
			n = m[1];
		}else
			n = m[2];
		if(debug)
			fprint(2, "%.*s¦", utfnlen(p, n), p);
		p += n;
	}
	Bterm(&bout);
	exits(0);
}
예제 #28
0
파일: ether589.c 프로젝트: 8l/inferno
static int
reset(Ether* ether)
{
	int i, t, slot;
	char *type;
	int port;
	enum { WantAny, Want10BT, Want10B2 };
	int want;
	uchar ea[6];
	char *p;

	if(ether->irq == 0)
		ether->irq = 10;
	if(ether->port == 0)
		ether->port = 0x240;
	port = ether->port;

	if(ioalloc(port, 0x10, 0, "3C589") < 0)
		return -1;

	type = nil;
	slot = -1;
	for(i = 0; tcmpcmcia[i] != nil; i++){
		type = tcmpcmcia[i];
		if((slot = pcmspecial(type, ether)) >= 0)
			break;
	}
	if(slot < 0){
		iofree(port);
		return -1;
	}

	/*
	 * Read Ethernet address from card memory
	 * on 3C562, but only if the user has not 
	 * overridden it.
	 */
	memset(ea, 0, sizeof ea);
	if(memcmp(ea, ether->ea, 6) == 0 && strcmp(type, "3C562") == 0) {
		if(pcmcistuple(slot, 0x88, -1, ea, 6) == 6) {
			for(i = 0; i < 6; i += 2){
				t = ea[i];
				ea[i] = ea[i+1];
				ea[i+1] = t;
			}
			memmove(ether->ea, ea, 6);
		}
	}
	/*
	 * Allow user to specify desired media in plan9.ini
	 */
	want = WantAny;
	for(i = 0; i < ether->nopt; i++){
		if(cistrncmp(ether->opt[i], "media=", 6) != 0)
			continue;
		p = ether->opt[i]+6;
		if(cistrcmp(p, "10base2") == 0)
			want = Want10B2;
		else if(cistrcmp(p, "10baseT") == 0)
			want = Want10BT;
	}
	
	/* try configuring as a 10BaseT */
	if(want==WantAny || want==Want10BT){
		if(configASIC(ether, port, xcvr10BaseT) < 0){
			pcmspecialclose(slot);
			iofree(port);
			return -1;
		}
		delay(100);
		COMMAND(port, SelectRegisterWindow, Wdiagnostic);
		if((ins(port+MediaStatus)&linkBeatDetect) || want==Want10BT){
			COMMAND(port, SelectRegisterWindow, Wop);
			print("#l%d: xcvr10BaseT %s\n", ether->ctlrno, type);
			return 0;
		}
	}

	/* try configuring as a 10base2 */
	if(want==WantAny || want==Want10B2){
		COMMAND(port, GlobalReset, 0);
		if(configASIC(ether, port, xcvr10Base2) < 0){
			pcmspecialclose(slot);
			iofree(port);
			return -1;
		}
		print("#l%d: xcvr10Base2 %s\n", ether->ctlrno, type);
		return 0;
	}
	return -1;		/* not reached */
}
예제 #29
0
파일: main.c 프로젝트: Nurb432/plan9front
static char*
fswalk1(Fid *fid, char *name, Qid *qid)
{
	FInfo fi;
	int rc, n, i;
	Aux *a = fid->aux;
	static char e[ERRMAX];
	char *p, *npath, *winpath;

	*e = 0;
	npath = newpath(a->path, name);
	if(strcmp(npath, "/") == 0){			/* root dir */
		*qid = mkqid("/", 1, 1, Proot, 0);
		free(a->path);
		a->path = npath;
		fid->qid = *qid;
		return nil;
	}

	if(strrchr(npath, '/') == npath){		/* top level dir */
		if((n = walkinfo(name)) != -1){		/* info file */
			*qid = mkqid(npath, 0, 1, Pinfo, n);
		}
		else {					/* volume name */
			for(i = 0; i < Nshares; i++){
				n = strlen(Shares[i].name);
				if(cistrncmp(npath+1, Shares[i].name, n) != 0)
					continue;
				if(Checkcase && strncmp(npath+1, Shares[i].name, n) != 0)
					continue;
				if(npath[n+1] != 0 && npath[n+1] != '/')
					continue;
				break;
			}
			if(i >= Nshares){
				free(npath);
				return "not found";
			}
			a->sp = Shares+i;
			*qid = mkqid(npath, 1, 1, Pshare, i);
		}
		free(a->path);
		a->path = npath;
		fid->qid = *qid;
		return nil;
	}

	/* must be a vanilla file or directory */
again:
	if(mapshare(npath, &a->sp) == -1){
		rerrstr(e, sizeof(e));
		free(npath);
		return e;
	}

	winpath = mapfile(npath);
	memset(&fi, 0, sizeof fi);
	if(Sess->caps & CAP_NT_SMBS)
		rc = T2queryall(Sess, a->sp, winpath, &fi);
	else
		rc = T2querystandard(Sess, a->sp, winpath, &fi);

	if(rc == -1){
		rerrstr(e, sizeof(e));
		free(npath);
		return e;
	}

	if((a->sp->options & SMB_SHARE_IS_IN_DFS) != 0 &&
	    (fi.attribs & ATTR_REPARSE) != 0){
		if(redirect(Sess, a->sp, npath) != -1)
			goto again;
	}

	if((p = strrchr(fi.name, '/')) == nil && (p = strrchr(fi.name, '\\')) == nil)
		p = fi.name;
	else
		p++;

	if(! validfile(p, name, winpath, a->sp)){
		free(npath);
		return "not found";

	}
	*qid = mkqid(npath, fi.attribs & ATTR_DIRECTORY, fi.changed, 0, 0);

	free(a->path);
	a->path = npath;
	fid->qid = *qid;
	return nil;
}