Add command-not-found utility.

This commit is contained in:
Jonas 'Sortie' Termansen 2013-06-09 13:44:34 +02:00
parent 3de23d8cec
commit b5509037bc
4 changed files with 64 additions and 2 deletions

1
utils/.gitignore vendored
View File

@ -6,6 +6,7 @@ chvideomode
clear
colormake
column
command-not-found
cp
date
du

View File

@ -22,6 +22,7 @@ chvideomode \
clear \
colormake \
column \
command-not-found \
cp \
date \
du \

View File

@ -0,0 +1,54 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
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, see <http://www.gnu.org/licenses/>.
command-not-found.cpp
Prints a notice that the attempted command wasn't found and possibly
suggests how to install the command or suggests the proper spelling in case
of a typo.
*******************************************************************************/
#include <stdio.h>
#include <string.h>
void suggest_editor(const char* filename)
{
fprintf(stderr, "No command '%s' found, did you mean:\n", filename);
fprintf(stderr, " Command 'editor' from package 'utils'\n");
}
void suggest_pager(const char* filename)
{
fprintf(stderr, "No command '%s' found, did you mean:\n", filename);
fprintf(stderr, " Command 'pager' from package 'utils'\n");
}
int main(int argc, char* argv[])
{
const char* filename = 2 <= argc ? argv[1] : argv[0];
if ( !strcmp(filename, "ed") ||
!strcmp(filename, "emacs") ||
!strcmp(filename, "nano") ||
!strcmp(filename, "vi") ||
!strcmp(filename, "vim") )
suggest_editor(filename);
else if ( !strcmp(filename, "less") ||
!strcmp(filename, "more") )
suggest_pager(filename);
fprintf(stderr, "%s: command not found\n", filename);
return 127;
}

View File

@ -68,7 +68,7 @@ void updateenv()
}
}
int runcommandline(const char** tokens, bool* exitexec)
int runcommandline(const char** tokens, bool* exitexec, bool interactive)
{
int result = 127;
size_t cmdnext = 0;
@ -273,6 +273,12 @@ readcmd:
}
execvp(argv[0], argv);
if ( interactive )
{
int errno_saved = errno;
execlp("command-not-found", "command-not-found", argv[0], NULL);
errno = errno_saved;
}
error(127, errno, "%s", argv[0]);
return 127;
@ -454,7 +460,7 @@ int get_and_run_command(FILE* fp, const char* fpname, bool interactive,
return status;
argv[argc] = NULL;
status = runcommandline(argv, exitexec);
status = runcommandline(argv, exitexec, interactive);
if ( status && exit_on_error )
*exitexec = true;
return status;