Commit graph

48 commits

Author SHA1 Message Date
Ori Bernstein 826d64c11c rc: squelch 'Write error' warning
When writing to a large variable in /env, we
spam 'Write error', in spite of the env var
working. This is new behavior, as of commit
b90036a062.

It produces a lot of scary, noisy warnings,
which are probably bogus.
2022-05-30 21:55:37 -04:00
cinap_lenrek 755880b19f rc: fix globbing with lists (thanks qwx)
Pattern matching with lists no longer works:

	; ls /tmp/*.c
	/tmp/npage.c
	/tmp/pagedebug.c
	/tmp/pageold.c
	/tmp/scheduler.c
	/tmp/writeimagetest.c
	; ls /tmp/^(*.c)
	ls: /tmp/*.c: '/tmp/*.c' directory entry not found
	; 9fs dump
	; bind /n/dump/2021/1002/amd64/bin/rc /bin/rc
	; rc
	; ls /tmp/^(*.c)
	/tmp/npage.c
	/tmp/pagedebug.c
	/tmp/pageold.c
	/tmp/scheduler.c
	/tmp/writeimagetest.c

the fix:

we have to propagate the glob attribute thru lists
as well. before it was only handled for single words
and propagated thru concatenations...

the Xglob instruction now works on list, and we
propagate the glob attribute thru PAREN and WORDS
and ARGLIST nodes.

also, avoid using negative numbers for the Tree.glob
field as char might be unsigned on some targets.
2022-02-16 18:07:21 +00:00
cinap_lenrek 538b810712 rc: fix pwrd() regression, forgot <= ' ' case from needsrcquote()... sorry :( 2022-01-10 17:41:46 +00:00
cinap_lenrek 369cba5f93 rc: read heredoc when receiving '\n' (thanks Eckard Brauer)
Eckard's test case:

cat <<! | cat
asdf
!

The issue is that we have to continue parsing until we see
the '\n' before consuming the here document.

So we revert to the old approach of having two functions:

heredoc() which remembers if we'v seen a heredoc redirection
and a second readhere() function that reads the doc from
the lexers input and sets Tree.str on thee REDIR node.
2022-01-07 20:50:00 +00:00
cinap_lenrek 3568e27ec8 rc: only have single instance of a symbol, extern in header (thanks mcf) 2022-01-04 00:19:36 +00:00
cinap_lenrek 699d2e0ed9 rc: simplify Makefile, use yacc default rule (thanks k0ga) 2022-01-03 22:48:44 +00:00
cinap_lenrek 189731aad0 rc: make it portable (for UNIX)
Fixup remaining Plan9 dependencies (chartorune()).
Add Makefile for UNIX-like systems (tested with Linux and APE).
Make error printing consistent, use Errstr() explicitely.
Get rid of NSTATUS buffer limit, just malloc it.
2022-01-03 18:41:48 +00:00
cinap_lenrek c51a5cfa06 rc: Xerror is not a instruction, remove from pfnc 2022-01-03 18:22:29 +00:00
cinap_lenrek 99d54e420e rc: add Xhereq instruction to trace 2022-01-02 03:35:50 +00:00
cinap_lenrek 11d573d7f9 rc: rstr() shouldnt skip trailing NUL bytes (thanks ori) 2022-01-02 03:33:34 +00:00
cinap_lenrek 2025214dfc rc: fix here document handling with quoted end-marker (thanks sigrid)
when end marker is quoted, we should not substitute.
also, pcmd() needs to print the end marker without quotes.
2021-12-31 23:26:59 +00:00
cinap_lenrek b00b3d3739 rc(1): fix synopsis 2021-12-31 16:08:30 +00:00
cinap_lenrek 9773f55318 rc: add err != nil check for early exit 2021-12-31 16:05:28 +00:00
cinap_lenrek b90036a062 rc: fix everything
Untangle the lexer and interpreter thread state.

Fix the file and line number error reporting, getting rid of
Xsrcfile instruction, as the whole code block can only come
from a single file, stuff the source file in slot[1] of the
code block instead.

Remove limitations for globber (path element limits)
and be more intelligent about handling globbing by
inserting Xglob instruction only when needed and not
run it over every Xsimple argument list.

Remove fragile ndot magic and make it explicit by adding
the -q flag to . builtin command.

Add -b flag for full compilation.

Make exitnext() smart, so we can speculate thru rcmain and
avoid the fork().

Get rid of all print(2) format functions and use io
instead.

Improve the io library, adding rstr() to handle tokenization,
which allows us to look ahead in the already read buffer
for the terminators, avoiding alot of string copies.

Auto indent pcmd(), to make line number reporting more usefull.

Implement here documents properly, so they can work everywhere.
2021-12-31 15:27:10 +00:00
Igor Böhm c7775b365e rc: fix leaking runq->cmdfile
To reproduce run the following on a terminal:
<snip>
cpu% leak -s `{pstree | grep termrc | sed 1q | awk '{print $1}'}
src(0x00209a82); // 12
src(0x0020b2a6); // 1
cpu% acid `{pstree | grep termrc | sed 1q | awk '{print $1}'}
/proc/358/text:amd64 plan 9 executable
/sys/lib/acid/port
/sys/lib/acid/amd64
acid: src(0x0020b2a6)
/sys/src/cmd/rc/plan9.c:169
 164		if(runq->argv->words == 0)
 165			poplist();
 166		else {
 167			free(runq->cmdfile);
 168			int f = open(runq->argv->words->word, 0);
>169			runq->cmdfile = strdup(runq->argv->words->word);
 170			runq->lexline = 1;
 171			runq->pc--;
 172			popword();
 173			if(f>=0) execcmds(openfd(f));
 174		}
acid:
</snap>

Another `runq->cmdfile` leak is present here (captured on a cpu server):
<snip>
277         ├listen [tcp * /rc/bin/service <nil>]
321         │├listen [/net/tcp/2 tcp!*!80]
322         │├listen [/net/tcp/3 tcp!*!17019]
324         ││└rc [/net/tcp/5 tcp!185.64.155.70!3516]
334         ││ ├rc -li
382         ││ │└pstree
336         ││ └rc
338         ││  └cat
323         │└listen [/net/tcp/4 tcp!*!17020]
278         ├listen [tcp * /rc/bin/service.auth <nil>]
320         │└listen [/net/tcp/1 tcp!*!567]
381         └closeproc
cpu% leak -s 336
src(0x00209a82); // 2
src(0x002051d2); // 1
cpu% acid 336
/proc/336/text:amd64 plan 9 executable
/sys/lib/acid/port
/sys/lib/acid/amd64
acid: src(0x002051d2)
/sys/src/cmd/rc/exec.c:1056
 1051
 1052	void
 1053	Xsrcfile(void)
 1054	{
 1055		free(runq->cmdfile);
>1056		runq->cmdfile = strdup(runq->code[runq->pc++].s);
 1057	}
acid:
</snap>

These leaks happen because we do not free cmdfile on all execution paths
where `Xreturn()` is invoked. In `/sys/src/cmd/rc/exec.c:/^Xreturn`

<snip>
void
Xreturn(void)
{
	struct thread *p = runq;
	turfredir();
	while(p->argv) poplist();
	codefree(p->code);
	runq = p->ret;
	free(p);
	if(runq==0)
		Exit(getstatus());
}
</snip>

Note how the function `Xreturn()` frees a heap allocated instance of type
`thread` with its members *except* the `cmdfile` member.

On some code paths where `Xreturn()` is called there is an attempt to free
`cmdfile`, however, there are some code paths where `Xreturn()` is called
where `cmdfile` is not freed, leading to a leak.

The attached patch calls `free(p->cmdfile)` in `Xreturn()` to avoid leaking
memory and handling the free in one place.

After applying the patch this particular leak is removed. There are still
other leaks in rc:

<snip>
277         ├listen [tcp * /rc/bin/service <nil>]
321         │├listen [/net/tcp/2 tcp!*!80]
322         │├listen [/net/tcp/3 tcp!*!17019]
324         ││└rc [/net/tcp/5 tcp!185.64.155.70!3516]
334         ││ ├rc -li
382         ││ │└pstree
336         ││ └rc
338         ││  └cat
323         │└listen [/net/tcp/4 tcp!*!17020]
278         ├listen [tcp * /rc/bin/service.auth <nil>]
320         │└listen [/net/tcp/1 tcp!*!567]
381         └closeproc
cpu% leak -s 336
src(0x00209a82); // 2
src(0x002051d2); // 1
cpu% acid 336
/proc/336/text:amd64 plan 9 executable
/sys/lib/acid/port
/sys/lib/acid/amd64
acid: src(0x00209a82)
/sys/src/cmd/rc/subr.c:9
 4	#include "fns.h"
 5
 6	void *
 7	emalloc(long n)
 8	{
>9		void *p = malloc(n);
 10		if(p==0)
 11			panic("Can't malloc %d bytes", n);
 12		return p;
 13	}
 14
</snap>

To help fixing those leaks emalloc(…) and erealloc(…) have been amended to use
setmalloctag(…) and setrealloctag(…). The actual fixes for other reported leaks
are *not* part of this merge and will follow.
2021-11-10 13:01:38 +00:00
Ori Bernstein 7ea6821a83 rc: revert 2f8a59f4b5
this patch doesn't pull its weight; it's not worth it.
2021-09-08 14:24:25 +00:00
Ori Bernstein 2f8a59f4b5 rc: add subshell-function syntax
fn foo @{bar} is now equivalent to
fn foo {@{bar}}. As a side effect,
this disallows creating functions
named after keywords without first
quoting them.
2021-07-08 21:35:34 +00:00
Ori Bernstein 759aafa007 rc: skip arguments to Xsrcline, Xsrcfile in codefree
We weren't correctly skipping the location operators
in codefree. This would mostly be work, but sometimes
you'd get unlucky and have one of the argmuents mismatch,
and that would lead to an invalid free.

This correctly skips the args in codefree.
2021-06-25 23:10:07 +00:00
Ori Bernstein 5770332282 rc: correct line numbers
When loading a file using ".", we could
end up with our line numbers thrown off
due to the mutation of lexline. Putting
lexline into the runq beside the file
that we're reading from causes it to get
pushed and popped correctly, so that we
no longer lose track of our location.
2021-06-22 23:40:11 +00:00
Ori Bernstein d75d842cf5 rc: show line numbers on error
This change provides a location for errors
like 'null list in concatenation'.
2020-11-01 11:56:26 -08:00
cinap_lenrek 53800208bd rc: avoid stat calls for directory globbing
On Plan9, we can count on Readdir() onlydirs argument
to work, which allows us to avoid stating every single
file to see if it is a directory.
2020-08-04 16:21:37 +02:00
cinap_lenrek a48b462e44 rc: avoid forking for final command that has variable assignments (to get $apid right)
basically, we want the following commands to print
the same pid twice:

rc -c 'cat /dev/pid &;echo $apid'

vs:

rc -c 'a=1 cat /dev/pid &;echo $apid'

basically, Xsimple() calls exitnext() to determine if
a simple command should be promoted to exec, by peeking
ahead into the code and searching for Xexit instruction.

Xexit might not follow immediately after the Xsimple
instruction because of redirections, which exitnext()
would skip.

but it would not skip the Xunlocal instructions that
where added by the variable assignment.
2020-05-30 02:03:18 +02:00
cinap_lenrek 94bd7700aa rc: fix code serialization for PIPEFD (thanks BurnZeZ)
BurnZeZ reported this the other day. It seems like if we have
a pipeline that looks like:

	fn foo{cat < <{echo hi}}

then the '<' will get merged in /env/'fn#foo'. This change
fixes pcmd to add a space. It looks to me like this is the
only token that can get merged this way by pcmd.
2020-04-18 18:20:34 +02:00
Ori Bernstein fc90f7a666 fix heredoc crash
we emitted an error on heredoc tags, but we
continued on, and added a heredoc entry to
the list, with a tag that we couldn't handle.

when processing this heredoc, rc would segfault.

fix: don't add a heredoc to the list on error.
2020-03-09 14:24:02 -07:00
cinap_lenrek 116b075371 rc: clear out redirections on "rfork F" (RFCFDG)
rfork F closes all file descriptors, so we have to
invalidate the redirections as they are now refering
to closed files. not doing so causes the wrong file
descriptors being closed later on as the fd numbers
get reused.
2019-01-20 12:02:03 +01:00
cinap_lenrek b6251bff91 rc: implement $"x in terms of Xdol() and new Xqw() instruction
to get $"1 right, remove Xqdol() and instead implement it in
terms of Xdol() instruction and use the new Xqw() instruction
to quote the resulting list.
2018-11-18 04:56:48 +01:00
cinap_lenrek d81f4d4866 rc: skip searchpath for "", "." and ".." 2018-10-27 18:32:00 +02:00
cinap_lenrek d17a049c5b rc: use searchpath() logic to handle $cdpath 2018-10-26 10:25:23 +02:00
cinap_lenrek 1a55b8d1f0 rc: ignore $cdpath for # device rooted paths (thanks kivik) 2018-10-26 09:52:05 +02:00
cinap_lenrek 2839760066 rc: fix Xpipefd unbalancing the redir stack
Xpipefd wants the pipe descriptor to be closed in turfredir(), so
it pushes the redirection, but this breaks Xpopredir after normal
redirection. so we shuffle the Xpipefd redir to the bottom of the
stack.
2018-09-08 14:49:00 +02:00
cinap_lenrek 3ec84c5d73 rc: implement 9atoms ` split {command} syntax extension 2016-06-30 19:09:27 +02:00
cinap_lenrek 65977417e7 rc: write /dev/wdir after printing the prompt, not after executing "cd" command 2016-06-30 00:45:37 +02:00
cinap_lenrek b15430979d rc: simplify execfinit() / Xrdfn() using the globber to lookup /env/fn'#'* 2016-05-16 19:57:53 +02:00
cinap_lenrek ce7f7d3ab9 rc: fix double close() in addenv() 2016-05-16 18:03:57 +02:00
cinap_lenrek 31a6f6c83d rc: remove duplicate Xrdfn entry from fname[] array 2016-05-16 00:46:46 +02:00
cinap_lenrek 4ad3f4f2fd rc: remove historical unix and win32 ports 2016-05-16 00:37:49 +02:00
cinap_lenrek 45d6bca5f0 rc: fix shift regresison, sorry 2016-05-15 23:09:23 +02:00
cinap_lenrek d0e510b29a rc: remove pointless Memcpy(),Malloc(),Realloc() and efree() wrappers 2016-05-15 19:58:24 +02:00
cinap_lenrek 7717051e3c rc: fix inband globbing bugs, cleanup
add glob information to the word structure so we wont accidently
deglob quoted strings containing the GLOB. we store Globsize(word)
in in word->glob which avoids recalculating that values and the
check if a word should be globbed quick.

globlist() now substitutes the word inplace avoiding the copying
when all words are literals and avoids recursion.

minor cleanups: use list2str() in execeval(), move octal() to
unix.c, remove the (char*) casts to efree().
2016-05-15 19:10:37 +02:00
cinap_lenrek 258fe87faf rc: terminate rc when exec fails, cleanup
The execexec() function should never return, as it irreversably changes
the filedescriptor table for the new program. This means rc's internal
filedesciptors for reading the script get implicitely closed and we cannot
continue the rc interpreter when Execute() fails. So Execute() now sets the
error status, and execexec() runs Xexit() in case Execute() returns.
2016-02-22 22:25:21 +01:00
cinap_lenrek a2f7d03d4e rc: fix slow Xqdol(), avoid recursion in conclist(), estrdup(), avoid copying
Xqdol() used to take quadratic time because of strcat(),
the code isnt really needed as list2str() aready does the
same thing in linear time without the strcat().

add estrdup() which uses emalloc() so allocation error are
catched.

move strdups() of name from callers into newvar().

avoid recursion of conclist(), and avoid copying of word
strings by providing Newword() function which doesnt copy
the word string.
2014-07-23 15:28:37 +02:00
cinap_lenrek 9a3a722dde rc: flush environment variables (update /env) before fork
on races... normal forks will all share the /env environment but
not the in memory variables of rc. so when we would normally fork
whoever does an exec (flush) first will override what the values of the
/env variables are, *independent* of the variables that where
actually modified *in* the process.

when we flush *before* fork, then at least both processes start out
with marked clean in memory variables and the processes will flush
only the things they actually change.
2013-08-20 23:56:15 +02:00
cinap_lenrek 667010554b make all the commands agnostic about Rune width. (from sources) 2013-04-24 20:13:18 +02:00
cinap_lenrek c7522489a3 rc: remove silly efree check 2012-02-08 04:14:32 +01:00
cinap_lenrek f2f19dd888 rc: change plan9 envname limit to 128, cleanup 2012-02-08 04:09:48 +01:00
stanley lieber a504af8833 erik's patches to lift 8192 byte word size restriction 2011-04-04 20:22:56 +00:00
Taru Karttunen a9060cc06b Import sources from 2011-03-30 iso image - lib 2011-03-30 19:35:09 +03:00
Taru Karttunen e5888a1ffd Import sources from 2011-03-30 iso image 2011-03-30 15:46:40 +03:00