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

📄 indent.c

📁 代码格式化工具。 其实就是linux下indent的windows版本。
💻 C
📖 第 1 页 / 共 5 页
字号:
/* Copyright (c) 1999, 2000 Carlo Wood.  All rights reserved. * Copyright (c) 1994, 1996, 1997 Joseph Arceneaux.  All rights reserved. * Copyright (c) 1992, Free Software Foundation, Inc.  All rights reserved. * * Copyright (c) 1985 Sun Microsystems, Inc.  * Copyright (c) 1980 The Regents of the University of California. * Copyright (c) 1976 Board of Trustees of the University of Illinois. All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that * the above copyright notice and this paragraph are duplicated in all such * forms and that any documentation, advertising materials, and other * materials related to such distribution and use acknowledge that the * software was developed by the University of California, Berkeley, the * University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of * either University or Sun Microsystems may not be used to endorse or * promote products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * Updates; * 2002-08-05: Matthias <moh@itec.uni-klu.ac.at> and Eric Lloyd <ewlloyd@neta.com> *             Added support for -brf to place function opening brace after function *             declaration. */#include "sys.h"#if defined (HAVE_UNISTD_H)#include <unistd.h>#endif#ifdef PRESERVE_MTIME#include <time.h>#ifdef HAVE_UTIME_H#include <utime.h>#elif defined(HAVE_SYS_UTIME_H)#include <sys/utime.h>#endif#endif#include <sys/stat.h>#include <string.h>#include <ctype.h>#include <stdlib.h>#ifdef HAVE_LOCALE_H#include <locale.h>#endif#include "indent.h"#include "backup.h"#include "io.h"#include "globs.h"#include "parse.h"#include "comments.h"#include "args.h"#include "output.h"#include <io.h>RCSTAG_CC ("$Id: indent.c,v 1.77 2002/10/28 20:00:56 david Exp $");/* Round up P to be a multiple of SIZE. */#ifndef ROUND_UP#define ROUND_UP(p, size) (((unsigned long) (p) + (size) - 1) & ~((size) - 1))#endif/* Stuff that needs to be shared with the rest of indent. * Documented in indent.h. */char           * labbuf                      = NULL;char           * s_lab                       = NULL;char           * e_lab                       = NULL;char           * l_lab                       = NULL;char           * codebuf                     = NULL;char           * s_code                      = NULL;char           * e_code                      = NULL;char           * l_code                      = NULL;char           * combuf                      = NULL;char           * s_com                       = NULL;char           * e_com                       = NULL;char           * l_com                       = NULL;char           * s_code_corresponds_to       = NULL;buf_ty           save_com;char           * bp_save                     = NULL;char           * be_save                     = NULL;int              code_lines                  = 0;int              line_no                     = 0;int              break_comma                 = 0;int              n_real_blanklines           = 0;int              prefix_blankline_requested  = 0;codes_ty         prefix_blankline_requested_code;int              postfix_blankline_requested  = 0;codes_ty         postfix_blankline_requested_code;char           * in_name                     = 0; /* Points to current input file name */file_buffer_ty * current_input               = 0; /* Points to the current input buffer */int              embedded_comment_on_line    = 0; /* True if there is an embedded comment on this code line */int              else_or_endif               = 0;int            * di_stack                    = NULL; /* structure indentation levels */int              di_stack_alloc              = 0; /* Currently allocated size of di_stack.  */int              squest                      = 0; /* when this is positive, we have seen a ? without                                                * the matching : in a <c>?<s>:<s> construct */unsigned long    in_prog_size   = 0U;char           * in_prog        = NULL;int              break_line     = 0;/* The position that we will line the current line up with when it comes time * to print it (if we are lining up to parentheses).  */static int       paren_target   = 0;#ifdef DEBUGint            debug = 1;#endif/******************************************************************************/static void check_code_size(void){    if (e_code >= l_code)                                   {                                                           int nsize = l_code - s_code + 400;                       codebuf   = (char *) xrealloc (codebuf, nsize);          e_code    = codebuf + (e_code - s_code) + 1;         l_code    = codebuf + nsize - 5;         s_code    = codebuf + 1;     }}/******************************************************************************/static void check_lab_size(void){    if (e_lab >= l_lab)    {        int nsize  = l_lab - s_lab + 400;                       labbuf = (char *) xrealloc (labbuf, nsize);         e_lab  = labbuf + (e_lab - s_lab) + 1;                l_lab  = labbuf + nsize - 5;                        s_lab  = labbuf + 1;                            }}/******************************************************************************/static INLINE void need_chars (    buf_ty * bp,    int      needed){    int current_size = (bp->end - bp->ptr);    if ((current_size + needed) >= bp->size)    {        bp->size = ROUND_UP (current_size + needed, 1024);        bp->ptr = xrealloc (bp->ptr, bp->size);        if (bp->ptr == NULL)        {            fatal (_("Ran out of memory"), 0);        }        bp->end = bp->ptr + current_size;    }}/******************************************************************************//* Compute the length of the line we will be outputting. */int output_line_length (void){    int code_length = 0;    int com_length = 0;    int length;    if (s_lab == e_lab)    {        length = 0;    }    else    {        length = count_columns (compute_label_target (), s_lab, EOL) - 1;    }    if (s_code != e_code)    {        int code_col = compute_code_target (paren_target);        code_length = count_columns (code_col, s_code, EOL) - code_col;    }    if (s_com != e_com)    {        int com_col = parser_state_tos->com_col;        com_length = count_columns (com_col, s_com, EOL) - com_col;    }    if (code_length != 0)    {        length += compute_code_target(paren_target) - 1 + code_length;        if (embedded_comment_on_line)        {            length += com_length;        }    }    return length;}/******************************************************************************/static void copy_id(    const codes_ty   type_code,    BOOLEAN        * force_nl,    exit_values_ty * file_exit_value,    const bb_code_ty can_break){    char           * t_ptr;        if (parser_state_tos->want_blank)    {        set_buf_break (bb_ident, paren_target);        *e_code++ = ' ';    }    else if (can_break)    {        set_buf_break (can_break, paren_target);    }    if (s_code == e_code)    {        s_code_corresponds_to = token;    }    for (t_ptr = token; t_ptr < token_end; ++t_ptr)    {        check_code_size();        *e_code++ = *t_ptr;    }    *e_code = '\0'; /* null terminate code sect */    parser_state_tos->want_blank = true;    /* Handle the options -nsaf, -nsai and -nsaw */    if ((type_code == sp_paren) &&        ((!settings.space_after_if && (*token == 'i')) ||         (!settings.space_after_for && (*token == 'f')) ||         (!settings.space_after_while && (*token == 'w'))))    {        parser_state_tos->want_blank = false;    }    /* If the token is one of the GNU gettext macro's '_' or 'N_'     * then we don't want a blank */    if ((((token_end - token) == 1) && (*token == '_')) ||        (((token_end - token) == 2) && (*token == 'N') &&         (token[1] == '_')))    {        parser_state_tos->want_blank = false;    }    /* If the token is va_dcl, it appears without a semicolon, so we     * need to pretend that one was there.  */    if (((token_end - token) == 6) && (strncmp (token, "va_dcl", 6) == 0))    {        parser_state_tos->in_or_st = 0;        parser_state_tos->just_saw_decl--;        parser_state_tos->in_decl = false;        do        {            if (parse (semicolon) != total_success)             {                *file_exit_value = indent_error;            }        } while(0);                        *force_nl = true;    }}/******************************************************************************/static void handle_token_form_feed(void){    parser_state_tos->use_ff = true;        /* a form feed is treated                                             * much like a newline */    dump_line (true, &paren_target);    parser_state_tos->want_blank = false;}/****************************************************************************** * 2002-06-13 D.Ingamells Reset force_nl if the line is dumped. */static void handle_token_newline(    BOOLEAN * force_nl){                if (s_lab != e_lab && *s_lab == '#')    {        dump_line (true, &paren_target);        if (s_code == e_code)        {            parser_state_tos->want_blank = false;        }        *force_nl = false;    }    else    {        if ( ( (parser_state_tos->last_token != comma) ||                !settings.leave_comma ||               !break_comma ||                (parser_state_tos->p_l_follow > 0) ||               parser_state_tos->block_init ||                (s_com != e_com)) &&             ( ( (parser_state_tos->last_token != rbrace) ||                 !(settings.braces_on_struct_decl_line &&                    parser_state_tos->in_decl))))        {            /* Attempt to detect the newline before a procedure name,             * and if e.g., K&R style, leave the procedure on the             * same line as the type. */            if (!settings.procnames_start_line &&                 (s_lab == e_lab) &&                (parser_state_tos->last_token != lparen) &&                (parser_state_tos->last_token != semicolon) &&                (parser_state_tos->last_token != comma) &&                (parser_state_tos->last_rw == rw_decl) &&                (parser_state_tos->last_rw_depth == 0) &&                (!parser_state_tos->block_init) &&                (parser_state_tos->in_decl))            {                /* Put a space between the type and the procedure name,                 * unless it was a pointer type and the user doesn't                 * want such spaces after '*'. */                if (!((e_code > s_code) &&                      (e_code[-1] == '*')))                {                    parser_state_tos->want_blank = true;                }            }            if (!parser_state_tos->in_stmt ||                (s_com != e_com) ||                 embedded_comment_on_line)            {                dump_line (true, &paren_target);                if (s_code == e_code)                {                    parser_state_tos->want_blank = false;                }                *force_nl = false;            }        }    }    /* If we were on the line with a #else or a #endif, we aren't     * anymore.  */    else_or_endif = false;    ++line_no;              /* keep track of input line number */}/******************************************************************************/static void handle_token_lparen(    BOOLEAN        * force_nl,   BOOLEAN        * sp_sw,   int            * dec_ind){    char lparen = *token;        /* Braces in initializer lists should be put on new lines. This is     * necessary so that -gnu does not cause things like char     * *this_is_a_string_array[] = { "foo", "this_string_does_not_fit",     * "nor_does_this_rather_long_string" } which is what happens     * because we are trying to line the strings up with the     * parentheses, and those that are too long are moved to the right     * an ugly amount.     *     * However, if the current line is empty, the left brace is     * already on a new line, so don't molest it.     */    if ((*token == '{') && ((s_code != e_code) ||                              (s_com  != e_com)  ||                              (s_lab  != e_lab)))    {        dump_line (true, &paren_target);        /* Do not put a space before the '{'.  */        parser_state_tos->want_blank = false;    }    /* Count parens so we know how deep we are.  */        ++parser_state_tos->p_l_follow;        if (parser_state_tos->p_l_follow >= parser_state_tos->paren_indents_size)    {        parser_state_tos->paren_indents_size *= 2;        parser_state_tos->paren_indents =                (short *) xrealloc ((char *) parser_state_tos->paren_indents,                                    parser_state_tos->paren_indents_size *                                    sizeof (short));    }    parser_state_tos->paren_depth++;    if (parser_state_tos->want_blank &&        (*token != '[') &&        ( (parser_state_tos->last_token != ident) ||          settings.proc_calls_space ||          (parser_state_tos->its_a_keyword &&           (!parser_state_tos->sizeof_keyword || settings.blank_after_sizeof))))    {        set_buf_break (bb_proc_call, paren_target);        *e_code++ = ' ';        *e_code = '\0';     /* null terminate code sect */    }    else    {

⌨️ 快捷键说明

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