| 1 | // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ |
| 2 | // |
| 3 | // Copyright (c) 2001-2003, OpenBeOS |
| 4 | // Copyright (c) 2010, Haiku Inc. |
| 5 | // |
| 6 | // This software is part of the OpenBeOS distribution and is covered |
| 7 | // by the OpenBeOS license. |
| 8 | // |
| 9 | // |
| 10 | // File: listport.c (originally, changed to ListPort.cpp by Jorma |
| 11 | // Karvonen <karvonen.jorma @ gmail.com>) |
| 12 | // Author: Daniel Reinhold (danielre@users.sf.net) |
| 13 | // Description: lists all open ports in the system, organized by team |
| 14 | // |
| 15 | // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | |
| 20 | #include <Application.h> |
| 21 | #include <Catalog.h> |
| 22 | #include <Locale.h> |
| 23 | #include <OS.h> |
| 24 | |
| 25 | #undef B_TRANSLATE_CONTEXT |
| 26 | #define B_TRANSLATE_CONTEXT "ListPort" |
| 27 | |
| 28 | class ListPort : public BApplication |
| 29 | { |
| 30 | public: |
| 31 | ListPort(); |
| 32 | ListPort(int32 argc, char** argv); |
| 33 | ~ListPort(); |
| 34 | |
| 35 | void ArgvReceived(int32 argc, char** argv); |
| 36 | void MessageReceived(BMessage* message); |
| 37 | virtual void ReadyToRun(); |
| 38 | |
| 39 | private: |
| 40 | void List_team_ports(team_id id); |
| 41 | void Show_port_totals(void); |
| 42 | void AboutRequested(); |
| 43 | void HelpRequested(); |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | ListPort::ListPort() |
| 48 | : BApplication("application/x-vnd.Haiku-listport") |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | |
| 53 | ListPort::ListPort(int32 argc, char** argv) |
| 54 | : BApplication("application/x-vnd.Haiku-listport") |
| 55 | { |
| 56 | if (argc == 1) |
| 57 | ArgvReceived(argc, argv); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | ListPort::~ListPort() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void |
| 67 | ListPort::ReadyToRun() |
| 68 | { |
| 69 | Quit(); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void |
| 74 | ListPort::AboutRequested() |
| 75 | { |
| 76 | BString str = B_TRANSLATE("listport\n" |
| 77 | "\twritten by %1\n\n" |
| 78 | "\tThis software is part of the OpenBeOS distribution and is covered\n" |
| 79 | "\tby the OpenBeOS license.\n" |
| 80 | "\tConverted to BApplication by %2\n" |
| 81 | "\tCopyright (c) 2001-2003, OpenBeOS\n" |
| 82 | "\tCopyright %3, Haiku Inc.\n"); |
| 83 | str.ReplaceFirst("%1", "Daniel Reinhold (danielre@users.sf.net)"); |
| 84 | str.ReplaceFirst("%2", "Jorma Karvonen (karvonen.jorma @ gmail.com)"); |
| 85 | str.ReplaceFirst("%3", "2010"); |
| 86 | printf("%s\n", str.String()); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void |
| 91 | ListPort::HelpRequested() |
| 92 | { |
| 93 | BString str = B_TRANSLATE("Lists all open ports in the system, organized " |
| 94 | "by team\n" |
| 95 | "Usage: listport [team_id][--help]\n" |
| 96 | "Examples:\n" |
| 97 | "listport\n" |
| 98 | "\tprints all applications and their ports\n\n" |
| 99 | "listport NUMBER\n" |
| 100 | "\tprints information about ports of team NUMBER\n\n" |
| 101 | "listport --help\n" |
| 102 | "\tprints information about application\n" |
| 103 | "\tand this help text\n"); |
| 104 | printf("%s\n", str.String()); |
| 105 | Quit(); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void |
| 110 | ListPort::ArgvReceived(int32 argc, char** argv) |
| 111 | { |
| 112 | if (argc == 1) { |
| 113 | int32 cookie = 0; |
| 114 | team_info info; |
| 115 | |
| 116 | Show_port_totals(); |
| 117 | // list for all teams |
| 118 | while (get_next_team_info(&cookie, &info) >= B_OK) |
| 119 | List_team_ports(info.team); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | if (argc == 2) { |
| 124 | if (strcmp(argv[1], "--help") == 0) { |
| 125 | AboutRequested(); |
| 126 | HelpRequested(); |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | Show_port_totals(); |
| 131 | // list for each team_id on the command line |
| 132 | while (--argc) |
| 133 | List_team_ports(atoi(*++argv)); |
| 134 | Quit(); |
| 135 | } |
| 136 | printf("\n"); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | void |
| 141 | ListPort::MessageReceived(BMessage* message) |
| 142 | { |
| 143 | switch(message->what) |
| 144 | { |
| 145 | case B_ABOUT_REQUESTED: AboutRequested(); break; |
| 146 | case B_QUIT_REQUESTED: BApplication::QuitRequested(); break; |
| 147 | default: BApplication::MessageReceived(message); break; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void |
| 153 | ListPort::List_team_ports(team_id id) |
| 154 | { |
| 155 | int32 cookie = 0; |
| 156 | port_info this_port; |
| 157 | team_info this_team; |
| 158 | |
| 159 | if (get_team_info(id, &this_team) == B_BAD_TEAM_ID) { |
| 160 | printf(B_TRANSLATE("\nteam %ld unknown\n"), id); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | printf(B_TRANSLATE("\nTEAM %5ld (%s):\n"), id, this_team.args); |
| 165 | printf(B_TRANSLATE_COMMENT(" ID name " |
| 166 | "capacity queued\n", "A table header, try to keep the end of " |
| 167 | "single words in the same column.")); |
| 168 | printf("-----------------------------------------------------------\n"); |
| 169 | |
| 170 | while (get_next_port_info(id, &cookie, &this_port) == B_OK) { |
| 171 | printf("%10ld %30s %8ld %6ld\n", |
| 172 | this_port.port, |
| 173 | this_port.name, |
| 174 | this_port.capacity, |
| 175 | this_port.queue_count); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | void |
| 181 | ListPort::Show_port_totals(void) |
| 182 | { |
| 183 | int32 max = 0, used = 0, left; |
| 184 | system_info sys; |
| 185 | |
| 186 | if (get_system_info(&sys) == B_OK) { |
| 187 | max = sys.max_ports; |
| 188 | used = sys.used_ports; |
| 189 | } |
| 190 | |
| 191 | left = max - used; |
| 192 | |
| 193 | printf(B_TRANSLATE("port: total: %5ld, used: %5ld, left: %5ld\n"), |
| 194 | max, used, left); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | int |
| 199 | main(int argc, char **argv) |
| 200 | { |
| 201 | new ListPort(argc, argv); |
| 202 | be_app->Run(); |
| 203 | be_app->Quit(); |
| 204 | return 0; |
| 205 | } |