xref: /haiku/src/bin/roster.cpp (revision fef6144999c2fa611f59ee6ffe6dd7999501385c)
1 /*
2  * Copyright 2002-2005 Haiku Inc.
3  * Distributed under the terms of the MIT license
4  *
5  * Authors:
6  *		Mathew Hounsell
7  *		Axel Dörfler, axeld@pinc-software.de.
8  */
9 
10 
11 #include <Roster.h>
12 #include <Path.h>
13 #include <Entry.h>
14 #include <List.h>
15 #include <String.h>
16 
17 #include <stdio.h>
18 #include <getopt.h>
19 
20 
21 static struct option const kLongOptions[] = {
22 	{"name", no_argument, 0, 'n'},
23 	{"no-trunc", no_argument, 0, 't'},
24 	{"help", no_argument, 0, 'h'},
25 	{NULL}
26 };
27 
28 extern const char *__progname;
29 static const char *kProgramName = __progname;
30 
31 static const int32 kNameFieldWidth = 34;
32 
33 // view modes
34 static const int32 kStandardMode	= 0x0;
35 static const int32 kNameOnlyMode	= 0x1;
36 static const int32 kNoTruncateMode	= 0x2;
37 
38 
39 void
40 truncate_string(BString &name, int32 length)
41 {
42 	if (name.Length() <= length)
43 		return;
44 	if (length < 6)
45 		length = 6;
46 
47 	int32 beginLength = length / 3 - 1;
48 	int32 endLength = length - 3 - beginLength;
49 
50 	BString begin, end;
51 	name.CopyInto(begin, 0, beginLength);
52 	name.CopyInto(end, name.Length() - endLength, endLength);
53 
54 	name = begin;
55 	name.Append("...").Append(end);
56 }
57 
58 
59 void
60 output_team(team_id id, int32 mode)
61 {
62 	// Get info on team
63 	app_info info;
64 	if (be_roster->GetRunningAppInfo(id, &info) != B_OK)
65 		return;
66 
67 	// Allocate a entry and get it's path.
68 	// - works as they are independant (?)
69 	BEntry entry(&info.ref);
70 	BPath path(&entry);
71 
72 	BString name;
73 	if (mode & kNameOnlyMode)
74 		name = info.ref.name;
75 	else
76 		name = path.Path();
77 
78 	if ((mode & kNoTruncateMode) == 0)
79 		truncate_string(name, kNameFieldWidth);
80 
81 	printf("%6ld %*s %5ld %5lx (%s)\n",
82 		id, (int)kNameFieldWidth, name.String(),
83 		info.port, info.flags, info.signature);
84 }
85 
86 
87 void
88 usage(int exitCode)
89 {
90 	fprintf(stderr, "usage: %s [-nt]\n"
91 		"  -n, --name\t\tInstead of the full path, only the name of the teams are written\n"
92 		"  -t, --no-trunc\tDon't truncate the path name\n"
93 		"  -h, --help\t\tDisplay this help and exit\n",
94 		kProgramName);
95 
96 	exit(exitCode);
97 }
98 
99 
100 int
101 main(int argc, char **argv)
102 {
103 	int32 mode = kStandardMode;
104 
105 	// Don't have an BApplication as it is not needed for what we do
106 
107 	int c;
108 	while ((c = getopt_long(argc, argv, "nth", kLongOptions, NULL)) != -1) {
109 		switch (c) {
110 			case 'n':
111 				mode |= kNameOnlyMode;
112 				break;
113 			case 't':
114 				mode |= kNoTruncateMode;
115 				break;
116 			case 0:
117 				break;
118 			case 'h':
119 				usage(0);
120 				break;
121 			default:
122 				usage(1);
123 				break;
124 		}
125 	}
126 
127 	// print title line
128 
129 	printf("  team %*s  port flags signature\n",
130 		(int)kNameFieldWidth, mode & kNameOnlyMode ? "name" : "path");
131 
132 	printf("------ ");
133 	for (int32 i = 0; i < kNameFieldWidth; i++)
134 		putchar('-');
135 	puts(" ----- ----- ---------");
136 
137 	// Retrieve the running list.
138 	BList applicationList;
139 	be_roster->GetAppList(&applicationList);
140 
141 	// Iterate through the list.
142 	for (int32 i = 0; i < applicationList.CountItems(); i++)
143 		output_team((team_id)applicationList.ItemAt(i), mode);
144 
145 	return 0;
146 }
147