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

📄 indent.c

📁 GNU 系统开发优化 C 语言程序的应用程序
💻 C
📖 第 1 页 / 共 4 页
字号:
/* 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. */#include "sys.h"#include "indent.h"#include <ctype.h>voidusage (){  fprintf (stderr, "usage: indent file [-o outfile ] [ options ]\n");  fprintf (stderr, "       indent file1 file2 ... fileN [ options ]\n");  exit (1);}/* Stuff that needs to be shared with the rest of indent.   Documented in indent.h.  */char *labbuf;char *s_lab;char *e_lab;char *l_lab;char *codebuf;char *s_code;char *e_code;char *l_code;char *combuf;char *s_com;char *e_com;char *l_com;struct buf save_com;char *bp_save;char *be_save;int code_lines;int line_no;struct fstate keywordf;struct fstate stringf;struct fstate boxcomf;struct fstate blkcomf;struct fstate scomf;struct fstate bodyf;int break_comma;/* Insure that BUFSTRUC has at least REQ more chars left, if not extend it.      Note:  This may change bufstruc.ptr.  */#define need_chars(bufstruc, req) \  if ((bufstruc.end - bufstruc.ptr + (req)) >= bufstruc.size) \{\         int cur_chars = bufstruc.end - bufstruc.ptr;\         bufstruc.size *= 2;\         bufstruc.ptr = xrealloc (bufstruc.ptr,bufstruc.size);\         bufstruc.end = bufstruc.ptr + cur_chars;\}/* True if there is an embedded comment on this code line */int embedded_comment_on_line;int else_or_endif;/* structure indentation levels */int *di_stack;/* Currently allocated size of di_stack.  */int di_stack_alloc;/* when this is positive, we have seen a ? without   the matching : in a <c>?<s>:<s> construct */int squest;#define CHECK_CODE_SIZE \	if (e_code >= l_code) { \	    register 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; \	}#define CHECK_LAB_SIZE \	if (e_lab >= l_lab) { \	    register 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 voidindent (this_file)     struct file_buffer *this_file;{  register int i;  enum codes hd_type;  register char *t_ptr;  enum codes type_code;  /* current indentation for declarations */  int dec_ind = 0;  /* used when buffering up comments to remember that     a newline was passed over */  int flushed_nl = 0;		  int force_nl = 0;  /* true when we've just see a case; determines what to do     with the following colon */  int scase = 0;  /* true when in the expression part of if(...), while(...), etc. */  int sp_sw = 0;  /* True if we have just encountered the end of an if (...), etc. (i.e. the     ')' of the if (...) was the last token).  The variable is set to 2 in     the middle of the main token reading loop and is decremented at the     beginning of the loop, so it will reach zero when the second token after     the ')' is read.  */  int last_token_ends_sp = 0;  /* true iff last keyword was an else */  int last_else = 0;  in_prog = in_prog_pos = this_file->data;  in_prog_size = this_file->size;  hd_type = code_eof;  dec_ind = 0;  last_token_ends_sp = false;  last_else = false;  sp_sw = force_nl = false;  scase = false;  squest = false;#if 0  if (com_ind <= 1)    com_ind = 2;		/* dont put normal comments before column 2 */#endif  if (troff)    {      if (bodyf.font[0] == 0)	parsefont (&bodyf, "R");      if (scomf.font[0] == 0)	parsefont (&scomf, "I");      if (blkcomf.font[0] == 0)	blkcomf = scomf, blkcomf.size += 2;      if (boxcomf.font[0] == 0)	boxcomf = blkcomf;      if (stringf.font[0] == 0)	parsefont (&stringf, "L");      if (keywordf.font[0] == 0)	parsefont (&keywordf, "B");      writefdef (&bodyf, 'B');      writefdef (&scomf, 'C');      writefdef (&blkcomf, 'L');      writefdef (&boxcomf, 'X');      writefdef (&stringf, 'S');      writefdef (&keywordf, 'K');    }  if (block_comment_max_col <= 0)    block_comment_max_col = max_col;  if (decl_com_ind <= 0)	/* if not specified by user, set this */    decl_com_ind =      ljust_decl ? (com_ind <= 10 ? 2 : com_ind - 8) : com_ind;  if (continuation_indent == 0)    continuation_indent = ind_size;  fill_buffer ();		/* get first batch of stuff into input buffer */#if 0  parse (semicolon);#endif  {    register char *p = buf_ptr;    register col = 1;    while (1)      {	if (*p == ' ')	  col++;	else if (*p == TAB)	  col = tabsize - (col % tabsize) + 1;	else if (*p == EOL)	  col = 1;	else	  break;	p++;      }#if 0    if (col > ind_size)      parser_state_tos->ind_level = parser_state_tos->i_l_follow = col;#endif/  }  if (troff)    {      register char *p = in_name, *beg = in_name;      while (*p)	if (*p++ == '/')	  beg = p;      fprintf (output, ".Fn \"%s\"\n", beg);    }  /* START OF MAIN LOOP */  while (1)    {				/* this is the main loop.  it will go until				   we reach eof */      int is_procname;      type_code = lexi ();	/* lexi reads one token.  "token" points to				   the actual characters. lexi returns a code				   indicating the type of token */      if (last_token_ends_sp > 0)	last_token_ends_sp--;      is_procname = parser_state_tos->procname[0];      /* The following code moves everything following an if (), while (),         else, etc. up to the start of the following stmt to a buffer. This         allows proper handling of both kinds of brace placement. */      flushed_nl = false;      while (parser_state_tos->search_brace)	{	  /* After scanning an if(), while (), etc., it might be necessary to	     keep track of the text between the if() and the start of the	     statement which follows.  Use save_com to do so.  */	  switch (type_code)	    {	    case newline:	      ++line_no;	      flushed_nl = true;	    case form_feed:	      break;		/* form feeds and newlines found here will be				   ignored */	    case lbrace:	/* this is a brace that starts the compound				   stmt */	      if (save_com.end == save_com.ptr)		{		  /* ignore buffering if a comment wasnt stored up */		  parser_state_tos->search_brace = false;		  goto check_type;		}	      /* We need to put the '{' back into save_com somewhere.  */	      if (btype_2)		/* Put it at the beginning, e.g. if (foo) { / * comment here *		   / */		save_com.ptr[0] = '{';	      else		{		  /* Put it at the end, e.g. if (foo) / * comment here * / { */		  /* Putting in this newline causes a dump_line to occur		     right after the comment, thus insuring that it will be		     put in the correct column.  */		  *save_com.end++ = EOL;		  *save_com.end++ = '{';		}	      /* Go to common code to get out of this loop.  */	      goto sw_buffer;	    case comment:	/* we have a comment, so we must copy it into				   the buffer */	      if (!flushed_nl || save_com.end != save_com.ptr)		{		  need_chars (save_com, 10);		  if (save_com.end == save_com.ptr)		    {		/* if this is the first comment, we must set				   up the buffer */		      save_com.ptr[0] = save_com.ptr[1] = ' ';		      save_com.end = save_com.ptr + 2;		    }		  else		    {		      *save_com.end++ = EOL;	/* add newline between						   comments */		      *save_com.end++ = ' ';		      --line_no;		    }		  *save_com.end++ = '/';	/* copy in start of comment */		  *save_com.end++ = '*';		  for (;;)		    {		/* loop until we get to the end of the				   comment */		      /* make sure there is room for this character and		         (while we're at it) the '/' we might add at the end		         of the loop. */		      need_chars (save_com, 2);		      *save_com.end = *buf_ptr++;		      if (buf_ptr >= buf_end)			{			  fill_buffer ();			  if (had_eof)			    {			      diag (1, "Unclosed comment", 0, 0);			      exit (1);			    }			}		      if (*save_com.end++ == '*' && *buf_ptr == '/')			break;	/* we are at end of comment */		    }		  *save_com.end++ = '/';	/* add ending slash */		  if (++buf_ptr >= buf_end)	/* get past / in buffer */		    fill_buffer ();		  break;		}	    default:		/* it is the start of a normal statment */	      if (flushed_nl)	/* if we flushed a newline, make sure it is				   put back */		force_nl = true;	      if ((type_code == sp_paren && *token == 'i'		   && last_else && else_if)		  ||		  (type_code == sp_nparen && *token == 'e'		   && e_code != s_code && e_code[-1] == '}'))		force_nl = false;	      if (save_com.end == save_com.ptr)		{		  /* ignore buffering if comment wasnt saved up */		  parser_state_tos->search_brace = false;		  goto check_type;		}	      if (force_nl)		{		/* if we should insert a nl here, put it into				   the buffer */		  force_nl = false;		  --line_no;	/* this will be re-increased when the nl is				   read from the buffer */		  need_chars (save_com, 2);		  *save_com.end++ = EOL;		  *save_com.end++ = ' ';		  if (verbose && !flushed_nl)	/* print error msg if the						   line was not already						   broken */		    diag (0, "Line broken", 0, 0);		  flushed_nl = false;		}	      for (t_ptr = token; t_ptr < token_end; ++t_ptr)		{		  need_chars (save_com, 1);		  *save_com.end++ = *t_ptr;	/* copy token into temp						   buffer */		}	      parser_state_tos->procname = "\0";	    sw_buffer:	      parser_state_tos->search_brace = false;	/* stop looking for							   start of stmt */	      bp_save = buf_ptr;/* save current input buffer */	      be_save = buf_end;	      buf_ptr = save_com.ptr;	/* fix so that subsequent calls to					   lexi will take tokens out of					   save_com */	      need_chars (save_com, 1);	      *save_com.end++ = ' ';	/* add trailing blank, just in case */	      buf_end = save_com.end;	      save_com.end = save_com.ptr;	/* make save_com empty */	      break;	    }			/* end of switch */	  /* we must make this check, just in case there was an unexpected	     EOF */	  if (type_code != code_eof)	    type_code = lexi ();/* read another token */	  /* if (parser_state_tos->search_brace)	     parser_state_tos->procname[0] = 0; */	  if ((is_procname = parser_state_tos->procname[0]) && flushed_nl	      && !procnames_start_line && parser_state_tos->in_decl	      && type_code == ident)	    flushed_nl = 0;	}			/* end of while (search_brace) */      last_else = 0;    check_type:      if (type_code == code_eof)	{			/* we got eof */	  if (s_lab != e_lab || s_code != e_code	      || s_com != e_com)/* must dump end of line */	    dump_line ();	  if (parser_state_tos->tos > 1)	/* check for balanced braces */	    diag (1, "Stuff missing from end of file.", 0, 0);	  if (verbose)	    {	      printf ("There were %d output lines and %d comments\n",		      (int) out_lines, (int) out_coms);	      printf ("(Lines with comments)/(Lines with code): %6.3f\n",		      (1.0 * com_lines) / code_lines);	    }	  fflush (output);	  if (found_err)	    exit (found_err);	  return;	}      if ((type_code != comment) &&	  (type_code != cplus_comment) &&	  (type_code != newline) &&	  (type_code != preesc) &&	  (type_code != form_feed))	{	  if (force_nl &&	      (type_code != semicolon) &&	      (type_code != lbrace || !btype_2))	    {	      /* we should force a broken line here */	      if (verbose && !flushed_nl)		diag (0, "Line broken", 0, 0);	      flushed_nl = false;	      dump_line ();	      parser_state_tos->want_blank = false;	/* dont insert blank at							   line start */	      force_nl = false;	    }	  parser_state_tos->in_stmt = true;	/* turn on flag which causes						   an extra level of						   indentation. this is						   turned off by a ; or } */	  if (s_com != e_com)	    {			/* the turkey has embedded a comment in a				   line. Move it from the com buffer to the				   code buffer.  */	      /* Do not add a space before the comment if it is the first	         thing on the line.  */	      if (e_code != s_code)		{

⌨️ 快捷键说明

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