⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 print_cmd.c

📁 android-w.song.android.widget
💻 C
📖 第 1 页 / 共 3 页
字号:
/* print_command -- A way to make readable commands from a command tree. *//* Copyright (C) 1989-2010 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>#if defined (HAVE_UNISTD_H)#  ifdef _MINIX#    include <sys/types.h>#  endif#  include <unistd.h>#endif#if defined (PREFER_STDARG)#  include <stdarg.h>#else#  include <varargs.h>#endif#include "bashansi.h"#include "bashintl.h"#include "shell.h"#include "flags.h"#include <y.tab.h>	/* use <...> so we pick it up from the build directory */#include "shmbutil.h"#include "builtins/common.h"#if !HAVE_DECL_PRINTFextern int printf __P((const char *, ...));	/* Yuck.  Double yuck. */#endifextern int indirection_level;static int indentation;static int indentation_amount = 4;#if defined (PREFER_STDARG)typedef void PFUNC __P((const char *, ...));static void cprintf __P((const char *, ...))  __attribute__((__format__ (printf, 1, 2)));static void xprintf __P((const char *, ...))  __attribute__((__format__ (printf, 1, 2)));#else#define PFUNC VFunctionstatic void cprintf ();static void xprintf ();#endifstatic void reset_locals __P((void));static void newline __P((char *));static void indent __P((int));static void semicolon __P((void));static void the_printed_command_resize __P((int));static void make_command_string_internal __P((COMMAND *));static void _print_word_list __P((WORD_LIST *, char *, PFUNC *));static void command_print_word_list __P((WORD_LIST *, char *));static void print_case_clauses __P((PATTERN_LIST *));static void print_redirection_list __P((REDIRECT *));static void print_redirection __P((REDIRECT *));static void print_heredoc_header __P((REDIRECT *));static void print_heredoc_body __P((REDIRECT *));static void print_heredocs __P((REDIRECT *));static void print_deferred_heredocs __P((const char *));static void print_for_command __P((FOR_COM *));#if defined (ARITH_FOR_COMMAND)static void print_arith_for_command __P((ARITH_FOR_COM *));#endif#if defined (SELECT_COMMAND)static void print_select_command __P((SELECT_COM *));#endifstatic void print_group_command __P((GROUP_COM *));static void print_case_command __P((CASE_COM *));static void print_while_command __P((WHILE_COM *));static void print_until_command __P((WHILE_COM *));static void print_until_or_while __P((WHILE_COM *, char *));static void print_if_command __P((IF_COM *));#if defined (COND_COMMAND)static void print_cond_node __P((COND_COM *));#endifstatic void print_function_def __P((FUNCTION_DEF *));#define PRINTED_COMMAND_INITIAL_SIZE 64#define PRINTED_COMMAND_GROW_SIZE 128char *the_printed_command = (char *)NULL;int the_printed_command_size = 0;int command_string_index = 0;int xtrace_fd = -1;FILE *xtrace_fp = 0;#define CHECK_XTRACE_FP	xtrace_fp = (xtrace_fp ? xtrace_fp : stderr)#define PRINT_DEFERRED_HEREDOCS(x) \  do { \    if (deferred_heredocs) \      print_deferred_heredocs (x); \  } while (0)/* Non-zero means the stuff being printed is inside of a function def. */static int inside_function_def;static int skip_this_indent;static int was_heredoc;static int printing_connection;static REDIRECT *deferred_heredocs;/* The depth of the group commands that we are currently printing.  This   includes the group command that is a function body. */static int group_command_nesting;/* A buffer to indicate the indirection level (PS4) when set -x is enabled. */static char indirection_string[100];/* Print COMMAND (a command tree) on standard output. */voidprint_command (command)     COMMAND *command;{  command_string_index = 0;  printf ("%s", make_command_string (command));}/* Make a string which is the printed representation of the command   tree in COMMAND.  We return this string.  However, the string is   not consed, so you have to do that yourself if you want it to   remain around. */char *make_command_string (command)     COMMAND *command;{  command_string_index = was_heredoc = 0;  deferred_heredocs = 0;  make_command_string_internal (command);  return (the_printed_command);}/* The internal function.  This is the real workhorse. */static voidmake_command_string_internal (command)     COMMAND *command;{  char s[3];  if (command == 0)    cprintf ("");  else    {      if (skip_this_indent)	skip_this_indent--;      else	indent (indentation);      if (command->flags & CMD_TIME_PIPELINE)	{	  cprintf ("time ");	  if (command->flags & CMD_TIME_POSIX)	    cprintf ("-p ");	}      if (command->flags & CMD_INVERT_RETURN)	cprintf ("! ");      switch (command->type)	{	case cm_for:	  print_for_command (command->value.For);	  break;#if defined (ARITH_FOR_COMMAND)	case cm_arith_for:	  print_arith_for_command (command->value.ArithFor);	  break;#endif#if defined (SELECT_COMMAND)	case cm_select:	  print_select_command (command->value.Select);	  break;#endif	case cm_case:	  print_case_command (command->value.Case);	  break;	case cm_while:	  print_while_command (command->value.While);	  break;	case cm_until:	  print_until_command (command->value.While);	  break;	case cm_if:	  print_if_command (command->value.If);	  break;#if defined (DPAREN_ARITHMETIC)	case cm_arith:	  print_arith_command (command->value.Arith->exp);	  break;#endif#if defined (COND_COMMAND)	case cm_cond:	  print_cond_command (command->value.Cond);	  break;#endif	case cm_simple:	  print_simple_command (command->value.Simple);	  break;	case cm_connection:	  skip_this_indent++;	  printing_connection++;	  make_command_string_internal (command->value.Connection->first);	  switch (command->value.Connection->connector)	    {	    case '&':	    case '|':	      {		char c = command->value.Connection->connector;		s[0] = ' ';		s[1] = c;		s[2] = '\0';				print_deferred_heredocs (s);		if (c != '&' || command->value.Connection->second)		  {		    cprintf (" ");		    skip_this_indent++;		  }	      }	      break;	    case AND_AND:	      print_deferred_heredocs (" && ");	      if (command->value.Connection->second)		skip_this_indent++;	      break;	    case OR_OR:	      print_deferred_heredocs (" || ");	      if (command->value.Connection->second)		skip_this_indent++;	      break;	    case ';':	      if (deferred_heredocs == 0)		{		  if (was_heredoc == 0)		    cprintf (";");		  else		    was_heredoc = 0;		}	      else		print_deferred_heredocs (inside_function_def ? "" : ";");	      if (inside_function_def)		cprintf ("\n");	      else		{		  cprintf (" ");		  if (command->value.Connection->second)		    skip_this_indent++;		}	      break;	    default:	      cprintf (_("print_command: bad connector `%d'"),		       command->value.Connection->connector);	      break;	    }	  make_command_string_internal (command->value.Connection->second);	  PRINT_DEFERRED_HEREDOCS ("");	  printing_connection--;	  	  	  break;	case cm_function_def:	  print_function_def (command->value.Function_def);	  break;	case cm_group:	  print_group_command (command->value.Group);	  break;	case cm_subshell:	  cprintf ("( ");	  skip_this_indent++;	  make_command_string_internal (command->value.Subshell->command);	  cprintf (" )");	  break;	case cm_coproc:	  cprintf ("coproc %s ", command->value.Coproc->name);	  skip_this_indent++;	  make_command_string_internal (command->value.Coproc->command);	  break;	default:	  command_error ("print_command", CMDERR_BADTYPE, command->type, 0);	  break;	}      if (command->redirects)	{	  cprintf (" ");	  print_redirection_list (command->redirects);	}    }}static void_print_word_list (list, separator, pfunc)     WORD_LIST *list;     char *separator;     PFUNC *pfunc;{  WORD_LIST *w;  for (w = list; w; w = w->next)    (*pfunc) ("%s%s", w->word->word, w->next ? separator : "");}voidprint_word_list (list, separator)     WORD_LIST *list;     char *separator;{  _print_word_list (list, separator, xprintf);}voidxtrace_set (fd, fp)     int fd;     FILE *fp;{  if (fd >= 0 && sh_validfd (fd) == 0)    {      internal_error (_("xtrace_set: %d: invalid file descriptor"), fd);      return;    }  if (fp == 0)    {      internal_error (_("xtrace_set: NULL file pointer"));      return;    }  if (fd >= 0 && fileno (fp) != fd)    internal_warning (_("xtrace fd (%d) != fileno xtrace fp (%d)"), fd, fileno (fp));    xtrace_fd = fd;  xtrace_fp = fp;}voidxtrace_init (){  xtrace_set (-1, stderr);}voidxtrace_reset (){  if (xtrace_fd >= 0 && xtrace_fp)    {      fflush (xtrace_fp);      fclose (xtrace_fp);    }  else if (xtrace_fd >= 0)    close (xtrace_fd);  xtrace_fd = -1;  xtrace_fp = stderr;}voidxtrace_fdchk (fd)     int fd;{  if (fd == xtrace_fd)    xtrace_reset ();}/* Return a string denoting what our indirection level is. */char *indirection_level_string (){  register int i, j;  char *ps4;  char ps4_firstc[MB_LEN_MAX+1];  int ps4_firstc_len, ps4_len;  indirection_string[0] = '\0';  ps4 = get_string_value ("PS4");  if (ps4 == 0 || *ps4 == '\0')    return (indirection_string);  change_flag ('x', FLAG_OFF);  ps4 = decode_prompt_string (ps4);  change_flag ('x', FLAG_ON);  if (ps4 == 0 || *ps4 == '\0')    return (indirection_string);#if defined (HANDLE_MULTIBYTE)  ps4_len = strnlen (ps4, MB_CUR_MAX);  ps4_firstc_len = MBLEN (ps4, ps4_len);  if (ps4_firstc_len == 1 || ps4_firstc_len == 0 || MB_INVALIDCH (ps4_firstc_len))    {      ps4_firstc[0] = ps4[0];      ps4_firstc[ps4_firstc_len = 1] = '\0';    }  else    memcpy (ps4_firstc, ps4, ps4_firstc_len);#else  ps4_firstc[0] = ps4[0];  ps4_firstc[ps4_firstc_len = 1] = '\0';#endif        for (i = j = 0; ps4_firstc[0] && j < indirection_level && i < 99; i += ps4_firstc_len, j++)    {      if (ps4_firstc_len == 1)	indirection_string[i] = ps4_firstc[0];      else	memcpy (indirection_string+i, ps4_firstc, ps4_firstc_len);    }        for (j = ps4_firstc_len; *ps4 && ps4[j] && i < 99; i++, j++)    indirection_string[i] = ps4[j];  indirection_string[i] = '\0';  free (ps4);  return (indirection_string);}voidxtrace_print_assignment (name, value, assign_list, xflags)     char *name, *value;     int assign_list, xflags;{  char *nval;  CHECK_XTRACE_FP;  if (xflags)    fprintf (xtrace_fp, "%s", indirection_level_string ());  /* VALUE should not be NULL when this is called. */  if (*value == '\0' || assign_list)    nval = value;  else if (sh_contains_shell_metas (value))    nval = sh_single_quote (value);  else if (ansic_shouldquote (value))    nval = ansic_quote (value, 0, (int *)0);  else    nval = value;  if (assign_list)    fprintf (xtrace_fp, "%s=(%s)\n", name, nval);  else    fprintf (xtrace_fp, "%s=%s\n", name, nval);  if (nval != value)    FREE (nval);  fflush (xtrace_fp);}/* A function to print the words of a simple command when set -x is on. */voidxtrace_print_word_list (list, xtflags)     WORD_LIST *list;     int xtflags;{  WORD_LIST *w;  char *t, *x;  CHECK_XTRACE_FP;  if (xtflags)    fprintf (xtrace_fp, "%s", indirection_level_string ());  for (w = list; w; w = w->next)    {      t = w->word->word;      if (t == 0 || *t == '\0')	fprintf (xtrace_fp, "''%s", w->next ? " " : "");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -