/* subst.c -- The part of the shell that does parameter, command, and
globbing substitutions. */
/* Copyright (C) 1987,1989 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "config.h"
#include "bashtypes.h"
#include <stdio.h>
#include <pwd.h>
#include <signal.h>
#include <errno.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "bashansi.h"
#include "posixstat.h"
#include "shell.h"
#include "flags.h"
#include "jobs.h"
#include "execute_cmd.h"
#include "filecntl.h"
#include "trap.h"
#include "pathexp.h"
#include "mailcheck.h"
#if !defined (HAVE_RESTARTABLE_SYSCALLS) /* for getc_with_restart */
#include "input.h"
#endif
#include "builtins/getopt.h"
#include "builtins/common.h"
#include <tilde/tilde.h>
#include <glob/fnmatch.h>
#if !defined (errno)
extern int errno;
#endif /* !errno */
/* The size that strings change by. */
#define DEFAULT_INITIAL_ARRAY_SIZE 112
#define DEFAULT_ARRAY_SIZE 128
/* Variable types. */
#define VT_VARIABLE 0
#define VT_POSPARMS 1
#define VT_ARRAYVAR 2
#define VT_ARRAYMEMBER 3
/* Flags for quoted_strchr */
#define ST_BACKSL 0x01
#define ST_CTLESC 0x02
/* These defs make it easier to use the editor. */
#define LBRACE '{'
#define RBRACE '}'
#define LPAREN '('
#define RPAREN ')'
/* Process ID of the last command executed within command substitution. */
pid_t last_command_subst_pid = NO_PID;
/* Extern functions and variables from different files. */
extern int last_command_exit_value, interactive, interactive_shell;
extern int subshell_environment, startup_state;
extern int dollar_dollar_pid;
extern int posixly_correct;
extern char *this_command_name;
extern struct fd_bitmap *current_fds_to_close;
extern int wordexp_only;
extern void getopts_reset ();
/* Non-zero means to allow unmatched globbed filenames to expand to
a null file. */
int allow_null_glob_expansion;
/* Variables to keep track of which words in an expanded word list (the
output of expand_word_list_internal) are the result of globbing
expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c. */
char *glob_argv_flags;
static int glob_argv_flags_size;
static WORD_LIST expand_word_error, expand_word_fatal;
static char expand_param_error, expand_param_fatal;
static char *make_quoted_char ();
static void remove_quoted_nulls ();
static char *param_expand ();
static char *maybe_expand_string ();
static WORD_LIST *call_expand_word_internal ();
static WORD_LIST *expand_string_internal ();
static WORD_LIST *expand_word_internal (), *expand_word_list_internal ();
static WORD_LIST *expand_string_leave_quoted ();
static WORD_LIST *expand_string_for_rhs ();
static WORD_LIST *word_list_split ();
static WORD_LIST *quote_list (), *dequote_list ();
static char *quote_escapes ();
static WORD_LIST *list_quote_escapes ();
static int unquoted_substring (), unquoted_member ();
static int do_assignment_internal ();
static char *string_extract_verbatim (), *string_extract ();
static char *string_extract_double_quoted (), *string_extract_single_quoted ();
static char *string_list_dollar_at (), *string_list_dollar_star ();
static inline int skip_single_quoted (), skip_double_quoted ();
static char *extract_delimited_string ();
static char *extract_dollar_brace_string ();
/* **************************************************************** */
/* */
/* Utility Functions */
/* */
/* **************************************************************** */
/* Cons a new string from STRING starting at START and ending at END,
not including END. */
char *
substring (string, start, end)
char *string;
int start, end;
{
register int len;
register char *result;
len = end - start;
result = xmalloc (len + 1);
strncpy (result, string + start, len);
result[len] = '\0';
return (result);
}
static char *
quoted_substring (string, start, end)
char *string;
int start, end;
{
register int len, l;
register char *result, *s, *r;
len = end - start;
/* Move to string[start], skipping quoted characters. */
for (s = string, l = 0; *s && l < start; )
{
if (*s == CTLESC)
{
s++;
continue;
}
l++;
if (*s == 0)
break;
}
r = result = xmalloc (2*len + 1); /* save room for quotes */
/* Copy LEN characters, including quote characters. */
s = string + l;
for (l = 0; l < len; s++)
{
if (*s == CTLESC)
*r++ = *s++;
*r++ = *s;
l++;
if (*s == 0)
break;
}
*r = '\0';
return result;
}
/* Find the first occurrence of character C in string S, obeying shell
quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
escaped with CTLESC are skipped. */
static inline char *
quoted_strchr (s, c, flags)
char *s;
int c, flags;
{
register char *p;
for (p = s; *p; p++)
{
if (((flags & ST_BACKSL) && *p == '\\')
|| ((flags & ST_CTLESC) && *p == CTLESC))
{
p++;
if (*p == '\0')
return ((char *)NULL);
continue;
}
else if (*p == c)
return p;
}
return ((char *)NULL);
}
/* Return 1 if CHARACTER appears in an unquoted portion of
STRING. Return 0 otherwise. */
static int
unquoted_member (character, string)
int character;
char *string;
{
int sindex, c;
for (sindex = 0; c = string[sindex]; )
{
if (c == character)
return (1);
switch (c)
{
default:
sindex++;
break;
case '\\':
sindex++;
if (string[sindex])
sindex++;
break;
case '\'':
sindex = skip_single_quoted (string, ++sindex);
break;
case '"':
sindex = skip_double_quoted (string, ++sindex);
break;
}
}
return (0);
}
/* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
static int
unquoted_substring (substr, string)
char *substr, *string;
{
int sindex, c, sublen;
if (substr == 0 || *substr == '\0')
return (0);
sublen = strlen (substr);
for (sindex = 0; c = string[sindex]; )
{
if (STREQN (string + sindex, substr, sublen))
return (1);
switch (c)
{
case '\\':
sindex++;
if (string[sindex])
sindex++;
break;
case '\'':
sindex = skip_single_quoted (string, ++sindex);
break;
case '"':
sindex = skip_double_quoted (string, ++sindex);
break;
default:
sindex++;
break;
}
}
return (0);
}
/* Most of the substitutions must be done in parallel. In order
to avoid using tons of unclear goto's, I have some functions
for manipulating malloc'ed strings. They all take INDX, a
pointer to an integer which is the offset into the string
where manipulation is taking place. They also take SIZE, a
pointer to an integer which is the current length of the
character array for this string. */
/* Append SOURCE to TARGET at INDEX. SIZE is the current amount
of space allocated to TARGET. SOURCE can be NULL, in which
case nothing happens. Gets rid of SOURCE by freeing it.
Returns TARGET in case the location has changed. */
inline char *
sub_append_string (source, target, indx, size)
char *source, *target;
int *indx, *size;
{
if (source)
{
int srclen, n;
srclen = STRLEN (source);
if (srclen >=
没有合适的资源?快使用搜索试试~ 我知道了~
bash-2.02.tar.gz
需积分: 1 0 下载量 15 浏览量
2024-01-30
00:30:27
上传
评论
收藏 1.44MB GZ 举报
温馨提示
共644个文件
c:132个
h:81个
right:44个
Bash 是一种广泛使用的 Unix shell,它是一个命令语言解释器,允许用户通过命令行界面与操作系统交互。Bash 同时是许多类 Unix 系统(如 Linux 和 macOS)的默认 shell,并且支持多种脚本编程功能。 Bash 作为一个强大的命令行工具,不仅用于日常的文件管理和系统操作任务,而且在自动化脚本、任务调度和高级编程方面也非常有用。它的灵活性和易用性使其成为开发者和系统管理员的首选 shell。
资源推荐
资源详情
资源评论
收起资源包目录
bash-2.02.tar.gz (644个子文件)
bash.1 192KB
bashbug.1 1KB
builtins.1 682B
test-minus-e.1 149B
test-minus-e.2 186B
readline.3 32KB
arc2tarz 2KB
array-at-star 2KB
AUTHORS 15KB
autoload 3KB
basename 645B
basename2 762B
getoptx.bash 9KB
require.bash 5KB
number.bash 5KB
string.bash 5KB
send_mail.bash 4KB
cdhist.bash 3KB
fixfiles.bash 3KB
jdate.bash 2KB
krand.bash 2KB
remap_keys.bash 2KB
y_or_n_p.bash 2KB
mktmp.bash 2KB
stty.bash 2KB
source.bash 2KB
cal2day.bash 1KB
showperm.bash 1KB
bash_version.bash 1KB
prompt.bash 1KB
shcat.bash 933B
meta.bash 884B
nohup.bash 862B
aliasconv.bash 806B
aref.bash 758B
bash.sub.bash 748B
isnum.bash 733B
notify.bash 732B
xalias.bash 471B
randomcard.bash 409B
hanoi.bash 400B
spin.bash 285B
jj.bash 111B
bash-profile 887B
Bash_aliases 1KB
Bash_profile 339B
bashdb 772B
bashrand 2KB
bashrc 2KB
Bashrc.bfox 2KB
bison 77B
config.h.bot 2KB
braces-tests 386B
subst.c 149KB
y.tab.c 143KB
execute_cmd.c 89KB
man2html.c 81KB
jobs.c 77KB
variables.c 70KB
bashline.c 55KB
ogmalloc.c 48KB
complete.c 47KB
readline.c 47KB
bind.c 46KB
display.c 43KB
shell.c 42KB
gmalloc.c 41KB
mkbuiltins.c 34KB
emacs_keymap.c 33KB
histexpand.c 32KB
vi_keymap.c 32KB
vi_mode.c 29KB
general.c 25KB
print_cmd.c 24KB
malloc.c 24KB
redir.c 23KB
expr.c 21KB
test.c 20KB
common.c 20KB
trap.c 19KB
fnmatch.c 19KB
omalloc.c 19KB
bashhist.c 17KB
getconf.c 17KB
glob.c 16KB
rltty.c 16KB
nojobs.c 16KB
termcap.c 16KB
make_cmd.c 16KB
findcmd.c 16KB
pushd.c 15KB
kill.c 14KB
alias.c 14KB
alloca.c 14KB
terminal.c 14KB
input.c 13KB
array.c 12KB
tilde.c 11KB
tilde.c 11KB
sig.c 11KB
共 644 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
程序员Chino的日记
- 粉丝: 3037
- 资源: 4万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功