aboutsummaryrefslogtreecommitdiffhomepage
path: root/fish_indent.cpp
diff options
context:
space:
mode:
authorGravatar Łukasz Niemier <lukasz@niemier.pl>2012-11-18 11:23:22 +0100
committerGravatar Łukasz Niemier <lukasz@niemier.pl>2012-11-18 11:23:22 +0100
commit47df1ae40adecd0a02fc7dd06ab0745cb18c3fe0 (patch)
tree13bf3e8fdcae60fdfb5fa5e26c95818dc7a49790 /fish_indent.cpp
parentb79854ad1aa814d9d35d76a1929b4726fa4bffa5 (diff)
Remove trailing whitespaces and change tabs to spaces
Diffstat (limited to 'fish_indent.cpp')
-rw-r--r--fish_indent.cpp562
1 files changed, 281 insertions, 281 deletions
diff --git a/fish_indent.cpp b/fish_indent.cpp
index 298d52e8..c04e6ded 100644
--- a/fish_indent.cpp
+++ b/fish_indent.cpp
@@ -17,7 +17,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/** \file fish_indent.cpp
- The fish_indent proegram.
+ The fish_indent proegram.
*/
#include "config.h"
@@ -51,22 +51,22 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
static void read_file( FILE *f, wcstring &b )
{
- while( 1 )
- {
- errno=0;
- wint_t c = fgetwc( f );
- if( c == WEOF )
- {
- if( errno )
- {
- wperror(L"fgetwc");
- exit(1);
- }
-
- break;
- }
- b.push_back((wchar_t)c);
- }
+ while( 1 )
+ {
+ errno=0;
+ wint_t c = fgetwc( f );
+ if( c == WEOF )
+ {
+ if( errno )
+ {
+ wperror(L"fgetwc");
+ exit(1);
+ }
+
+ break;
+ }
+ b.push_back((wchar_t)c);
+ }
}
/**
@@ -76,7 +76,7 @@ static void insert_tabs( wcstring &out, int indent )
{
if (indent > 0)
out.append((size_t)indent, L'\t');
-
+
}
/**
@@ -84,175 +84,175 @@ static void insert_tabs( wcstring &out, int indent )
*/
static int indent( wcstring &out, const wcstring &in, int flags )
{
- tokenizer tok;
- int res=0;
- int is_command = 1;
- int indent = 0;
- int do_indent = 1;
- int prev_type = 0;
- int prev_prev_type = 0;
-
- tok_init( &tok, in.c_str(), TOK_SHOW_COMMENTS );
-
- for( ; tok_has_next( &tok ); tok_next( &tok ) )
- {
- int type = tok_last_type( &tok );
- wchar_t *last = tok_last( &tok );
-
- switch( type )
- {
- case TOK_STRING:
- {
- if( is_command )
- {
- int next_indent = indent;
- is_command = 0;
-
+ tokenizer tok;
+ int res=0;
+ int is_command = 1;
+ int indent = 0;
+ int do_indent = 1;
+ int prev_type = 0;
+ int prev_prev_type = 0;
+
+ tok_init( &tok, in.c_str(), TOK_SHOW_COMMENTS );
+
+ for( ; tok_has_next( &tok ); tok_next( &tok ) )
+ {
+ int type = tok_last_type( &tok );
+ wchar_t *last = tok_last( &tok );
+
+ switch( type )
+ {
+ case TOK_STRING:
+ {
+ if( is_command )
+ {
+ int next_indent = indent;
+ is_command = 0;
+
wcstring unesc = last;
unescape_string(unesc, UNESCAPE_SPECIAL);
-
- if( parser_keywords_is_block(unesc))
- {
- next_indent++;
- }
- else if (unesc == L"else")
- {
- indent--;
- }
- /* case should have the same indent level as switch*/
- else if (unesc == L"case")
- {
- indent--;
- }
- else if (unesc == L"end")
- {
- indent--;
- next_indent--;
- }
-
-
- if( do_indent && flags && prev_type != TOK_PIPE )
- {
- insert_tabs( out, indent );
- }
-
+
+ if( parser_keywords_is_block(unesc))
+ {
+ next_indent++;
+ }
+ else if (unesc == L"else")
+ {
+ indent--;
+ }
+ /* case should have the same indent level as switch*/
+ else if (unesc == L"case")
+ {
+ indent--;
+ }
+ else if (unesc == L"end")
+ {
+ indent--;
+ next_indent--;
+ }
+
+
+ if( do_indent && flags && prev_type != TOK_PIPE )
+ {
+ insert_tabs( out, indent );
+ }
+
append_format(out, L"%ls", last );
-
- indent = next_indent;
-
- }
- else
- {
- if ( prev_type != TOK_REDIRECT_FD )
- out.append( L" " );
- out.append( last );
- }
-
- break;
- }
-
- case TOK_END:
- {
- if( prev_type != TOK_END || prev_prev_type != TOK_END )
- out.append( L"\n" );
- do_indent = 1;
- is_command = 1;
- break;
- }
-
- case TOK_PIPE:
- {
- out.append( L" " );
- if ( last[0] == '2' && !last[1] ) {
- out.append( L"^" );
- } else if ( last[0] != '1' || last[1] ) {
- out.append( last);
+
+ indent = next_indent;
+
+ }
+ else
+ {
+ if ( prev_type != TOK_REDIRECT_FD )
+ out.append( L" " );
+ out.append( last );
+ }
+
+ break;
+ }
+
+ case TOK_END:
+ {
+ if( prev_type != TOK_END || prev_prev_type != TOK_END )
+ out.append( L"\n" );
+ do_indent = 1;
+ is_command = 1;
+ break;
+ }
+
+ case TOK_PIPE:
+ {
+ out.append( L" " );
+ if ( last[0] == '2' && !last[1] ) {
+ out.append( L"^" );
+ } else if ( last[0] != '1' || last[1] ) {
+ out.append( last);
out.append( L">" );
- }
- out.append( L" | " );
- is_command = 1;
- break;
- }
-
- case TOK_REDIRECT_OUT:
- {
- out.append( L" " );
- if ( wcscmp( last, L"2" ) == 0 ) {
- out.append( L"^" );
- } else {
- if ( wcscmp( last, L"1" ) != 0 )
- out.append( last );
- out.append( L"> " );
- }
- break;
- }
-
- case TOK_REDIRECT_APPEND:
- {
- out.append( L" " );
- if ( wcscmp( last, L"2" ) == 0 ) {
- out.append( L"^^" );
- } else {
- if ( wcscmp( last, L"1" ) != 0 )
- out.append( last );
- out.append( L">> " );
- }
- break;
- }
-
- case TOK_REDIRECT_IN:
- {
- out.append( L" " );
- if ( wcscmp( last, L"0" ) != 0 )
- out.append( last );
- out.append( L"< " );
- break;
- }
-
- case TOK_REDIRECT_FD:
- {
- out.append( L" " );
- if ( wcscmp( last, L"1" ) != 0 )
- out.append( last );
- out.append( L">& " );
- break;
- }
-
- case TOK_BACKGROUND:
- {
- out.append( L"&\n" );
- do_indent = 1;
- is_command = 1;
- break;
- }
-
- case TOK_COMMENT:
- {
- if( do_indent && flags)
- {
- insert_tabs( out, indent );
- }
-
- append_format( out, L"%ls", last );
- do_indent = 1;
- break;
- }
-
- default:
- {
- debug( 0, L"Unknown token '%ls'", last );
- exit(1);
- }
- }
-
- prev_prev_type = prev_type;
- prev_type = type;
-
- }
-
- tok_destroy( &tok );
-
- return res;
+ }
+ out.append( L" | " );
+ is_command = 1;
+ break;
+ }
+
+ case TOK_REDIRECT_OUT:
+ {
+ out.append( L" " );
+ if ( wcscmp( last, L"2" ) == 0 ) {
+ out.append( L"^" );
+ } else {
+ if ( wcscmp( last, L"1" ) != 0 )
+ out.append( last );
+ out.append( L"> " );
+ }
+ break;
+ }
+
+ case TOK_REDIRECT_APPEND:
+ {
+ out.append( L" " );
+ if ( wcscmp( last, L"2" ) == 0 ) {
+ out.append( L"^^" );
+ } else {
+ if ( wcscmp( last, L"1" ) != 0 )
+ out.append( last );
+ out.append( L">> " );
+ }
+ break;
+ }
+
+ case TOK_REDIRECT_IN:
+ {
+ out.append( L" " );
+ if ( wcscmp( last, L"0" ) != 0 )
+ out.append( last );
+ out.append( L"< " );
+ break;
+ }
+
+ case TOK_REDIRECT_FD:
+ {
+ out.append( L" " );
+ if ( wcscmp( last, L"1" ) != 0 )
+ out.append( last );
+ out.append( L">& " );
+ break;
+ }
+
+ case TOK_BACKGROUND:
+ {
+ out.append( L"&\n" );
+ do_indent = 1;
+ is_command = 1;
+ break;
+ }
+
+ case TOK_COMMENT:
+ {
+ if( do_indent && flags)
+ {
+ insert_tabs( out, indent );
+ }
+
+ append_format( out, L"%ls", last );
+ do_indent = 1;
+ break;
+ }
+
+ default:
+ {
+ debug( 0, L"Unknown token '%ls'", last );
+ exit(1);
+ }
+ }
+
+ prev_prev_type = prev_type;
+ prev_type = type;
+
+ }
+
+ tok_destroy( &tok );
+
+ return res;
}
/**
@@ -267,7 +267,7 @@ static void trim( wcstring &str )
size_t pos = str.find_first_not_of(L" \n");
if (pos > 0)
str.erase(0, pos);
-
+
pos = str.find_last_not_of(L" \n");
if (pos != wcstring::npos && pos + 1 < str.length())
str.erase(pos + 1);
@@ -278,106 +278,106 @@ static void trim( wcstring &str )
The main mathod. Run the program.
*/
int main( int argc, char **argv )
-{
- int do_indent=1;
- set_main_thread();
+{
+ int do_indent=1;
+ set_main_thread();
setup_fork_guards();
-
- wsetlocale( LC_ALL, L"" );
- program_name=L"fish_indent";
-
- while( 1 )
- {
- static struct option
- long_options[] =
- {
- {
- "no-indent", no_argument, 0, 'i'
- }
- ,
- {
- "help", no_argument, 0, 'h'
- }
- ,
- {
- "version", no_argument, 0, 'v'
- }
- ,
- {
- 0, 0, 0, 0
- }
- }
- ;
-
- int opt_index = 0;
-
- int opt = getopt_long( argc,
- argv,
- GETOPT_STRING,
- long_options,
- &opt_index );
-
- if( opt == -1 )
- break;
-
- switch( opt )
- {
- case 0:
- {
- break;
- }
-
- case 'h':
- {
- print_help( "fish_indent", 1 );
- exit( 0 );
- break;
- }
-
- case 'v':
- {
- fwprintf( stderr,
- _(L"%ls, version %s\n"),
- program_name,
- PACKAGE_VERSION );
- exit( 0 );
- }
-
- case 'i':
- {
- do_indent = 0;
- break;
- }
-
-
- case '?':
- {
- exit( 1 );
- }
-
- }
- }
+
+ wsetlocale( LC_ALL, L"" );
+ program_name=L"fish_indent";
+
+ while( 1 )
+ {
+ static struct option
+ long_options[] =
+ {
+ {
+ "no-indent", no_argument, 0, 'i'
+ }
+ ,
+ {
+ "help", no_argument, 0, 'h'
+ }
+ ,
+ {
+ "version", no_argument, 0, 'v'
+ }
+ ,
+ {
+ 0, 0, 0, 0
+ }
+ }
+ ;
+
+ int opt_index = 0;
+
+ int opt = getopt_long( argc,
+ argv,
+ GETOPT_STRING,
+ long_options,
+ &opt_index );
+
+ if( opt == -1 )
+ break;
+
+ switch( opt )
+ {
+ case 0:
+ {
+ break;
+ }
+
+ case 'h':
+ {
+ print_help( "fish_indent", 1 );
+ exit( 0 );
+ break;
+ }
+
+ case 'v':
+ {
+ fwprintf( stderr,
+ _(L"%ls, version %s\n"),
+ program_name,
+ PACKAGE_VERSION );
+ exit( 0 );
+ }
+
+ case 'i':
+ {
+ do_indent = 0;
+ break;
+ }
+
+
+ case '?':
+ {
+ exit( 1 );
+ }
+
+ }
+ }
wcstring sb_in, sb_out;
- read_file( stdin, sb_in );
-
- wutil_init();
+ read_file( stdin, sb_in );
+
+ wutil_init();
- if( !indent( sb_out, sb_in, do_indent ) )
- {
+ if( !indent( sb_out, sb_in, do_indent ) )
+ {
trim(sb_out);
- fwprintf( stdout, L"%ls", sb_out.c_str() );
- }
- else
- {
- /*
- Indenting failed - print original input
- */
- fwprintf( stdout, L"%ls", sb_in.c_str() );
- }
-
-
- wutil_destroy();
-
- return 0;
+ fwprintf( stdout, L"%ls", sb_out.c_str() );
+ }
+ else
+ {
+ /*
+ Indenting failed - print original input
+ */
+ fwprintf( stdout, L"%ls", sb_in.c_str() );
+ }
+
+
+ wutil_destroy();
+
+ return 0;
}