Code cleanup

darcs-hash:20051215135902-ac50b-e9fc18bca34459ddb56e970a16c05e9b3cc54848.gz
This commit is contained in:
axel 2005-12-15 23:59:02 +10:00
parent e4a95cb989
commit fa75fc3901
5 changed files with 176 additions and 126 deletions

215
builtin.c
View file

@ -72,30 +72,37 @@
*/ */
#define READ_MODE_NAME L"fish_read" #define READ_MODE_NAME L"fish_read"
/**
The send stuff to foreground message
*/
#define FG_MSG L"Send job %d, '%ls' to foreground\n"
/**
Print modes for the jobs builtin
*/
enum
{
JOBS_DEFAULT, /**< Print lots of general info */
JOBS_PRINT_PID, /**< Print pid of each process in job */
JOBS_PRINT_COMMAND, /**< Print command name of each process in job */
JOBS_PRINT_GROUP, /**< Print group id of job */
}
;
/** /**
Table of all builtins Table of all builtins
*/ */
enum
{
JOBS_DEFAULT,
JOBS_PRINT_PID,
JOBS_PRINT_COMMAND,
JOBS_PRINT_GROUP,
}
;
static hash_table_t builtin; static hash_table_t builtin;
int builtin_out_redirect; int builtin_out_redirect;
int builtin_err_redirect; int builtin_err_redirect;
/** /*
Buffers for storing the output of builtin functions Buffers for storing the output of builtin functions
*/ */
string_buffer_t *sb_out=0, *sb_err=0; string_buffer_t *sb_out=0, *sb_err=0;
/** /**
Stack containing builtin I/O for recursive builtin calls. Stack containing builtin I/O for recursive builtin calls.
*/ */
@ -422,8 +429,6 @@ static int builtin_block( wchar_t **argv )
} }
/** /**
The builtin builtin, used for given builtins precedence over functions. Mostly handled by the parser. All this code does is some additional operational modes, such as printing a list of all builtins. The builtin builtin, used for given builtins precedence over functions. Mostly handled by the parser. All this code does is some additional operational modes, such as printing a list of all builtins.
*/ */
@ -530,7 +535,6 @@ static int builtin_builtin( wchar_t **argv )
only a placeholder that prints the help message. Useful for only a placeholder that prints the help message. Useful for
commands that live in hte parser. commands that live in hte parser.
*/ */
static int builtin_generic( wchar_t **argv ) static int builtin_generic( wchar_t **argv )
{ {
int argc=builtin_count_args( argv ); int argc=builtin_count_args( argv );
@ -594,7 +598,6 @@ static int builtin_generic( wchar_t **argv )
/** /**
The exec bultin. This is only a placeholder that prints the help message. Ther actual implementation lives in exec.c. The exec bultin. This is only a placeholder that prints the help message. Ther actual implementation lives in exec.c.
*/ */
static int builtin_exec( wchar_t **argv ) static int builtin_exec( wchar_t **argv )
{ {
int argc=builtin_count_args( argv ); int argc=builtin_count_args( argv );
@ -784,9 +787,9 @@ static int builtin_functions( wchar_t **argv )
if( argc-woptind != 1 ) if( argc-woptind != 1 )
{ {
sb_append2( sb_err, sb_printf( sb_err,
L"functions: Expected exactly one function name\n", L"%ls: Expected exactly one function name\n",
(void *)0); argv[0] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
@ -794,11 +797,11 @@ static int builtin_functions( wchar_t **argv )
func = argv[woptind]; func = argv[woptind];
if( !function_exists( func ) ) if( !function_exists( func ) )
{ {
sb_append2( sb_err, sb_printf( sb_err,
L"functions: Function ", L"%ls: Function '%ls' does not exist\n",
func, argv[0],
L" does not exist\n", func );
(void *)0);
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
@ -1158,24 +1161,21 @@ static int builtin_function( wchar_t **argv )
} }
else if( !(is_binding?wcsbindingname( argv[woptind] ) : wcsvarname( argv[woptind] ) )) else if( !(is_binding?wcsbindingname( argv[woptind] ) : wcsvarname( argv[woptind] ) ))
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: illegal function name '%ls'\n",
L": illegal function name \'", argv[0],
argv[woptind], argv[woptind] );
L"\'\n",
(void *)0 );
res=1; res=1;
} }
else if( parser_is_reserved(argv[woptind] ) ) else if( parser_is_reserved(argv[woptind] ) )
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: the name '%ls' is reserved,\nand can not be used as a function name\n",
L": the name \'", argv[0],
argv[woptind], argv[woptind] );
L"\' is reserved,\nand can not be used as a function name\n",
(void *)0 );
res=1; res=1;
} }
} }
@ -1248,7 +1248,6 @@ static int builtin_function( wchar_t **argv )
/** /**
The random builtin. For generating random numbers. The random builtin. For generating random numbers.
*/ */
static int builtin_random( wchar_t **argv ) static int builtin_random( wchar_t **argv )
{ {
static int seeded=0; static int seeded=0;
@ -1333,9 +1332,10 @@ static int builtin_random( wchar_t **argv )
foo = wcstol( argv[woptind], &end, 10 ); foo = wcstol( argv[woptind], &end, 10 );
if( errno || *end ) if( errno || *end )
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: Seed value '%ls' is not a valid number\n",
L": Seed value '" , argv[woptind], L"' is not a valid number\n", (void *)0); argv[0],
argv[woptind] );
return 1; return 1;
} }
@ -1348,6 +1348,7 @@ static int builtin_random( wchar_t **argv )
{ {
sb_printf( sb_err, sb_printf( sb_err,
L"%ls: Expected zero or one argument, got %d\n", L"%ls: Expected zero or one argument, got %d\n",
argv[0],
argc-woptind ); argc-woptind );
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
@ -1788,7 +1789,10 @@ static int builtin_exit( wchar_t **argv )
ec = wcstol(argv[1],&end,10); ec = wcstol(argv[1],&end,10);
if( errno || *end != 0) if( errno || *end != 0)
{ {
sb_append2( sb_err, argv[0], L": Argument must be an integer '", argv[1], L"'\n", (void *)0 ); sb_printf( sb_err,
L"%ls: Argument must be an integer '%ls'\n",
argv[0],
argv[1] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }
@ -1796,7 +1800,10 @@ static int builtin_exit( wchar_t **argv )
} }
default: default:
sb_append2( sb_err, argv[0], L": Too many arguments\n", (void *)0 ); sb_printf( sb_err,
L"%ls: Too many arguments\n",
argv[0] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
@ -2295,14 +2302,19 @@ static int builtin_fg( wchar_t **argv )
if( builtin_err_redirect ) if( builtin_err_redirect )
{ {
sb_printf( sb_err, sb_printf( sb_err,
L"Send job %d, '%ls' to foreground\n", FG_MSG,
j->job_id, j->job_id,
j->command ); j->command );
} }
else else
{ {
/*
If we aren't redirecting, send output to real stderr,
since stuff in sb_err won't get printed until the
command finishes.
*/
fwprintf( stderr, fwprintf( stderr,
L"Send job %d, '%ls' to foreground\n", FG_MSG,
j->job_id, j->job_id,
j->command ); j->command );
} }
@ -2313,11 +2325,7 @@ static int builtin_fg( wchar_t **argv )
env_set( L"_", ft, ENV_EXPORT ); env_set( L"_", ft, ENV_EXPORT );
free(ft); free(ft);
reader_write_title(); reader_write_title();
/*
fwprintf( stderr, L"Send job %d, \'%ls\' to foreground\n",
j->job_id,
j->command );
*/
make_first( j ); make_first( j );
j->fg=1; j->fg=1;
@ -2539,12 +2547,11 @@ static int builtin_jobs( wchar_t **argv )
case 0: case 0:
if(long_options[opt_index].flag != 0) if(long_options[opt_index].flag != 0)
break; break;
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: Unknown option '%ls'\n",
L": Unknown option ", argv[0],
long_options[opt_index].name, long_options[opt_index].name );
L"\n",
(void *)0 );
sb_append( sb_err, sb_append( sb_err,
parser_current_line() ); parser_current_line() );
// builtin_print_help( argv[0], sb_err ); // builtin_print_help( argv[0], sb_err );
@ -2620,7 +2627,10 @@ static int builtin_jobs( wchar_t **argv )
pid=wcstol( argv[i], &end, 10 ); pid=wcstol( argv[i], &end, 10 );
if( errno || *end ) if( errno || *end )
{ {
sb_append2( sb_err, argv[0], L": Not a process id: ", argv[i], L"\n", (void *)0 ); sb_printf( sb_err,
L"%ls: Not a process id: '%ls'\n",
argv[0],
argv[i] );
return 1; return 1;
} }
@ -2632,7 +2642,10 @@ static int builtin_jobs( wchar_t **argv )
} }
else else
{ {
sb_printf( sb_err, L"%ls: No suitable job: %d\n", argv[0], pid ); sb_printf( sb_err,
L"%ls: No suitable job: %d\n",
argv[0],
pid );
return 1; return 1;
} }
} }
@ -2655,7 +2668,9 @@ static int builtin_jobs( wchar_t **argv )
if( !found ) if( !found )
{ {
sb_append2( sb_out, argv[0], L": There are no running jobs\n", (void *)0 ); sb_printf( sb_out,
L"%ls: There are no running jobs\n",
argv[0] );
} }
return 0; return 0;
@ -2672,28 +2687,24 @@ static int builtin_for( wchar_t **argv )
if( argc < 3) if( argc < 3)
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: Expected at least two arguments\n",
L": Expected at least two arguments\n", argv[0] );
(void *)0);
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
} }
else if ( !wcsvarname(argv[1]) ) else if ( !wcsvarname(argv[1]) )
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: '%ls' invalid variable name\n",
L": \'", argv[0],
argv[1], argv[1] );
L"\' invalid variable name\n",
(void *)0);
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
} }
else if (wcscmp( argv[2], L"in") != 0 ) else if (wcscmp( argv[2], L"in") != 0 )
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: Second argument must be 'in'\n",
L": Second argument must be \'in\'\n", argv[0] );
(void *)0);
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
} }
else else
@ -2753,10 +2764,10 @@ static int builtin_end( wchar_t **argv )
current_block->type == OR || current_block->type == OR ||
current_block->type == AND ) current_block->type == AND )
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: Not inside of block\n",
L": Not inside of block\n", argv[0] );
(void *)0);
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }
@ -2877,10 +2888,9 @@ static int builtin_else( wchar_t **argv )
current_block->type != IF || current_block->type != IF ||
current_block->param1.if_state != 1) current_block->param1.if_state != 1)
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: not inside of if block\n",
L": not inside of if block\n", argv[0] );
(void *)0);
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }
@ -2911,9 +2921,10 @@ static int builtin_break_continue( wchar_t **argv )
if( argc != 1 ) if( argc != 1 )
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: Unknown option '%ls'\n",
L": Unknown option \'", argv[1], L"\'", (void *)0 ); argv[0],
argv[1] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }
@ -2928,9 +2939,9 @@ static int builtin_break_continue( wchar_t **argv )
if( b == 0 ) if( b == 0 )
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: Not inside of loop\n",
L": Not inside of loop\n", (void *)0 ); argv[0] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }
@ -2968,12 +2979,10 @@ static int builtin_return( wchar_t **argv )
status = wcstol(argv[1],&end,10); status = wcstol(argv[1],&end,10);
if( errno || *end != 0) if( errno || *end != 0)
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: Argument must be an integer '%ls'\n",
L": Argument must be an integer '", argv[0],
argv[1], argv[1] );
L"'\n",
(void *)0 );
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }
@ -2981,9 +2990,9 @@ static int builtin_return( wchar_t **argv )
break; break;
} }
default: default:
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: Too many arguments\n",
L": Too many arguments\n", (void *)0 ); argv[0] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }
@ -2997,9 +3006,9 @@ static int builtin_return( wchar_t **argv )
if( b == 0 ) if( b == 0 )
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: Not inside of function\n",
L": Not inside of function\n", (void *)0 ); argv[0] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }
@ -3057,10 +3066,9 @@ static int builtin_case( wchar_t **argv )
if( current_block->type != SWITCH ) if( current_block->type != SWITCH )
{ {
sb_append2( sb_err, sb_printf( sb_err,
argv[0], L"%ls: syntax error, case command while not in switch block\n",
L": syntax error, case command while not in switch block\n", argv[0] );
(void *)0);
builtin_print_help( L"case", sb_err ); builtin_print_help( L"case", sb_err );
return 1; return 1;
} }
@ -3094,6 +3102,7 @@ static int builtin_case( wchar_t **argv )
END OF BUILTIN COMMANDS END OF BUILTIN COMMANDS
Below are functions for handling the builtin commands Below are functions for handling the builtin commands
*/ */
void builtin_init() void builtin_init()
{ {
al_init( &io_stack ); al_init( &io_stack );

View file

@ -28,10 +28,10 @@ Functions used for implementing the commandline builtin.
*/ */
enum enum
{ {
STRING_MODE=1, // Operate on entire buffer STRING_MODE=1, /**< Operate on entire buffer */
JOB_MODE, // Operate on job under cursor JOB_MODE, /**< Operate on job under cursor */
PROCESS_MODE, // Operate on process under cursor PROCESS_MODE, /**< Operate on process under cursor */
TOKEN_MODE // Operate on token under cursor TOKEN_MODE /**< Operate on token under cursor */
} }
; ;
@ -40,9 +40,9 @@ enum
*/ */
enum enum
{ {
REPLACE_MODE=1, // Replace current text REPLACE_MODE=1, /**< Replace current text */
INSERT_MODE, // Insert at cursor position INSERT_MODE, /**< Insert at cursor position */
APPEND_MODE // Insert at end of current token/command/buffer APPEND_MODE /**< Insert at end of current token/command/buffer */
} }
; ;
@ -66,10 +66,6 @@ static void replace_part( wchar_t *begin,
sb_init( &out ); sb_init( &out );
// wchar_t *tmp = wcsndup( begin, end-begin );
// fwprintf( stderr, L"Commandline '%ls', current command '%ls'\n", reader_get_buffer(), tmp );
sb_append_substring( &out, buff, begin-buff ); sb_append_substring( &out, buff, begin-buff );
switch( append_mode) switch( append_mode)

View file

@ -17,6 +17,9 @@
*/ */
#define EVENT_ANY_PID 0 #define EVENT_ANY_PID 0
/**
Enumeration of event types
*/
enum enum
{ {
EVENT_ANY, /**< Matches any event type (Not always any event, as the function name may limit the choice as well */ EVENT_ANY, /**< Matches any event type (Not always any event, as the function name may limit the choice as well */
@ -40,6 +43,10 @@ typedef struct
Type of event Type of event
*/ */
int type; int type;
/**
The type-specific parameter
*/
union union
{ {
/** /**

View file

@ -313,12 +313,12 @@ void parser_forbid_function( wchar_t *function );
void parser_allow_function(); void parser_allow_function();
/** /**
Initialize the parser Initialize static parser data
*/ */
void parser_init(); void parser_init();
/** /**
Destroy the parser Destroy static parser data
*/ */
void parser_destroy(); void parser_destroy();

58
proc.h
View file

@ -20,15 +20,30 @@
/** /**
Types of internal processes Types of processes
*/ */
enum enum
{ {
/**
A regular external command
*/
EXTERNAL, EXTERNAL,
/**
A builtin command
*/
INTERNAL_BUILTIN, INTERNAL_BUILTIN,
/**
A shellscript function
*/
INTERNAL_FUNCTION, INTERNAL_FUNCTION,
/**
A block of commands
*/
INTERNAL_BLOCK, INTERNAL_BLOCK,
INTERNAL_EXEC /**
The exec builtin
*/
INTERNAL_EXEC,
} }
; ;
@ -142,19 +157,39 @@ typedef struct job
} }
job_t; job_t;
/** Whether we are running a subshell command */ /**
Whether we are running a subshell command
*/
extern int is_subshell; extern int is_subshell;
/** Whether we are running a block of commands */
/**
Whether we are running a block of commands
*/
extern int is_block; extern int is_block;
/** Whether we are reading from the keyboard right now*/
/**
Whether we are reading from the keyboard right now
*/
extern int is_interactive; extern int is_interactive;
/** Whether this shell is attached to the keyboard at all*/
/**
Whether this shell is attached to the keyboard at all
*/
extern int is_interactive_session; extern int is_interactive_session;
/** Whether we are a login shell*/
/**
Whether we are a login shell
*/
extern int is_login; extern int is_login;
/** Whether we are a event handler*/
/**
Whether we are a event handler
*/
extern int is_event; extern int is_event;
/** Linked list of all jobs */
/**
Linked list of all jobs
*/
extern job_t *first_job; extern job_t *first_job;
/** /**
@ -175,6 +210,7 @@ extern pid_t proc_last_bg_pid;
Sets the status of the last process to exit Sets the status of the last process to exit
*/ */
void proc_set_last_status( int s ); void proc_set_last_status( int s );
/** /**
Returns the status of the last process to exit Returns the status of the last process to exit
*/ */
@ -184,6 +220,7 @@ int proc_get_last_status();
Remove the specified job Remove the specified job
*/ */
void job_free( job_t* j ); void job_free( job_t* j );
/** /**
Create a new job Create a new job
*/ */
@ -219,6 +256,7 @@ int job_is_completed( const job_t *j );
\param cont Whether the function should wait for the job to complete before returning \param cont Whether the function should wait for the job to complete before returning
*/ */
void job_continue( job_t *j, int cont ); void job_continue( job_t *j, int cont );
/** /**
Notify user of nog events. Notify the user about stopped or Notify user of nog events. Notify the user about stopped or
terminated jobs. Delete terminated jobs from the active job list. terminated jobs. Delete terminated jobs from the active job list.
@ -226,10 +264,10 @@ void job_continue( job_t *j, int cont );
\param interactive whether interactive jobs should be reaped as well \param interactive whether interactive jobs should be reaped as well
*/ */
int job_reap( int interactive ); int job_reap( int interactive );
/** /**
Signal handler for SIGCHLD. Mark any processes with relevant Signal handler for SIGCHLD. Mark any processes with relevant
information. information.
*/ */
void job_handle_signal( int signal, siginfo_t *info, void *con ); void job_handle_signal( int signal, siginfo_t *info, void *con );