Esempio n. 1
0
MAIN()
{
	char		buf[1024], *s;
	Sfio_t*	f;

	f = sfnew(NIL(Sfio_t*),NIL(Void_t*),(size_t)SF_UNBOUND,-1,SF_WRITE|SF_STRING);
	sfsetbuf(sfstdout,buf,sizeof(buf));
	sfsetbuf(sfstderr,buf,sizeof(buf));
	sfset(sfstdout,SF_SHARE,0);
	sfset(sfstderr,SF_SHARE,0);

	if(!sfpool(sfstdout,f,SF_SHARE) || !sfpool(sfstderr,f,SF_SHARE) )
		terror("Pooling\n");

	if(sfputr(sfstdout,"01234",-1) != 5)
		terror("Writing to stderr\n");
	if(sfputr(sfstderr,"56789",-1) != 5)
		terror("Writing to stdout\n");

	if(sfputc(f,'\0') < 0)
		terror("Writing to string stream\n");

	sfseek(f,(Sfoff_t)0,0);
	if(!(s = sfreserve(f,SF_UNBOUND,1)) )
		terror("Peeking\n");
	sfwrite(f,s,0);
	if(strcmp(s,"0123456789") != 0)
		terror("Data is wrong\n");

	TSTEXIT(0);
}
Esempio n. 2
0
void exerror(const char *format, ...)
{
    Sfio_t *sp;

    if (expr.program->disc->errorf && !expr.program->errors
	&& (sp = sfstropen())) {
	va_list ap;
	char *s;
	char buf[64];

	expr.program->errors = 1;
	excontext(expr.program, buf, sizeof(buf));
	sfputr(sp, buf, -1);
	sfputr(sp, "\n -- ", -1);
	va_start(ap, format);
	sfvprintf(sp, format, ap);
	va_end(ap);
	s = sfstruse(sp);
	(*expr.program->disc->errorf) (expr.program, expr.program->disc,
				       (expr.program->disc->
					flags & EX_FATAL) ? ERROR_FATAL :
				       ERROR_ERROR, "%s", s);
	sfclose(sp);
    } else if (expr.program->disc->flags & EX_FATAL)
	exit(1);
}
Esempio n. 3
0
MAIN()
{
	Sfio_t*	f;
	char	buf[1024], *s;
	int	n;
#ifdef DEBUG
	Sfio_t*	logf = sfopen(0,"LOG","a"); sfsetbuf(logf,NIL(Void_t*),0);
#endif

	alarm(10);
	if(argc > 1)
	{	/* coprocess only */
		while((s = sfreserve(sfstdin,-1,0)) )
		{
#ifdef DEBUG
			sfwrite(logf, s, sfvalue(sfstdin));
#endif
			sfwrite(sfstdout, s, sfvalue(sfstdin));
		}
		return 0;
	}

	/* make coprocess */
	if(!(f = sfpopen(NIL(Sfio_t*), sfprints("%s -p",argv[0]), "r+")))
		terror("Opening for read/write\n");
	for(n = 0; n < 10; ++n)
	{	sfsprintf(buf,sizeof(buf),"Line %d",n);
		sfputr(f,buf,'\n');
		if(!(s = sfgetr(f,'\n',1)))
			terror("Did not read back line\n");
		if(strcmp(s,buf) != 0)
			terror("Input=%s, Expect=%s\n",s,buf);
	}

	if(sfputr(f,"123456789",'\n') != 10)
		terror("Bad write");

	if(sfread(f,buf,3) != 3)
		terror("Did not get data back\n");
	if(strncmp(s,"123",3) != 0)
		terror("Wrong data\n");

	if(sfwrite(f,"aaa",3) != 3 || sfputc(f,'\n') != '\n')
		terror("Fail on write\n");

	if(!(s = sfgetr(f,'\n',1)) )
		terror("Should have gotten 456789\n"); 
	if(strcmp(s,"456789") != 0)
		terror("Wrong data2\n");

	if(!(s = sfgetr(f,'\n',1)) )
		terror("Should have gotten aaa\n"); 
	if(strcmp(s,"aaa") != 0)
		terror("Wrong data3\n");

	sfclose(f);
	
	TSTEXIT(0);
}
Esempio n. 4
0
static void
outline(Fmt_t* fp)
{
	register char*	cp = fp->outbuf;
	int		n = 0;
	int		c;
	int		d;

	if (!fp->outp)
		return;
	while (fp->outp[-1] == ' ')
		fp->outp--;
	*fp->outp = 0;
	while (*cp++ == ' ')
		n++;
	if (n >= TABSZ)
	{
		n /= TABSZ;
		cp = &fp->outbuf[TABSZ*n];
		while (n--)
			*--cp = '\t';
	}
	else
		cp = fp->outbuf;
	fp->nwords = 0;
	if (!isoption(fp, 'o'))
		sfputr(fp->out, cp, '\n');
	else if (*cp)
	{
		n = fp->indent;
		if (*cp != '[')
		{
			if (*cp == ' ')
				cp++;
			n += INDENT;
		}
		while (n--)
			sfputc(fp->out, ' ');
		if (fp->quote)
		{
			if ((d = (fp->outp - cp)) <= 0)
				c = 0;
			else if ((c = fp->outp[-1]) == 'n' && d > 1 && fp->outp[-2] == '\\')
				c = '}';
			sfprintf(fp->out, "\"%s%s\"\n", cp, c == ']' || c == '{' || c == '}' ? "" : " ");
		}
		else
			sfputr(fp->out, cp, '\n');
		if (fp->nextdent)
		{
			fp->indent += fp->nextdent;
			fp->endbuf -= fp->nextdent;
			fp->nextdent = 0;
		}
	}
	fp->outp = 0;
}
Esempio n. 5
0
static void
print(register Sfio_t* sp, register char* name, char* delim)
{
	if (mbwide())
		sfputr(sp, name, -1);
	else
	{
#if CC_NATIVE != CC_ASCII
		register int		c;
		register unsigned char*	n2a;
		register unsigned char*	a2n;
		register int		aa;
		register int		as;

		n2a = ccmap(CC_NATIVE, CC_ASCII);
		a2n = ccmap(CC_ASCII, CC_NATIVE);
		aa = n2a['A'];
		as = n2a[' '];
		while (c = *name++)
		{
			c = n2a[c];
			if (c & 0200)
			{
				c &= 0177;
				sfputc(sp, '?');
			}
			if (c < as)
			{
				c += aa - 1;
				sfputc(sp, '^');
			}
			c = a2n[c];
			sfputc(sp, c);
		}
#else
		register int		c;

		while (c = *name++)
		{
			if (c & 0200)
			{
				c &= 0177;
				sfputc(sp, '?');
			}
			if (c < ' ')
			{
				c += 'A' - 1;
				sfputc(sp, '^');
			}
			sfputc(sp, c);
		}
#endif
	}
	if (delim)
		sfputr(sp, delim, -1);
}
Esempio n. 6
0
tmain()
{
	char	buf[100];
	Sfio_t	*fp;
	int	i;
	char	*s;

	if(!(fp = sftmp(8)))
		terror("Can't open temp file");

	sfset(fp,SF_LINE,1);
	for(i = 0; i < 1000; ++i)
	{	sfsprintf(buf,sizeof(buf),"Number: %d",i);
		if(sfputr(fp,buf,'\n') <= 0)
			terror("Writing %s",buf);
	}

	sfseek(fp,(Sfoff_t)0,0);

	for(i = 0; i < 1000; ++i)
	{	sfsprintf(buf,sizeof(buf),"Number: %d",i);
		if(!(s = sfgetr(fp,'\n',1)))
			terror("Reading %s",buf);
		if(strcmp(s,buf) != 0)
			terror("Input=%s, Expect=%s",s,buf);
	}

	sfseek(fp,(Sfoff_t)0,0);
	s = sfgetr(fp,'\0',1);
	if(s)
		terror("Expecting a null string");
	s = sfgetr(fp,'\0',-1);
	if(!s)
		terror("Expecting a non-null string");
	if(sfvalue(fp) != sfsize(fp))
		terror("Wrong size");

	sfclose(fp);
	if(!(fp = sfnew(0, buf, 12, 1, SF_WRITE)) )
		terror("Opening a test stream");
	sfsetbuf(fp, buf, 12);
	sfset(fp, SF_LINE, 0);
	sfdisc(fp, &Disc);
	if(sfputr(fp, "0123456789", '\n') != 11)
		terror("Sfputr failed1");
	if(sfputr(fp, "0", -1) != 1)
		terror("Sfputr failed2");
	if(sfputr(fp, "1", -1) != 1)
		terror("Sfputr failed3");

	texit(0);
}
Esempio n. 7
0
File: deparse.c Progetto: att/ast
static_fn void p_arg(const struct argnod *arg, int endchar, int opts) {
    int flag = -1;

    do {
        if (!arg->argnxt.ap) {
            flag = endchar;
        } else if (opts & PRE) {  // case alternation lists in reverse order
            p_arg(arg->argnxt.ap, '|', opts);
            flag = endchar;
        } else if (opts) {
            flag = ' ';
        }
        const char *cp = arg->argval;
        if (*cp == 0 && (arg->argflag & ARG_EXP) && arg->argchn.ap) {
            int c = (arg->argflag & ARG_RAW) ? '>' : '<';
            sfputc(outfile, c);
            sfputc(outfile, '(');
            p_tree((Shnode_t *)arg->argchn.ap, 0);
            sfputc(outfile, ')');
        } else if (*cp == 0 && opts == POST && arg->argchn.ap) {  // compound assignment
            struct fornod *fp = (struct fornod *)arg->argchn.ap;
            sfprintf(outfile, "%s=(\n", fp->fornam);
            sfnputc(outfile, '\t', ++level);
            p_tree(fp->fortre, 0);
            if (--level) sfnputc(outfile, '\t', level);
            sfputc(outfile, ')');
        } else if ((arg->argflag & ARG_RAW) && (cp[1] || (*cp != '[' && *cp != ']'))) {
            cp = sh_fmtq(cp);
        }
        sfputr(outfile, cp, flag);
        if (flag == '\n') begin_line = 1;
        arg = arg->argnxt.ap;
    } while ((opts & POST) && arg);
}
Esempio n. 8
0
int
b_logname(int argc, char** argv, void* context)
{
	register char*	logname;

	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
	for (;;)
	{
		switch (optget(argv, usage))
		{
		case ':':
			error(2, "%s", opt_info.arg);
			continue;
		case '?':
			error(ERROR_usage(2), "%s", opt_info.arg);
			continue;
		}
		break;
	}
	if (error_info.errors)
		error(ERROR_usage(2), "%s", optusage(NiL));
	if (!(logname = getlogin()))
		logname = fmtuid(getuid());
	sfputr(sfstdout, logname, '\n');
	return 0;
}
Esempio n. 9
0
extern char*	getpass(const char *prompt)
{
	struct termios told,tnew;
	Sfio_t *iop;
	static char *cp, passwd[32];
	void (*savesig)(int);
	if(!(iop = sfopen((Sfio_t*)0, "/dev/tty", "r")))
		return(0);
	if(tcgetattr(sffileno(iop),&told) < 0)
		return(0);
	interrupt = 0;
	tnew = told;
	tnew.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
	if(tcsetattr(sffileno(iop),TCSANOW,&tnew) < 0)
		return(0);
	savesig = signal(SIGINT, handler);
	sfputr(sfstderr,prompt,-1);
	if(cp = sfgetr(iop,'\n',1))
		strncpy(passwd,cp,sizeof(passwd)-1);
	tcsetattr(sffileno(iop),TCSANOW,&told);
	sfputc(sfstderr,'\n');
	sfclose(iop);
	signal(SIGINT, savesig);
	if(interrupt)
		kill(getpid(),SIGINT);
	return(cp?passwd:0);
}
Esempio n. 10
0
static void
init(void)
{
	register Sfio_t**	ss;
	register int		c;

	ed.interactive = -1;
	ed.msg = sfstdout;
	ed.all = BLOCK_LINE;
	ed.page.size = BREAK_PAGE;
	ed.redisc.re_version = REG_VERSION;
	ed.redisc.re_errorf = errorf;
	ed.re.re_disc = &ed.redisc;
	ed.reflags = REG_DISCIPLINE|REG_DELIMITED;
	if (!conformance(0, 0))
		ed.reflags |= REG_LENIENT;
	ed.verbose = 1;
	for (c = 0; c < elementsof(signals); c++)
		if (signal(signals[c], interrupt) == SIG_IGN)
			signal(signals[c], SIG_IGN);
	for (ss = (Sfio_t**)&ed.buffer; ss < (Sfio_t**)(((char*)&ed.buffer) + sizeof(ed.buffer)); ss++) {
		if (!(*ss = sfstropen()))
			error(ERROR_SYSTEM|3, "cannot initialize internal buffer");
		sfputc(*ss, 0);
		sfstrseek(*ss, 0, SEEK_SET);
	}
	sfputr(ed.buffer.help, "?", 0);
	if (!(ed.zero = newof(NiL, Line_t, ed.all, 0)))
		error(ERROR_SYSTEM|3, "out of space [zero]");
}
Esempio n. 11
0
static void p_comlist(const struct dolnod *dol,int endchar)
{
	register char *cp, *const*argv;
	register int flag = ' ', special;
	argv = dol->dolval+ARG_SPARE;
	cp = *argv;
	special = (*cp=='[' && cp[1]==0);
	do
	{
		if(cp)
			argv++;
		else
			cp = "";
		if(*argv==0)
		{
			if((flag=endchar)=='\n')
				begin_line = 1;
			special = (*cp==']' && cp[1]==0);
		}
		sfputr(outfile,special?cp:sh_fmtq(cp),flag);
		special = 0;
	}
	while(cp  = *argv);
	return;
}
Esempio n. 12
0
static void
global(int sense, int query)
{
	register char*		s;
	register int		c;
	register Line_t*	a1;

	if (ed.global)
		error(2, "recursive global not allowed");
	setwide();
	squeeze(ed.dol > ed.zero);
	compile();
	if (query)
		newline();
	else {
		s = getrec(ed.buffer.global, '\n', REC_SPLICE|REC_TERMINATE);
		if (s[0] == '\n' && !s[1])
			sfputr(ed.buffer.global, "p\n", 0);
	}
	for (a1 = ed.zero; a1 <= ed.dol; a1++) {
		a1->offset &= ~LINE_GLOBAL;
		if (a1 >= ed.addr1 && a1 <= ed.addr2 && execute(a1, 0) == sense)
			a1->offset |= LINE_GLOBAL;
	}

	/* special case: g/.../d (avoid n^2 algorithm) */

	if (!query && s[0] == 'd' && s[1] == '\n' && !s[2])
		gdelete();
	else {
		for (a1 = ed.zero; a1 <= ed.dol; a1++) {
			if (a1->offset & LINE_GLOBAL) {
				a1->offset &= ~LINE_GLOBAL;
				ed.dot = a1;
				if (query) {
					putrec(lineget(a1->offset));
					if ((c = getchr()) == EOF)
						break;
					else if (c == '\n')
						continue;
					else if (c == '&') {
						newline();
						if (!*(ed.global = sfstrbase(ed.buffer.query)))
							error(2, "no saved command");
					}
					else {
						ed.peekc = c;
						ed.global = getrec(ed.buffer.query, '\n', REC_TERMINATE);
					}
				}
				else
					ed.global = s;
				commands();
				a1 = ed.zero;
			}
		}
	}
}
Esempio n. 13
0
int
main(int argc, char** argv)
{
	register Coshell_t*	sp;
	register char*		s;
	register int		op;
	Coattr_t		attr;

	NoP(argc);
	NoP(argv);
	error(-1, "debug");
	init();
	while ((s = sfgetr(sfstdin, '\n', 0)) && sfvalue(sfstdin) > 1) switch (s[sfvalue(sfstdin) - 1] = 0, op = *s == ':' ? (s++, *s++) : '?')
	{
	case '#':
		break;
	case '?':
	case ':':
		attributes(s, &attr, NiL);
		sp = state.shell;
		do
		{
			if (match(sp, &attr, 0))
			{
				if (op == '?') sfputr(sfstdout, sp->name, '\n');
				else
				{
					sfputr(sfstdout, sp->name, '\t');
					sfputr(sfstdout, sp->misc, '\n');
				}
			}
		} while ((sp = sp->next) != state.shell);
		break;
	case '=':
		if (!search(SET, s, NiL, NiL))
			error(2, "%s: invalid host name", s);
		break;
	default:
		error(2, "`%s': invalid command", s - 2);
		break;
	}
	exit(0);
}
Esempio n. 14
0
MAIN()
{
	char	*s = "1234567890\n";
	Sfoff_t	n, i;
	Sfio_t	*f;
	char	buf[1024];
	char*	addr;

	if(sfopen(sfstdout,tstfile(0),"w+") != sfstdout)
		terror("Opening output file\n");
	for(i = 0; i < 10000; ++i)
		if(sfputr(sfstdout,s,-1) < 0)
			terror("Writing data\n");

	if(!(f = sfopen((Sfio_t*)0,tstfile(1),"w")))
		terror("Opening output file \n");

	sfseek(sfstdout,(Sfoff_t)0,0);
	if((n = sfmove(sfstdout,f,(Sfoff_t)SF_UNBOUND,'\n')) != i)
		terror("Move %d lines, Expect %d\n",n,i);

	sfseek(sfstdout,(Sfoff_t)0,0);
	sfseek(f,(Sfoff_t)0,0);
	sfsetbuf(sfstdout,buf,sizeof(buf));
	if((n = sfmove(sfstdout,f,(Sfoff_t)SF_UNBOUND,'\n')) != i)
		terror("Move %d lines, Expect %d\n",n,i);

	sfopen(sfstdin,tstfile(0),"r");
	sfopen(sfstdout,tstfile(1),"w");
	sfmove(sfstdin,sfstdout,(Sfoff_t)SF_UNBOUND,-1);
	if(!sfeof(sfstdin))
		terror("Sfstdin is not eof\n");
	if(sferror(sfstdin))
		terror("Sfstdin is in error\n");
	if(sferror(sfstdout))
		terror("Sfstdout is in error\n");

	sfseek(sfstdin,(Sfoff_t)0,0);
	sfseek(sfstdout,(Sfoff_t)0,0);
	sfsetbuf(sfstdin,buf,sizeof(buf));

	addr = (char*)sbrk(0);
	sfmove(sfstdin,sfstdout,(Sfoff_t)((unsigned long)(~0L)>>1),-1);
	if((ssize_t)((char*)sbrk(0)-addr) > 256*1024)
		terror("Too much space allocated in sfmove\n");

	if(!sfeof(sfstdin))
		terror("Sfstdin is not eof2\n");
	if(sferror(sfstdin))
		terror("Sfstdin is in error2\n");
	if(sferror(sfstdout))
		terror("Sfstdout is in error2\n");

	TSTEXIT(0);
}
Esempio n. 15
0
File: args.c Progetto: att/ast
static_fn int infof(Opt_t *op, Sfio_t *sp, const char *s, Optdisc_t *dp) {
    UNUSED(op);
    UNUSED(dp);
#if SHOPT_BASH
    Shell_t *shp = sh_getinterp();
    extern const char sh_bash1[], sh_bash2[];

    if (strcmp(s, "bash1") == 0) {
        if (sh_isoption(shp, SH_BASH)) sfputr(sp, sh_bash1, -1);
    } else if (strcmp(s, "bash2") == 0) {
        if (sh_isoption(shp, SH_BASH)) sfputr(sp, sh_bash2, -1);
    } else if (*s == ':' && sh_isoption(shp, SH_BASH)) {
        sfputr(sp, s, -1);
    } else
#endif
        if (*s != ':') {
        sfputr(sp, sh_set, -1);
    }
    return 1;
}
Esempio n. 16
0
void exwarn(const char *format, ...)
{
    Sfio_t *sp;

    if (expr.program->disc->errorf && (sp = sfstropen())) {
	va_list ap;
	char *s;
	char buf[64];

	excontext(expr.program, buf, sizeof(buf));
	sfputr(sp, buf, -1);
	sfputr(sp, "\n -- ", -1);
	va_start(ap, format);
	sfvprintf(sp, format, ap);
	va_end(ap);
	s = sfstruse(sp);
	(*expr.program->disc->errorf) (expr.program, expr.program->disc,
				       ERROR_WARNING, "%s", s);
	sfclose(sp);
    }
}
Esempio n. 17
0
static ssize_t
helpwrite(int fd, const void* buf, size_t len)
{
	ssize_t	n;

	NoP(fd);
	n = ed.help ? sfwrite(sfstderr, buf, len) : ed.verbose ? sfputr(ed.msg, "?", '\n') : 0;
	sfstrseek(ed.buffer.help, 0, SEEK_SET);
	sfwrite(ed.buffer.help, buf, len - 1);
	sfputc(ed.buffer.help, 0);
	return n;
}
Esempio n. 18
0
File: history.c Progetto: att/ast
void hist_list(History_t *hp, Sfio_t *outfile, off_t offset, int last, const char *nl) {
    int oldc = 0;
    int c;

    if (offset < 0 || !hp) {
        sfputr(outfile, sh_translate(e_unknown), '\n');
        return;
    }
    sfseek(hp->histfp, offset, SEEK_SET);
    while ((c = sfgetc(hp->histfp)) != EOF) {
        if (c && oldc == '\n') {
            sfputr(outfile, nl, -1);
        } else if (last && (c == 0 || (c == '\n' && oldc == last))) {
            return;
        } else if (oldc) {
            sfputc(outfile, oldc);
        }
        oldc = c;
        if (c == 0) return;
    }
    return;
}
Esempio n. 19
0
static int infof(Opt_t* op, Sfio_t* sp, const char* s, Optdisc_t* dp)
{
#if SHOPT_BASH
	extern const char sh_bash1[], sh_bash2[];
	if(strcmp(s,"bash1")==0) 
	{
		if(sh_isoption(SH_BASH))
			sfputr(sp,sh_bash1,-1);
	}
	else if(strcmp(s,"bash2")==0)
	{
		if(sh_isoption(SH_BASH))
			sfputr(sp,sh_bash2,-1);
	}
	else if(*s==':' && sh_isoption(SH_BASH))
		sfputr(sp,s,-1);
	else
#endif
	if(*s!=':')
		sfputr(sp,sh_set,-1);
	return(1);
}
Esempio n. 20
0
int
pzheadprint(register Pz_t* pz, register Sfio_t* op, int parts)
{
	register Pzpart_t*	pp;
	char			t;

	if (pz->flags & PZ_FORCE)
		sfprintf(op, "# fixed record input\n");
	else
	{
		sfprintf(op, "# pzip %d.%d partition\n", pz->major, pz->minor);
		if (pz->disc->comment)
			sfprintf(op, "# %s\n", pz->disc->comment);
		sfprintf(op, "# window %I*u\n", sizeof(pz->win), pz->win);
		if (pz->prefix.count)
		{
			sfprintf(op, "\nprefix=%I*u", sizeof(pz->prefix.count), pz->prefix.count);
			if (pz->prefix.terminator >= 0)
			{
				t = pz->prefix.terminator;
				sfprintf(op, "*%s\n", fmtquote(&t, "'", "'", 1, FMT_ALWAYS));
			}
			else
				sfputc(op, '\n');
		}
		if (pz->headoptions || pz->det)
		{
			sfputc(op, '\n');
			if (pz->headoptions)
				sfputr(op, pz->headoptions, '\n');
			if (pz->det)
			{
				sfwrite(op, sfstrbase(pz->det), sfstrtell(pz->det));
				sfputc(op, '\n');
			}
		}
	}
	if (parts)
	{
		pp = pz->partdict ? (Pzpart_t*)dtfirst(pz->partdict) : pz->part;
		while (pp)
		{
			if (pzpartprint(pz, pp, op))
				return -1;
			if (!pz->partdict)
				break;
			pp = (Pzpart_t*)dtnext(pz->partdict, pp);
		}
	}
	return sferror(op) ? -1 : 0;
}
Esempio n. 21
0
File: tleak.c Progetto: gitpan/sfio
main()
{
	if(sfopen(sfstdout,"xxx","w") != sfstdout)
		terror("Can't open xxx to write\n");
	if(sfputr(sfstdout,"012345678\n",-1) != 10)
		terror("Can't write to xxx\n");
	sfsync(sfstdout);

	if(sfopen(sfstdout,"yyy","w") != sfstdout)
		terror("Can't open yyy to write\n");
	sfclose(sfstdout);

	return 0;
}
Esempio n. 22
0
static off_t
lineput(char* s)
{
	off_t	off;

	modify();
	off = ed.tmpoff;
	if (sfseek(ed.tmp, off, SEEK_SET) != off)
		error(ERROR_SYSTEM|2, "temp file write seek error");
	if (sfputr(ed.tmp, s, 0) < 0)
		error(ERROR_SYSTEM|2, "temp file write error at offset %I*d", sizeof(off), off);
	if ((ed.tmpoff = sfseek(ed.tmp, (off_t)0, SEEK_CUR)) == (off_t)-1)
		error(ERROR_SYSTEM|2, "temp file tell error");
	return off;
}
Esempio n. 23
0
File: deparse.c Progetto: att/ast
// Output `here` documents.
static_fn void here_body(const struct ionod *iop) {
    Sfio_t *infile;
#if 0
    // TODO: Figure out if this should be enabled. Originally excluded via `#ifdef xxx`.
    if (iop->iolink) here_body((struct inode *)iop->iolink);
    iop->iolink = 0;
#endif
    if (iop->iofile & IOSTRG) {
        infile = sfnew(NULL, iop->ioname, iop->iosize, -1, SF_STRING | SF_READ);
    } else {
        sfseek(infile = sh.heredocs, iop->iooffset, SEEK_SET);
    }
    sfmove(infile, outfile, iop->iosize, -1);
    if (iop->iofile & IOSTRG) sfclose(infile);
    sfputr(outfile, iop->iodelim, '\n');
}
Esempio n. 24
0
static int
print(Expr_t* ex, Exnode_t* expr, void* env, Sfio_t* sp)
{
	register Print_t*	x;
	Extype_t		v;
	Fmt_t			fmt;

	if (!sp)
	{
		v = eval(ex, expr->data.print.descriptor, env);
		if (v.integer < 0 || v.integer >= elementsof(ex->file) || !(sp = ex->file[v.integer]) && !(sp = ex->file[v.integer] = sfnew(NiL, NiL, SF_UNBOUND, v.integer, SF_READ|SF_WRITE)))
		{
			exerror("printf: %d: invalid descriptor", v.integer);
			return 0;
		}
	}
	memset(&fmt, 0, sizeof(fmt));
	fmt.fmt.version = SFIO_VERSION;
	fmt.fmt.extf = prformat;
	fmt.expr = ex;
	fmt.env = env;
	x = expr->data.print.args;
	if (x->format)
		do
		{
			if (x->arg)
			{
				fmt.fmt.form = x->format;
				fmt.args = x;
				sfprintf(sp, "%!", &fmt);
			}
			else
				sfputr(sp, x->format, -1);
		} while (x = x->next);
	else
	{
		v = eval(ex, x->arg->data.operand.left, env);
		fmt.fmt.form = v.string;
		fmt.actuals = x->arg;
		sfprintf(sp, "%!", &fmt);
		if (fmt.actuals->data.operand.right)
			exerror("printf: \"%s\": too many arguments", fmt.fmt.form);
	}
	if (fmt.tmp)
		sfstrclose(fmt.tmp);
	return 1;
}
Esempio n. 25
0
/*
 * output here documents
 */
