2007-04-22 18:03:12 +08:00
|
|
|
/*
|
2008-01-13 03:18:48 +08:00
|
|
|
Copyright (C) 2005-2008 Axel Liljencrantz
|
2007-04-22 18:03:12 +08:00
|
|
|
|
|
|
|
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-23 04:00:02 +08:00
|
|
|
/** \file fish_indent.cpp
|
2007-08-02 01:38:01 +08:00
|
|
|
The fish_indent proegram.
|
2007-04-22 18:03:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2008-01-14 00:47:47 +08:00
|
|
|
/**
|
2012-02-23 04:00:02 +08:00
|
|
|
Read the entire contents of a file into the specified string
|
2008-01-14 00:47:47 +08:00
|
|
|
*/
|
2012-02-23 04:00:02 +08:00
|
|
|
static void read_file( FILE *f, wcstring &b )
|
2007-04-22 18:03:12 +08:00
|
|
|
{
|
|
|
|
while( 1 )
|
|
|
|
{
|
2008-01-14 00:49:05 +08:00
|
|
|
errno=0;
|
2007-04-22 18:03:12 +08:00
|
|
|
wint_t c = fgetwc( f );
|
|
|
|
if( c == WEOF )
|
|
|
|
{
|
2008-01-14 00:49:05 +08:00
|
|
|
if( errno )
|
|
|
|
{
|
|
|
|
wperror(L"fgetwc");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-06-02 05:38:41 +08:00
|
|
|
b.push_back((wchar_t)c);
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-14 00:47:47 +08:00
|
|
|
/**
|
2012-02-23 04:00:02 +08:00
|
|
|
Insert the specified number of tabs into the output buffer
|
2008-01-14 00:47:47 +08:00
|
|
|
*/
|
2012-02-23 04:00:02 +08:00
|
|
|
static void insert_tabs( wcstring &out, int indent )
|
2007-04-22 18:03:12 +08:00
|
|
|
{
|
2012-06-02 05:38:41 +08:00
|
|
|
out.append(indent, L'\t');
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
|
|
|
|
2008-01-14 00:47:47 +08:00
|
|
|
/**
|
|
|
|
Indent the specified input
|
|
|
|
*/
|
2012-02-23 04:00:02 +08:00
|
|
|
static int indent( wcstring &out, const wcstring &in, int flags )
|
2007-04-22 18:03:12 +08:00
|
|
|
{
|
|
|
|
tokenizer tok;
|
|
|
|
int res=0;
|
|
|
|
int is_command = 1;
|
|
|
|
int indent = 0;
|
|
|
|
int do_indent = 1;
|
2007-04-23 02:54:29 +08:00
|
|
|
int prev_type = 0;
|
|
|
|
int prev_prev_type = 0;
|
2007-04-22 18:03:12 +08:00
|
|
|
|
2012-02-23 04:00:02 +08:00
|
|
|
tok_init( &tok, in.c_str(), TOK_SHOW_COMMENTS );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
for( ; tok_has_next( &tok ); tok_next( &tok ) )
|
|
|
|
{
|
|
|
|
int type = tok_last_type( &tok );
|
|
|
|
wchar_t *last = tok_last( &tok );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case TOK_STRING:
|
|
|
|
{
|
|
|
|
if( is_command )
|
|
|
|
{
|
|
|
|
int next_indent = indent;
|
|
|
|
is_command = 0;
|
2012-05-09 17:55:36 +08:00
|
|
|
|
|
|
|
wcstring unesc = last;
|
|
|
|
unescape_string(unesc, UNESCAPE_SPECIAL);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-05-09 17:55:36 +08:00
|
|
|
if( parser_keywords_is_block(unesc))
|
2007-04-22 18:03:12 +08:00
|
|
|
{
|
|
|
|
next_indent++;
|
|
|
|
}
|
2012-05-09 17:55:36 +08:00
|
|
|
else if (unesc == L"else")
|
2007-04-22 18:03:12 +08:00
|
|
|
{
|
|
|
|
indent--;
|
|
|
|
}
|
2010-12-02 23:42:48 +08:00
|
|
|
/* case should have the same indent level as switch*/
|
2012-05-09 17:55:36 +08:00
|
|
|
else if (unesc == L"case")
|
2010-12-02 23:42:48 +08:00
|
|
|
{
|
|
|
|
indent--;
|
|
|
|
}
|
2012-05-09 17:55:36 +08:00
|
|
|
else if (unesc == L"end")
|
2007-04-22 18:03:12 +08:00
|
|
|
{
|
|
|
|
indent--;
|
|
|
|
next_indent--;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
|
2010-12-02 23:42:24 +08:00
|
|
|
if( do_indent && flags && prev_type != TOK_PIPE )
|
2007-04-22 18:03:12 +08:00
|
|
|
{
|
|
|
|
insert_tabs( out, indent );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-23 04:00:02 +08:00
|
|
|
append_format(out, L"%ls", last );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
indent = next_indent;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-01 13:05:10 +08:00
|
|
|
if ( prev_type != TOK_REDIRECT_FD )
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L" " );
|
|
|
|
out.append( last );
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
case TOK_END:
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
if( prev_type != TOK_END || prev_prev_type != TOK_END )
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L"\n" );
|
2007-04-22 18:03:12 +08:00
|
|
|
do_indent = 1;
|
|
|
|
is_command = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TOK_PIPE:
|
|
|
|
{
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L" " );
|
2010-12-01 13:05:10 +08:00
|
|
|
if ( last[0] == '2' && !last[1] ) {
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L"^" );
|
2010-12-01 13:05:10 +08:00
|
|
|
} else if ( last[0] != '1' || last[1] ) {
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( last);
|
|
|
|
out.append( L">" );
|
2010-12-01 13:05:10 +08:00
|
|
|
}
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L" | " );
|
2007-04-22 18:03:12 +08:00
|
|
|
is_command = 1;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
case TOK_REDIRECT_OUT:
|
|
|
|
{
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L" " );
|
2010-12-01 13:05:10 +08:00
|
|
|
if ( wcscmp( last, L"2" ) == 0 ) {
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L"^" );
|
2010-12-01 13:05:10 +08:00
|
|
|
} else {
|
|
|
|
if ( wcscmp( last, L"1" ) != 0 )
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( last );
|
|
|
|
out.append( L"> " );
|
2010-12-01 13:05:10 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
2010-12-01 13:05:10 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
case TOK_REDIRECT_APPEND:
|
2010-12-01 13:05:10 +08:00
|
|
|
{
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L" " );
|
2010-12-01 13:05:10 +08:00
|
|
|
if ( wcscmp( last, L"2" ) == 0 ) {
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L"^^" );
|
2010-12-01 13:05:10 +08:00
|
|
|
} else {
|
|
|
|
if ( wcscmp( last, L"1" ) != 0 )
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( last );
|
|
|
|
out.append( L">> " );
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
case TOK_REDIRECT_IN:
|
2010-12-01 13:05:10 +08:00
|
|
|
{
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L" " );
|
2010-12-01 13:05:10 +08:00
|
|
|
if ( wcscmp( last, L"0" ) != 0 )
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( last );
|
|
|
|
out.append( L"< " );
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
2010-12-01 13:05:10 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
case TOK_REDIRECT_FD:
|
2010-12-01 13:05:10 +08:00
|
|
|
{
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L" " );
|
2010-12-01 13:05:10 +08:00
|
|
|
if ( wcscmp( last, L"1" ) != 0 )
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( last );
|
|
|
|
out.append( L">& " );
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-04-22 18:03:12 +08:00
|
|
|
|
|
|
|
case TOK_BACKGROUND:
|
|
|
|
{
|
2012-02-23 04:00:02 +08:00
|
|
|
out.append( L"&\n" );
|
2007-04-22 18:03:12 +08:00
|
|
|
do_indent = 1;
|
|
|
|
is_command = 1;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
case TOK_COMMENT:
|
|
|
|
{
|
2007-04-23 02:54:29 +08:00
|
|
|
if( do_indent && flags)
|
|
|
|
{
|
|
|
|
insert_tabs( out, indent );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-23 04:00:02 +08:00
|
|
|
append_format( out, L"%ls", last );
|
2007-04-22 18:03:12 +08:00
|
|
|
do_indent = 1;
|
2011-12-27 11:18:46 +08:00
|
|
|
break;
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
default:
|
|
|
|
{
|
2007-09-21 22:07:52 +08:00
|
|
|
debug( 0, L"Unknown token '%ls'", last );
|
2007-04-22 18:03:12 +08:00
|
|
|
exit(1);
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-23 02:54:29 +08:00
|
|
|
prev_prev_type = prev_type;
|
|
|
|
prev_type = type;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
tok_destroy( &tok );
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-01-14 00:47:47 +08:00
|
|
|
/**
|
|
|
|
Remove any prefix and suffix newlines from the specified
|
2012-02-23 04:00:02 +08:00
|
|
|
string.
|
2008-01-14 00:47:47 +08:00
|
|
|
*/
|
2012-02-23 04:00:02 +08:00
|
|
|
static void trim( wcstring &str )
|
2007-04-23 02:54:29 +08:00
|
|
|
{
|
2012-02-23 04:00:02 +08:00
|
|
|
if (str.empty())
|
|
|
|
return;
|
2007-04-23 02:54:29 +08:00
|
|
|
|
2012-02-23 04:00:02 +08: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);
|
2007-04-23 02:54:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-14 00:47:47 +08:00
|
|
|
/**
|
|
|
|
The main mathod. Run the program.
|
|
|
|
*/
|
2007-04-22 18:03:12 +08:00
|
|
|
int main( int argc, char **argv )
|
2012-02-23 04:00:02 +08:00
|
|
|
{
|
2007-04-22 18:03:12 +08:00
|
|
|
int do_indent=1;
|
2012-03-08 03:35:22 +08:00
|
|
|
set_main_thread();
|
|
|
|
setup_fork_guards();
|
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
wsetlocale( LC_ALL, L"" );
|
|
|
|
program_name=L"fish_indent";
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
static struct option
|
|
|
|
long_options[] =
|
|
|
|
{
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
"no-indent", no_argument, 0, 'i'
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
"help", no_argument, 0, 'h'
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
"version", no_argument, 0, 'v'
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
|
|
|
,
|
2011-12-27 11:18:46 +08:00
|
|
|
{
|
|
|
|
0, 0, 0, 0
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
int opt_index = 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
int opt = getopt_long( argc,
|
2011-12-27 11:18:46 +08:00
|
|
|
argv,
|
2007-09-21 22:07:52 +08:00
|
|
|
GETOPT_STRING,
|
2011-12-27 11:18:46 +08:00
|
|
|
long_options,
|
2007-09-21 22:07:52 +08:00
|
|
|
&opt_index );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
if( opt == -1 )
|
|
|
|
break;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
switch( opt )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
case 'h':
|
|
|
|
{
|
|
|
|
print_help( "fish_indent", 1 );
|
2011-12-27 11:18:46 +08:00
|
|
|
exit( 0 );
|
2007-04-22 18:03:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
case 'v':
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
fwprintf( stderr,
|
|
|
|
_(L"%ls, version %s\n"),
|
2007-04-22 18:03:12 +08:00
|
|
|
program_name,
|
|
|
|
PACKAGE_VERSION );
|
2011-12-27 11:18:46 +08:00
|
|
|
exit( 0 );
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
{
|
|
|
|
do_indent = 0;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
case '?':
|
|
|
|
{
|
|
|
|
exit( 1 );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
}
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
|
|
|
|
2012-02-23 04:00:02 +08:00
|
|
|
wcstring sb_in, sb_out;
|
|
|
|
read_file( stdin, sb_in );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
wutil_init();
|
|
|
|
|
2012-02-23 04:00:02 +08:00
|
|
|
if( !indent( sb_out, sb_in, do_indent ) )
|
2007-04-22 18:03:12 +08:00
|
|
|
{
|
2012-02-23 04:00:02 +08:00
|
|
|
trim(sb_out);
|
|
|
|
fwprintf( stdout, L"%ls", sb_out.c_str() );
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Indenting failed - print original input
|
|
|
|
*/
|
2012-02-23 04:00:02 +08:00
|
|
|
fwprintf( stdout, L"%ls", sb_in.c_str() );
|
2007-04-22 18:03:12 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-04-22 18:03:12 +08:00
|
|
|
|
|
|
|
wutil_destroy();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|