fish-shell/fish_indent.cpp

381 lines
6.4 KiB
C++
Raw Normal View History

/*
Copyright (C) 2005-2008 Axel Liljencrantz
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
2012-02-22 20:00:02 +00:00
/** \file fish_indent.cpp
The fish_indent proegram.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <locale.h>
#include "fallback.h"
#include "util.h"
#include "common.h"
#include "wutil.h"
#include "tokenizer.h"
#include "print_help.h"
#include "parser_keywords.h"
/**
The string describing the single-character options accepted by the main fish binary
*/
#define GETOPT_STRING "hvi"
/**
2012-02-22 20:00:02 +00:00
Read the entire contents of a file into the specified string
*/
2012-02-22 20:00:02 +00:00
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;
}
2012-02-22 20:00:02 +00:00
b.push_back(c);
}
}
/**
2012-02-22 20:00:02 +00:00
Insert the specified number of tabs into the output buffer
*/
2012-02-22 20:00:02 +00:00
static void insert_tabs( wcstring &out, int indent )
{
2012-02-22 20:00:02 +00:00
out.append(L'\t', indent);
}
/**
Indent the specified input
*/
2012-02-22 20:00:02 +00:00
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;
2012-02-22 20:00:02 +00:00
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;
wchar_t *unesc = unescape( last, UNESCAPE_SPECIAL );
if( parser_keywords_is_block( unesc ) )
{
next_indent++;
}
else if( wcscmp( unesc, L"else" ) == 0 )
{
indent--;
}
/* case should have the same indent level as switch*/
else if( wcscmp( unesc, L"case" ) == 0 )
{
indent--;
}
else if( wcscmp( unesc, L"end" ) == 0 )
{
indent--;
next_indent--;
}
if( do_indent && flags && prev_type != TOK_PIPE )
{
insert_tabs( out, indent );
}
2012-02-22 20:00:02 +00:00
append_format(out, L"%ls", last );
indent = next_indent;
}
else
{
if ( prev_type != TOK_REDIRECT_FD )
2012-02-22 20:00:02 +00:00
out.append( L" " );
out.append( last );
}
break;
}
case TOK_END:
{
if( prev_type != TOK_END || prev_prev_type != TOK_END )
2012-02-22 20:00:02 +00:00
out.append( L"\n" );
do_indent = 1;
is_command = 1;
break;
}
case TOK_PIPE:
{
2012-02-22 20:00:02 +00:00
out.append( L" " );
if ( last[0] == '2' && !last[1] ) {
2012-02-22 20:00:02 +00:00
out.append( L"^" );
} else if ( last[0] != '1' || last[1] ) {
2012-02-22 20:00:02 +00:00
out.append( last);
out.append( L">" );
}
2012-02-22 20:00:02 +00:00
out.append( L" | " );
is_command = 1;
break;
}
case TOK_REDIRECT_OUT:
{
2012-02-22 20:00:02 +00:00
out.append( L" " );
if ( wcscmp( last, L"2" ) == 0 ) {
2012-02-22 20:00:02 +00:00
out.append( L"^" );
} else {
if ( wcscmp( last, L"1" ) != 0 )
2012-02-22 20:00:02 +00:00
out.append( last );
out.append( L"> " );
}
break;
}
case TOK_REDIRECT_APPEND:
{
2012-02-22 20:00:02 +00:00
out.append( L" " );
if ( wcscmp( last, L"2" ) == 0 ) {
2012-02-22 20:00:02 +00:00
out.append( L"^^" );
} else {
if ( wcscmp( last, L"1" ) != 0 )
2012-02-22 20:00:02 +00:00
out.append( last );
out.append( L">> " );
}
break;
}
case TOK_REDIRECT_IN:
{
2012-02-22 20:00:02 +00:00
out.append( L" " );
if ( wcscmp( last, L"0" ) != 0 )
2012-02-22 20:00:02 +00:00
out.append( last );
out.append( L"< " );
break;
}
case TOK_REDIRECT_FD:
{
2012-02-22 20:00:02 +00:00
out.append( L" " );
if ( wcscmp( last, L"1" ) != 0 )
2012-02-22 20:00:02 +00:00
out.append( last );
out.append( L">& " );
break;
}
case TOK_BACKGROUND:
{
2012-02-22 20:00:02 +00:00
out.append( L"&\n" );
do_indent = 1;
is_command = 1;
break;
}
case TOK_COMMENT:
{
if( do_indent && flags)
{
insert_tabs( out, indent );
}
2012-02-22 20:00:02 +00:00
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;
}
/**
Remove any prefix and suffix newlines from the specified
2012-02-22 20:00:02 +00:00
string.
*/
2012-02-22 20:00:02 +00:00
static void trim( wcstring &str )
{
2012-02-22 20:00:02 +00:00
if (str.empty())
return;
2012-02-22 20:00:02 +00:00
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);
}
/**
The main mathod. Run the program.
*/
int main( int argc, char **argv )
2012-02-22 20:00:02 +00:00
{
int do_indent=1;
set_main_thread();
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 );
}
}
}
2012-02-22 20:00:02 +00:00
wcstring sb_in, sb_out;
read_file( stdin, sb_in );
wutil_init();
2012-02-22 20:00:02 +00:00
if( !indent( sb_out, sb_in, do_indent ) )
{
2012-02-22 20:00:02 +00:00
trim(sb_out);
fwprintf( stdout, L"%ls", sb_out.c_str() );
}
else
{
/*
Indenting failed - print original input
*/
2012-02-22 20:00:02 +00:00
fwprintf( stdout, L"%ls", sb_in.c_str() );
}
wutil_destroy();
return 0;
}