static void here_body(register const struct ionod *iop)
{
	Sfio_t *infile;
#ifdef xxx
	if(iop->iolink)
		here_body((struct inode*)iop->iolink);
	iop->iolink = 0;
#endif
	if(iop->iofile&IOSTRG)
		infile = sfnew((Sfio_t*)0,iop->ioname,iop->iosize,-1,SF_STRING|SF_READ);
	else
		sfseek(infile=sh.heredocs,iop->iooffset,SEEK_SET);
	sfmove(infile,outfile,iop->iosize,-1);
	if(iop->iofile&IOSTRG)
		sfclose(infile);
	sfputr(outfile,iop->iodelim,'\n');
}
Esempio n. 26
0
static void
join(void)
{
	register Line_t*	a1;
	char*			s;

	nonzero();
	sfstrseek(ed.buffer.work, 0, SEEK_SET);
	for (a1 = ed.addr1; a1 <= ed.addr2;)
		sfputr(ed.buffer.work, lineget((a1++)->offset), -1);
	a1 = ed.dot = ed.addr1;
	if (!(s = sfstruse(ed.buffer.work)))
		error(ERROR_SYSTEM|3, "out of space");
	replace(a1, s);
	if (a1 < ed.addr2)
		rdelete(a1 + 1, ed.addr2);
}
Esempio n. 27
0
static void
putfile(void)
{
	register Line_t*	a1;
	register int		n;

	ed.bytes = 0;
	ed.lines = 0;
	a1 = ed.addr1;
	do {
		if ((n = sfputr(ed.iop, lineget((a1++)->offset), '\n')) < 0)
			error(ERROR_SYSTEM|2, "write error");
		ed.bytes += n;
		ed.lines++;
	} while (a1 <= ed.addr2);
	if (sfsync(ed.iop))
		error(ERROR_SYSTEM|2, "write error");
}
Esempio n. 28
0
File: deparse.c Progetto: att/ast
static_fn void p_comlist(const struct dolnod *dol, int endchar) {
    char *cp, *const *argv;
    int flag = ' ', special;

    argv = dol->dolval + ARG_SPARE;
    cp = *argv++;
    special = strcmp(cp, "[") == 0;
    do {
        if (*argv == 0) {
            flag = endchar;
            if (flag == '\n') begin_line = 1;
            special = (*cp == ']' && cp[1] == 0);
        }
        sfputr(outfile, special ? cp : sh_fmtq(cp), flag);
        special = 0;
        cp = *argv++;
    } while (cp);
}
Esempio n. 29
0
static void
putrec(register char* s)
{
	register int	n;
	register char*	t;

	if ((ed.print & REG_SUB_LIST) && (t = fmtesc(s))) {
		s = t;
		n = strlen(s);
		while (n > BREAK_LINE) {
			n -= BREAK_LINE;
			sfprintf(ed.msg, "%-*.*s\\\n", BREAK_LINE, BREAK_LINE, s);
			s += BREAK_LINE;
		}
		sfprintf(ed.msg, "%s$\n", s);
	}
	else
		sfputr(ed.msg, s, '\n');
}
Esempio n. 30
0
static void
context(register Sfio_t* sp, register Error_context_t* cp)
{
	if (cp->context)
		context(sp, CONTEXT(cp->flags, cp->context));
	if (!(cp->flags & ERROR_SILENT))
	{
		if (cp->id)
			print(sp, cp->id, NiL);
		if (cp->line > ((cp->flags & ERROR_INTERACTIVE) != 0))
		{
			if (cp->file)
				sfprintf(sp, ": \"%s\", %s %d", cp->file, ERROR_translate(NiL, NiL, ast.id, "line"), cp->line);
			else
				sfprintf(sp, "[%d]", cp->line);
		}
		sfputr(sp, ": ", -1);
	}
}