📄 print_cmd.c
字号:
char *x; kill_leading = redirect->instruction == r_deblank_reading_until; /* Here doc header */ if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}", redirect->redirector.filename->word); else if (redirect->redirector.dest != 0) cprintf ("%d", redirect->redirector.dest); /* If the here document delimiter is quoted, single-quote it. */ if (redirect->redirectee.filename->flags & W_QUOTED) { x = sh_single_quote (redirect->here_doc_eof); cprintf ("<<%s%s", kill_leading ? "-" : "", x); free (x); } else cprintf ("<<%s%s", kill_leading ? "-" : "", redirect->here_doc_eof);}static voidprint_heredoc_body (redirect) REDIRECT *redirect;{ /* Here doc body */ cprintf ("%s%s", redirect->redirectee.filename->word, redirect->here_doc_eof);}static voidprint_redirection (redirect) REDIRECT *redirect;{ int redirector, redir_fd; WORD_DESC *redirectee, *redir_word; redirectee = redirect->redirectee.filename; redir_fd = redirect->redirectee.dest; redir_word = redirect->redirector.filename; redirector = redirect->redirector.dest; switch (redirect->instruction) { case r_input_direction: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}", redir_word->word); else if (redirector != 0) cprintf ("%d", redirector); cprintf ("< %s", redirectee->word); break; case r_output_direction: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}", redir_word->word); else if (redirector != 1) cprintf ("%d", redirector); cprintf ("> %s", redirectee->word); break; case r_inputa_direction: /* Redirection created by the shell. */ cprintf ("&"); break; case r_output_force: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}", redir_word->word); else if (redirector != 1) cprintf ("%d", redirector); cprintf (">|%s", redirectee->word); break; case r_appending_to: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}", redir_word->word); else if (redirector != 1) cprintf ("%d", redirector); cprintf (">> %s", redirectee->word); break; case r_input_output: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}", redir_word->word); else if (redirector != 1) cprintf ("%d", redirector); cprintf ("<> %s", redirectee->word); break; case r_deblank_reading_until: case r_reading_until: print_heredoc_header (redirect); cprintf ("\n"); print_heredoc_body (redirect); break; case r_reading_string: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}", redir_word->word); else if (redirector != 0) cprintf ("%d", redirector);#if 0 /* Don't need to check whether or not to requote, since original quotes are still intact. The only thing that has happened is that $'...' has been replaced with 'expanded ...'. */ if (ansic_shouldquote (redirect->redirectee.filename->word)) { char *x; x = ansic_quote (redirect->redirectee.filename->word, 0, (int *)0); cprintf ("<<< %s", x); free (x); } else#endif cprintf ("<<< %s", redirect->redirectee.filename->word); break; case r_duplicating_input: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}<&%d", redir_word->word, redir_fd); else cprintf ("%d<&%d", redirector, redir_fd); break; case r_duplicating_output: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}>&%d", redir_word->word, redir_fd); else cprintf ("%d>&%d", redirector, redir_fd); break; case r_duplicating_input_word: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}<&%s", redir_word->word, redirectee->word); else cprintf ("%d<&%s", redirector, redirectee->word); break; case r_duplicating_output_word: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}>&%s", redir_word->word, redirectee->word); else cprintf ("%d>&%s", redirector, redirectee->word); break; case r_move_input: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}<&%d-", redir_word->word, redir_fd); else cprintf ("%d<&%d-", redirector, redir_fd); break; case r_move_output: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}>&%d-", redir_word->word, redir_fd); else cprintf ("%d>&%d-", redirector, redir_fd); break; case r_move_input_word: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}<&%s-", redir_word->word, redirectee->word); else cprintf ("%d<&%s-", redirector, redirectee->word); break; case r_move_output_word: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}>&%s-", redir_word->word, redirectee->word); else cprintf ("%d>&%s-", redirector, redirectee->word); break; case r_close_this: if (redirect->rflags & REDIR_VARASSIGN) cprintf ("{%s}>&-", redir_word->word); else cprintf ("%d>&-", redirector); break; case r_err_and_out: cprintf ("&>%s", redirectee->word); break; case r_append_err_and_out: cprintf ("&>>%s", redirectee->word); break; }}static voidreset_locals (){ inside_function_def = 0; indentation = 0; printing_connection = 0; deferred_heredocs = 0;}static voidprint_function_def (func) FUNCTION_DEF *func;{ COMMAND *cmdcopy; REDIRECT *func_redirects; func_redirects = NULL; cprintf ("function %s () \n", func->name->word); add_unwind_protect (reset_locals, 0); indent (indentation); cprintf ("{ \n"); inside_function_def++; indentation += indentation_amount; cmdcopy = copy_command (func->command); if (cmdcopy->type == cm_group) { func_redirects = cmdcopy->redirects; cmdcopy->redirects = (REDIRECT *)NULL; } make_command_string_internal (cmdcopy->type == cm_group ? cmdcopy->value.Group->command : cmdcopy); remove_unwind_protect (); indentation -= indentation_amount; inside_function_def--; if (func_redirects) { /* { */ newline ("} "); print_redirection_list (func_redirects); cmdcopy->redirects = func_redirects; } else newline ("}"); dispose_command (cmdcopy);}/* Return the string representation of the named function. NAME is the name of the function. COMMAND is the function body. It should be a GROUP_COM. flags&FUNC_MULTILINE is non-zero to pretty-print, or zero for all on one line. flags&FUNC_EXTERNAL means convert from internal to external form */char *named_function_string (name, command, flags) char *name; COMMAND *command; int flags;{ char *result; int old_indent, old_amount; COMMAND *cmdcopy; REDIRECT *func_redirects; old_indent = indentation; old_amount = indentation_amount; command_string_index = was_heredoc = 0; deferred_heredocs = 0; if (name && *name) cprintf ("%s ", name); cprintf ("() "); if ((flags & FUNC_MULTILINE) == 0) { indentation = 1; indentation_amount = 0; } else { cprintf ("\n"); indentation += indentation_amount; } inside_function_def++; cprintf ((flags & FUNC_MULTILINE) ? "{ \n" : "{ "); cmdcopy = copy_command (command); /* Take any redirections specified in the function definition (which should apply to the function as a whole) and save them for printing later. */ func_redirects = (REDIRECT *)NULL; if (cmdcopy->type == cm_group) { func_redirects = cmdcopy->redirects; cmdcopy->redirects = (REDIRECT *)NULL; } make_command_string_internal (cmdcopy->type == cm_group ? cmdcopy->value.Group->command : cmdcopy); indentation = old_indent; indentation_amount = old_amount; inside_function_def--; if (func_redirects) { /* { */ newline ("} "); print_redirection_list (func_redirects); cmdcopy->redirects = func_redirects; } else newline ("}"); result = the_printed_command; if ((flags & FUNC_MULTILINE) == 0) {#if 0 register int i; for (i = 0; result[i]; i++) if (result[i] == '\n') { strcpy (result + i, result + i + 1); --i; }#else if (result[2] == '\n') /* XXX -- experimental */ strcpy (result + 2, result + 3);#endif } dispose_command (cmdcopy); if (flags & FUNC_EXTERNAL) result = remove_quoted_escapes (result); return (result);}static voidnewline (string) char *string;{ cprintf ("\n"); indent (indentation); if (string && *string) cprintf ("%s", string);}static char *indentation_string;static int indentation_size;static voidindent (amount) int amount;{ register int i; RESIZE_MALLOCED_BUFFER (indentation_string, 0, amount, indentation_size, 16); for (i = 0; amount > 0; amount--) indentation_string[i++] = ' '; indentation_string[i] = '\0'; cprintf (indentation_string);}static voidsemicolon (){ if (command_string_index > 0 && (the_printed_command[command_string_index - 1] == '&' || the_printed_command[command_string_index - 1] == '\n')) return; cprintf (";");}/* How to make the string. */static void#if defined (PREFER_STDARG)cprintf (const char *control, ...)#elsecprintf (control, va_alist) const char *control; va_dcl#endif{ register const char *s; char char_arg[2], *argp, intbuf[INT_STRLEN_BOUND (int) + 1]; int digit_arg, arg_len, c; va_list args; SH_VA_START (args, control); arg_len = strlen (control); the_printed_command_resize (arg_len + 1); char_arg[1] = '\0'; s = control; while (s && *s) { c = *s++; argp = (char *)NULL; if (c != '%' || !*s) { char_arg[0] = c; argp = char_arg; arg_len = 1; } else { c = *s++; switch (c) { case '%': char_arg[0] = c; argp = char_arg; arg_len = 1; break; case 's': argp = va_arg (args, char *); arg_len = strlen (argp); break; case 'd': /* Represent an out-of-range file descriptor with an out-of-range integer value. We can do this because the only use of `%d' in the calls to cprintf is to output a file descriptor number for a redirection. */ digit_arg = va_arg (args, int); if (digit_arg < 0) { sprintf (intbuf, "%u", (unsigned)-1); argp = intbuf; } else argp = inttostr (digit_arg, intbuf, sizeof (intbuf)); arg_len = strlen (argp); break; case 'c': char_arg[0] = va_arg (args, int); argp = char_arg; arg_len = 1; break; default: programming_error (_("cprintf: `%c': invalid format character"), c); /*NOTREACHED*/ } } if (argp && arg_len) { the_printed_command_resize (arg_len + 1); FASTCOPY (argp, the_printed_command + command_string_index, arg_len); command_string_index += arg_len; } } the_printed_command[command_string_index] = '\0';}/* Ensure that there is enough space to stuff LENGTH characters into THE_PRINTED_COMMAND. */static voidthe_printed_command_resize (length) int length;{ if (the_printed_command == 0) { the_printed_command_size = (length + PRINTED_COMMAND_INITIAL_SIZE - 1) & ~(PRINTED_COMMAND_INITIAL_SIZE - 1); the_printed_command = (char *)xmalloc (the_printed_command_size); command_string_index = 0; } else if ((command_string_index + length) >= the_printed_command_size) { int new; new = command_string_index + length + 1; /* Round up to the next multiple of PRINTED_COMMAND_GROW_SIZE. */ new = (new + PRINTED_COMMAND_GROW_SIZE - 1) & ~(PRINTED_COMMAND_GROW_SIZE - 1); the_printed_command_size = new; the_printed_command = (char *)xrealloc (the_printed_command, the_printed_command_size); }}#if defined (HAVE_VPRINTF)/* ``If vprintf is available, you may assume that vfprintf and vsprintf are also available.'' */static void#if defined (PREFER_STDARG)xprintf (const char *format, ...)#elsexprintf (format, va_alist) const char *format; va_dcl#endif{ va_list args; SH_VA_START (args, format); vfprintf (stdout, format, args); va_end (args);}#elsestatic voidxprintf (format, arg1, arg2, arg3, arg4, arg5) const char *format;{ printf (format, arg1, arg2, arg3, arg4, arg5);}#endif /* !HAVE_VPRINTF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -