diff --git a/src/apps/bin/Jamfile b/src/apps/bin/Jamfile index a5e5b5a545..d79ab9d6f6 100644 --- a/src/apps/bin/Jamfile +++ b/src/apps/bin/Jamfile @@ -64,6 +64,7 @@ StdBinCommands play.cpp query.cpp quit.cpp + roster.cpp setversion.cpp shutdown.cpp version.cpp @@ -83,12 +84,6 @@ StdBinCommands : be libmedia.so ; -# standard commands that need libbe.so, libstdc++.r4.so -StdBinCommands - roster.cpp - : be stdc++.r4 - ; - # standard commands that need libbe.so, libmail.so StdBinCommands mail.cpp diff --git a/src/apps/bin/roster.cpp b/src/apps/bin/roster.cpp index a4b000610b..49c2eddc88 100644 --- a/src/apps/bin/roster.cpp +++ b/src/apps/bin/roster.cpp @@ -1,87 +1,145 @@ -/********************************************************************* -** Roster Applet -********************************************************************** -** 2002-04-30 Mathew Hounsell Created. -*/ +/* + * Copyright 2002-2005 Haiku Inc. + * Distributed under the terms of the MIT license + * + * Authors: + * Mathew Hounsell + * Axel Dörfler, axeld@pinc-software.de. + */ + #include - #include #include - #include +#include -#include -#include +#include +#include -#include -#include -#include -/********************************************************************* -*/ -bool OutputTeam( void * item_p ) +static struct option const kLongOptions[] = { + {"name", no_argument, 0, 'n'}, + {"no-trunc", no_argument, 0, 't'}, + {"help", no_argument, 0, 'h'}, + {NULL} +}; + +extern const char *__progname; +static const char *kProgramName = __progname; + +static const int32 kNameFieldWidth = 34; + +// view modes +static const int32 kStandardMode = 0x0; +static const int32 kNameOnlyMode = 0x1; +static const int32 kNoTruncateMode = 0x2; + + +void +truncate_string(BString &name, int32 length) { - static uint32 i = 0u; // counter - - app_info info; - - // Roster stores team_id not team_id * - team_id id = reinterpret_cast( item_p ); - - // Get info on team - if( be_roster->GetRunningAppInfo( id, &info ) == B_OK ) { - - // Allocate a entry and get it's path. - // - works as they are independant (?) - BEntry entry( & info.ref ); - BPath path( & entry ); - - // Output - leave the work to ostream - std::cout - << setw(2) << i << ' ' -// short << setw(32) << info.ref.name << ' ' - << setw(32) << path.Path() << ' ' - << setw(6) << info.thread << ' ' - << setw(4) << info.team << ' ' - << setw(4) << info.port << ' ' - << setw(4) - ; - // Go hex, output, the go dec - hex( std::cout ); - std::cout << info.flags; - dec( std::cout ); + if (name.Length() <= length) + return; + if (length < 6) + length = 6; - // Output a signature - std::cout - << " (" << info.signature << ')' - << std::endl - ; - - /* NOW */ ++i; - } - return false; + int32 beginLength = length / 3 - 1; + int32 endLength = length - 3 - beginLength; + + BString begin, end; + name.CopyInto(begin, 0, beginLength); + name.CopyInto(end, name.Length() - endLength, endLength); + + name = begin; + name.Append("...").Append(end); } - -/********************************************************************* -*/ -int main( int argc, char ** argv ) + + +void +output_team(team_id id, int32 mode) { - // Don't have an BApplication as it is a waste. - - cout - << " path thread team port flags\n" - "-- -------------------------------- ------ ---- ---- -----" - << std::endl - ; + // Get info on team + app_info info; + if (be_roster->GetRunningAppInfo(id, &info) != B_OK) + return; + + // Allocate a entry and get it's path. + // - works as they are independant (?) + BEntry entry(&info.ref); + BPath path(&entry); + + BString name; + if (mode & kNameOnlyMode) + name = info.ref.name; + else + name = path.Path(); + + if ((mode & kNoTruncateMode) == 0) + truncate_string(name, kNameFieldWidth); + + printf("%6ld %*s %5ld %5lx (%s)\n", + id, (int)kNameFieldWidth, name.String(), + info.port, info.flags, info.signature); +} + + +void +usage(int exitCode) +{ + fprintf(stderr, "usage: %s [-nt]\n" + " -n, --name\t\tInstead of the full path, only the name of the teams are written\n" + " -t, --no-trunc\tDon't truncate the path name\n", + kProgramName); + + exit(exitCode); +} + + +int +main(int argc, char **argv) +{ + int32 mode = kStandardMode; + + // Don't have an BApplication as it is not needed for what we do + + int c; + while ((c = getopt_long(argc, argv, "nt", kLongOptions, NULL)) != -1) { + switch (c) { + case 'n': + mode |= kNameOnlyMode; + break; + case 't': + mode |= kNoTruncateMode; + break; + case 0: + break; + case 'h': + usage(0); + break; + default: + usage(1); + break; + } + } + + // print title line + + printf(" team %*s port flags signature\n", + (int)kNameFieldWidth, mode & kNameOnlyMode ? "name" : "path"); + + printf("------ "); + for (int32 i = 0; i < kNameFieldWidth; i++) + putchar('-'); + puts(" ----- ----- ---------"); // Retrieve the running list. - BList applist( 0 ); - be_roster->GetAppList( & applist ); + BList applicationList; + be_roster->GetAppList(&applicationList); // Iterate through the list. - // void DoForEach(bool (*func)(void *) ) - applist.DoForEach( OutputTeam ); - + for (int32 i = 0; i < applicationList.CountItems(); i++) + output_team((team_id)applicationList.ItemAt(i), mode); + return 0; }