Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

top(1): support command name and argument grepping #479

Closed
wants to merge 1 commit into from

Conversation

jgrafton
Copy link
Contributor

This feature is similar to the grep functionality in OpenBSD's top command.

@bsdimp
Copy link
Member

bsdimp commented Jun 18, 2021

This passes my initial traige, and also looks cool :)

@jgrafton
Copy link
Contributor Author

@allanjude mentioned this feature on BSD Now and I thought it sounded like a great challenge to tackle ;)

new_message(MT_standout,
"Grep command name: ");
if (readline(tempbuf1, sizeof(tempbuf1), false) > 0)
{
Copy link
Member

@bsdimp bsdimp Jun 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This { needs to be at the end of the prior line.

Also, if I do /getty I see just the getty routines. However, if I do / next, it doesn't reset the display to all processes because readline is return -1 in this case... This seems wrong because the cmd_matches code seems to want to handle this case. I think we should set ps.command back to NULL after freeing it..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And there's a resource leak, both of these can be fixed by free'ing ps.command and setting it to NULL before the readline...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resetting the display to all processes is handled by /+ That's the behavior in OpenBSD. I like it because it allows you to back out of a search.

The missing free is fixed. A const qualifier was added to the process_select struct in fc36f5a though I'm unsure why.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The const can be removed, I think.

@bsdimp
Copy link
Member

bsdimp commented Jun 22, 2021

So this needs an update to proceed.

@bsdimp
Copy link
Member

bsdimp commented Jun 22, 2021

Also, the cmd_match appears to be lifted 100% from OpenBSD w/o credit in the commit message.

ps.command = NULL;
else if ((ps.command = strdup(tempbuf1)) == NULL)
quit(1);
clear_message();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this clear_message() needs to be outside the if, because we need to do it either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, good catch

@bsdimp
Copy link
Member

bsdimp commented Jun 23, 2021

I'm still thinking about the const char *command -> char *command change... Otherwise I think this may be good. The code snagged from OpenBSD doesn't exactly match our current preferred style, but I think it's OK as it's in the style top was written in...

"Grep command name: ");
if (readline(tempbuf1, sizeof(tempbuf1), false) > 0) {
if (tempbuf1[0] == '+' && tempbuf1[1] == '\0') {
free(ps.command);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This free needs to be moved up to before the if since we need to do this in both cases.
Otherwise we leak the previous ps.command

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, you're right. It needs to be freed regardless if display is reset or a new value is entered. Thanks for the quick review, Warner!

@bsdimp
Copy link
Member

bsdimp commented Jun 29, 2021

This looks good at first blush. I'll take a look at it (in that I'll import it into a trial branch and compile it, test it, etc)

@bsdimp
Copy link
Member

bsdimp commented Jun 29, 2021

Landed as a00d703

@bsdimp bsdimp closed this Jun 29, 2021
freebsd-git pushed a commit that referenced this pull request Jun 29, 2021
Obtained from:  OpenBSD
Reviewed by:	imp@
Pull Request:	#479
netgate-git-updates pushed a commit to pfsense/FreeBSD-src that referenced this pull request Sep 13, 2021
Obtained from:  OpenBSD
Reviewed by:	imp@
Pull Request:	freebsd/freebsd-src#479

(cherry picked from commit a00d703)
freebsd-git pushed a commit that referenced this pull request Sep 13, 2021
Obtained from:  OpenBSD
Reviewed by:	imp@
Pull Request:	#479

(cherry picked from commit a00d703)
bsdjhb pushed a commit to bsdjhb/cheribsd that referenced this pull request Dec 14, 2021
Obtained from:  OpenBSD
Reviewed by:	imp@
Pull Request:	freebsd/freebsd-src#479
@emaste emaste added the merged label Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants