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