📄 make_cmd.c
字号:
/* make_cmd.c -- Functions for making instances of the various parser constructs. *//* Copyright (C) 1989-2009 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.*/#include "config.h"#include <stdio.h>#include "bashtypes.h"#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)# include <sys/file.h>#endif#include "filecntl.h"#include "bashansi.h"#if defined (HAVE_UNISTD_H)# include <unistd.h>#endif#include "bashintl.h"#include "parser.h"#include "syntax.h"#include "command.h"#include "general.h"#include "error.h"#include "flags.h"#include "make_cmd.h"#include "dispose_cmd.h"#include "variables.h"#include "subst.h"#include "input.h"#include "ocache.h"#include "externs.h"#if defined (JOB_CONTROL)#include "jobs.h"#endif#include "shmbutil.h"extern int line_number, current_command_line_count, parser_state;extern int last_command_exit_value;/* Object caching */sh_obj_cache_t wdcache = {0, 0, 0};sh_obj_cache_t wlcache = {0, 0, 0};#define WDCACHESIZE 60#define WLCACHESIZE 60static COMMAND *make_for_or_select __P((enum command_type, WORD_DESC *, WORD_LIST *, COMMAND *, int));#if defined (ARITH_FOR_COMMAND)static WORD_LIST *make_arith_for_expr __P((char *));#endifstatic COMMAND *make_until_or_while __P((enum command_type, COMMAND *, COMMAND *));voidcmd_init (){ ocache_create (wdcache, WORD_DESC, WDCACHESIZE); ocache_create (wlcache, WORD_LIST, WLCACHESIZE);}WORD_DESC *alloc_word_desc (){ WORD_DESC *temp; ocache_alloc (wdcache, WORD_DESC, temp); temp->flags = 0; temp->word = 0; return temp;}WORD_DESC *make_bare_word (string) const char *string;{ WORD_DESC *temp; temp = alloc_word_desc (); if (*string) temp->word = savestring (string); else { temp->word = (char *)xmalloc (1); temp->word[0] = '\0'; } return (temp);}WORD_DESC *make_word_flags (w, string) WORD_DESC *w; const char *string;{ register int i; size_t slen; DECLARE_MBSTATE; i = 0; slen = strlen (string); while (i < slen) { switch (string[i]) { case '$': w->flags |= W_HASDOLLAR; break; case '\\': break; /* continue the loop */ case '\'': case '`': case '"': w->flags |= W_QUOTED; break; } ADVANCE_CHAR (string, slen, i); } return (w);}WORD_DESC *make_word (string) const char *string;{ WORD_DESC *temp; temp = make_bare_word (string); return (make_word_flags (temp, string));}WORD_DESC *make_word_from_token (token) int token;{ char tokenizer[2]; tokenizer[0] = token; tokenizer[1] = '\0'; return (make_word (tokenizer));}WORD_LIST *make_word_list (word, wlink) WORD_DESC *word; WORD_LIST *wlink;{ WORD_LIST *temp; ocache_alloc (wlcache, WORD_LIST, temp); temp->word = word; temp->next = wlink; return (temp);}COMMAND *make_command (type, pointer) enum command_type type; SIMPLE_COM *pointer;{ COMMAND *temp; temp = (COMMAND *)xmalloc (sizeof (COMMAND)); temp->type = type; temp->value.Simple = pointer; temp->value.Simple->flags = temp->flags = 0; temp->redirects = (REDIRECT *)NULL; return (temp);}COMMAND *command_connect (com1, com2, connector) COMMAND *com1, *com2; int connector;{ CONNECTION *temp; temp = (CONNECTION *)xmalloc (sizeof (CONNECTION)); temp->connector = connector; temp->first = com1; temp->second = com2; return (make_command (cm_connection, (SIMPLE_COM *)temp));}static COMMAND *make_for_or_select (type, name, map_list, action, lineno) enum command_type type; WORD_DESC *name; WORD_LIST *map_list; COMMAND *action; int lineno;{ FOR_COM *temp; temp = (FOR_COM *)xmalloc (sizeof (FOR_COM)); temp->flags = 0; temp->name = name; temp->line = lineno; temp->map_list = map_list; temp->action = action; return (make_command (type, (SIMPLE_COM *)temp));}COMMAND *make_for_command (name, map_list, action, lineno) WORD_DESC *name; WORD_LIST *map_list; COMMAND *action; int lineno;{ return (make_for_or_select (cm_for, name, map_list, action, lineno));}COMMAND *make_select_command (name, map_list, action, lineno) WORD_DESC *name; WORD_LIST *map_list; COMMAND *action; int lineno;{#if defined (SELECT_COMMAND) return (make_for_or_select (cm_select, name, map_list, action, lineno));#else last_command_exit_value = 2; return ((COMMAND *)NULL);#endif}#if defined (ARITH_FOR_COMMAND)static WORD_LIST *make_arith_for_expr (s) char *s;{ WORD_LIST *result; WORD_DESC *wd; if (s == 0 || *s == '\0') return ((WORD_LIST *)NULL); wd = make_word (s); wd->flags |= W_NOGLOB|W_NOSPLIT|W_QUOTED|W_DQUOTE; /* no word splitting or globbing */ result = make_word_list (wd, (WORD_LIST *)NULL); return result;}#endif/* Note that this function calls dispose_words on EXPRS, since it doesn't use the word list directly. We free it here rather than at the caller because no other function in this file requires that the caller free any arguments. */COMMAND *make_arith_for_command (exprs, action, lineno) WORD_LIST *exprs; COMMAND *action; int lineno;{#if defined (ARITH_FOR_COMMAND) ARITH_FOR_COM *temp; WORD_LIST *init, *test, *step; char *s, *t, *start; int nsemi; init = test = step = (WORD_LIST *)NULL; /* Parse the string into the three component sub-expressions. */ start = t = s = exprs->word->word; for (nsemi = 0; ;) { /* skip whitespace at the start of each sub-expression. */ while (whitespace (*s)) s++; start = s; /* skip to the semicolon or EOS */ while (*s && *s != ';') s++; t = (s > start) ? substring (start, 0, s - start) : (char *)NULL; nsemi++; switch (nsemi) { case 1: init = make_arith_for_expr (t); break; case 2: test = make_arith_for_expr (t); break; case 3: step = make_arith_for_expr (t); break; } FREE (t); if (*s == '\0') break; s++; /* skip over semicolon */ } if (nsemi != 3) { if (nsemi < 3) parser_error (lineno, _("syntax error: arithmetic expression required")); else parser_error (lineno, _("syntax error: `;' unexpected")); parser_error (lineno, _("syntax error: `((%s))'"), exprs->word->word); last_command_exit_value = 2; return ((COMMAND *)NULL); } temp = (ARITH_FOR_COM *)xmalloc (sizeof (ARITH_FOR_COM)); temp->flags = 0; temp->line = lineno; temp->init = init ? init : make_arith_for_expr ("1"); temp->test = test ? test : make_arith_for_expr ("1"); temp->step = step ? step : make_arith_for_expr ("1"); temp->action = action; dispose_words (exprs); return (make_command (cm_arith_for, (SIMPLE_COM *)temp));#else dispose_words (exprs); last_command_exit_value = 2; return ((COMMAND *)NULL);#endif /* ARITH_FOR_COMMAND */}COMMAND *make_group_command (command) COMMAND *command;{ GROUP_COM *temp; temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM)); temp->command = command; return (make_command (cm_group, (SIMPLE_COM *)temp));}COMMAND *make_case_command (word, clauses, lineno) WORD_DESC *word; PATTERN_LIST *clauses; int lineno;{ CASE_COM *temp; temp = (CASE_COM *)xmalloc (sizeof (CASE_COM)); temp->flags = 0; temp->line = lineno; temp->word = word; temp->clauses = REVERSE_LIST (clauses, PATTERN_LIST *); return (make_command (cm_case, (SIMPLE_COM *)temp));}PATTERN_LIST *make_pattern_list (patterns, action) WORD_LIST *patterns; COMMAND *action;{ PATTERN_LIST *temp; temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST)); temp->patterns = REVERSE_LIST (patterns, WORD_LIST *); temp->action = action; temp->next = NULL; temp->flags = 0; return (temp);}COMMAND *make_if_command (test, true_case, false_case) COMMAND *test, *true_case, *false_case;{ IF_COM *temp; temp = (IF_COM *)xmalloc (sizeof (IF_COM)); temp->flags = 0; temp->test = test; temp->true_case = true_case; temp->false_case = false_case; return (make_command (cm_if, (SIMPLE_COM *)temp));}static COMMAND *make_until_or_while (which, test, action) enum command_type which; COMMAND *test, *action;{ WHILE_COM *temp; temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM)); temp->flags = 0; temp->test = test; temp->action = action; return (make_command (which, (SIMPLE_COM *)temp));}COMMAND *make_while_command (test, action) COMMAND *test, *action;{ return (make_until_or_while (cm_while, test, action));}COMMAND *make_until_command (test, action) COMMAND *test, *action;{ return (make_until_or_while (cm_until, test, action));}COMMAND *make_arith_command (exp) WORD_LIST *exp;{#if defined (DPAREN_ARITHMETIC) COMMAND *command; ARITH_COM *temp; command = (COMMAND *)xmalloc (sizeof (COMMAND)); command->value.Arith = temp = (ARITH_COM *)xmalloc (sizeof (ARITH_COM)); temp->flags = 0; temp->line = line_number; temp->exp = exp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -