├── Makefile.am ├── ChangeLog.O ├── autogen.sh ├── src ├── .gitignore ├── hetio.h ├── bltin │ ├── times.c │ ├── bltin.h │ ├── echo.1 │ └── test.1 ├── miscbltin.h ├── cd.h ├── funcs │ ├── newgrp │ ├── suspend │ ├── login │ ├── kill │ ├── cmv │ ├── dirs │ ├── popd │ └── pushd ├── mail.h ├── init.h ├── show.h ├── Makefile.am ├── myhistedit.h ├── alias.h ├── main.h ├── machdep.h ├── trap.h ├── redir.h ├── mystring.h ├── input.h ├── eval.h ├── arith_yacc.h ├── exec.h ├── expand.h ├── mktokens ├── builtins.def.in ├── system.h ├── options.h ├── mail.c ├── memalloc.h ├── shell.h ├── mkbuiltins ├── parser.h ├── output.h ├── jobs.h ├── nodes.c.pat ├── error.h ├── system.c ├── nodetypes ├── alias.c ├── arith_yylex.c ├── var.h ├── error.c ├── mystring.c ├── cd.c ├── arith_yacc.c ├── memalloc.c ├── main.c └── mksyntax.c ├── COPYING └── configure.ac /Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = src 2 | -------------------------------------------------------------------------------- /ChangeLog.O: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nyuichi/dash/HEAD/ChangeLog.O -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | aclocal \ 4 | && autoheader \ 5 | && automake --add-missing \ 6 | && autoconf 7 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | builtins.[ch] 3 | builtins.def 4 | dash 5 | init.c 6 | mkinit 7 | mknodes 8 | mksignames 9 | mksyntax 10 | nodes.[ch] 11 | signames.c 12 | syntax.[ch] 13 | token.h 14 | -------------------------------------------------------------------------------- /src/hetio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Termios command line History and Editting for NetBSD sh (ash) 3 | * Copyright (c) 1999 4 | * Main code: Adam Rogoyski 5 | * Etc: Dave Cinege 6 | * 7 | * You may use this code as you wish, so long as the original author(s) 8 | * are attributed in any redistributions of the source code. 9 | * This code is 'as is' with no warranty. 10 | * This code may safely be consumed by a BSD or GPL license. 11 | * 12 | * v 0.5 19990328 Initial release 13 | * 14 | * Future plans: Simple file and path name completion. (like BASH) 15 | * 16 | */ 17 | 18 | void hetio_init(void); 19 | int hetio_read_input(int fd); 20 | void hetio_reset_term(void); 21 | 22 | extern int hetio_inter; 23 | -------------------------------------------------------------------------------- /src/bltin/times.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1999 Herbert Xu 3 | * This file contains code for the times builtin. 4 | */ 5 | 6 | #include 7 | #include 8 | #ifdef USE_GLIBC_STDIO 9 | #include 10 | #else 11 | #include "bltin.h" 12 | #endif 13 | #include "system.h" 14 | 15 | int timescmd() { 16 | struct tms buf; 17 | long int clk_tck = sysconf(_SC_CLK_TCK); 18 | 19 | times(&buf); 20 | printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n", 21 | (int) (buf.tms_utime / clk_tck / 60), 22 | ((double) buf.tms_utime) / clk_tck, 23 | (int) (buf.tms_stime / clk_tck / 60), 24 | ((double) buf.tms_stime) / clk_tck, 25 | (int) (buf.tms_cutime / clk_tck / 60), 26 | ((double) buf.tms_cutime) / clk_tck, 27 | (int) (buf.tms_cstime / clk_tck / 60), 28 | ((double) buf.tms_cstime) / clk_tck); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /src/miscbltin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1997 Christos Zoulas. All rights reserved. 3 | * Copyright (c) 1997-2005 4 | * Herbert Xu . All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 3. The name of the author may not be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | int readcmd(int, char **); 30 | int umaskcmd(int, char **); 31 | int ulimitcmd(int, char **); 32 | -------------------------------------------------------------------------------- /src/cd.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1995 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions 9 | * are met: 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 3. Neither the name of the University nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 | * SUCH DAMAGE. 30 | * 31 | */ 32 | 33 | int cdcmd(int, char **); 34 | int pwdcmd(int, char **); 35 | void setpwd(const char *, int); 36 | -------------------------------------------------------------------------------- /src/funcs/newgrp: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1991, 1993 2 | # The Regents of the University of California. All rights reserved. 3 | # Copyright (c) 1997-2005 4 | # Herbert Xu . All rights reserved. 5 | # 6 | # This code is derived from software contributed to Berkeley by 7 | # Kenneth Almquist. 8 | # 9 | # Redistribution and use in source and binary forms, with or without 10 | # modification, are permitted provided that the following conditions 11 | # are met: 12 | # 1. Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # 2. Redistributions in binary form must reproduce the above copyright 15 | # notice, this list of conditions and the following disclaimer in the 16 | # documentation and/or other materials provided with the distribution. 17 | # 3. Neither the name of the University nor the names of its contributors 18 | # may be used to endorse or promote products derived from this software 19 | # without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | # SUCH DAMAGE. 32 | # 33 | # @(#)newgrp 8.2 (Berkeley) 5/4/95 34 | 35 | newgrp() exec newgrp "$@" 36 | -------------------------------------------------------------------------------- /src/funcs/suspend: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1991, 1993 2 | # The Regents of the University of California. All rights reserved. 3 | # Copyright (c) 1997-2005 4 | # Herbert Xu . All rights reserved. 5 | # 6 | # This code is derived from software contributed to Berkeley by 7 | # Kenneth Almquist. 8 | # 9 | # Redistribution and use in source and binary forms, with or without 10 | # modification, are permitted provided that the following conditions 11 | # are met: 12 | # 1. Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # 2. Redistributions in binary form must reproduce the above copyright 15 | # notice, this list of conditions and the following disclaimer in the 16 | # documentation and/or other materials provided with the distribution. 17 | # 3. Neither the name of the University nor the names of its contributors 18 | # may be used to endorse or promote products derived from this software 19 | # without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | # SUCH DAMAGE. 32 | # 33 | # @(#)suspend 8.2 (Berkeley) 5/4/95 34 | 35 | suspend() { 36 | local - 37 | set +j 38 | kill -TSTP 0 39 | } 40 | -------------------------------------------------------------------------------- /src/funcs/login: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1991, 1993 2 | # The Regents of the University of California. All rights reserved. 3 | # Copyright (c) 1997-2005 4 | # Herbert Xu . All rights reserved. 5 | # 6 | # This code is derived from software contributed to Berkeley by 7 | # Kenneth Almquist. 8 | # 9 | # Redistribution and use in source and binary forms, with or without 10 | # modification, are permitted provided that the following conditions 11 | # are met: 12 | # 1. Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # 2. Redistributions in binary form must reproduce the above copyright 15 | # notice, this list of conditions and the following disclaimer in the 16 | # documentation and/or other materials provided with the distribution. 17 | # 3. Neither the name of the University nor the names of its contributors 18 | # may be used to endorse or promote products derived from this software 19 | # without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | # SUCH DAMAGE. 32 | # 33 | # @(#)login 8.2 (Berkeley) 5/4/95 34 | 35 | # replaces the login builtin in the BSD shell 36 | login () exec login "$@" 37 | -------------------------------------------------------------------------------- /src/mail.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)mail.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | void chkmail(void); 38 | void changemail(const char *); 39 | -------------------------------------------------------------------------------- /src/init.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)init.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | void init(void); 38 | void reset(void); 39 | void initshellproc(void); 40 | -------------------------------------------------------------------------------- /src/show.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1995 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions 9 | * are met: 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 3. Neither the name of the University nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 | * SUCH DAMAGE. 30 | * 31 | * @(#)show.h 1.1 (Berkeley) 5/4/95 32 | */ 33 | 34 | #include 35 | 36 | #ifdef DEBUG 37 | union node; 38 | void showtree(union node *); 39 | void trace(const char *, ...); 40 | void tracev(const char *, va_list); 41 | void trargs(char **); 42 | void trputc(int); 43 | void trputs(const char *); 44 | void opentrace(void); 45 | #endif 46 | -------------------------------------------------------------------------------- /src/funcs/kill: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1991, 1993 2 | # The Regents of the University of California. All rights reserved. 3 | # Copyright (c) 1997-2005 4 | # Herbert Xu . All rights reserved. 5 | # 6 | # This code is derived from software contributed to Berkeley by 7 | # Kenneth Almquist. 8 | # 9 | # Redistribution and use in source and binary forms, with or without 10 | # modification, are permitted provided that the following conditions 11 | # are met: 12 | # 1. Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # 2. Redistributions in binary form must reproduce the above copyright 15 | # notice, this list of conditions and the following disclaimer in the 16 | # documentation and/or other materials provided with the distribution. 17 | # 3. Neither the name of the University nor the names of its contributors 18 | # may be used to endorse or promote products derived from this software 19 | # without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | # SUCH DAMAGE. 32 | # 33 | # @(#)kill 8.2 (Berkeley) 5/4/95 34 | 35 | # Convert job names to process ids and then run /bin/kill. 36 | 37 | kill() { 38 | local args x 39 | args= 40 | for x in "$@" 41 | do case $x in 42 | %*) x=`jobid "$x"` ;; 43 | esac 44 | args="$args $x" 45 | done 46 | /bin/kill $args 47 | } 48 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_YFLAGS = -d 2 | 3 | COMMON_CFLAGS = -Wall 4 | COMMON_CPPFLAGS = \ 5 | -include $(top_builddir)/config.h \ 6 | -DBSD=1 -DSHELL \ 7 | -DIFS_BROKEN 8 | 9 | AM_CFLAGS = $(COMMON_CFLAGS) 10 | AM_CPPFLAGS = $(COMMON_CPPFLAGS) 11 | AM_CFLAGS_FOR_BUILD = -g -O2 $(COMMON_CFLAGS) 12 | AM_CPPFLAGS_FOR_BUILD = $(COMMON_CPPFLAGS) 13 | 14 | COMPILE_FOR_BUILD = \ 15 | $(CC_FOR_BUILD) $(DEFAULT_INCLUDES) $(AM_CPPFLAGS_FOR_BUILD) \ 16 | $(CPPFLAGS_FOR_BUILD) \ 17 | $(AM_CFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) 18 | 19 | bin_PROGRAMS = dash 20 | 21 | dash_CFILES = \ 22 | alias.c arith_yacc.c arith_yylex.c cd.c error.c eval.c exec.c expand.c \ 23 | histedit.c input.c jobs.c mail.c main.c memalloc.c miscbltin.c \ 24 | mystring.c options.c parser.c redir.c show.c trap.c output.c \ 25 | bltin/printf.c system.c bltin/test.c bltin/times.c var.c 26 | dash_SOURCES = \ 27 | $(dash_CFILES) \ 28 | alias.h arith_yacc.h bltin/bltin.h cd.h error.h eval.h exec.h \ 29 | expand.h hetio.h \ 30 | init.h input.h jobs.h machdep.h mail.h main.h memalloc.h miscbltin.h \ 31 | myhistedit.h mystring.h options.h output.h parser.h redir.h shell.h \ 32 | show.h system.h trap.h var.h 33 | dash_LDADD = builtins.o init.o nodes.o signames.o syntax.o 34 | 35 | HELPERS = mkinit mksyntax mknodes mksignames 36 | 37 | BUILT_SOURCES = builtins.h nodes.h syntax.h token.h token_vars.h 38 | CLEANFILES = \ 39 | $(BUILT_SOURCES) $(patsubst %.o,%.c,$(dash_LDADD)) \ 40 | $(HELPERS) builtins.def 41 | 42 | man_MANS = dash.1 43 | EXTRA_DIST = \ 44 | $(man_MANS) \ 45 | mktokens mkbuiltins builtins.def.in mkinit.c \ 46 | mknodes.c nodetypes nodes.c.pat mksyntax.c mksignames.c 47 | 48 | token.h token_vars.h: mktokens 49 | sh $^ 50 | 51 | builtins.def: builtins.def.in $(top_builddir)/config.h 52 | $(COMPILE) -E -x c -o $@ $< 53 | 54 | builtins.c builtins.h: mkbuiltins builtins.def 55 | sh $^ 56 | 57 | init.c: mkinit $(dash_CFILES) 58 | ./$^ 59 | 60 | nodes.c nodes.h: mknodes nodetypes nodes.c.pat 61 | ./$^ 62 | 63 | syntax.c syntax.h: mksyntax 64 | ./$^ 65 | 66 | signames.c: mksignames 67 | ./$^ 68 | 69 | mksyntax: token.h 70 | 71 | $(HELPERS): %: %.c 72 | $(COMPILE_FOR_BUILD) -o $@ $< 73 | -------------------------------------------------------------------------------- /src/funcs/cmv: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1991, 1993 2 | # The Regents of the University of California. All rights reserved. 3 | # Copyright (c) 1997-2005 4 | # Herbert Xu . All rights reserved. 5 | # 6 | # This code is derived from software contributed to Berkeley by 7 | # Kenneth Almquist. 8 | # 9 | # Redistribution and use in source and binary forms, with or without 10 | # modification, are permitted provided that the following conditions 11 | # are met: 12 | # 1. Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # 2. Redistributions in binary form must reproduce the above copyright 15 | # notice, this list of conditions and the following disclaimer in the 16 | # documentation and/or other materials provided with the distribution. 17 | # 3. Neither the name of the University nor the names of its contributors 18 | # may be used to endorse or promote products derived from this software 19 | # without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | # SUCH DAMAGE. 32 | # 33 | # @(#)cmv 8.2 (Berkeley) 5/4/95 34 | 35 | # Conditional move--don't replace an existing file. 36 | 37 | cmv() { 38 | if test $# != 2 39 | then echo "cmv: arg count" 40 | return 2 41 | fi 42 | if test -f "$2" -o -w "$2" 43 | then echo "$2 exists" 44 | return 2 45 | fi 46 | /bin/mv "$1" "$2" 47 | } 48 | -------------------------------------------------------------------------------- /src/myhistedit.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions 9 | * are met: 10 | * 1. Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 3. Neither the name of the University nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 | * SUCH DAMAGE. 30 | * 31 | * @(#)myhistedit.h 8.2 (Berkeley) 5/4/95 32 | */ 33 | 34 | #include 35 | 36 | extern History *hist; 37 | extern EditLine *el; 38 | extern int displayhist; 39 | 40 | void histedit(void); 41 | void sethistsize(const char *); 42 | void setterm(const char *); 43 | int histcmd(int, char **); 44 | int not_fcnumber(char *); 45 | int str_to_event(const char *, int); 46 | 47 | -------------------------------------------------------------------------------- /src/alias.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)alias.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | #define ALIASINUSE 1 38 | #define ALIASDEAD 2 39 | 40 | struct alias { 41 | struct alias *next; 42 | char *name; 43 | char *val; 44 | int flag; 45 | }; 46 | 47 | struct alias *lookupalias(const char *, int); 48 | int aliascmd(int, char **); 49 | int unaliascmd(int, char **); 50 | void rmaliases(void); 51 | int unalias(const char *); 52 | void printalias(const struct alias *); 53 | -------------------------------------------------------------------------------- /src/main.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)main.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | #include 38 | 39 | /* pid of main shell */ 40 | extern int rootpid; 41 | /* shell level: 0 for the main shell, 1 for its children, and so on */ 42 | extern int shlvl; 43 | #define rootshell (!shlvl) 44 | 45 | #ifdef __GLIBC__ 46 | /* glibc sucks */ 47 | extern int *dash_errno; 48 | #undef errno 49 | #define errno (*dash_errno) 50 | #endif 51 | 52 | void readcmdfile(char *); 53 | int dotcmd(int, char **); 54 | int exitcmd(int, char **); 55 | -------------------------------------------------------------------------------- /src/machdep.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)machdep.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | /* 38 | * Most machines require the value returned from malloc to be aligned 39 | * in some way. The following macro will get this right on many machines. 40 | */ 41 | 42 | #define SHELL_SIZE (sizeof(union {int i; char *cp; double d; }) - 1) 43 | /* 44 | * It appears that grabstackstr() will barf with such alignments 45 | * because stalloc() will return a string allocated in a new stackblock. 46 | */ 47 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) 48 | -------------------------------------------------------------------------------- /src/trap.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)trap.h 8.3 (Berkeley) 6/5/95 35 | */ 36 | 37 | #include 38 | 39 | extern int trapcnt; 40 | extern char sigmode[]; 41 | extern volatile sig_atomic_t pendingsigs; 42 | extern int gotsigchld; 43 | 44 | int trapcmd(int, char **); 45 | void clear_traps(void); 46 | void setsignal(int); 47 | void ignoresig(int); 48 | void onsig(int); 49 | void dotrap(void); 50 | void setinteractive(int); 51 | void exitshell(void) __attribute__((__noreturn__)); 52 | int decode_signal(const char *, int); 53 | 54 | static inline int have_traps(void) 55 | { 56 | return trapcnt; 57 | } 58 | -------------------------------------------------------------------------------- /src/redir.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)redir.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | /* flags passed to redirect */ 38 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ 39 | #ifdef notyet 40 | #define REDIR_BACKQ 02 /* save the command output in memory */ 41 | #endif 42 | #define REDIR_SAVEFD2 03 /* set preverrout */ 43 | 44 | struct redirtab; 45 | union node; 46 | void redirect(union node *, int); 47 | void popredir(int); 48 | void clearredir(void); 49 | int savefd(int, int); 50 | int redirectsafe(union node *, int); 51 | void unwindredir(struct redirtab *stop); 52 | struct redirtab *pushredir(union node *redir); 53 | 54 | -------------------------------------------------------------------------------- /src/funcs/dirs: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1991, 1993 2 | # The Regents of the University of California. All rights reserved. 3 | # Copyright (c) 1997-2005 4 | # Herbert Xu . All rights reserved. 5 | # 6 | # This code is derived from software contributed to Berkeley by 7 | # Kenneth Almquist. 8 | # 9 | # Redistribution and use in source and binary forms, with or without 10 | # modification, are permitted provided that the following conditions 11 | # are met: 12 | # 1. Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # 2. Redistributions in binary form must reproduce the above copyright 15 | # notice, this list of conditions and the following disclaimer in the 16 | # documentation and/or other materials provided with the distribution. 17 | # 3. Neither the name of the University nor the names of its contributors 18 | # may be used to endorse or promote products derived from this software 19 | # without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | # SUCH DAMAGE. 32 | # 33 | # @(#)dirs 8.2 (Berkeley) 5/4/95 34 | 35 | # pushd, popd, and dirs --- written by Chris Bertin 36 | # Pixel Computer Inc. ...!wjh12!pixel!pixutl!chris 37 | # as modified by Patrick Elam of GTRI and Kenneth Almquist at UW 38 | 39 | pushd () { 40 | SAVE=`pwd` 41 | if [ "$1" = "" ] 42 | then if [ "$DSTACK" = "" ] 43 | then echo "pushd: directory stack empty." 44 | return 1 45 | fi 46 | set $DSTACK 47 | cd $1 || return 48 | shift 1 49 | DSTACK="$*" 50 | else cd $1 > /dev/null || return 51 | fi 52 | DSTACK="$SAVE $DSTACK" 53 | dirs 54 | } 55 | 56 | popd () { 57 | if [ "$DSTACK" = "" ] 58 | then echo "popd: directory stack empty." 59 | return 1 60 | fi 61 | set $DSTACK 62 | cd $1 63 | shift 64 | DSTACK=$* 65 | dirs 66 | } 67 | 68 | dirs () { 69 | echo "`pwd` $DSTACK" 70 | return 0 71 | } 72 | -------------------------------------------------------------------------------- /src/funcs/popd: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1991, 1993 2 | # The Regents of the University of California. All rights reserved. 3 | # Copyright (c) 1997-2005 4 | # Herbert Xu . All rights reserved. 5 | # 6 | # This code is derived from software contributed to Berkeley by 7 | # Kenneth Almquist. 8 | # 9 | # Redistribution and use in source and binary forms, with or without 10 | # modification, are permitted provided that the following conditions 11 | # are met: 12 | # 1. Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # 2. Redistributions in binary form must reproduce the above copyright 15 | # notice, this list of conditions and the following disclaimer in the 16 | # documentation and/or other materials provided with the distribution. 17 | # 3. Neither the name of the University nor the names of its contributors 18 | # may be used to endorse or promote products derived from this software 19 | # without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | # SUCH DAMAGE. 32 | # 33 | # @(#)popd 8.2 (Berkeley) 5/4/95 34 | 35 | # pushd, popd, and dirs --- written by Chris Bertin 36 | # Pixel Computer Inc. ...!wjh12!pixel!pixutl!chris 37 | # as modified by Patrick Elam of GTRI and Kenneth Almquist at UW 38 | 39 | pushd () { 40 | SAVE=`pwd` 41 | if [ "$1" = "" ] 42 | then if [ "$DSTACK" = "" ] 43 | then echo "pushd: directory stack empty." 44 | return 1 45 | fi 46 | set $DSTACK 47 | cd $1 || return 48 | shift 1 49 | DSTACK="$*" 50 | else cd $1 > /dev/null || return 51 | fi 52 | DSTACK="$SAVE $DSTACK" 53 | dirs 54 | } 55 | 56 | popd () { 57 | if [ "$DSTACK" = "" ] 58 | then echo "popd: directory stack empty." 59 | return 1 60 | fi 61 | set $DSTACK 62 | cd $1 63 | shift 64 | DSTACK=$* 65 | dirs 66 | } 67 | 68 | dirs () { 69 | echo "`pwd` $DSTACK" 70 | return 0 71 | } 72 | -------------------------------------------------------------------------------- /src/funcs/pushd: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1991, 1993 2 | # The Regents of the University of California. All rights reserved. 3 | # Copyright (c) 1997-2005 4 | # Herbert Xu . All rights reserved. 5 | # 6 | # This code is derived from software contributed to Berkeley by 7 | # Kenneth Almquist. 8 | # 9 | # Redistribution and use in source and binary forms, with or without 10 | # modification, are permitted provided that the following conditions 11 | # are met: 12 | # 1. Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # 2. Redistributions in binary form must reproduce the above copyright 15 | # notice, this list of conditions and the following disclaimer in the 16 | # documentation and/or other materials provided with the distribution. 17 | # 3. Neither the name of the University nor the names of its contributors 18 | # may be used to endorse or promote products derived from this software 19 | # without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | # SUCH DAMAGE. 32 | # 33 | # @(#)pushd 8.2 (Berkeley) 5/4/95 34 | 35 | # pushd, popd, and dirs --- written by Chris Bertin 36 | # Pixel Computer Inc. ...!wjh12!pixel!pixutl!chris 37 | # as modified by Patrick Elam of GTRI and Kenneth Almquist at UW 38 | 39 | pushd () { 40 | SAVE=`pwd` 41 | if [ "$1" = "" ] 42 | then if [ "$DSTACK" = "" ] 43 | then echo "pushd: directory stack empty." 44 | return 1 45 | fi 46 | set $DSTACK 47 | cd $1 || return 48 | shift 1 49 | DSTACK="$*" 50 | else cd $1 > /dev/null || return 51 | fi 52 | DSTACK="$SAVE $DSTACK" 53 | dirs 54 | } 55 | 56 | popd () { 57 | if [ "$DSTACK" = "" ] 58 | then echo "popd: directory stack empty." 59 | return 1 60 | fi 61 | set $DSTACK 62 | cd $1 63 | shift 64 | DSTACK=$* 65 | dirs 66 | } 67 | 68 | dirs () { 69 | echo "`pwd` $DSTACK" 70 | return 0 71 | } 72 | -------------------------------------------------------------------------------- /src/mystring.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)mystring.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | #include 38 | #include 39 | 40 | extern const char snlfmt[]; 41 | extern const char spcstr[]; 42 | extern const char dolatstr[]; 43 | #define DOLATSTRLEN 6 44 | extern const char qchars[]; 45 | extern const char illnum[]; 46 | extern const char homestr[]; 47 | 48 | #if 0 49 | void scopyn(const char *, char *, int); 50 | #endif 51 | char *prefix(const char *, const char *); 52 | void badnum(const char *s) __attribute__ ((noreturn)); 53 | intmax_t atomax(const char *, int); 54 | intmax_t atomax10(const char *); 55 | int number(const char *); 56 | int is_number(const char *); 57 | char *single_quote(const char *); 58 | char *sstrdup(const char *); 59 | int pstrcmp(const void *, const void *); 60 | const char *const *findstring(const char *, const char *const *, size_t); 61 | 62 | #define equal(s1, s2) (strcmp(s1, s2) == 0) 63 | #define scopy(s1, s2) ((void)strcpy(s2, s1)) 64 | -------------------------------------------------------------------------------- /src/input.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)input.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | /* PEOF (the end of file marker) is defined in syntax.h */ 38 | 39 | enum { 40 | INPUT_PUSH_FILE = 1, 41 | INPUT_NOFILE_OK = 2, 42 | }; 43 | 44 | /* 45 | * The input line number. Input.c just defines this variable, and saves 46 | * and restores it when files are pushed and popped. The user of this 47 | * package must set its value. 48 | */ 49 | extern int plinno; 50 | extern int parsenleft; /* number of characters left in input buffer */ 51 | extern char *parsenextc; /* next character in input buffer */ 52 | 53 | int pgetc(void); 54 | int pgetc2(void); 55 | int preadbuffer(void); 56 | void pungetc(void); 57 | void pushstring(char *, void *); 58 | void popstring(void); 59 | int setinputfile(const char *, int); 60 | void setinputstring(char *); 61 | void popfile(void); 62 | void popallfiles(void); 63 | void closescript(void); 64 | 65 | #define pgetc_macro() \ 66 | (--parsenleft >= 0 ? (signed char)*parsenextc++ : preadbuffer()) 67 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 1989-1994 2 | The Regents of the University of California. All rights reserved. 3 | Copyright (c) 1997 Christos Zoulas. All rights reserved. 4 | Copyright (c) 1997-2005 5 | Herbert Xu . All rights reserved. 6 | 7 | This code is derived from software contributed to Berkeley by Kenneth Almquist. 8 | 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions 12 | are met: 13 | 1. Redistributions of source code must retain the above copyright 14 | notice, this list of conditions and the following disclaimer. 15 | 2. Redistributions in binary form must reproduce the above copyright 16 | notice, this list of conditions and the following disclaimer in the 17 | documentation and/or other materials provided with the distribution. 18 | 3. Neither the name of the University nor the names of its contributors 19 | may be used to endorse or promote products derived from this software 20 | without specific prior written permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | SUCH DAMAGE. 33 | 34 | mksignames.c: 35 | 36 | This file is not directly linked with dash. However, its output is. 37 | 38 | Copyright (C) 1992 Free Software Foundation, Inc. 39 | 40 | This file is part of GNU Bash, the Bourne Again SHell. 41 | 42 | Bash is free software; you can redistribute it and/or modify it under 43 | the terms of the GNU General Public License as published by the Free 44 | Software Foundation; either version 2, or (at your option) any later 45 | version. 46 | 47 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY 48 | WARRANTY; without even the implied warranty of MERCHANTABILITY or 49 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 50 | for more details. 51 | 52 | You should have received a copy of the GNU General Public License with 53 | your Debian GNU/Linux system, in /usr/share/common-licenses/GPL, or with the 54 | Debian GNU/Linux hello source package as the file COPYING. If not, 55 | write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 56 | Boston, MA 02111 USA. 57 | -------------------------------------------------------------------------------- /src/eval.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)eval.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | extern char *commandname; /* currently executing command */ 38 | extern int exitstatus; /* exit status of last command */ 39 | extern int back_exitstatus; /* exit status of backquoted command */ 40 | 41 | 42 | struct backcmd { /* result of evalbackcmd */ 43 | int fd; /* file descriptor to read from */ 44 | char *buf; /* buffer */ 45 | int nleft; /* number of chars in buffer */ 46 | struct job *jp; /* job structure for command */ 47 | }; 48 | 49 | /* flags in argument to evaltree */ 50 | #define EV_EXIT 01 /* exit after evaluating tree */ 51 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ 52 | 53 | int evalstring(char *, int); 54 | union node; /* BLETCH for ansi C */ 55 | void evaltree(union node *, int); 56 | void evalbackcmd(union node *, struct backcmd *); 57 | 58 | extern int evalskip; 59 | 60 | /* reasons for skipping commands (see comment on breakcmd routine) */ 61 | #define SKIPBREAK (1 << 0) 62 | #define SKIPCONT (1 << 1) 63 | #define SKIPFUNC (1 << 2) 64 | -------------------------------------------------------------------------------- /src/arith_yacc.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 2007 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | #define ARITH_ASS 1 36 | 37 | #define ARITH_OR 2 38 | #define ARITH_AND 3 39 | #define ARITH_BAD 4 40 | #define ARITH_NUM 5 41 | #define ARITH_VAR 6 42 | #define ARITH_NOT 7 43 | 44 | #define ARITH_BINOP_MIN 8 45 | #define ARITH_LE 8 46 | #define ARITH_GE 9 47 | #define ARITH_LT 10 48 | #define ARITH_GT 11 49 | #define ARITH_EQ 12 50 | #define ARITH_REM 13 51 | #define ARITH_BAND 14 52 | #define ARITH_LSHIFT 15 53 | #define ARITH_RSHIFT 16 54 | #define ARITH_MUL 17 55 | #define ARITH_ADD 18 56 | #define ARITH_BOR 19 57 | #define ARITH_SUB 20 58 | #define ARITH_BXOR 21 59 | #define ARITH_DIV 22 60 | #define ARITH_NE 23 61 | #define ARITH_BINOP_MAX 24 62 | 63 | #define ARITH_ASS_MIN 24 64 | #define ARITH_REMASS 24 65 | #define ARITH_BANDASS 25 66 | #define ARITH_LSHIFTASS 26 67 | #define ARITH_RSHIFTASS 27 68 | #define ARITH_MULASS 28 69 | #define ARITH_ADDASS 29 70 | #define ARITH_BORASS 30 71 | #define ARITH_SUBASS 31 72 | #define ARITH_BXORASS 32 73 | #define ARITH_DIVASS 33 74 | #define ARITH_ASS_MAX 34 75 | 76 | #define ARITH_LPAREN 34 77 | #define ARITH_RPAREN 35 78 | #define ARITH_BNOT 36 79 | #define ARITH_QMARK 37 80 | #define ARITH_COLON 38 81 | 82 | union yystype { 83 | intmax_t val; 84 | char *name; 85 | }; 86 | 87 | extern union yystype yylval; 88 | 89 | int yylex(void); 90 | -------------------------------------------------------------------------------- /src/exec.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)exec.h 8.3 (Berkeley) 6/8/95 35 | */ 36 | 37 | /* values of cmdtype */ 38 | #define CMDUNKNOWN -1 /* no entry in table for command */ 39 | #define CMDNORMAL 0 /* command is an executable program */ 40 | #define CMDFUNCTION 1 /* command is a shell function */ 41 | #define CMDBUILTIN 2 /* command is a shell builtin */ 42 | 43 | 44 | struct cmdentry { 45 | int cmdtype; 46 | union param { 47 | int index; 48 | const struct builtincmd *cmd; 49 | struct funcnode *func; 50 | } u; 51 | }; 52 | 53 | 54 | /* action to find_command() */ 55 | #define DO_ERR 0x01 /* prints errors */ 56 | #define DO_ABS 0x02 /* checks absolute paths */ 57 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ 58 | #define DO_ALTPATH 0x08 /* using alternate path */ 59 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ 60 | 61 | extern const char *pathopt; /* set by padvance */ 62 | 63 | void shellexec(char **, const char *, int) 64 | __attribute__((__noreturn__)); 65 | char *padvance(const char **, const char *); 66 | int hashcmd(int, char **); 67 | void find_command(char *, struct cmdentry *, int, const char *); 68 | struct builtincmd *find_builtin(const char *); 69 | void hashcd(void); 70 | void changepath(const char *); 71 | #ifdef notdef 72 | void getcmdentry(char *, struct cmdentry *); 73 | #endif 74 | void defun(union node *); 75 | void unsetfunc(const char *); 76 | int typecmd(int, char **); 77 | int commandcmd(int, char **); 78 | -------------------------------------------------------------------------------- /src/bltin/bltin.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)bltin.h 8.1 (Berkeley) 5/31/93 35 | */ 36 | 37 | /* 38 | * This file is included by programs which are optionally built into the 39 | * shell. If SHELL is defined, we try to map the standard UNIX library 40 | * routines to ash routines using defines. 41 | */ 42 | 43 | #include "../shell.h" 44 | #include "../mystring.h" 45 | #include "../options.h" 46 | #ifdef SHELL 47 | #include "../memalloc.h" 48 | #include "../output.h" 49 | #include "../error.h" 50 | #ifndef USE_GLIBC_STDIO 51 | #undef stdout 52 | #undef stderr 53 | #undef putc 54 | #undef putchar 55 | #undef fileno 56 | #define stdout out1 57 | #define stderr out2 58 | #define printf out1fmt 59 | #define putc(c, file) outc(c, file) 60 | #define putchar(c) out1c(c) 61 | #define FILE struct output 62 | #define fprintf outfmt 63 | #define fputs outstr 64 | #define fflush flushout 65 | #define fileno(f) ((f)->fd) 66 | #define ferror outerr 67 | #endif 68 | #define INITARGS(argv) 69 | #define error sh_error 70 | #define warn sh_warn 71 | #define warnx sh_warnx 72 | #define exit sh_exit 73 | #define setprogname(s) 74 | #define getprogname() commandname 75 | #define setlocate(l,s) 0 76 | 77 | #define getenv(p) bltinlookup((p),0) 78 | 79 | #else 80 | #undef NULL 81 | #include 82 | #undef main 83 | #define INITARGS(argv) if ((commandname = argv[0]) == NULL) {fputs("Argc is zero\n", stderr); exit(2);} else 84 | #endif 85 | 86 | int echocmd(int, char **); 87 | 88 | 89 | extern const char *commandname; 90 | -------------------------------------------------------------------------------- /src/expand.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)expand.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | #include 38 | 39 | struct strlist { 40 | struct strlist *next; 41 | char *text; 42 | }; 43 | 44 | 45 | struct arglist { 46 | struct strlist *list; 47 | struct strlist **lastp; 48 | }; 49 | 50 | /* 51 | * expandarg() flags 52 | */ 53 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ 54 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ 55 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ 56 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ 57 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ 58 | #define EXP_QPAT 0x20 /* pattern in quoted parameter expansion */ 59 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ 60 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ 61 | #define EXP_QUOTED 0x100 /* expand word in double quotes */ 62 | 63 | 64 | union node; 65 | void expandarg(union node *, struct arglist *, int); 66 | void expari(int); 67 | #define rmescapes(p) _rmescapes((p), 0) 68 | char *_rmescapes(char *, int); 69 | int casematch(union node *, char *); 70 | void recordregion(int, int, int); 71 | void removerecordregions(int); 72 | void ifsbreakup(char *, struct arglist *); 73 | void ifsfree(void); 74 | 75 | /* From arith.y */ 76 | intmax_t arith(const char *); 77 | int expcmd(int , char **); 78 | #ifdef USE_LEX 79 | void arith_lex_reset(void); 80 | #else 81 | #define arith_lex_reset() 82 | #endif 83 | int yylex(void); 84 | -------------------------------------------------------------------------------- /src/mktokens: -------------------------------------------------------------------------------- 1 | #!/bin/sh - 2 | # Copyright (c) 1991, 1993 3 | # The Regents of the University of California. All rights reserved. 4 | # Copyright (c) 1997-2005 5 | # Herbert Xu . All rights reserved. 6 | # 7 | # This code is derived from software contributed to Berkeley by 8 | # Kenneth Almquist. 9 | # 10 | # Redistribution and use in source and binary forms, with or without 11 | # modification, are permitted provided that the following conditions 12 | # are met: 13 | # 1. Redistributions of source code must retain the above copyright 14 | # notice, this list of conditions and the following disclaimer. 15 | # 2. Redistributions in binary form must reproduce the above copyright 16 | # notice, this list of conditions and the following disclaimer in the 17 | # documentation and/or other materials provided with the distribution. 18 | # 3. Neither the name of the University nor the names of its contributors 19 | # may be used to endorse or promote products derived from this software 20 | # without specific prior written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | # SUCH DAMAGE. 33 | # 34 | # @(#)mktokens 8.1 (Berkeley) 5/31/93 35 | 36 | # The following is a list of tokens. The second column is nonzero if the 37 | # token marks the end of a list. The third column is the name to print in 38 | # error messages. 39 | 40 | cat > /tmp/ka$$ <<\! 41 | TEOF 1 end of file 42 | TNL 0 newline 43 | TSEMI 0 ";" 44 | TBACKGND 0 "&" 45 | TAND 0 "&&" 46 | TOR 0 "||" 47 | TPIPE 0 "|" 48 | TLP 0 "(" 49 | TRP 1 ")" 50 | TENDCASE 1 ";;" 51 | TENDBQUOTE 1 "`" 52 | TREDIR 0 redirection 53 | TWORD 0 word 54 | TNOT 0 "!" 55 | TCASE 0 "case" 56 | TDO 1 "do" 57 | TDONE 1 "done" 58 | TELIF 1 "elif" 59 | TELSE 1 "else" 60 | TESAC 1 "esac" 61 | TFI 1 "fi" 62 | TFOR 0 "for" 63 | TIF 0 "if" 64 | TIN 0 "in" 65 | TTHEN 1 "then" 66 | TUNTIL 0 "until" 67 | TWHILE 0 "while" 68 | TBEGIN 0 "{" 69 | TEND 1 "}" 70 | ! 71 | nl=`wc -l /tmp/ka$$` 72 | exec > token.h 73 | awk '{print "#define " $1 " " NR-1}' /tmp/ka$$ 74 | 75 | exec > token_vars.h 76 | 77 | echo ' 78 | /* Array indicating which tokens mark the end of a list */ 79 | static const char tokendlist[] = {' 80 | awk '{print "\t" $2 ","}' /tmp/ka$$ 81 | echo '}; 82 | 83 | static const char *const tokname[] = {' 84 | sed -e 's/"/\\"/g' \ 85 | -e 's/[^ ]*[ ][ ]*[^ ]*[ ][ ]*\(.*\)/ "\1",/' \ 86 | /tmp/ka$$ 87 | echo '}; 88 | ' 89 | sed 's/"//g' /tmp/ka$$ | awk ' 90 | /TNOT/{print "#define KWDOFFSET " NR-1; print ""; 91 | print "static const char *const parsekwd[] = {"} 92 | /TNOT/,/neverfound/{if (last) print " \"" last "\","; last = $3} 93 | END{print " \"" last "\"\n};"}' 94 | 95 | rm /tmp/ka$$ 96 | -------------------------------------------------------------------------------- /src/builtins.def.in: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)builtins.def 8.4 (Berkeley) 5/4/95 35 | */ 36 | 37 | /* 38 | * This file lists all the builtin commands. The first column is the name 39 | * of a C routine. 40 | * The -a flag specifies that this is a posix 'assignment builtin' command. 41 | * The -s flag specifies that this is a posix 'special builtin' command. 42 | * The -u flag specifies that this is a posix 'standard utility'. 43 | * The -n flag specifies that this command has a special entry point. 44 | * The rest of the line specifies the command name or names used to run 45 | * the command. 46 | */ 47 | 48 | #ifndef JOBS 49 | #define JOBS 1 50 | #endif 51 | 52 | #if JOBS 53 | bgcmd -u bg 54 | fgcmd -u fg 55 | #endif 56 | 57 | #ifndef SMALL 58 | histcmd -u fc 59 | #endif 60 | 61 | breakcmd -s break -s continue 62 | cdcmd -u cd chdir 63 | commandcmd -u command 64 | dotcmd -s . 65 | echocmd echo 66 | evalcmd -ns eval 67 | execcmd -s exec 68 | exitcmd -s exit 69 | exportcmd -as export -as readonly 70 | falsecmd -u false 71 | getoptscmd -u getopts 72 | hashcmd hash 73 | jobscmd -u jobs 74 | localcmd -as local 75 | printfcmd printf 76 | pwdcmd pwd 77 | readcmd -u read 78 | returncmd -s return 79 | setcmd -s set 80 | shiftcmd -s shift 81 | timescmd -s times 82 | trapcmd -s trap 83 | truecmd -s : -u true 84 | typecmd type 85 | umaskcmd -u umask 86 | unaliascmd -u unalias 87 | unsetcmd -s unset 88 | waitcmd -u wait 89 | aliascmd -au alias 90 | #ifdef HAVE_GETRLIMIT 91 | ulimitcmd ulimit 92 | #endif 93 | testcmd test [ 94 | killcmd -u kill 95 | -------------------------------------------------------------------------------- /src/system.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2004 3 | * Herbert Xu . All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3. The name of the author may not be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 | * SUCH DAMAGE. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #ifndef SSIZE_MAX 34 | #define SSIZE_MAX ((ssize_t)((size_t)-1 >> 1)) 35 | #endif 36 | 37 | static inline void sigclearmask(void) 38 | { 39 | #ifdef HAVE_SIGSETMASK 40 | sigsetmask(0); 41 | #else 42 | sigset_t set; 43 | sigemptyset(&set); 44 | sigprocmask(SIG_SETMASK, &set, 0); 45 | #endif 46 | } 47 | 48 | #ifndef HAVE_MEMPCPY 49 | void *mempcpy(void *, const void *, size_t); 50 | #endif 51 | 52 | #ifndef HAVE_STPCPY 53 | char *stpcpy(char *, const char *); 54 | #endif 55 | 56 | #ifndef HAVE_STRCHRNUL 57 | char *strchrnul(const char *, int); 58 | #endif 59 | 60 | #ifndef HAVE_STRSIGNAL 61 | char *strsignal(int); 62 | #endif 63 | 64 | #ifndef HAVE_STRTOD 65 | static inline double strtod(const char *nptr, char **endptr) 66 | { 67 | *endptr = (char *)nptr; 68 | return 0; 69 | } 70 | #endif 71 | 72 | #ifndef HAVE_STRTOIMAX 73 | #define strtoimax strtoll 74 | #endif 75 | 76 | #ifndef HAVE_STRTOUMAX 77 | #define strtoumax strtoull 78 | #endif 79 | 80 | #ifndef HAVE_BSEARCH 81 | void *bsearch(const void *, const void *, size_t, size_t, 82 | int (*)(const void *, const void *)); 83 | #endif 84 | 85 | #ifndef HAVE_KILLPG 86 | static inline int killpg(pid_t pid, int signal) 87 | { 88 | #ifdef DEBUG 89 | if (pid < 0) 90 | abort(); 91 | #endif 92 | return kill(-pid, signal); 93 | } 94 | #endif 95 | 96 | #ifndef HAVE_SYSCONF 97 | #define _SC_CLK_TCK 2 98 | long sysconf(int) __attribute__((__noreturn__)); 99 | #endif 100 | 101 | #if !HAVE_DECL_ISBLANK 102 | int isblank(int c); 103 | #endif 104 | 105 | /* 106 | * A trick to suppress uninitialized variable warning without generating any 107 | * code 108 | */ 109 | #define uninitialized_var(x) x = x 110 | -------------------------------------------------------------------------------- /src/options.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)options.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | struct shparam { 38 | int nparam; /* # of positional parameters (without $0) */ 39 | unsigned char malloc; /* if parameter list dynamically allocated */ 40 | char **p; /* parameter list */ 41 | int optind; /* next parameter to be processed by getopts */ 42 | int optoff; /* used by getopts */ 43 | }; 44 | 45 | 46 | 47 | #define eflag optlist[0] 48 | #define fflag optlist[1] 49 | #define Iflag optlist[2] 50 | #define iflag optlist[3] 51 | #define mflag optlist[4] 52 | #define nflag optlist[5] 53 | #define sflag optlist[6] 54 | #define xflag optlist[7] 55 | #define vflag optlist[8] 56 | #define Vflag optlist[9] 57 | #define Eflag optlist[10] 58 | #define Cflag optlist[11] 59 | #define aflag optlist[12] 60 | #define bflag optlist[13] 61 | #define uflag optlist[14] 62 | #define nolog optlist[15] 63 | #define debug optlist[16] 64 | 65 | #define NOPTS 17 66 | 67 | extern const char optletters[NOPTS]; 68 | extern char optlist[NOPTS]; 69 | 70 | 71 | extern char *minusc; /* argument to -c option */ 72 | extern char *arg0; /* $0 */ 73 | extern struct shparam shellparam; /* $@ */ 74 | extern char **argptr; /* argument list for builtin commands */ 75 | extern char *optionarg; /* set by nextopt */ 76 | extern char *optptr; /* used by nextopt */ 77 | 78 | int procargs(int, char **); 79 | void optschanged(void); 80 | void setparam(char **); 81 | void freeparam(volatile struct shparam *); 82 | int shiftcmd(int, char **); 83 | int setcmd(int, char **); 84 | int getoptscmd(int, char **); 85 | int nextopt(const char *); 86 | void getoptsreset(const char *); 87 | -------------------------------------------------------------------------------- /src/mail.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | /* 36 | * Routines to check for mail. (Perhaps make part of main.c?) 37 | */ 38 | #include 39 | #include 40 | #include 41 | 42 | #include "shell.h" 43 | #include "nodes.h" 44 | #include "exec.h" /* defines padvance() */ 45 | #include "var.h" 46 | #include "output.h" 47 | #include "memalloc.h" 48 | #include "error.h" 49 | #include "mail.h" 50 | #include "mystring.h" 51 | 52 | 53 | #define MAXMBOXES 10 54 | 55 | /* times of mailboxes */ 56 | static time_t mailtime[MAXMBOXES]; 57 | /* Set if MAIL or MAILPATH is changed. */ 58 | static int changed; 59 | 60 | 61 | 62 | /* 63 | * Print appropriate message(s) if mail has arrived. If changed is set, 64 | * then the value of MAIL has changed, so we just update the values. 65 | */ 66 | 67 | void 68 | chkmail(void) 69 | { 70 | const char *mpath; 71 | char *p; 72 | char *q; 73 | time_t *mtp; 74 | struct stackmark smark; 75 | struct stat64 statb; 76 | 77 | setstackmark(&smark); 78 | mpath = mpathset() ? mpathval() : mailval(); 79 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { 80 | p = padvance(&mpath, nullstr); 81 | if (p == NULL) 82 | break; 83 | if (*p == '\0') 84 | continue; 85 | for (q = p ; *q ; q++); 86 | #ifdef DEBUG 87 | if (q[-1] != '/') 88 | abort(); 89 | #endif 90 | q[-1] = '\0'; /* delete trailing '/' */ 91 | if (stat64(p, &statb) < 0) { 92 | *mtp = 0; 93 | continue; 94 | } 95 | if (!changed && statb.st_mtime != *mtp) { 96 | outfmt( 97 | &errout, snlfmt, 98 | pathopt ? pathopt : "you have mail" 99 | ); 100 | } 101 | *mtp = statb.st_mtime; 102 | } 103 | changed = 0; 104 | popstackmark(&smark); 105 | } 106 | 107 | 108 | void 109 | changemail(const char *val) 110 | { 111 | changed++; 112 | } 113 | -------------------------------------------------------------------------------- /src/memalloc.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)memalloc.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | #include 38 | 39 | struct stackmark { 40 | struct stack_block *stackp; 41 | char *stacknxt; 42 | size_t stacknleft; 43 | }; 44 | 45 | 46 | extern char *stacknxt; 47 | extern size_t stacknleft; 48 | extern char *sstrend; 49 | 50 | pointer ckmalloc(size_t); 51 | pointer ckrealloc(pointer, size_t); 52 | char *savestr(const char *); 53 | pointer stalloc(size_t); 54 | void stunalloc(pointer); 55 | void pushstackmark(struct stackmark *mark, size_t len); 56 | void setstackmark(struct stackmark *); 57 | void popstackmark(struct stackmark *); 58 | void growstackblock(void); 59 | void *growstackstr(void); 60 | char *makestrspace(size_t, char *); 61 | char *stnputs(const char *, size_t, char *); 62 | char *stputs(const char *, char *); 63 | 64 | 65 | static inline void grabstackblock(size_t len) 66 | { 67 | stalloc(len); 68 | } 69 | 70 | static inline char *_STPUTC(int c, char *p) { 71 | if (p == sstrend) 72 | p = growstackstr(); 73 | *p++ = c; 74 | return p; 75 | } 76 | 77 | #define stackblock() ((void *)stacknxt) 78 | #define stackblocksize() stacknleft 79 | #define STARTSTACKSTR(p) ((p) = stackblock()) 80 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) 81 | #define CHECKSTRSPACE(n, p) \ 82 | ({ \ 83 | char *q = (p); \ 84 | size_t l = (n); \ 85 | size_t m = sstrend - q; \ 86 | if (l > m) \ 87 | (p) = makestrspace(l, q); \ 88 | 0; \ 89 | }) 90 | #define USTPUTC(c, p) (*p++ = (c)) 91 | #define STACKSTRNUL(p) ((p) == sstrend? (p = growstackstr(), *p = '\0') : (*p = '\0')) 92 | #define STUNPUTC(p) (--p) 93 | #define STTOPC(p) p[-1] 94 | #define STADJUST(amount, p) (p += (amount)) 95 | 96 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) 97 | #define ungrabstackstr(s, p) stunalloc((s)) 98 | #define stackstrend() ((void *)sstrend) 99 | 100 | #define ckfree(p) free((pointer)(p)) 101 | -------------------------------------------------------------------------------- /src/shell.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)shell.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | /* 38 | * The follow should be set to reflect the type of system you have: 39 | * JOBS -> 1 if you have Berkeley job control, 0 otherwise. 40 | * SHORTNAMES -> 1 if your linker cannot handle long names. 41 | * define BSD if you are running 4.2 BSD or later. 42 | * define SYSV if you are running under System V. 43 | * define DEBUG=1 to compile in debugging ('set -o debug' to turn on) 44 | * define DEBUG=2 to compile in and turn on debugging. 45 | * define DO_SHAREDVFORK to indicate that vfork(2) shares its address 46 | * with its parent. 47 | * 48 | * When debugging is on, debugging info will be written to ./trace and 49 | * a quit signal will generate a core dump. 50 | */ 51 | 52 | #include 53 | 54 | #ifndef JOBS 55 | #define JOBS 1 56 | #endif 57 | #ifndef BSD 58 | #define BSD 1 59 | #endif 60 | 61 | #ifndef DO_SHAREDVFORK 62 | #if __NetBSD_Version__ >= 104000000 63 | #define DO_SHAREDVFORK 64 | #endif 65 | #endif 66 | 67 | typedef void *pointer; 68 | #ifndef NULL 69 | #define NULL (void *)0 70 | #endif 71 | #define STATIC static 72 | #define MKINIT /* empty */ 73 | 74 | extern char nullstr[1]; /* null string */ 75 | 76 | 77 | #ifdef DEBUG 78 | #define TRACE(param) trace param 79 | #define TRACEV(param) tracev param 80 | #else 81 | #define TRACE(param) 82 | #define TRACEV(param) 83 | #endif 84 | 85 | #if defined(__GNUC__) && __GNUC__ < 3 86 | #define va_copy __va_copy 87 | #endif 88 | 89 | #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) 90 | #define __builtin_expect(x, expected_value) (x) 91 | #endif 92 | 93 | #define likely(x) __builtin_expect(!!(x),1) 94 | #define unlikely(x) __builtin_expect(!!(x),0) 95 | 96 | /* 97 | * Hack to calculate maximum length. 98 | * (length * 8 - 1) * log10(2) + 1 + 1 + 12 99 | * The second 1 is for the minus sign and the 12 is a safety margin. 100 | */ 101 | static inline int max_int_length(int bytes) 102 | { 103 | return (bytes * 8 - 1) * 0.30102999566398119521 + 14; 104 | } 105 | -------------------------------------------------------------------------------- /src/bltin/echo.1: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 1991, 1993 2 | .\" The Regents of the University of California. All rights reserved. 3 | .\" Copyright (c) 1997-2005 4 | .\" Herbert Xu . All rights reserved. 5 | .\" 6 | .\" This code is derived from software contributed to Berkeley by 7 | .\" Kenneth Almquist. 8 | .\" Copyright 1989 by Kenneth Almquist 9 | .\" 10 | .\" Redistribution and use in source and binary forms, with or without 11 | .\" modification, are permitted provided that the following conditions 12 | .\" are met: 13 | .\" 1. Redistributions of source code must retain the above copyright 14 | .\" notice, this list of conditions and the following disclaimer. 15 | .\" 2. Redistributions in binary form must reproduce the above copyright 16 | .\" notice, this list of conditions and the following disclaimer in the 17 | .\" documentation and/or other materials provided with the distribution. 18 | .\" 3. Neither the name of the University nor the names of its contributors 19 | .\" may be used to endorse or promote products derived from this software 20 | .\" without specific prior written permission. 21 | .\" 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | .\" SUCH DAMAGE. 33 | .\" 34 | .\" @(#)echo.1 8.1 (Berkeley) 5/31/93 35 | .\" 36 | .Dd May 31, 1993 37 | .Dt ECHO 1 38 | .Os 39 | .Sh NAME 40 | .Nm echo 41 | .Nd produce message in a shell script 42 | .Sh SYNOPSIS 43 | .Nm 44 | .Op Fl n | Fl e 45 | .Ar args ... 46 | .Sh DESCRIPTION 47 | .Nm 48 | prints its arguments on the standard output, separated by spaces. 49 | Unless the 50 | .Fl n 51 | option is present, a newline is output following the arguments. 52 | The 53 | .Fl e 54 | option causes 55 | .Nm 56 | to treat the escape sequences specially, as described in the following 57 | paragraph. 58 | The 59 | .Fl e 60 | option is the default, and is provided solely for compatibility with 61 | other systems. 62 | Only one of the options 63 | .Fl n 64 | and 65 | .Fl e 66 | may be given. 67 | .Pp 68 | If any of the following sequences of characters is encountered during 69 | output, the sequence is not output. Instead, the specified action is 70 | performed: 71 | .Bl -tag -width indent 72 | .It Li \eb 73 | A backspace character is output. 74 | .It Li \ec 75 | Subsequent output is suppressed. This is normally used at the end of the 76 | last argument to suppress the trailing newline that 77 | .Nm 78 | would otherwise output. 79 | .It Li \ef 80 | Output a form feed. 81 | .It Li \en 82 | Output a newline character. 83 | .It Li \er 84 | Output a carriage return. 85 | .It Li \et 86 | Output a (horizontal) tab character. 87 | .It Li \ev 88 | Output a vertical tab. 89 | .It Li \e0 Ns Ar digits 90 | Output the character whose value is given by zero to three digits. 91 | If there are zero digits, a nul character is output. 92 | .It Li \e\e 93 | Output a backslash. 94 | .El 95 | .Sh HINTS 96 | Remember that backslash is special to the shell and needs to be escaped. 97 | To output a message to standard error, say 98 | .Pp 99 | .D1 echo message \*[Gt]\*[Am]2 100 | .Sh BUGS 101 | The octal character escape mechanism 102 | .Pq Li \e0 Ns Ar digits 103 | differs from the 104 | C language mechanism. 105 | .Pp 106 | There is no way to force 107 | .Nm 108 | to treat its arguments literally, rather than interpreting them as 109 | options and escape sequences. 110 | -------------------------------------------------------------------------------- /src/mkbuiltins: -------------------------------------------------------------------------------- 1 | #!/bin/sh - 2 | # $NetBSD: mkbuiltins,v 1.17 2002/11/24 22:35:41 christos Exp $ 3 | # 4 | # Copyright (c) 1991, 1993 5 | # The Regents of the University of California. All rights reserved. 6 | # Copyright (c) 1997-2005 7 | # Herbert Xu . All rights reserved. 8 | # 9 | # This code is derived from software contributed to Berkeley by 10 | # Kenneth Almquist. 11 | # 12 | # Redistribution and use in source and binary forms, with or without 13 | # modification, are permitted provided that the following conditions 14 | # are met: 15 | # 1. Redistributions of source code must retain the above copyright 16 | # notice, this list of conditions and the following disclaimer. 17 | # 2. Redistributions in binary form must reproduce the above copyright 18 | # notice, this list of conditions and the following disclaimer in the 19 | # documentation and/or other materials provided with the distribution. 20 | # 3. Neither the name of the University nor the names of its contributors 21 | # may be used to endorse or promote products derived from this software 22 | # without specific prior written permission. 23 | # 24 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 | # SUCH DAMAGE. 35 | # 36 | # @(#)mkbuiltins 8.2 (Berkeley) 5/4/95 37 | 38 | tempfile=tempfile 39 | if ! type tempfile > /dev/null 2>&1 && ! type mktemp > /dev/null 2>&1; then 40 | _my_tempfile() 41 | { 42 | local index=0 43 | while test -f "${TMPDIR:-/tmp}/builtin.$$.$index"; do 44 | index=`expr $index + 1` 45 | done 46 | 47 | touch "${TMPDIR:-/tmp}/builtin.$$.$index" 48 | echo "${TMPDIR:-/tmp}/builtin.$$.$index" 49 | } 50 | 51 | tempfile="_my_tempfile" 52 | elif ! type tempfile > /dev/null 2>&1; then 53 | tempfile="mktemp ${TMPDIR:-/tmp}/builtin.XXXXXX" 54 | fi 55 | 56 | trap 'rm -f $temp $temp2' EXIT 57 | temp=$($tempfile) 58 | temp2=$($tempfile) 59 | 60 | builtins=$1 61 | 62 | exec > builtins.c 63 | cat <<\! 64 | /* 65 | * This file was generated by the mkbuiltins program. 66 | */ 67 | 68 | #include "shell.h" 69 | #include "builtins.h" 70 | 71 | ! 72 | < $builtins sed '/^#/d; /^$/d' > $temp 73 | awk '{ printf "int %s(int, char **);\n", $1}' $temp 74 | echo ' 75 | const struct builtincmd builtincmd[] = {' 76 | awk '{ for (i = 2 ; i <= NF ; i++) { 77 | line = $i "\t" $1 78 | if ($i ~ /^-/) 79 | line = $(++i) "\t" line 80 | print line 81 | }}' $temp | LC_COLLATE=C sort -k 1,1 | tee $temp2 | awk '{ 82 | opt = "" 83 | if (NF > 2) { 84 | opt = substr($2, 2) 85 | $2 = $3 86 | } 87 | printf "\t{ \"%s\", %s, %d },\n", $1, 88 | (opt ~ /n/) ? "NULL" : $2, 89 | (opt ~ /s/) + (opt ~ /[su]/) * 2 + (opt ~ /a/) * 4 90 | }' 91 | echo '};' 92 | 93 | exec > builtins.h 94 | cat <<\! 95 | /* 96 | * This file was generated by the mkbuiltins program. 97 | */ 98 | 99 | ! 100 | sed 's/ -[a-z]*//' $temp2 | nl -b a -v 0 | LC_COLLATE=C sort -u -k 3,3 | 101 | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ | 102 | awk '{ printf "#define %s (builtincmd + %d)\n", $3, $1}' 103 | printf '\n#define NUMBUILTINS %d\n' $(wc -l < $temp2) 104 | echo ' 105 | #define BUILTIN_SPECIAL 0x1 106 | #define BUILTIN_REGULAR 0x2 107 | #define BUILTIN_ASSIGN 0x4 108 | 109 | struct builtincmd { 110 | const char *name; 111 | int (*builtin)(int, char **); 112 | unsigned flags; 113 | }; 114 | 115 | extern const struct builtincmd builtincmd[];' 116 | -------------------------------------------------------------------------------- /src/parser.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)parser.h 8.3 (Berkeley) 5/4/95 35 | */ 36 | 37 | #include "token.h" 38 | 39 | /* control characters in argument strings */ 40 | #define CTL_FIRST -127 /* first 'special' character */ 41 | #define CTLESC -127 /* escape next character */ 42 | #define CTLVAR -126 /* variable defn */ 43 | #define CTLENDVAR -125 44 | #define CTLBACKQ -124 45 | #define CTLARI -122 /* arithmetic expression */ 46 | #define CTLENDARI -121 47 | #define CTLQUOTEMARK -120 48 | #define CTL_LAST -120 /* last 'special' character */ 49 | 50 | /* variable substitution byte (follows CTLVAR) */ 51 | #define VSTYPE 0x0f /* type of variable substitution */ 52 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ 53 | 54 | /* values of VSTYPE field */ 55 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ 56 | #define VSMINUS 0x2 /* ${var-text} */ 57 | #define VSPLUS 0x3 /* ${var+text} */ 58 | #define VSQUESTION 0x4 /* ${var?message} */ 59 | #define VSASSIGN 0x5 /* ${var=text} */ 60 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ 61 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ 62 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ 63 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ 64 | #define VSLENGTH 0xa /* ${#var} */ 65 | 66 | /* values of checkkwd variable */ 67 | #define CHKALIAS 0x1 68 | #define CHKKWD 0x2 69 | #define CHKNL 0x4 70 | #define CHKEOFMARK 0x8 71 | 72 | 73 | /* 74 | * NEOF is returned by parsecmd when it encounters an end of file. It 75 | * must be distinct from NULL, so we use the address of a variable that 76 | * happens to be handy. 77 | */ 78 | extern int lasttoken; 79 | extern int tokpushback; 80 | #define NEOF ((union node *)&tokpushback) 81 | extern int whichprompt; /* 1 == PS1, 2 == PS2 */ 82 | extern int checkkwd; 83 | 84 | 85 | union node *parsecmd(int); 86 | void fixredir(union node *, const char *, int); 87 | const char *getprompt(void *); 88 | const char *const *findkwd(const char *); 89 | char *endofname(const char *); 90 | const char *expandstr(const char *); 91 | 92 | static inline int 93 | goodname(const char *p) 94 | { 95 | return !*endofname(p); 96 | } 97 | 98 | static inline int parser_eof(void) 99 | { 100 | return tokpushback && lasttoken == TEOF; 101 | } 102 | -------------------------------------------------------------------------------- /src/output.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)output.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | #ifndef OUTPUT_INCL 38 | 39 | #include 40 | #ifdef USE_GLIBC_STDIO 41 | #include 42 | #endif 43 | #include 44 | 45 | struct output { 46 | #ifdef USE_GLIBC_STDIO 47 | FILE *stream; 48 | #endif 49 | char *nextc; 50 | char *end; 51 | char *buf; 52 | size_t bufsize; 53 | int fd; 54 | int flags; 55 | }; 56 | 57 | extern struct output output; 58 | extern struct output errout; 59 | extern struct output preverrout; 60 | #ifdef notyet 61 | extern struct output memout; 62 | #endif 63 | extern struct output *out1; 64 | extern struct output *out2; 65 | 66 | void outstr(const char *, struct output *); 67 | #ifndef USE_GLIBC_STDIO 68 | void outcslow(int, struct output *); 69 | #endif 70 | void flushall(void); 71 | void flushout(struct output *); 72 | void outfmt(struct output *, const char *, ...) 73 | __attribute__((__format__(__printf__,2,3))); 74 | void out1fmt(const char *, ...) 75 | __attribute__((__format__(__printf__,1,2))); 76 | int fmtstr(char *, size_t, const char *, ...) 77 | __attribute__((__format__(__printf__,3,4))); 78 | #ifndef USE_GLIBC_STDIO 79 | void doformat(struct output *, const char *, va_list); 80 | #endif 81 | int xwrite(int, const void *, size_t); 82 | #ifdef notyet 83 | #ifdef USE_GLIBC_STDIO 84 | void initstreams(void); 85 | void openmemout(void); 86 | int __closememout(void); 87 | #endif 88 | #endif 89 | 90 | static inline void 91 | freestdout() 92 | { 93 | output.nextc = output.buf; 94 | output.flags = 0; 95 | } 96 | 97 | #define OUTPUT_ERR 01 /* error occurred on output */ 98 | 99 | #ifdef USE_GLIBC_STDIO 100 | static inline void outc(int ch, struct output *file) 101 | { 102 | putc(ch, file->stream); 103 | } 104 | #define doformat(d, f, a) vfprintf((d)->stream, (f), (a)) 105 | #else 106 | static inline void outc(int ch, struct output *file) 107 | { 108 | if (file->nextc == file->end) 109 | outcslow(ch, file); 110 | else { 111 | *file->nextc = ch; 112 | file->nextc++; 113 | } 114 | } 115 | #endif 116 | #define out1c(c) outc((c), out1) 117 | #define out2c(c) outcslow((c), out2) 118 | #define out1str(s) outstr((s), out1) 119 | #define out2str(s) outstr((s), out2) 120 | #define outerr(f) (f)->flags 121 | 122 | #define OUTPUT_INCL 123 | #endif 124 | -------------------------------------------------------------------------------- /src/jobs.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)jobs.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | #include 38 | #include 39 | 40 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ 41 | #define FORK_FG 0 42 | #define FORK_BG 1 43 | #define FORK_NOJOB 2 44 | 45 | /* mode flags for showjob(s) */ 46 | #define SHOW_PGID 0x01 /* only show pgid - for jobs -p */ 47 | #define SHOW_PID 0x04 /* include process pid */ 48 | #define SHOW_CHANGED 0x08 /* only jobs whose state has changed */ 49 | 50 | 51 | /* 52 | * A job structure contains information about a job. A job is either a 53 | * single process or a set of processes contained in a pipeline. In the 54 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated 55 | * array of pids. 56 | */ 57 | 58 | struct procstat { 59 | pid_t pid; /* process id */ 60 | int status; /* last process status from wait() */ 61 | char *cmd; /* text of command being run */ 62 | }; 63 | 64 | struct job { 65 | struct procstat ps0; /* status of process */ 66 | struct procstat *ps; /* status or processes when more than one */ 67 | #if JOBS 68 | int stopstatus; /* status of a stopped job */ 69 | #endif 70 | uint32_t 71 | nprocs: 16, /* number of processes */ 72 | state: 8, 73 | #define JOBRUNNING 0 /* at least one proc running */ 74 | #define JOBSTOPPED 1 /* all procs are stopped */ 75 | #define JOBDONE 2 /* all procs are completed */ 76 | #if JOBS 77 | sigint: 1, /* job was killed by SIGINT */ 78 | jobctl: 1, /* job running under job control */ 79 | #endif 80 | waited: 1, /* true if this entry has been waited for */ 81 | used: 1, /* true if this entry is in used */ 82 | changed: 1; /* true if status has changed */ 83 | struct job *prev_job; /* previous job */ 84 | }; 85 | 86 | extern pid_t backgndpid; /* pid of last background process */ 87 | extern int job_warning; /* user was warned about stopped jobs */ 88 | #if JOBS 89 | extern int jobctl; /* true if doing job control */ 90 | #else 91 | #define jobctl 0 92 | #endif 93 | 94 | void setjobctl(int); 95 | int killcmd(int, char **); 96 | int fgcmd(int, char **); 97 | int bgcmd(int, char **); 98 | int jobscmd(int, char **); 99 | struct output; 100 | void showjobs(struct output *, int); 101 | int waitcmd(int, char **); 102 | struct job *makejob(union node *, int); 103 | int forkshell(struct job *, union node *, int); 104 | int waitforjob(struct job *); 105 | int stoppedjobs(void); 106 | 107 | #if ! JOBS 108 | #define setjobctl(on) ((void)(on)) /* do nothing */ 109 | #endif 110 | -------------------------------------------------------------------------------- /src/nodes.c.pat: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)nodes.c.pat 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | #include 38 | /* 39 | * Routine for dealing with parsed shell commands. 40 | */ 41 | 42 | #include "shell.h" 43 | #include "nodes.h" 44 | #include "memalloc.h" 45 | #include "machdep.h" 46 | #include "mystring.h" 47 | #include "system.h" 48 | 49 | 50 | int funcblocksize; /* size of structures in function */ 51 | int funcstringsize; /* size of strings in node */ 52 | pointer funcblock; /* block to allocate function from */ 53 | char *funcstring; /* block to allocate strings from */ 54 | 55 | %SIZES 56 | 57 | 58 | STATIC void calcsize(union node *); 59 | STATIC void sizenodelist(struct nodelist *); 60 | STATIC union node *copynode(union node *); 61 | STATIC struct nodelist *copynodelist(struct nodelist *); 62 | STATIC char *nodesavestr(char *); 63 | 64 | 65 | 66 | /* 67 | * Make a copy of a parse tree. 68 | */ 69 | 70 | struct funcnode * 71 | copyfunc(union node *n) 72 | { 73 | struct funcnode *f; 74 | size_t blocksize; 75 | 76 | funcblocksize = offsetof(struct funcnode, n); 77 | funcstringsize = 0; 78 | calcsize(n); 79 | blocksize = funcblocksize; 80 | f = ckmalloc(blocksize + funcstringsize); 81 | funcblock = (char *) f + offsetof(struct funcnode, n); 82 | funcstring = (char *) f + blocksize; 83 | copynode(n); 84 | f->count = 0; 85 | return f; 86 | } 87 | 88 | 89 | 90 | STATIC void 91 | calcsize(n) 92 | union node *n; 93 | { 94 | %CALCSIZE 95 | } 96 | 97 | 98 | 99 | STATIC void 100 | sizenodelist(lp) 101 | struct nodelist *lp; 102 | { 103 | while (lp) { 104 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); 105 | calcsize(lp->n); 106 | lp = lp->next; 107 | } 108 | } 109 | 110 | 111 | 112 | STATIC union node * 113 | copynode(n) 114 | union node *n; 115 | { 116 | union node *new; 117 | 118 | %COPY 119 | return new; 120 | } 121 | 122 | 123 | STATIC struct nodelist * 124 | copynodelist(lp) 125 | struct nodelist *lp; 126 | { 127 | struct nodelist *start; 128 | struct nodelist **lpp; 129 | 130 | lpp = &start; 131 | while (lp) { 132 | *lpp = funcblock; 133 | funcblock = (char *) funcblock + 134 | SHELL_ALIGN(sizeof(struct nodelist)); 135 | (*lpp)->n = copynode(lp->n); 136 | lp = lp->next; 137 | lpp = &(*lpp)->next; 138 | } 139 | *lpp = NULL; 140 | return start; 141 | } 142 | 143 | 144 | 145 | STATIC char * 146 | nodesavestr(s) 147 | char *s; 148 | { 149 | char *rtn = funcstring; 150 | 151 | funcstring = stpcpy(funcstring, s) + 1; 152 | return rtn; 153 | } 154 | 155 | 156 | 157 | /* 158 | * Free a parse tree. 159 | */ 160 | 161 | void 162 | freefunc(struct funcnode *f) 163 | { 164 | if (f && --f->count < 0) 165 | ckfree(f); 166 | } 167 | -------------------------------------------------------------------------------- /src/error.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)error.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | #include 38 | #include 39 | 40 | /* 41 | * Types of operations (passed to the errmsg routine). 42 | */ 43 | 44 | #define E_OPEN 01 /* opening a file */ 45 | #define E_CREAT 02 /* creating a file */ 46 | #define E_EXEC 04 /* executing a program */ 47 | 48 | 49 | /* 50 | * We enclose jmp_buf in a structure so that we can declare pointers to 51 | * jump locations. The global variable handler contains the location to 52 | * jump to when an exception occurs, and the global variable exception 53 | * contains a code identifying the exeception. To implement nested 54 | * exception handlers, the user should save the value of handler on entry 55 | * to an inner scope, set handler to point to a jmploc structure for the 56 | * inner scope, and restore handler on exit from the scope. 57 | */ 58 | 59 | struct jmploc { 60 | jmp_buf loc; 61 | }; 62 | 63 | extern struct jmploc *handler; 64 | extern int exception; 65 | 66 | /* exceptions */ 67 | #define EXINT 0 /* SIGINT received */ 68 | #define EXERROR 1 /* a generic error */ 69 | #define EXEXIT 4 /* exit the shell */ 70 | 71 | 72 | /* 73 | * These macros allow the user to suspend the handling of interrupt signals 74 | * over a period of time. This is similar to SIGHOLD to or sigblock, but 75 | * much more efficient and portable. (But hacking the kernel is so much 76 | * more fun than worrying about efficiency and portability. :-)) 77 | */ 78 | 79 | extern int suppressint; 80 | extern volatile sig_atomic_t intpending; 81 | 82 | #define barrier() ({ __asm__ __volatile__ ("": : :"memory"); }) 83 | #define INTOFF \ 84 | ({ \ 85 | suppressint++; \ 86 | barrier(); \ 87 | 0; \ 88 | }) 89 | #ifdef REALLY_SMALL 90 | void __inton(void); 91 | #define INTON __inton() 92 | #else 93 | #define INTON \ 94 | ({ \ 95 | barrier(); \ 96 | if (--suppressint == 0 && intpending) onint(); \ 97 | 0; \ 98 | }) 99 | #endif 100 | #define FORCEINTON \ 101 | ({ \ 102 | barrier(); \ 103 | suppressint = 0; \ 104 | if (intpending) onint(); \ 105 | 0; \ 106 | }) 107 | #define SAVEINT(v) ((v) = suppressint) 108 | #define RESTOREINT(v) \ 109 | ({ \ 110 | barrier(); \ 111 | if ((suppressint = (v)) == 0 && intpending) onint(); \ 112 | 0; \ 113 | }) 114 | #define CLEAR_PENDING_INT intpending = 0 115 | #define int_pending() intpending 116 | 117 | void exraise(int) __attribute__((__noreturn__)); 118 | #ifdef USE_NORETURN 119 | void onint(void) __attribute__((__noreturn__)); 120 | #else 121 | void onint(void); 122 | #endif 123 | extern int errlinno; 124 | void sh_error(const char *, ...) __attribute__((__noreturn__)); 125 | void exerror(int, const char *, ...) __attribute__((__noreturn__)); 126 | const char *errmsg(int, int); 127 | 128 | void sh_warnx(const char *, ...); 129 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_INIT(dash, 0.5.7) 2 | AM_INIT_AUTOMAKE([foreign]) 3 | AC_CONFIG_SRCDIR([src/main.c]) 4 | 5 | AC_CONFIG_HEADERS(config.h) 6 | 7 | dnl Checks for programs. 8 | AC_PROG_CC 9 | AC_GNU_SOURCE 10 | AC_PROG_YACC 11 | 12 | AC_MSG_CHECKING([for build system compiler]) 13 | if test "$cross_compiling" = yes; then 14 | CC_FOR_BUILD=${CC_FOR_BUILD-cc} 15 | else 16 | CC_FOR_BUILD=${CC} 17 | fi 18 | AC_MSG_RESULT(${CC_FOR_BUILD}) 19 | AC_SUBST(CC_FOR_BUILD) 20 | 21 | AC_MSG_CHECKING([for __attribute__((__alias__()))]) 22 | dash_cv_have_attribute_alias=no 23 | AC_LINK_IFELSE([AC_LANG_PROGRAM([void t() {} 24 | void a() __attribute__((__alias__("t")));], 25 | [a();])], 26 | [dash_cv_have_attribute_alias=yes]) 27 | AC_MSG_RESULT($dash_cv_have_attribute_alias) 28 | if test "x$dash_cv_have_attribute_alias" = xyes; then 29 | AC_DEFINE([HAVE_ALIAS_ATTRIBUTE], 1, 30 | [Define if __attribute__((__alias__())) is supported]) 31 | fi 32 | 33 | AC_ARG_ENABLE(static, AS_HELP_STRING(--enable-static, \ 34 | [Build statical linked program])) 35 | if test "$enable_static" = "yes"; then 36 | export LDFLAGS="-static -Wl,--fatal-warnings" 37 | fi 38 | 39 | AC_ARG_ENABLE(fnmatch, AS_HELP_STRING(--enable-fnmatch, \ 40 | [Use fnmatch(3) from libc])) 41 | AC_ARG_ENABLE(glob, AS_HELP_STRING(--enable-glob, [Use glob(3) from libc])) 42 | 43 | dnl Checks for libraries. 44 | 45 | dnl Checks for header files. 46 | AC_CHECK_HEADERS(alloca.h paths.h) 47 | 48 | dnl Check for declarations 49 | AC_CHECK_DECL([_PATH_BSHELL],,AC_DEFINE_UNQUOTED([_PATH_BSHELL], "/bin/sh", [Define to system shell path]),[ 50 | #ifdef HAVE_PATHS_H 51 | #include 52 | #endif 53 | ]) 54 | AC_CHECK_DECL([_PATH_DEVNULL],,AC_DEFINE_UNQUOTED([_PATH_DEVNULL], "/dev/null", [Define to devnull device node path]),[ 55 | #ifdef HAVE_PATHS_H 56 | #include 57 | #endif 58 | ]) 59 | AC_CHECK_DECL([_PATH_TTY],,AC_DEFINE_UNQUOTED([_PATH_TTY], "/dev/tty", [Define to tty device node path]),[ 60 | #ifdef HAVE_PATHS_H 61 | #include 62 | #endif 63 | ]) 64 | 65 | dnl Some systems lack isblank 66 | AC_CHECK_DECLS([isblank],,,[#include ]) 67 | 68 | dnl Check for sizes of types 69 | AC_CHECK_SIZEOF([intmax_t]) 70 | AC_CHECK_SIZEOF([long long int]) 71 | 72 | dnl Select a fallback format string for intmax_t in case we don't find PRIdMAX 73 | if test "x$ac_cv_sizeof_intmax_t" = "x$ac_cv_sizeof_long_long_int"; then 74 | intmax_fstr="lld" 75 | else 76 | intmax_fstr="jd" 77 | fi 78 | 79 | dnl Check for PRIdMAX and define it to a fallback if not found 80 | AC_CHECK_DECL([PRIdMAX],, 81 | [AC_DEFINE_UNQUOTED([PRIdMAX], "$intmax_fstr", 82 | [Define to printf format string for intmax_t])], 83 | [ 84 | #include 85 | ]) 86 | 87 | dnl Checks for library functions. 88 | AC_CHECK_FUNCS(bsearch faccessat getpwnam getrlimit isalpha killpg \ 89 | mempcpy \ 90 | sigsetmask stpcpy strchrnul strsignal strtod strtoimax \ 91 | strtoumax sysconf) 92 | 93 | if test "$enable_fnmatch" = yes; then 94 | use_fnmatch= 95 | AC_CHECK_FUNCS(fnmatch, use_fnmatch=yes) 96 | fi 97 | 98 | if test "$use_fnmatch" = yes && test "$enable_glob" = yes; then 99 | AC_CHECK_FUNCS(glob) 100 | fi 101 | 102 | dnl Check for klibc signal. 103 | AC_CHECK_FUNC(signal) 104 | if test "$ac_cv_func_signal" != yes; then 105 | AC_CHECK_FUNC(bsd_signal, 106 | [AC_DEFINE(signal, bsd_signal, 107 | [klibc has bsd_signal instead of signal])]) 108 | fi 109 | 110 | dnl Check for stat64 (dietlibc/klibc). 111 | AC_CHECK_FUNC(stat64,, [ 112 | AC_DEFINE(fstat64, fstat, [64-bit operations are the same as 32-bit]) 113 | AC_DEFINE(lstat64, lstat, [64-bit operations are the same as 32-bit]) 114 | AC_DEFINE(stat64, stat, [64-bit operations are the same as 32-bit]) 115 | ]) 116 | 117 | AC_CHECK_FUNC(open64,, [ 118 | AC_DEFINE(open64, open, [64-bit operations are the same as 32-bit]) 119 | ]) 120 | 121 | AC_ARG_WITH(libedit, AS_HELP_STRING(--with-libedit, [Compile with libedit support])) 122 | use_libedit= 123 | if test "$with_libedit" = "yes"; then 124 | AC_CHECK_LIB(edit, history_init, [ 125 | AC_CHECK_HEADER([histedit.h], [use_libedit="yes"], 126 | AC_MSG_ERROR( 127 | [Can't find required header files.]))]) 128 | fi 129 | if test "$use_libedit" != "yes"; then 130 | AC_DEFINE([SMALL], 1, [Define if you build with -DSMALL]) 131 | else 132 | export LIBS="$LIBS -ledit" 133 | fi 134 | AC_ARG_ENABLE(lineno, AS_HELP_STRING(--disable-lineno, \ 135 | [Disable LINENO support])) 136 | if test "$enable_lineno" != "no"; then 137 | AC_DEFINE([WITH_LINENO], 1, [Define if you build with -DWITH_LINENO]) 138 | fi 139 | AC_CONFIG_FILES([Makefile src/Makefile]) 140 | AC_OUTPUT 141 | -------------------------------------------------------------------------------- /src/system.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2004 3 | * Herbert Xu . All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3. The name of the author may not be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 | * SUCH DAMAGE. 27 | */ 28 | 29 | #ifndef HAVE_ISALPHA 30 | #define isalnum _isalnum 31 | #define iscntrl _iscntrl 32 | #define islower _islower 33 | #define isspace _isspace 34 | #define isalpha _isalpha 35 | #define isdigit _isdigit 36 | #define isprint _isprint 37 | #define isupper _isupper 38 | #define isblank _isblank 39 | #define isgraph _isgraph 40 | #define ispunct _ispunct 41 | #define isxdigit _isxdigit 42 | #include 43 | #undef isalnum 44 | #undef iscntrl 45 | #undef islower 46 | #undef isspace 47 | #undef isalpha 48 | #undef isdigit 49 | #undef isprint 50 | #undef isupper 51 | #undef isblank 52 | #undef isgraph 53 | #undef ispunct 54 | #undef isxdigit 55 | #endif 56 | 57 | #include 58 | #include 59 | 60 | #include "error.h" 61 | #include "output.h" 62 | #include "system.h" 63 | 64 | #ifndef HAVE_MEMPCPY 65 | void *mempcpy(void *dest, const void *src, size_t n) 66 | { 67 | return memcpy(dest, src, n) + n; 68 | } 69 | #endif 70 | 71 | #ifndef HAVE_STPCPY 72 | char *stpcpy(char *dest, const char *src) 73 | { 74 | size_t len = strlen(src); 75 | dest[len] = 0; 76 | return mempcpy(dest, src, len); 77 | } 78 | #endif 79 | 80 | #ifndef HAVE_STRCHRNUL 81 | char *strchrnul(const char *s, int c) 82 | { 83 | char *p = strchr(s, c); 84 | if (!p) 85 | p = (char *)s + strlen(s); 86 | return p; 87 | } 88 | #endif 89 | 90 | #ifndef HAVE_STRSIGNAL 91 | char *strsignal(int sig) 92 | { 93 | static char buf[19]; 94 | 95 | if ((unsigned)sig < NSIG && sys_siglist[sig]) 96 | return (char *)sys_siglist[sig]; 97 | fmtstr(buf, sizeof(buf), "Signal %d", sig); 98 | return buf; 99 | } 100 | #endif 101 | 102 | #ifndef HAVE_BSEARCH 103 | void *bsearch(const void *key, const void *base, size_t nmemb, 104 | size_t size, int (*cmp)(const void *, const void *)) 105 | { 106 | while (nmemb) { 107 | size_t mididx = nmemb / 2; 108 | const void *midobj = base + mididx * size; 109 | int diff = cmp(key, midobj); 110 | 111 | if (diff == 0) 112 | return (void *)midobj; 113 | 114 | if (diff > 0) { 115 | base = midobj + size; 116 | nmemb -= mididx + 1; 117 | } else 118 | nmemb = mididx; 119 | } 120 | 121 | return 0; 122 | } 123 | #endif 124 | 125 | #ifndef HAVE_SYSCONF 126 | long sysconf(int name) 127 | { 128 | sh_error("no sysconf for: %d", name); 129 | } 130 | #endif 131 | 132 | #ifndef HAVE_ISALPHA 133 | int isalnum(int c) { 134 | return _isalnum(c); 135 | } 136 | 137 | 138 | int iscntrl(int c) { 139 | return _iscntrl(c); 140 | } 141 | 142 | 143 | int islower(int c) { 144 | return _islower(c); 145 | } 146 | 147 | 148 | int isspace(int c) { 149 | return _isspace(c); 150 | } 151 | 152 | 153 | int isalpha(int c) { 154 | return _isalpha(c); 155 | } 156 | 157 | 158 | int isdigit(int c) { 159 | return _isdigit(c); 160 | } 161 | 162 | 163 | int isprint(int c) { 164 | return _isprint(c); 165 | } 166 | 167 | 168 | int isupper(int c) { 169 | return _isupper(c); 170 | } 171 | 172 | 173 | #if HAVE_DECL_ISBLANK 174 | int isblank(int c) { 175 | return _isblank(c); 176 | } 177 | #endif 178 | 179 | 180 | int isgraph(int c) { 181 | return _isgraph(c); 182 | } 183 | 184 | 185 | int ispunct(int c) { 186 | return _ispunct(c); 187 | } 188 | 189 | 190 | int isxdigit(int c) { 191 | return _isxdigit(c); 192 | } 193 | #endif 194 | 195 | #if !HAVE_DECL_ISBLANK 196 | int isblank(int c) { 197 | return c == ' ' || c == '\t'; 198 | } 199 | #endif 200 | -------------------------------------------------------------------------------- /src/nodetypes: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1991, 1993 2 | # The Regents of the University of California. All rights reserved. 3 | # Copyright (c) 1997-2005 4 | # Herbert Xu . All rights reserved. 5 | # 6 | # This code is derived from software contributed to Berkeley by 7 | # Kenneth Almquist. 8 | # 9 | # Redistribution and use in source and binary forms, with or without 10 | # modification, are permitted provided that the following conditions 11 | # are met: 12 | # 1. Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | # 2. Redistributions in binary form must reproduce the above copyright 15 | # notice, this list of conditions and the following disclaimer in the 16 | # documentation and/or other materials provided with the distribution. 17 | # 3. Neither the name of the University nor the names of its contributors 18 | # may be used to endorse or promote products derived from this software 19 | # without specific prior written permission. 20 | # 21 | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | # SUCH DAMAGE. 32 | # 33 | # @(#)nodetypes 8.2 (Berkeley) 5/4/95 34 | 35 | # This file describes the nodes used in parse trees. Unindented lines 36 | # contain a node type followed by a structure tag. Subsequent indented 37 | # lines specify the fields of the structure. Several node types can share 38 | # the same structure, in which case the fields of the structure should be 39 | # specified only once. 40 | # 41 | # A field of a structure is described by the name of the field followed 42 | # by a type. The currently implemented types are: 43 | # nodeptr - a pointer to a node 44 | # nodelist - a pointer to a list of nodes 45 | # string - a pointer to a nul terminated string 46 | # int - an integer 47 | # other - any type that can be copied by assignment 48 | # temp - a field that doesn't have to be copied when the node is copied 49 | # The last two types should be followed by the text of a C declaration for 50 | # the field. 51 | 52 | NCMD ncmd # a simple command 53 | type int 54 | linno int 55 | assign nodeptr # variable assignments 56 | args nodeptr # the arguments 57 | redirect nodeptr # list of file redirections 58 | 59 | NPIPE npipe # a pipeline 60 | type int 61 | backgnd int # set to run pipeline in background 62 | cmdlist nodelist # the commands in the pipeline 63 | 64 | NREDIR nredir # redirection (of a complex command) 65 | type int 66 | linno int 67 | n nodeptr # the command 68 | redirect nodeptr # list of file redirections 69 | 70 | NBACKGND nredir # run command in background 71 | NSUBSHELL nredir # run command in a subshell 72 | 73 | NAND nbinary # the && operator 74 | NOR nbinary # the || operator 75 | 76 | NSEMI nbinary # two commands separated by a semicolon 77 | type int 78 | ch1 nodeptr # the first child 79 | ch2 nodeptr # the second child 80 | 81 | NIF nif # the if statement. Elif clauses are handled 82 | type int # using multiple if nodes. 83 | test nodeptr # if test 84 | ifpart nodeptr # then ifpart 85 | elsepart nodeptr # else elsepart 86 | 87 | NWHILE nbinary # the while statement. First child is the test 88 | NUNTIL nbinary # the until statement 89 | 90 | NFOR nfor # the for statement 91 | type int 92 | linno int 93 | args nodeptr # for var in args 94 | body nodeptr # do body; done 95 | var string # the for variable 96 | 97 | NCASE ncase # a case statement 98 | type int 99 | linno int 100 | expr nodeptr # the word to switch on 101 | cases nodeptr # the list of cases (NCLIST nodes) 102 | 103 | NCLIST nclist # a case 104 | type int 105 | next nodeptr # the next case in list 106 | pattern nodeptr # list of patterns for this case 107 | body nodeptr # code to execute for this case 108 | 109 | 110 | NDEFUN ndefun # a function 111 | type int 112 | linno int 113 | text string 114 | body nodeptr 115 | 116 | NARG narg # represents a word 117 | type int 118 | next nodeptr # next word in list 119 | text string # the text of the word 120 | backquote nodelist # list of commands in back quotes 121 | 122 | NTO nfile # fd> fname 123 | NCLOBBER nfile # fd>| fname 124 | NFROM nfile # fd< fname 125 | NFROMTO nfile # fd<> fname 126 | NAPPEND nfile # fd>> fname 127 | type int 128 | next nodeptr # next redirection in list 129 | fd int # file descriptor being redirected 130 | fname nodeptr # file name, in a NARG node 131 | expfname temp char *expfname # actual file name 132 | 133 | NTOFD ndup # fd<&dupfd 134 | NFROMFD ndup # fd>&dupfd 135 | type int 136 | next nodeptr # next redirection in list 137 | fd int # file descriptor being redirected 138 | dupfd int # file descriptor to duplicate 139 | vname nodeptr # file name if fd>&$var 140 | 141 | 142 | NHERE nhere # fd<<\! 143 | NXHERE nhere # fd<. All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include "shell.h" 37 | #include "input.h" 38 | #include "output.h" 39 | #include "error.h" 40 | #include "memalloc.h" 41 | #include "mystring.h" 42 | #include "alias.h" 43 | #include "options.h" /* XXX for argptr (should remove?) */ 44 | 45 | #define ATABSIZE 39 46 | 47 | struct alias *atab[ATABSIZE]; 48 | 49 | STATIC void setalias(const char *, const char *); 50 | STATIC struct alias *freealias(struct alias *); 51 | STATIC struct alias **__lookupalias(const char *); 52 | 53 | STATIC 54 | void 55 | setalias(const char *name, const char *val) 56 | { 57 | struct alias *ap, **app; 58 | 59 | app = __lookupalias(name); 60 | ap = *app; 61 | INTOFF; 62 | if (ap) { 63 | if (!(ap->flag & ALIASINUSE)) { 64 | ckfree(ap->val); 65 | } 66 | ap->val = savestr(val); 67 | ap->flag &= ~ALIASDEAD; 68 | } else { 69 | /* not found */ 70 | ap = ckmalloc(sizeof (struct alias)); 71 | ap->name = savestr(name); 72 | ap->val = savestr(val); 73 | ap->flag = 0; 74 | ap->next = 0; 75 | *app = ap; 76 | } 77 | INTON; 78 | } 79 | 80 | int 81 | unalias(const char *name) 82 | { 83 | struct alias **app; 84 | 85 | app = __lookupalias(name); 86 | 87 | if (*app) { 88 | INTOFF; 89 | *app = freealias(*app); 90 | INTON; 91 | return (0); 92 | } 93 | 94 | return (1); 95 | } 96 | 97 | void 98 | rmaliases(void) 99 | { 100 | struct alias *ap, **app; 101 | int i; 102 | 103 | INTOFF; 104 | for (i = 0; i < ATABSIZE; i++) { 105 | app = &atab[i]; 106 | for (ap = *app; ap; ap = *app) { 107 | *app = freealias(*app); 108 | if (ap == *app) { 109 | app = &ap->next; 110 | } 111 | } 112 | } 113 | INTON; 114 | } 115 | 116 | struct alias * 117 | lookupalias(const char *name, int check) 118 | { 119 | struct alias *ap = *__lookupalias(name); 120 | 121 | if (check && ap && (ap->flag & ALIASINUSE)) 122 | return (NULL); 123 | return (ap); 124 | } 125 | 126 | /* 127 | * TODO - sort output 128 | */ 129 | int 130 | aliascmd(int argc, char **argv) 131 | { 132 | char *n, *v; 133 | int ret = 0; 134 | struct alias *ap; 135 | 136 | if (argc == 1) { 137 | int i; 138 | 139 | for (i = 0; i < ATABSIZE; i++) 140 | for (ap = atab[i]; ap; ap = ap->next) { 141 | printalias(ap); 142 | } 143 | return (0); 144 | } 145 | while ((n = *++argv) != NULL) { 146 | if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */ 147 | if ((ap = *__lookupalias(n)) == NULL) { 148 | outfmt(out2, "%s: %s not found\n", "alias", n); 149 | ret = 1; 150 | } else 151 | printalias(ap); 152 | } else { 153 | *v++ = '\0'; 154 | setalias(n, v); 155 | } 156 | } 157 | 158 | return (ret); 159 | } 160 | 161 | int 162 | unaliascmd(int argc, char **argv) 163 | { 164 | int i; 165 | 166 | while ((i = nextopt("a")) != '\0') { 167 | if (i == 'a') { 168 | rmaliases(); 169 | return (0); 170 | } 171 | } 172 | for (i = 0; *argptr; argptr++) { 173 | if (unalias(*argptr)) { 174 | outfmt(out2, "%s: %s not found\n", "unalias", *argptr); 175 | i = 1; 176 | } 177 | } 178 | 179 | return (i); 180 | } 181 | 182 | STATIC struct alias * 183 | freealias(struct alias *ap) { 184 | struct alias *next; 185 | 186 | if (ap->flag & ALIASINUSE) { 187 | ap->flag |= ALIASDEAD; 188 | return ap; 189 | } 190 | 191 | next = ap->next; 192 | ckfree(ap->name); 193 | ckfree(ap->val); 194 | ckfree(ap); 195 | return next; 196 | } 197 | 198 | void 199 | printalias(const struct alias *ap) { 200 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); 201 | } 202 | 203 | STATIC struct alias ** 204 | __lookupalias(const char *name) { 205 | unsigned int hashval; 206 | struct alias **app; 207 | const char *p; 208 | unsigned int ch; 209 | 210 | p = name; 211 | 212 | ch = (unsigned char)*p; 213 | hashval = ch << 4; 214 | while (ch) { 215 | hashval += ch; 216 | ch = (unsigned char)*++p; 217 | } 218 | app = &atab[hashval % ATABSIZE]; 219 | 220 | for (; *app; app = &(*app)->next) { 221 | if (equal(name, (*app)->name)) { 222 | break; 223 | } 224 | } 225 | 226 | return app; 227 | } 228 | -------------------------------------------------------------------------------- /src/arith_yylex.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2002 3 | * Herbert Xu. 4 | * Copyright (c) 1993 5 | * The Regents of the University of California. All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include 37 | #include 38 | #include "arith_yacc.h" 39 | #include "expand.h" 40 | #include "error.h" 41 | #include "shell.h" 42 | #include "memalloc.h" 43 | #include "syntax.h" 44 | #include "system.h" 45 | 46 | #if ARITH_BOR + 11 != ARITH_BORASS || ARITH_ASS + 11 != ARITH_EQ 47 | #error Arithmetic tokens are out of order. 48 | #endif 49 | 50 | extern const char *arith_buf; 51 | 52 | int 53 | yylex() 54 | { 55 | int value; 56 | const char *buf = arith_buf; 57 | const char *p; 58 | 59 | for (;;) { 60 | value = *buf; 61 | switch (value) { 62 | case ' ': 63 | case '\t': 64 | case '\n': 65 | buf++; 66 | continue; 67 | default: 68 | return ARITH_BAD; 69 | case '0': 70 | case '1': 71 | case '2': 72 | case '3': 73 | case '4': 74 | case '5': 75 | case '6': 76 | case '7': 77 | case '8': 78 | case '9': 79 | yylval.val = strtoimax(buf, (char **)&arith_buf, 0); 80 | return ARITH_NUM; 81 | case 'A': 82 | case 'B': 83 | case 'C': 84 | case 'D': 85 | case 'E': 86 | case 'F': 87 | case 'G': 88 | case 'H': 89 | case 'I': 90 | case 'J': 91 | case 'K': 92 | case 'L': 93 | case 'M': 94 | case 'N': 95 | case 'O': 96 | case 'P': 97 | case 'Q': 98 | case 'R': 99 | case 'S': 100 | case 'T': 101 | case 'U': 102 | case 'V': 103 | case 'W': 104 | case 'X': 105 | case 'Y': 106 | case 'Z': 107 | case '_': 108 | case 'a': 109 | case 'b': 110 | case 'c': 111 | case 'd': 112 | case 'e': 113 | case 'f': 114 | case 'g': 115 | case 'h': 116 | case 'i': 117 | case 'j': 118 | case 'k': 119 | case 'l': 120 | case 'm': 121 | case 'n': 122 | case 'o': 123 | case 'p': 124 | case 'q': 125 | case 'r': 126 | case 's': 127 | case 't': 128 | case 'u': 129 | case 'v': 130 | case 'w': 131 | case 'x': 132 | case 'y': 133 | case 'z': 134 | p = buf; 135 | while (buf++, is_in_name(*buf)) 136 | ; 137 | yylval.name = stalloc(buf - p + 1); 138 | *(char *)mempcpy(yylval.name, p, buf - p) = 0; 139 | value = ARITH_VAR; 140 | goto out; 141 | case '=': 142 | value += ARITH_ASS - '='; 143 | checkeq: 144 | buf++; 145 | checkeqcur: 146 | if (*buf != '=') 147 | goto out; 148 | value += 11; 149 | break; 150 | case '>': 151 | switch (*++buf) { 152 | case '=': 153 | value += ARITH_GE - '>'; 154 | break; 155 | case '>': 156 | value += ARITH_RSHIFT - '>'; 157 | goto checkeq; 158 | default: 159 | value += ARITH_GT - '>'; 160 | goto out; 161 | } 162 | break; 163 | case '<': 164 | switch (*++buf) { 165 | case '=': 166 | value += ARITH_LE - '<'; 167 | break; 168 | case '<': 169 | value += ARITH_LSHIFT - '<'; 170 | goto checkeq; 171 | default: 172 | value += ARITH_LT - '<'; 173 | goto out; 174 | } 175 | break; 176 | case '|': 177 | if (*++buf != '|') { 178 | value += ARITH_BOR - '|'; 179 | goto checkeqcur; 180 | } 181 | value += ARITH_OR - '|'; 182 | break; 183 | case '&': 184 | if (*++buf != '&') { 185 | value += ARITH_BAND - '&'; 186 | goto checkeqcur; 187 | } 188 | value += ARITH_AND - '&'; 189 | break; 190 | case '!': 191 | if (*++buf != '=') { 192 | value += ARITH_NOT - '!'; 193 | goto out; 194 | } 195 | value += ARITH_NE - '!'; 196 | break; 197 | case 0: 198 | goto out; 199 | case '(': 200 | value += ARITH_LPAREN - '('; 201 | break; 202 | case ')': 203 | value += ARITH_RPAREN - ')'; 204 | break; 205 | case '*': 206 | value += ARITH_MUL - '*'; 207 | goto checkeq; 208 | case '/': 209 | value += ARITH_DIV - '/'; 210 | goto checkeq; 211 | case '%': 212 | value += ARITH_REM - '%'; 213 | goto checkeq; 214 | case '+': 215 | value += ARITH_ADD - '+'; 216 | goto checkeq; 217 | case '-': 218 | value += ARITH_SUB - '-'; 219 | goto checkeq; 220 | case '~': 221 | value += ARITH_BNOT - '~'; 222 | break; 223 | case '^': 224 | value += ARITH_BXOR - '^'; 225 | goto checkeq; 226 | case '?': 227 | value += ARITH_QMARK - '?'; 228 | break; 229 | case ':': 230 | value += ARITH_COLON - ':'; 231 | break; 232 | } 233 | break; 234 | } 235 | 236 | buf++; 237 | out: 238 | arith_buf = buf; 239 | return value; 240 | } 241 | -------------------------------------------------------------------------------- /src/var.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | * 34 | * @(#)var.h 8.2 (Berkeley) 5/4/95 35 | */ 36 | 37 | #include 38 | 39 | /* 40 | * Shell variables. 41 | */ 42 | 43 | /* flags */ 44 | #define VEXPORT 0x01 /* variable is exported */ 45 | #define VREADONLY 0x02 /* variable cannot be modified */ 46 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ 47 | #define VTEXTFIXED 0x08 /* text is statically allocated */ 48 | #define VSTACK 0x10 /* text is allocated on the stack */ 49 | #define VUNSET 0x20 /* the variable is not set */ 50 | #define VNOFUNC 0x40 /* don't call the callback function */ 51 | #define VNOSET 0x80 /* do not set variable - just readonly test */ 52 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ 53 | 54 | 55 | struct var { 56 | struct var *next; /* next entry in hash list */ 57 | int flags; /* flags are defined above */ 58 | const char *text; /* name=value */ 59 | void (*func)(const char *); 60 | /* function to be called when */ 61 | /* the variable gets set/unset */ 62 | }; 63 | 64 | 65 | struct localvar { 66 | struct localvar *next; /* next local variable in list */ 67 | struct var *vp; /* the variable that was made local */ 68 | int flags; /* saved flags */ 69 | const char *text; /* saved text */ 70 | }; 71 | 72 | struct localvar_list; 73 | 74 | 75 | extern struct localvar *localvars; 76 | extern struct var varinit[]; 77 | 78 | #if ATTY 79 | #define vatty varinit[0] 80 | #define vifs varinit[1] 81 | #else 82 | #define vifs varinit[0] 83 | #endif 84 | #define vmail (&vifs)[1] 85 | #define vmpath (&vmail)[1] 86 | #define vpath (&vmpath)[1] 87 | #define vps1 (&vpath)[1] 88 | #define vps2 (&vps1)[1] 89 | #define vps4 (&vps2)[1] 90 | #define voptind (&vps4)[1] 91 | #ifdef WITH_LINENO 92 | #define vlineno (&voptind)[1] 93 | #endif 94 | #ifndef SMALL 95 | #ifdef WITH_LINENO 96 | #define vterm (&vlineno)[1] 97 | #else 98 | #define vterm (&voptind)[1] 99 | #endif 100 | #define vhistsize (&vterm)[1] 101 | #endif 102 | 103 | #ifdef IFS_BROKEN 104 | extern const char defifsvar[]; 105 | #define defifs (defifsvar + 4) 106 | #else 107 | extern const char defifs[]; 108 | #endif 109 | extern const char defpathvar[]; 110 | #define defpath (defpathvar + 5) 111 | 112 | extern int lineno; 113 | extern char linenovar[]; 114 | 115 | /* 116 | * The following macros access the values of the above variables. 117 | * They have to skip over the name. They return the null string 118 | * for unset variables. 119 | */ 120 | 121 | #define ifsval() (vifs.text + 4) 122 | #define ifsset() ((vifs.flags & VUNSET) == 0) 123 | #define mailval() (vmail.text + 5) 124 | #define mpathval() (vmpath.text + 9) 125 | #define pathval() (vpath.text + 5) 126 | #define ps1val() (vps1.text + 4) 127 | #define ps2val() (vps2.text + 4) 128 | #define ps4val() (vps4.text + 4) 129 | #define optindval() (voptind.text + 7) 130 | #define linenoval() (vlineno.text + 7) 131 | #ifndef SMALL 132 | #define histsizeval() (vhistsize.text + 9) 133 | #define termval() (vterm.text + 5) 134 | #endif 135 | 136 | #if ATTY 137 | #define attyset() ((vatty.flags & VUNSET) == 0) 138 | #endif 139 | #define mpathset() ((vmpath.flags & VUNSET) == 0) 140 | 141 | void initvar(void); 142 | struct var *setvar(const char *name, const char *val, int flags); 143 | intmax_t setvarint(const char *, intmax_t, int); 144 | struct var *setvareq(char *s, int flags); 145 | struct strlist; 146 | void listsetvar(struct strlist *, int); 147 | char *lookupvar(const char *); 148 | intmax_t lookupvarint(const char *); 149 | char **listvars(int, int, char ***); 150 | #define environment() listvars(VEXPORT, VUNSET, 0) 151 | int showvars(const char *, int, int); 152 | int exportcmd(int, char **); 153 | int localcmd(int, char **); 154 | void mklocal(char *); 155 | struct localvar_list *pushlocalvars(void); 156 | void poplocalvars(int); 157 | void unwindlocalvars(struct localvar_list *stop); 158 | int unsetcmd(int, char **); 159 | void unsetvar(const char *); 160 | int varcmp(const char *, const char *); 161 | 162 | static inline int varequal(const char *a, const char *b) { 163 | return !varcmp(a, b); 164 | } 165 | 166 | /* 167 | * Search the environment of a builtin command. 168 | */ 169 | 170 | static inline char *bltinlookup(const char *name) 171 | { 172 | return lookupvar(name); 173 | } 174 | 175 | 176 | -------------------------------------------------------------------------------- /src/error.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | /* 36 | * Errors and exceptions. 37 | */ 38 | 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | #include "shell.h" 47 | #include "main.h" 48 | #include "options.h" 49 | #include "output.h" 50 | #include "error.h" 51 | #include "show.h" 52 | #include "eval.h" 53 | #include "parser.h" 54 | #include "system.h" 55 | 56 | 57 | /* 58 | * Code to handle exceptions in C. 59 | */ 60 | 61 | struct jmploc *handler; 62 | int exception; 63 | int suppressint; 64 | volatile sig_atomic_t intpending; 65 | int errlinno; 66 | 67 | 68 | static void exverror(int, const char *, va_list) 69 | __attribute__((__noreturn__)); 70 | 71 | /* 72 | * Called to raise an exception. Since C doesn't include exceptions, we 73 | * just do a longjmp to the exception handler. The type of exception is 74 | * stored in the global variable "exception". 75 | */ 76 | 77 | void 78 | exraise(int e) 79 | { 80 | #ifdef DEBUG 81 | if (handler == NULL) 82 | abort(); 83 | #endif 84 | INTOFF; 85 | 86 | exception = e; 87 | longjmp(handler->loc, 1); 88 | } 89 | 90 | 91 | /* 92 | * Called from trap.c when a SIGINT is received. (If the user specifies 93 | * that SIGINT is to be trapped or ignored using the trap builtin, then 94 | * this routine is not called.) Suppressint is nonzero when interrupts 95 | * are held using the INTOFF macro. (The test for iflag is just 96 | * defensive programming.) 97 | */ 98 | 99 | void 100 | onint(void) { 101 | 102 | intpending = 0; 103 | sigclearmask(); 104 | if (!(rootshell && iflag)) { 105 | signal(SIGINT, SIG_DFL); 106 | raise(SIGINT); 107 | } 108 | exraise(EXINT); 109 | /* NOTREACHED */ 110 | } 111 | 112 | static void 113 | exvwarning2(const char *msg, va_list ap) 114 | { 115 | struct output *errs; 116 | const char *name; 117 | const char *fmt; 118 | 119 | errs = out2; 120 | name = arg0 ? arg0 : "sh"; 121 | if (!commandname) 122 | fmt = "%s: %d: "; 123 | else 124 | fmt = "%s: %d: %s: "; 125 | outfmt(errs, fmt, name, errlinno, commandname); 126 | doformat(errs, msg, ap); 127 | #if FLUSHERR 128 | outc('\n', errs); 129 | #else 130 | outcslow('\n', errs); 131 | #endif 132 | } 133 | 134 | #define exvwarning(a, b, c) exvwarning2(b, c) 135 | 136 | /* 137 | * Exverror is called to raise the error exception. If the second argument 138 | * is not NULL then error prints an error message using printf style 139 | * formatting. It then raises the error exception. 140 | */ 141 | static void 142 | exverror(int cond, const char *msg, va_list ap) 143 | { 144 | #ifdef DEBUG 145 | if (msg) { 146 | va_list aq; 147 | TRACE(("exverror(%d, \"", cond)); 148 | va_copy(aq, ap); 149 | TRACEV((msg, aq)); 150 | va_end(aq); 151 | TRACE(("\") pid=%d\n", getpid())); 152 | } else 153 | TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid())); 154 | if (msg) 155 | #endif 156 | exvwarning(-1, msg, ap); 157 | 158 | flushall(); 159 | exraise(cond); 160 | /* NOTREACHED */ 161 | } 162 | 163 | 164 | void 165 | sh_error(const char *msg, ...) 166 | { 167 | va_list ap; 168 | 169 | exitstatus = 2; 170 | 171 | va_start(ap, msg); 172 | exverror(EXERROR, msg, ap); 173 | /* NOTREACHED */ 174 | va_end(ap); 175 | } 176 | 177 | 178 | void 179 | exerror(int cond, const char *msg, ...) 180 | { 181 | va_list ap; 182 | 183 | va_start(ap, msg); 184 | exverror(cond, msg, ap); 185 | /* NOTREACHED */ 186 | va_end(ap); 187 | } 188 | 189 | /* 190 | * error/warning routines for external builtins 191 | */ 192 | 193 | void 194 | sh_warnx(const char *fmt, ...) 195 | { 196 | va_list ap; 197 | 198 | va_start(ap, fmt); 199 | exvwarning(-1, fmt, ap); 200 | va_end(ap); 201 | } 202 | 203 | 204 | /* 205 | * Return a string describing an error. The returned string may be a 206 | * pointer to a static buffer that will be overwritten on the next call. 207 | * Action describes the operation that got the error. 208 | */ 209 | 210 | const char * 211 | errmsg(int e, int action) 212 | { 213 | if (e != ENOENT && e != ENOTDIR) 214 | return strerror(e); 215 | 216 | if (action & E_OPEN) 217 | return "No such file"; 218 | else if (action & E_CREAT) 219 | return "Directory nonexistent"; 220 | else 221 | return "not found"; 222 | } 223 | 224 | 225 | #ifdef REALLY_SMALL 226 | void 227 | __inton() { 228 | if (--suppressint == 0 && intpending) { 229 | onint(); 230 | } 231 | } 232 | #endif 233 | -------------------------------------------------------------------------------- /src/mystring.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | /* 36 | * String functions. 37 | * 38 | * equal(s1, s2) Return true if strings are equal. 39 | * scopy(from, to) Copy a string. 40 | * scopyn(from, to, n) Like scopy, but checks for overflow. 41 | * number(s) Convert a string of digits to an integer. 42 | * is_number(s) Return true if s is a string of digits. 43 | */ 44 | 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include "shell.h" 52 | #include "syntax.h" 53 | #include "error.h" 54 | #include "mystring.h" 55 | #include "memalloc.h" 56 | #include "parser.h" 57 | #include "system.h" 58 | 59 | 60 | char nullstr[1]; /* zero length string */ 61 | const char spcstr[] = " "; 62 | const char snlfmt[] = "%s\n"; 63 | const char dolatstr[] = { CTLQUOTEMARK, CTLVAR, VSNORMAL, '@', '=', 64 | CTLQUOTEMARK, '\0' }; 65 | const char qchars[] = { CTLESC, CTLQUOTEMARK, 0 }; 66 | const char illnum[] = "Illegal number: %s"; 67 | const char homestr[] = "HOME"; 68 | 69 | /* 70 | * equal - #defined in mystring.h 71 | */ 72 | 73 | /* 74 | * scopy - #defined in mystring.h 75 | */ 76 | 77 | 78 | #if 0 79 | /* 80 | * scopyn - copy a string from "from" to "to", truncating the string 81 | * if necessary. "To" is always nul terminated, even if 82 | * truncation is performed. "Size" is the size of "to". 83 | */ 84 | 85 | void 86 | scopyn(const char *from, char *to, int size) 87 | { 88 | 89 | while (--size > 0) { 90 | if ((*to++ = *from++) == '\0') 91 | return; 92 | } 93 | *to = '\0'; 94 | } 95 | #endif 96 | 97 | 98 | /* 99 | * prefix -- see if pfx is a prefix of string. 100 | */ 101 | 102 | char * 103 | prefix(const char *string, const char *pfx) 104 | { 105 | while (*pfx) { 106 | if (*pfx++ != *string++) 107 | return 0; 108 | } 109 | return (char *) string; 110 | } 111 | 112 | void badnum(const char *s) 113 | { 114 | sh_error(illnum, s); 115 | } 116 | 117 | /* 118 | * Convert a string into an integer of type intmax_t. Alow trailing spaces. 119 | */ 120 | intmax_t atomax(const char *s, int base) 121 | { 122 | char *p; 123 | intmax_t r; 124 | 125 | errno = 0; 126 | r = strtoimax(s, &p, base); 127 | 128 | if (errno != 0) 129 | badnum(s); 130 | 131 | /* 132 | * Disallow completely blank strings in non-arithmetic (base != 0) 133 | * contexts. 134 | */ 135 | if (p == s && base) 136 | badnum(s); 137 | 138 | while (isspace((unsigned char)*p)) 139 | p++; 140 | 141 | if (*p) 142 | badnum(s); 143 | 144 | return r; 145 | } 146 | 147 | intmax_t atomax10(const char *s) 148 | { 149 | return atomax(s, 10); 150 | } 151 | 152 | /* 153 | * Convert a string of digits to an integer, printing an error message on 154 | * failure. 155 | */ 156 | 157 | int 158 | number(const char *s) 159 | { 160 | intmax_t n = atomax10(s); 161 | 162 | if (n < 0 || n > INT_MAX) 163 | badnum(s); 164 | 165 | return n; 166 | } 167 | 168 | 169 | 170 | /* 171 | * Check for a valid number. This should be elsewhere. 172 | */ 173 | 174 | int 175 | is_number(const char *p) 176 | { 177 | do { 178 | if (! is_digit(*p)) 179 | return 0; 180 | } while (*++p != '\0'); 181 | return 1; 182 | } 183 | 184 | 185 | /* 186 | * Produce a possibly single quoted string suitable as input to the shell. 187 | * The return string is allocated on the stack. 188 | */ 189 | 190 | char * 191 | single_quote(const char *s) { 192 | char *p; 193 | 194 | STARTSTACKSTR(p); 195 | 196 | do { 197 | char *q; 198 | size_t len; 199 | 200 | len = strchrnul(s, '\'') - s; 201 | 202 | q = p = makestrspace(len + 3, p); 203 | 204 | *q++ = '\''; 205 | q = mempcpy(q, s, len); 206 | *q++ = '\''; 207 | s += len; 208 | 209 | STADJUST(q - p, p); 210 | 211 | len = strspn(s, "'"); 212 | if (!len) 213 | break; 214 | 215 | q = p = makestrspace(len + 3, p); 216 | 217 | *q++ = '"'; 218 | q = mempcpy(q, s, len); 219 | *q++ = '"'; 220 | s += len; 221 | 222 | STADJUST(q - p, p); 223 | } while (*s); 224 | 225 | USTPUTC(0, p); 226 | 227 | return stackblock(); 228 | } 229 | 230 | /* 231 | * Like strdup but works with the ash stack. 232 | */ 233 | 234 | char * 235 | sstrdup(const char *p) 236 | { 237 | size_t len = strlen(p) + 1; 238 | return memcpy(stalloc(len), p, len); 239 | } 240 | 241 | /* 242 | * Wrapper around strcmp for qsort/bsearch/... 243 | */ 244 | int 245 | pstrcmp(const void *a, const void *b) 246 | { 247 | return strcmp(*(const char *const *) a, *(const char *const *) b); 248 | } 249 | 250 | /* 251 | * Find a string is in a sorted array. 252 | */ 253 | const char *const * 254 | findstring(const char *s, const char *const *array, size_t nmemb) 255 | { 256 | return bsearch(&s, array, nmemb, sizeof(const char *), pstrcmp); 257 | } 258 | -------------------------------------------------------------------------------- /src/cd.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | /* 43 | * The cd and pwd commands. 44 | */ 45 | 46 | #include "shell.h" 47 | #include "var.h" 48 | #include "nodes.h" /* for jobs.h */ 49 | #include "jobs.h" 50 | #include "options.h" 51 | #include "output.h" 52 | #include "memalloc.h" 53 | #include "error.h" 54 | #include "exec.h" 55 | #include "redir.h" 56 | #include "main.h" 57 | #include "mystring.h" 58 | #include "show.h" 59 | #include "cd.h" 60 | 61 | #define CD_PHYSICAL 1 62 | #define CD_PRINT 2 63 | 64 | STATIC int docd(const char *, int); 65 | STATIC const char *updatepwd(const char *); 66 | STATIC char *getpwd(void); 67 | STATIC int cdopt(void); 68 | 69 | STATIC char *curdir = nullstr; /* current working directory */ 70 | STATIC char *physdir = nullstr; /* physical working directory */ 71 | 72 | STATIC int 73 | cdopt() 74 | { 75 | int flags = 0; 76 | int i, j; 77 | 78 | j = 'L'; 79 | while ((i = nextopt("LP"))) { 80 | if (i != j) { 81 | flags ^= CD_PHYSICAL; 82 | j = i; 83 | } 84 | } 85 | 86 | return flags; 87 | } 88 | 89 | int 90 | cdcmd(int argc, char **argv) 91 | { 92 | const char *dest; 93 | const char *path; 94 | const char *p; 95 | char c; 96 | struct stat statb; 97 | int flags; 98 | 99 | flags = cdopt(); 100 | dest = *argptr; 101 | if (!dest) 102 | dest = bltinlookup(homestr); 103 | else if (dest[0] == '-' && dest[1] == '\0') { 104 | dest = bltinlookup("OLDPWD"); 105 | flags |= CD_PRINT; 106 | } 107 | if (!dest) 108 | dest = nullstr; 109 | if (*dest == '/') 110 | goto step6; 111 | if (*dest == '.') { 112 | c = dest[1]; 113 | dotdot: 114 | switch (c) { 115 | case '\0': 116 | case '/': 117 | goto step6; 118 | case '.': 119 | c = dest[2]; 120 | if (c != '.') 121 | goto dotdot; 122 | } 123 | } 124 | if (!*dest) 125 | dest = "."; 126 | path = bltinlookup("CDPATH"); 127 | while (path) { 128 | c = *path; 129 | p = padvance(&path, dest); 130 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { 131 | if (c && c != ':') 132 | flags |= CD_PRINT; 133 | docd: 134 | if (!docd(p, flags)) 135 | goto out; 136 | goto err; 137 | } 138 | } 139 | 140 | step6: 141 | p = dest; 142 | goto docd; 143 | 144 | err: 145 | sh_error("can't cd to %s", dest); 146 | /* NOTREACHED */ 147 | out: 148 | if (flags & CD_PRINT) 149 | out1fmt(snlfmt, curdir); 150 | return 0; 151 | } 152 | 153 | 154 | /* 155 | * Actually do the chdir. We also call hashcd to let the routines in exec.c 156 | * know that the current directory has changed. 157 | */ 158 | 159 | STATIC int 160 | docd(const char *dest, int flags) 161 | { 162 | const char *dir = 0; 163 | int err; 164 | 165 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); 166 | 167 | INTOFF; 168 | if (!(flags & CD_PHYSICAL)) { 169 | dir = updatepwd(dest); 170 | if (dir) 171 | dest = dir; 172 | } 173 | err = chdir(dest); 174 | if (err) 175 | goto out; 176 | setpwd(dir, 1); 177 | hashcd(); 178 | out: 179 | INTON; 180 | return err; 181 | } 182 | 183 | 184 | /* 185 | * Update curdir (the name of the current directory) in response to a 186 | * cd command. 187 | */ 188 | 189 | STATIC const char * 190 | updatepwd(const char *dir) 191 | { 192 | char *new; 193 | char *p; 194 | char *cdcomppath; 195 | const char *lim; 196 | 197 | cdcomppath = sstrdup(dir); 198 | STARTSTACKSTR(new); 199 | if (*dir != '/') { 200 | if (curdir == nullstr) 201 | return 0; 202 | new = stputs(curdir, new); 203 | } 204 | new = makestrspace(strlen(dir) + 2, new); 205 | lim = stackblock() + 1; 206 | if (*dir != '/') { 207 | if (new[-1] != '/') 208 | USTPUTC('/', new); 209 | if (new > lim && *lim == '/') 210 | lim++; 211 | } else { 212 | USTPUTC('/', new); 213 | cdcomppath++; 214 | if (dir[1] == '/' && dir[2] != '/') { 215 | USTPUTC('/', new); 216 | cdcomppath++; 217 | lim++; 218 | } 219 | } 220 | p = strtok(cdcomppath, "/"); 221 | while (p) { 222 | switch(*p) { 223 | case '.': 224 | if (p[1] == '.' && p[2] == '\0') { 225 | while (new > lim) { 226 | STUNPUTC(new); 227 | if (new[-1] == '/') 228 | break; 229 | } 230 | break; 231 | } else if (p[1] == '\0') 232 | break; 233 | /* fall through */ 234 | default: 235 | new = stputs(p, new); 236 | USTPUTC('/', new); 237 | } 238 | p = strtok(0, "/"); 239 | } 240 | if (new > lim) 241 | STUNPUTC(new); 242 | *new = 0; 243 | return stackblock(); 244 | } 245 | 246 | 247 | /* 248 | * Find out what the current directory is. If we already know the current 249 | * directory, this routine returns immediately. 250 | */ 251 | inline 252 | STATIC char * 253 | getpwd() 254 | { 255 | #ifdef __GLIBC__ 256 | char *dir = getcwd(0, 0); 257 | 258 | if (dir) 259 | return dir; 260 | #else 261 | char buf[PATH_MAX]; 262 | 263 | if (getcwd(buf, sizeof(buf))) 264 | return savestr(buf); 265 | #endif 266 | 267 | sh_warnx("getcwd() failed: %s", strerror(errno)); 268 | return nullstr; 269 | } 270 | 271 | int 272 | pwdcmd(int argc, char **argv) 273 | { 274 | int flags; 275 | const char *dir = curdir; 276 | 277 | flags = cdopt(); 278 | if (flags) { 279 | if (physdir == nullstr) 280 | setpwd(dir, 0); 281 | dir = physdir; 282 | } 283 | out1fmt(snlfmt, dir); 284 | return 0; 285 | } 286 | 287 | void 288 | setpwd(const char *val, int setold) 289 | { 290 | char *oldcur, *dir; 291 | 292 | oldcur = dir = curdir; 293 | 294 | if (setold) { 295 | setvar("OLDPWD", oldcur, VEXPORT); 296 | } 297 | INTOFF; 298 | if (physdir != nullstr) { 299 | if (physdir != oldcur) 300 | free(physdir); 301 | physdir = nullstr; 302 | } 303 | if (oldcur == val || !val) { 304 | char *s = getpwd(); 305 | physdir = s; 306 | if (!val) 307 | dir = s; 308 | } else 309 | dir = savestr(val); 310 | if (oldcur != dir && oldcur != nullstr) { 311 | free(oldcur); 312 | } 313 | curdir = dir; 314 | INTON; 315 | setvar("PWD", dir, VEXPORT); 316 | } 317 | -------------------------------------------------------------------------------- /src/bltin/test.1: -------------------------------------------------------------------------------- 1 | .\" Copyright (c) 1991, 1993 2 | .\" The Regents of the University of California. All rights reserved. 3 | .\" Copyright (c) 1997-2005 4 | .\" Herbert Xu . All rights reserved. 5 | .\" 6 | .\" This code is derived from software contributed to Berkeley by 7 | .\" the Institute of Electrical and Electronics Engineers, Inc. 8 | .\" 9 | .\" Redistribution and use in source and binary forms, with or without 10 | .\" modification, are permitted provided that the following conditions 11 | .\" are met: 12 | .\" 1. Redistributions of source code must retain the above copyright 13 | .\" notice, this list of conditions and the following disclaimer. 14 | .\" 2. Redistributions in binary form must reproduce the above copyright 15 | .\" notice, this list of conditions and the following disclaimer in the 16 | .\" documentation and/or other materials provided with the distribution. 17 | .\" 3. Neither the name of the University nor the names of its contributors 18 | .\" may be used to endorse or promote products derived from this software 19 | .\" without specific prior written permission. 20 | .\" 21 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | .\" SUCH DAMAGE. 32 | .\" 33 | .\" @(#)test.1 8.1 (Berkeley) 5/31/93 34 | .\" 35 | .Dd May 31, 1993 36 | .Dt TEST 1 37 | .Os 38 | .Sh NAME 39 | .Nm test , 40 | .Nm \&[ 41 | .Nd condition evaluation utility 42 | .Sh SYNOPSIS 43 | .Nm test 44 | .Ar expression 45 | .Nm \&[ 46 | .Ar expression Cm ] 47 | .Sh DESCRIPTION 48 | The 49 | .Nm test 50 | utility evaluates the expression and, if it evaluates 51 | to true, returns a zero (true) exit status; otherwise 52 | it returns 1 (false). 53 | If there is no expression, test also 54 | returns 1 (false). 55 | .Pp 56 | All operators and flags are separate arguments to the 57 | .Nm test 58 | utility. 59 | .Pp 60 | The following primaries are used to construct expression: 61 | .Bl -tag -width Ar 62 | .It Fl b Ar file 63 | True if 64 | .Ar file 65 | exists and is a block special 66 | file. 67 | .It Fl c Ar file 68 | True if 69 | .Ar file 70 | exists and is a character 71 | special file. 72 | .It Fl d Ar file 73 | True if 74 | .Ar file 75 | exists and is a directory. 76 | .It Fl e Ar file 77 | True if 78 | .Ar file 79 | exists (regardless of type). 80 | .It Fl f Ar file 81 | True if 82 | .Ar file 83 | exists and is a regular file. 84 | .It Fl g Ar file 85 | True if 86 | .Ar file 87 | exists and its set group ID flag 88 | is set. 89 | .It Fl h Ar file 90 | True if 91 | .Ar file 92 | exists and is a symbolic link. 93 | .It Fl k Ar file 94 | True if 95 | .Ar file 96 | exists and its sticky bit is set. 97 | .It Fl n Ar string 98 | True if the length of 99 | .Ar string 100 | is nonzero. 101 | .It Fl p Ar file 102 | True if 103 | .Ar file 104 | is a named pipe 105 | .Po Tn FIFO Pc . 106 | .It Fl r Ar file 107 | True if 108 | .Ar file 109 | exists and is readable. 110 | .It Fl s Ar file 111 | True if 112 | .Ar file 113 | exists and has a size greater 114 | than zero. 115 | .It Fl t Ar file_descriptor 116 | True if the file whose file descriptor number 117 | is 118 | .Ar file_descriptor 119 | is open and is associated with a terminal. 120 | .It Fl u Ar file 121 | True if 122 | .Ar file 123 | exists and its set user ID flag 124 | is set. 125 | .It Fl w Ar file 126 | True if 127 | .Ar file 128 | exists and is writable. 129 | True 130 | indicates only that the write flag is on. 131 | The file is not writable on a read-only file 132 | system even if this test indicates true. 133 | .It Fl x Ar file 134 | True if 135 | .Ar file 136 | exists and is executable. 137 | True 138 | indicates only that the execute flag is on. 139 | If 140 | .Ar file 141 | is a directory, true indicates that 142 | .Ar file 143 | can be searched. 144 | .It Fl z Ar string 145 | True if the length of 146 | .Ar string 147 | is zero. 148 | .It Fl L Ar file 149 | True if 150 | .Ar file 151 | exists and is a symbolic link. 152 | This operator is retained for compatibility with previous versions of 153 | this program. 154 | Do not rely on its existence; use 155 | .Fl h 156 | instead. 157 | .It Fl O Ar file 158 | True if 159 | .Ar file 160 | exists and its owner matches the effective user id of this process. 161 | .It Fl G Ar file 162 | True if 163 | .Ar file 164 | exists and its group matches the effective group id of this process. 165 | .It Fl S Ar file 166 | True if 167 | .Ar file 168 | exists and is a socket. 169 | .It Ar file1 Fl nt Ar file2 170 | True if 171 | .Ar file1 172 | exists and is newer than 173 | .Ar file2 . 174 | .It Ar file1 Fl ot Ar file2 175 | True if 176 | .Ar file1 177 | exists and is older than 178 | .Ar file2 . 179 | .It Ar file1 Fl ef Ar file2 180 | True if 181 | .Ar file1 182 | and 183 | .Ar file2 184 | exist and refer to the same file. 185 | .It Ar string 186 | True if 187 | .Ar string 188 | is not the null 189 | string. 190 | .It Ar \&s\&1 Cm \&= Ar \&s\&2 191 | True if the strings 192 | .Ar \&s\&1 193 | and 194 | .Ar \&s\&2 195 | are identical. 196 | .It Ar \&s\&1 Cm \&!= Ar \&s\&2 197 | True if the strings 198 | .Ar \&s\&1 199 | and 200 | .Ar \&s\&2 201 | are not identical. 202 | .It Ar \&s\&1 Cm \&\*[Lt] Ar \&s\&2 203 | True if string 204 | .Ar \&s\&1 205 | comes before 206 | .Ar \&s\&2 207 | based on the ASCII value of their characters. 208 | .It Ar \&s\&1 Cm \&\*[Gt] Ar \&s\&2 209 | True if string 210 | .Ar \&s\&1 211 | comes after 212 | .Ar \&s\&2 213 | based on the ASCII value of their characters. 214 | .It Ar \&n\&1 Fl \&eq Ar \&n\&2 215 | True if the integers 216 | .Ar \&n\&1 217 | and 218 | .Ar \&n\&2 219 | are algebraically 220 | equal. 221 | .It Ar \&n\&1 Fl \&ne Ar \&n\&2 222 | True if the integers 223 | .Ar \&n\&1 224 | and 225 | .Ar \&n\&2 226 | are not 227 | algebraically equal. 228 | .It Ar \&n\&1 Fl \> Ar \&n\&2 229 | True if the integer 230 | .Ar \&n\&1 231 | is algebraically 232 | greater than the integer 233 | .Ar \&n\&2 . 234 | .It Ar \&n\&1 Fl \&ge Ar \&n\&2 235 | True if the integer 236 | .Ar \&n\&1 237 | is algebraically 238 | greater than or equal to the integer 239 | .Ar \&n\&2 . 240 | .It Ar \&n\&1 Fl \< Ar \&n\&2 241 | True if the integer 242 | .Ar \&n\&1 243 | is algebraically less 244 | than the integer 245 | .Ar \&n\&2 . 246 | .It Ar \&n\&1 Fl \&le Ar \&n\&2 247 | True if the integer 248 | .Ar \&n\&1 249 | is algebraically less 250 | than or equal to the integer 251 | .Ar \&n\&2 . 252 | .El 253 | .Pp 254 | These primaries can be combined with the following operators: 255 | .Bl -tag -width Ar 256 | .It Cm \&! Ar expression 257 | True if 258 | .Ar expression 259 | is false. 260 | .It Ar expression1 Fl a Ar expression2 261 | True if both 262 | .Ar expression1 263 | and 264 | .Ar expression2 265 | are true. 266 | .It Ar expression1 Fl o Ar expression2 267 | True if either 268 | .Ar expression1 269 | or 270 | .Ar expression2 271 | are true. 272 | .It Cm \&( Ns Ar expression Ns Cm \&) 273 | True if expression is true. 274 | .El 275 | .Pp 276 | The 277 | .Fl a 278 | operator has higher precedence than the 279 | .Fl o 280 | operator. 281 | .Sh GRAMMAR AMBIGUITY 282 | The 283 | .Nm test 284 | grammar is inherently ambiguous. 285 | In order to assure a degree of consistency, the cases described in 286 | .St -p1003.2 287 | section 4.62.4, 288 | are evaluated consistently according to the rules specified in the 289 | standards document. 290 | All other cases are subject to the ambiguity in the command semantics. 291 | .Sh EXIT STATUS 292 | The 293 | .Nm test 294 | utility exits with one of the following values: 295 | .Bl -tag -width Ds 296 | .It 0 297 | expression evaluated to true. 298 | .It 1 299 | expression evaluated to false or expression was 300 | missing. 301 | .It \*[Gt]1 302 | An error occurred. 303 | .El 304 | .Sh STANDARDS 305 | The 306 | .Nm test 307 | utility implements a superset of the 308 | .St -p1003.2 309 | specification. 310 | -------------------------------------------------------------------------------- /src/arith_yacc.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 2007 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include 37 | #include "arith_yacc.h" 38 | #include "expand.h" 39 | #include "shell.h" 40 | #include "error.h" 41 | #include "output.h" 42 | #include "var.h" 43 | 44 | #if ARITH_BOR + 11 != ARITH_BORASS || ARITH_ASS + 11 != ARITH_EQ 45 | #error Arithmetic tokens are out of order. 46 | #endif 47 | 48 | static const char *arith_startbuf; 49 | 50 | const char *arith_buf; 51 | union yystype yylval; 52 | 53 | static int last_token; 54 | 55 | #define ARITH_PRECEDENCE(op, prec) [op - ARITH_BINOP_MIN] = prec 56 | 57 | static const char prec[ARITH_BINOP_MAX - ARITH_BINOP_MIN] = { 58 | ARITH_PRECEDENCE(ARITH_MUL, 0), 59 | ARITH_PRECEDENCE(ARITH_DIV, 0), 60 | ARITH_PRECEDENCE(ARITH_REM, 0), 61 | ARITH_PRECEDENCE(ARITH_ADD, 1), 62 | ARITH_PRECEDENCE(ARITH_SUB, 1), 63 | ARITH_PRECEDENCE(ARITH_LSHIFT, 2), 64 | ARITH_PRECEDENCE(ARITH_RSHIFT, 2), 65 | ARITH_PRECEDENCE(ARITH_LT, 3), 66 | ARITH_PRECEDENCE(ARITH_LE, 3), 67 | ARITH_PRECEDENCE(ARITH_GT, 3), 68 | ARITH_PRECEDENCE(ARITH_GE, 3), 69 | ARITH_PRECEDENCE(ARITH_EQ, 4), 70 | ARITH_PRECEDENCE(ARITH_NE, 4), 71 | ARITH_PRECEDENCE(ARITH_BAND, 5), 72 | ARITH_PRECEDENCE(ARITH_BXOR, 6), 73 | ARITH_PRECEDENCE(ARITH_BOR, 7), 74 | }; 75 | 76 | #define ARITH_MAX_PREC 8 77 | 78 | static void yyerror(const char *s) __attribute__ ((noreturn)); 79 | static void yyerror(const char *s) 80 | { 81 | sh_error("arithmetic expression: %s: \"%s\"", s, arith_startbuf); 82 | /* NOTREACHED */ 83 | } 84 | 85 | static inline int arith_prec(int op) 86 | { 87 | return prec[op - ARITH_BINOP_MIN]; 88 | } 89 | 90 | static inline int higher_prec(int op1, int op2) 91 | { 92 | return arith_prec(op1) < arith_prec(op2); 93 | } 94 | 95 | static intmax_t do_binop(int op, intmax_t a, intmax_t b) 96 | { 97 | switch (op) { 98 | default: 99 | case ARITH_REM: 100 | case ARITH_DIV: 101 | if (!b) 102 | yyerror("division by zero"); 103 | return op == ARITH_REM ? a % b : a / b; 104 | case ARITH_MUL: 105 | return a * b; 106 | case ARITH_ADD: 107 | return a + b; 108 | case ARITH_SUB: 109 | return a - b; 110 | case ARITH_LSHIFT: 111 | return a << b; 112 | case ARITH_RSHIFT: 113 | return a >> b; 114 | case ARITH_LT: 115 | return a < b; 116 | case ARITH_LE: 117 | return a <= b; 118 | case ARITH_GT: 119 | return a > b; 120 | case ARITH_GE: 121 | return a >= b; 122 | case ARITH_EQ: 123 | return a == b; 124 | case ARITH_NE: 125 | return a != b; 126 | case ARITH_BAND: 127 | return a & b; 128 | case ARITH_BXOR: 129 | return a ^ b; 130 | case ARITH_BOR: 131 | return a | b; 132 | } 133 | } 134 | 135 | static intmax_t assignment(int var, int noeval); 136 | 137 | static intmax_t primary(int token, union yystype *val, int op, int noeval) 138 | { 139 | intmax_t result; 140 | 141 | again: 142 | switch (token) { 143 | case ARITH_LPAREN: 144 | result = assignment(op, noeval); 145 | if (last_token != ARITH_RPAREN) 146 | yyerror("expecting ')'"); 147 | last_token = yylex(); 148 | return result; 149 | case ARITH_NUM: 150 | last_token = op; 151 | return val->val; 152 | case ARITH_VAR: 153 | last_token = op; 154 | return noeval ? val->val : lookupvarint(val->name); 155 | case ARITH_ADD: 156 | token = op; 157 | *val = yylval; 158 | op = yylex(); 159 | goto again; 160 | case ARITH_SUB: 161 | *val = yylval; 162 | return -primary(op, val, yylex(), noeval); 163 | case ARITH_NOT: 164 | *val = yylval; 165 | return !primary(op, val, yylex(), noeval); 166 | case ARITH_BNOT: 167 | *val = yylval; 168 | return ~primary(op, val, yylex(), noeval); 169 | default: 170 | yyerror("expecting primary"); 171 | } 172 | } 173 | 174 | static intmax_t binop2(intmax_t a, int op, int prec, int noeval) 175 | { 176 | for (;;) { 177 | union yystype val; 178 | intmax_t b; 179 | int op2; 180 | int token; 181 | 182 | token = yylex(); 183 | val = yylval; 184 | 185 | b = primary(token, &val, yylex(), noeval); 186 | 187 | op2 = last_token; 188 | if (op2 >= ARITH_BINOP_MIN && op2 < ARITH_BINOP_MAX && 189 | higher_prec(op2, op)) { 190 | b = binop2(b, op2, arith_prec(op), noeval); 191 | op2 = last_token; 192 | } 193 | 194 | a = noeval ? b : do_binop(op, a, b); 195 | 196 | if (op2 < ARITH_BINOP_MIN || op2 >= ARITH_BINOP_MAX || 197 | arith_prec(op2) >= prec) 198 | return a; 199 | 200 | op = op2; 201 | } 202 | } 203 | 204 | static intmax_t binop(int token, union yystype *val, int op, int noeval) 205 | { 206 | intmax_t a = primary(token, val, op, noeval); 207 | 208 | op = last_token; 209 | if (op < ARITH_BINOP_MIN || op >= ARITH_BINOP_MAX) 210 | return a; 211 | 212 | return binop2(a, op, ARITH_MAX_PREC, noeval); 213 | } 214 | 215 | static intmax_t and(int token, union yystype *val, int op, int noeval) 216 | { 217 | intmax_t a = binop(token, val, op, noeval); 218 | intmax_t b; 219 | 220 | op = last_token; 221 | if (op != ARITH_AND) 222 | return a; 223 | 224 | token = yylex(); 225 | *val = yylval; 226 | 227 | b = and(token, val, yylex(), noeval | !a); 228 | 229 | return a && b; 230 | } 231 | 232 | static intmax_t or(int token, union yystype *val, int op, int noeval) 233 | { 234 | intmax_t a = and(token, val, op, noeval); 235 | intmax_t b; 236 | 237 | op = last_token; 238 | if (op != ARITH_OR) 239 | return a; 240 | 241 | token = yylex(); 242 | *val = yylval; 243 | 244 | b = or(token, val, yylex(), noeval | !!a); 245 | 246 | return a || b; 247 | } 248 | 249 | static intmax_t cond(int token, union yystype *val, int op, int noeval) 250 | { 251 | intmax_t a = or(token, val, op, noeval); 252 | intmax_t b; 253 | intmax_t c; 254 | 255 | if (last_token != ARITH_QMARK) 256 | return a; 257 | 258 | b = assignment(yylex(), noeval | !a); 259 | 260 | if (last_token != ARITH_COLON) 261 | yyerror("expecting ':'"); 262 | 263 | token = yylex(); 264 | *val = yylval; 265 | 266 | c = cond(token, val, yylex(), noeval | !!a); 267 | 268 | return a ? b : c; 269 | } 270 | 271 | static intmax_t assignment(int var, int noeval) 272 | { 273 | union yystype val = yylval; 274 | int op = yylex(); 275 | intmax_t result; 276 | 277 | if (var != ARITH_VAR) 278 | return cond(var, &val, op, noeval); 279 | 280 | if (op != ARITH_ASS && (op < ARITH_ASS_MIN || op >= ARITH_ASS_MAX)) 281 | return cond(var, &val, op, noeval); 282 | 283 | result = assignment(yylex(), noeval); 284 | if (noeval) 285 | return result; 286 | 287 | return setvarint(val.name, 288 | op == ARITH_ASS ? result : 289 | do_binop(op - 11, lookupvarint(val.name), result), 0); 290 | } 291 | 292 | intmax_t arith(const char *s) 293 | { 294 | intmax_t result; 295 | 296 | arith_buf = arith_startbuf = s; 297 | 298 | result = assignment(yylex(), 0); 299 | 300 | if (last_token) 301 | yyerror("expecting EOF"); 302 | 303 | return result; 304 | } 305 | -------------------------------------------------------------------------------- /src/memalloc.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include 37 | 38 | #include "shell.h" 39 | #include "output.h" 40 | #include "memalloc.h" 41 | #include "error.h" 42 | #include "machdep.h" 43 | #include "mystring.h" 44 | #include "system.h" 45 | 46 | /* 47 | * Like malloc, but returns an error when out of space. 48 | */ 49 | 50 | pointer 51 | ckmalloc(size_t nbytes) 52 | { 53 | pointer p; 54 | 55 | p = malloc(nbytes); 56 | if (p == NULL) 57 | sh_error("Out of space"); 58 | return p; 59 | } 60 | 61 | 62 | /* 63 | * Same for realloc. 64 | */ 65 | 66 | pointer 67 | ckrealloc(pointer p, size_t nbytes) 68 | { 69 | p = realloc(p, nbytes); 70 | if (p == NULL) 71 | sh_error("Out of space"); 72 | return p; 73 | } 74 | 75 | 76 | /* 77 | * Make a copy of a string in safe storage. 78 | */ 79 | 80 | char * 81 | savestr(const char *s) 82 | { 83 | char *p = strdup(s); 84 | if (!p) 85 | sh_error("Out of space"); 86 | return p; 87 | } 88 | 89 | 90 | /* 91 | * Parse trees for commands are allocated in lifo order, so we use a stack 92 | * to make this more efficient, and also to avoid all sorts of exception 93 | * handling code to handle interrupts in the middle of a parse. 94 | * 95 | * The size 504 was chosen because the Ultrix malloc handles that size 96 | * well. 97 | */ 98 | 99 | /* minimum size of a block */ 100 | #define MINSIZE SHELL_ALIGN(504) 101 | 102 | struct stack_block { 103 | struct stack_block *prev; 104 | char space[MINSIZE]; 105 | }; 106 | 107 | struct stack_block stackbase; 108 | struct stack_block *stackp = &stackbase; 109 | char *stacknxt = stackbase.space; 110 | size_t stacknleft = MINSIZE; 111 | char *sstrend = stackbase.space + MINSIZE; 112 | 113 | pointer 114 | stalloc(size_t nbytes) 115 | { 116 | char *p; 117 | size_t aligned; 118 | 119 | aligned = SHELL_ALIGN(nbytes); 120 | if (aligned > stacknleft) { 121 | size_t len; 122 | size_t blocksize; 123 | struct stack_block *sp; 124 | 125 | blocksize = aligned; 126 | if (blocksize < MINSIZE) 127 | blocksize = MINSIZE; 128 | len = sizeof(struct stack_block) - MINSIZE + blocksize; 129 | if (len < blocksize) 130 | sh_error("Out of space"); 131 | INTOFF; 132 | sp = ckmalloc(len); 133 | sp->prev = stackp; 134 | stacknxt = sp->space; 135 | stacknleft = blocksize; 136 | sstrend = stacknxt + blocksize; 137 | stackp = sp; 138 | INTON; 139 | } 140 | p = stacknxt; 141 | stacknxt += aligned; 142 | stacknleft -= aligned; 143 | return p; 144 | } 145 | 146 | 147 | void 148 | stunalloc(pointer p) 149 | { 150 | #ifdef DEBUG 151 | if (!p || (stacknxt < (char *)p) || ((char *)p < stackp->space)) { 152 | write(2, "stunalloc\n", 10); 153 | abort(); 154 | } 155 | #endif 156 | stacknleft += stacknxt - (char *)p; 157 | stacknxt = p; 158 | } 159 | 160 | 161 | 162 | void pushstackmark(struct stackmark *mark, size_t len) 163 | { 164 | mark->stackp = stackp; 165 | mark->stacknxt = stacknxt; 166 | mark->stacknleft = stacknleft; 167 | grabstackblock(len); 168 | } 169 | 170 | void setstackmark(struct stackmark *mark) 171 | { 172 | pushstackmark(mark, stacknxt == stackp->space && stackp != &stackbase); 173 | } 174 | 175 | 176 | void 177 | popstackmark(struct stackmark *mark) 178 | { 179 | struct stack_block *sp; 180 | 181 | INTOFF; 182 | while (stackp != mark->stackp) { 183 | sp = stackp; 184 | stackp = sp->prev; 185 | ckfree(sp); 186 | } 187 | stacknxt = mark->stacknxt; 188 | stacknleft = mark->stacknleft; 189 | sstrend = mark->stacknxt + mark->stacknleft; 190 | INTON; 191 | } 192 | 193 | 194 | /* 195 | * When the parser reads in a string, it wants to stick the string on the 196 | * stack and only adjust the stack pointer when it knows how big the 197 | * string is. Stackblock (defined in stack.h) returns a pointer to a block 198 | * of space on top of the stack and stackblocklen returns the length of 199 | * this block. Growstackblock will grow this space by at least one byte, 200 | * possibly moving it (like realloc). Grabstackblock actually allocates the 201 | * part of the block that has been used. 202 | */ 203 | 204 | void 205 | growstackblock(void) 206 | { 207 | size_t newlen; 208 | 209 | newlen = stacknleft * 2; 210 | if (newlen < stacknleft) 211 | sh_error("Out of space"); 212 | if (newlen < 128) 213 | newlen += 128; 214 | 215 | if (stacknxt == stackp->space && stackp != &stackbase) { 216 | struct stack_block *sp; 217 | struct stack_block *prevstackp; 218 | size_t grosslen; 219 | 220 | INTOFF; 221 | sp = stackp; 222 | prevstackp = sp->prev; 223 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; 224 | sp = ckrealloc((pointer)sp, grosslen); 225 | sp->prev = prevstackp; 226 | stackp = sp; 227 | stacknxt = sp->space; 228 | stacknleft = newlen; 229 | sstrend = sp->space + newlen; 230 | INTON; 231 | } else { 232 | char *oldspace = stacknxt; 233 | int oldlen = stacknleft; 234 | char *p = stalloc(newlen); 235 | 236 | /* free the space we just allocated */ 237 | stacknxt = memcpy(p, oldspace, oldlen); 238 | stacknleft += newlen; 239 | } 240 | } 241 | 242 | /* 243 | * The following routines are somewhat easier to use than the above. 244 | * The user declares a variable of type STACKSTR, which may be declared 245 | * to be a register. The macro STARTSTACKSTR initializes things. Then 246 | * the user uses the macro STPUTC to add characters to the string. In 247 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is 248 | * grown as necessary. When the user is done, she can just leave the 249 | * string there and refer to it using stackblock(). Or she can allocate 250 | * the space for it using grabstackstr(). If it is necessary to allow 251 | * someone else to use the stack temporarily and then continue to grow 252 | * the string, the user should use grabstack to allocate the space, and 253 | * then call ungrabstr(p) to return to the previous mode of operation. 254 | * 255 | * USTPUTC is like STPUTC except that it doesn't check for overflow. 256 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there 257 | * is space for at least one character. 258 | */ 259 | 260 | void * 261 | growstackstr(void) 262 | { 263 | size_t len = stackblocksize(); 264 | growstackblock(); 265 | return stackblock() + len; 266 | } 267 | 268 | /* 269 | * Called from CHECKSTRSPACE. 270 | */ 271 | 272 | char * 273 | makestrspace(size_t newlen, char *p) 274 | { 275 | size_t len = p - stacknxt; 276 | size_t size; 277 | 278 | for (;;) { 279 | size_t nleft; 280 | 281 | size = stackblocksize(); 282 | nleft = size - len; 283 | if (nleft >= newlen) 284 | break; 285 | growstackblock(); 286 | } 287 | return stackblock() + len; 288 | } 289 | 290 | char * 291 | stnputs(const char *s, size_t n, char *p) 292 | { 293 | p = makestrspace(n, p); 294 | p = mempcpy(p, s, n); 295 | return p; 296 | } 297 | 298 | char * 299 | stputs(const char *s, char *p) 300 | { 301 | return stnputs(s, strlen(s), p); 302 | } 303 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | 42 | #include "shell.h" 43 | #include "main.h" 44 | #include "mail.h" 45 | #include "options.h" 46 | #include "output.h" 47 | #include "parser.h" 48 | #include "nodes.h" 49 | #include "expand.h" 50 | #include "eval.h" 51 | #include "jobs.h" 52 | #include "input.h" 53 | #include "trap.h" 54 | #include "var.h" 55 | #include "show.h" 56 | #include "memalloc.h" 57 | #include "error.h" 58 | #include "init.h" 59 | #include "mystring.h" 60 | #include "exec.h" 61 | #include "cd.h" 62 | 63 | #ifdef HETIO 64 | #include "hetio.h" 65 | #endif 66 | 67 | #define PROFILE 0 68 | 69 | int rootpid; 70 | int shlvl; 71 | #ifdef __GLIBC__ 72 | int *dash_errno; 73 | #endif 74 | #if PROFILE 75 | short profile_buf[16384]; 76 | extern int etext(); 77 | #endif 78 | 79 | STATIC void read_profile(const char *); 80 | STATIC char *find_dot_file(char *); 81 | static int cmdloop(int); 82 | int main(int, char **); 83 | 84 | /* 85 | * Main routine. We initialize things, parse the arguments, execute 86 | * profiles if we're a login shell, and then call cmdloop to execute 87 | * commands. The setjmp call sets up the location to jump to when an 88 | * exception occurs. When an exception occurs the variable "state" 89 | * is used to figure out how far we had gotten. 90 | */ 91 | 92 | int 93 | main(int argc, char **argv) 94 | { 95 | char *shinit; 96 | volatile int state; 97 | struct jmploc jmploc; 98 | struct stackmark smark; 99 | int login; 100 | 101 | #ifdef __GLIBC__ 102 | dash_errno = __errno_location(); 103 | #endif 104 | 105 | #if PROFILE 106 | monitor(4, etext, profile_buf, sizeof profile_buf, 50); 107 | #endif 108 | state = 0; 109 | if (unlikely(setjmp(jmploc.loc))) { 110 | int e; 111 | int s; 112 | 113 | reset(); 114 | 115 | e = exception; 116 | 117 | s = state; 118 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) 119 | exitshell(); 120 | 121 | if (e == EXINT 122 | #if ATTY 123 | && (! attyset() || equal(termval(), "emacs")) 124 | #endif 125 | ) { 126 | out2c('\n'); 127 | #ifdef FLUSHERR 128 | flushout(out2); 129 | #endif 130 | } 131 | popstackmark(&smark); 132 | FORCEINTON; /* enable interrupts */ 133 | if (s == 1) 134 | goto state1; 135 | else if (s == 2) 136 | goto state2; 137 | else if (s == 3) 138 | goto state3; 139 | else 140 | goto state4; 141 | } 142 | handler = &jmploc; 143 | #ifdef DEBUG 144 | opentrace(); 145 | trputs("Shell args: "); trargs(argv); 146 | #endif 147 | rootpid = getpid(); 148 | init(); 149 | setstackmark(&smark); 150 | login = procargs(argc, argv); 151 | if (login) { 152 | state = 1; 153 | read_profile("/etc/profile"); 154 | state1: 155 | state = 2; 156 | read_profile("$HOME/.profile"); 157 | } 158 | state2: 159 | state = 3; 160 | if ( 161 | #ifndef linux 162 | getuid() == geteuid() && getgid() == getegid() && 163 | #endif 164 | iflag 165 | ) { 166 | if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') { 167 | read_profile(shinit); 168 | } 169 | } 170 | popstackmark(&smark); 171 | state3: 172 | state = 4; 173 | if (minusc) 174 | evalstring(minusc, sflag ? 0 : EV_EXIT); 175 | 176 | if (sflag || minusc == NULL) { 177 | state4: /* XXX ??? - why isn't this before the "if" statement */ 178 | cmdloop(1); 179 | } 180 | #if PROFILE 181 | monitor(0); 182 | #endif 183 | #if GPROF 184 | { 185 | extern void _mcleanup(void); 186 | _mcleanup(); 187 | } 188 | #endif 189 | exitshell(); 190 | /* NOTREACHED */ 191 | } 192 | 193 | 194 | /* 195 | * Read and execute commands. "Top" is nonzero for the top level command 196 | * loop; it turns on prompting if the shell is interactive. 197 | */ 198 | 199 | static int 200 | cmdloop(int top) 201 | { 202 | union node *n; 203 | struct stackmark smark; 204 | int inter; 205 | int status = 0; 206 | int numeof = 0; 207 | 208 | TRACE(("cmdloop(%d) called\n", top)); 209 | #ifdef HETIO 210 | if(iflag && top) 211 | hetio_init(); 212 | #endif 213 | for (;;) { 214 | int skip; 215 | 216 | setstackmark(&smark); 217 | if (jobctl) 218 | showjobs(out2, SHOW_CHANGED); 219 | inter = 0; 220 | if (iflag && top) { 221 | inter++; 222 | chkmail(); 223 | } 224 | n = parsecmd(inter); 225 | /* showtree(n); DEBUG */ 226 | if (n == NEOF) { 227 | if (!top || numeof >= 50) 228 | break; 229 | if (!stoppedjobs()) { 230 | if (!Iflag) 231 | break; 232 | out2str("\nUse \"exit\" to leave shell.\n"); 233 | } 234 | numeof++; 235 | } else if (nflag == 0) { 236 | job_warning = (job_warning == 2) ? 1 : 0; 237 | numeof = 0; 238 | evaltree(n, 0); 239 | status = exitstatus; 240 | } 241 | popstackmark(&smark); 242 | 243 | skip = evalskip; 244 | if (skip) { 245 | evalskip &= ~SKIPFUNC; 246 | break; 247 | } 248 | } 249 | 250 | return status; 251 | } 252 | 253 | 254 | 255 | /* 256 | * Read /etc/profile or .profile. Return on error. 257 | */ 258 | 259 | STATIC void 260 | read_profile(const char *name) 261 | { 262 | name = expandstr(name); 263 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) 264 | return; 265 | 266 | cmdloop(0); 267 | popfile(); 268 | } 269 | 270 | 271 | 272 | /* 273 | * Read a file containing shell functions. 274 | */ 275 | 276 | void 277 | readcmdfile(char *name) 278 | { 279 | setinputfile(name, INPUT_PUSH_FILE); 280 | cmdloop(0); 281 | popfile(); 282 | } 283 | 284 | 285 | 286 | /* 287 | * Take commands from a file. To be compatible we should do a path 288 | * search for the file, which is necessary to find sub-commands. 289 | */ 290 | 291 | 292 | STATIC char * 293 | find_dot_file(char *basename) 294 | { 295 | char *fullname; 296 | const char *path = pathval(); 297 | struct stat statb; 298 | 299 | /* don't try this for absolute or relative paths */ 300 | if (strchr(basename, '/')) 301 | return basename; 302 | 303 | while ((fullname = padvance(&path, basename)) != NULL) { 304 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { 305 | /* 306 | * Don't bother freeing here, since it will 307 | * be freed by the caller. 308 | */ 309 | return fullname; 310 | } 311 | stunalloc(fullname); 312 | } 313 | 314 | /* not found in the PATH */ 315 | sh_error("%s: not found", basename); 316 | /* NOTREACHED */ 317 | } 318 | 319 | int 320 | dotcmd(int argc, char **argv) 321 | { 322 | int status = 0; 323 | 324 | if (argc >= 2) { /* That's what SVR2 does */ 325 | char *fullname; 326 | 327 | fullname = find_dot_file(argv[1]); 328 | setinputfile(fullname, INPUT_PUSH_FILE); 329 | commandname = fullname; 330 | status = cmdloop(0); 331 | popfile(); 332 | } 333 | return status; 334 | } 335 | 336 | 337 | int 338 | exitcmd(int argc, char **argv) 339 | { 340 | if (stoppedjobs()) 341 | return 0; 342 | if (argc > 1) 343 | exitstatus = number(argv[1]); 344 | exraise(EXEXIT); 345 | /* NOTREACHED */ 346 | } 347 | -------------------------------------------------------------------------------- /src/mksyntax.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * Copyright (c) 1997-2005 5 | * Herbert Xu . All rights reserved. 6 | * 7 | * This code is derived from software contributed to Berkeley by 8 | * Kenneth Almquist. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. Neither the name of the University nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 | * SUCH DAMAGE. 33 | */ 34 | 35 | /* 36 | * This program creates syntax.h and syntax.c. 37 | */ 38 | 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include "parser.h" 44 | 45 | 46 | struct synclass { 47 | char *name; 48 | char *comment; 49 | }; 50 | 51 | /* Syntax classes */ 52 | struct synclass synclass[] = { 53 | { "CWORD", "character is nothing special" }, 54 | { "CNL", "newline character" }, 55 | { "CBACK", "a backslash character" }, 56 | { "CSQUOTE", "single quote" }, 57 | { "CDQUOTE", "double quote" }, 58 | { "CENDQUOTE", "a terminating quote" }, 59 | { "CBQUOTE", "backwards single quote" }, 60 | { "CVAR", "a dollar sign" }, 61 | { "CENDVAR", "a '}' character" }, 62 | { "CLP", "a left paren in arithmetic" }, 63 | { "CRP", "a right paren in arithmetic" }, 64 | { "CEOF", "end of file" }, 65 | { "CCTL", "like CWORD, except it must be escaped" }, 66 | { "CSPCL", "these terminate a word" }, 67 | { "CIGN", "character should be ignored" }, 68 | { NULL, NULL } 69 | }; 70 | 71 | 72 | /* 73 | * Syntax classes for is_ functions. Warning: if you add new classes 74 | * you may have to change the definition of the is_in_name macro. 75 | */ 76 | struct synclass is_entry[] = { 77 | { "ISDIGIT", "a digit" }, 78 | { "ISUPPER", "an upper case letter" }, 79 | { "ISLOWER", "a lower case letter" }, 80 | { "ISUNDER", "an underscore" }, 81 | { "ISSPECL", "the name of a special parameter" }, 82 | { NULL, NULL } 83 | }; 84 | 85 | static char writer[] = "\ 86 | /*\n\ 87 | * This file was generated by the mksyntax program.\n\ 88 | */\n\ 89 | \n"; 90 | 91 | 92 | static FILE *cfile; 93 | static FILE *hfile; 94 | static char *syntax[513]; 95 | 96 | static void filltable(char *); 97 | static void init(void); 98 | static void add(char *, char *); 99 | static void print(char *); 100 | static void output_type_macros(void); 101 | int main(int, char **); 102 | 103 | int 104 | main(int argc, char **argv) 105 | { 106 | int i; 107 | char buf[80]; 108 | int pos; 109 | 110 | /* Create output files */ 111 | if ((cfile = fopen("syntax.c", "w")) == NULL) { 112 | perror("syntax.c"); 113 | exit(2); 114 | } 115 | if ((hfile = fopen("syntax.h", "w")) == NULL) { 116 | perror("syntax.h"); 117 | exit(2); 118 | } 119 | fputs(writer, hfile); 120 | fputs(writer, cfile); 121 | 122 | fputs("#include \n", hfile); 123 | fputs("\n", hfile); 124 | fputs("#ifdef CEOF\n", hfile); 125 | fputs("#undef CEOF\n", hfile); 126 | fputs("#endif\n", hfile); 127 | fputs("\n", hfile); 128 | 129 | /* Generate the #define statements in the header file */ 130 | fputs("/* Syntax classes */\n", hfile); 131 | for (i = 0 ; synclass[i].name ; i++) { 132 | sprintf(buf, "#define %s %d", synclass[i].name, i); 133 | fputs(buf, hfile); 134 | for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07) 135 | putc('\t', hfile); 136 | fprintf(hfile, "/* %s */\n", synclass[i].comment); 137 | } 138 | putc('\n', hfile); 139 | fputs("/* Syntax classes for is_ functions */\n", hfile); 140 | for (i = 0 ; is_entry[i].name ; i++) { 141 | sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i); 142 | fputs(buf, hfile); 143 | for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07) 144 | putc('\t', hfile); 145 | fprintf(hfile, "/* %s */\n", is_entry[i].comment); 146 | } 147 | putc('\n', hfile); 148 | fprintf(hfile, "#define SYNBASE %d\n", 130); 149 | fprintf(hfile, "#define PEOF %d\n\n", -130); 150 | fprintf(hfile, "#define PEOA %d\n\n", -129); 151 | putc('\n', hfile); 152 | fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile); 153 | fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile); 154 | fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile); 155 | fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile); 156 | putc('\n', hfile); 157 | output_type_macros(); /* is_digit, etc. */ 158 | putc('\n', hfile); 159 | 160 | /* Generate the syntax tables. */ 161 | fputs("#include \"shell.h\"\n", cfile); 162 | fputs("#include \"syntax.h\"\n\n", cfile); 163 | init(); 164 | fputs("/* syntax table used when not in quotes */\n", cfile); 165 | add("\n", "CNL"); 166 | add("\\", "CBACK"); 167 | add("'", "CSQUOTE"); 168 | add("\"", "CDQUOTE"); 169 | add("`", "CBQUOTE"); 170 | add("$", "CVAR"); 171 | add("}", "CENDVAR"); 172 | add("<>();&| \t", "CSPCL"); 173 | syntax[1] = "CSPCL"; 174 | print("basesyntax"); 175 | init(); 176 | fputs("\n/* syntax table used when in double quotes */\n", cfile); 177 | add("\n", "CNL"); 178 | add("\\", "CBACK"); 179 | add("\"", "CENDQUOTE"); 180 | add("`", "CBQUOTE"); 181 | add("$", "CVAR"); 182 | add("}", "CENDVAR"); 183 | /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */ 184 | add("!*?[=~:/-]", "CCTL"); 185 | print("dqsyntax"); 186 | init(); 187 | fputs("\n/* syntax table used when in single quotes */\n", cfile); 188 | add("\n", "CNL"); 189 | add("'", "CENDQUOTE"); 190 | /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */ 191 | add("!*?[=~:/-]\\", "CCTL"); 192 | print("sqsyntax"); 193 | init(); 194 | fputs("\n/* syntax table used when in arithmetic */\n", cfile); 195 | add("\n", "CNL"); 196 | add("\\", "CBACK"); 197 | add("`", "CBQUOTE"); 198 | add("$", "CVAR"); 199 | add("}", "CENDVAR"); 200 | add("(", "CLP"); 201 | add(")", "CRP"); 202 | print("arisyntax"); 203 | filltable("0"); 204 | fputs("\n/* character classification table */\n", cfile); 205 | add("0123456789", "ISDIGIT"); 206 | add("abcdefghijklmnopqrstucvwxyz", "ISLOWER"); 207 | add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER"); 208 | add("_", "ISUNDER"); 209 | add("#?$!-*@", "ISSPECL"); 210 | print("is_type"); 211 | exit(0); 212 | /* NOTREACHED */ 213 | } 214 | 215 | 216 | 217 | /* 218 | * Clear the syntax table. 219 | */ 220 | 221 | static void 222 | filltable(char *dftval) 223 | { 224 | int i; 225 | 226 | for (i = 0 ; i < 258; i++) 227 | syntax[i] = dftval; 228 | } 229 | 230 | 231 | /* 232 | * Initialize the syntax table with default values. 233 | */ 234 | 235 | static void 236 | init(void) 237 | { 238 | int ctl; 239 | 240 | filltable("CWORD"); 241 | syntax[0] = "CEOF"; 242 | syntax[1] = "CIGN"; 243 | for (ctl = CTL_FIRST; ctl <= CTL_LAST; ctl++ ) 244 | syntax[130 + ctl] = "CCTL"; 245 | } 246 | 247 | 248 | /* 249 | * Add entries to the syntax table. 250 | */ 251 | 252 | static void 253 | add(char *p, char *type) 254 | { 255 | while (*p) 256 | syntax[(signed char)*p++ + 130] = type; 257 | } 258 | 259 | 260 | 261 | /* 262 | * Output the syntax table. 263 | */ 264 | 265 | static void 266 | print(char *name) 267 | { 268 | int i; 269 | int col; 270 | 271 | fprintf(hfile, "extern const char %s[];\n", name); 272 | fprintf(cfile, "const char %s[] = {\n", name); 273 | col = 0; 274 | for (i = 0 ; i < 258; i++) { 275 | if (i == 0) { 276 | fputs(" ", cfile); 277 | } else if ((i & 03) == 0) { 278 | fputs(",\n ", cfile); 279 | col = 0; 280 | } else { 281 | putc(',', cfile); 282 | while (++col < 9 * (i & 03)) 283 | putc(' ', cfile); 284 | } 285 | fputs(syntax[i], cfile); 286 | col += strlen(syntax[i]); 287 | } 288 | fputs("\n};\n", cfile); 289 | } 290 | 291 | 292 | 293 | /* 294 | * Output character classification macros (e.g. is_digit). If digits are 295 | * contiguous, we can test for them quickly. 296 | */ 297 | 298 | static char *macro[] = { 299 | "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)\n", 300 | "#define is_alpha(c)\tisalpha((unsigned char)(c))\n", 301 | "#define is_name(c)\t((c) == '_' || isalpha((unsigned char)(c)))\n", 302 | "#define is_in_name(c)\t((c) == '_' || isalnum((unsigned char)(c)))\n", 303 | "#define is_special(c)\t((is_type+SYNBASE)[(signed char)(c)] & (ISSPECL|ISDIGIT))\n", 304 | NULL 305 | }; 306 | 307 | static void 308 | output_type_macros(void) 309 | { 310 | char **pp; 311 | 312 | for (pp = macro ; *pp ; pp++) 313 | fputs(*pp, hfile); 314 | fputs("#define digit_val(c)\t((c) - '0')\n", hfile); 315 | } 316 | --------------------------------------------------------------------------------