Ticket #10156: 0001-ps-Support-o-option.patch
File 0001-ps-Support-o-option.patch, 6.7 KB (added by , 11 years ago) |
---|
-
ps.cpp
From e468f90d38657ab5b4a91490c50998ff8125b03f Mon Sep 17 00:00:00 2001 From: Thomas Schmidt <thomas.compix@googlemail.com> Date: Thu, 21 Nov 2013 17:26:52 +0000 Subject: [PATCH] ps: Support -o option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Edited help output * Added required include * made sStates and printCustomizedDefault to const char* * improved styling Ü** Fixed author section --- ps.cpp | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 111 insertions(+), 24 deletions(-) diff --git a/ps.cpp b/ps.cpp index a650f96..cc23f92 100644
a b 5 5 * Authors: 6 6 * Francois Revol (mmu_man) 7 7 * Salvatore Benedetto <salvatore.benedetto@gmail.com> 8 * Thomas Schmidt <thomas.compix@googlemail.com> 8 9 * Bjoern Herzig (xRaich[o]2x) 9 10 */ 11 #include <cstdlib> 10 12 #include <stdio.h> 11 #include <unistd.h>12 13 #include <string.h> 14 #include <unistd.h> 13 15 14 16 #include <OS.h> 15 17 16 18 #define SNOOZE_TIME 100000 17 19 18 char *sStates[] = {"run", "rdy", "msg", "zzz", "sus", "wait" }; 20 struct option 21 { 22 int id; 23 const char* name; 24 const char* outputHeader; 25 const char* outputLine; 26 }; 27 28 option sOptions[] = { 29 {1, "Team", "%-50s", "%-50s"}, 30 {2, "Id", "%5s", "%5" B_PRId32}, 31 {3, "Threads", "%8s", "%8" B_PRId32}, 32 {4, "Gid", "%4s", "%4d"}, 33 {5, "Uid", "%4s", "%4d"}, 34 {0, NULL, NULL, NULL} // required for the loop to exit 35 }; 36 37 const char* sStates[] = {"run", "rdy", "msg", "zzz", "sus", "wait"}; 38 const char* printCustomizedDefault = "Team,Id,Threads,Gid,Uid"; 19 39 20 static void printTeamThreads(team_info *teamInfo, bool printSemaphoreInfo); 21 static void printTeamInfo(team_info *teamInfo, bool printHeader); 40 static void printTeamThreads(team_info* teamInfo, bool printSemaphoreInfo); 41 static void printTeamInfo(team_info* teamInfo, bool printHeader, 42 const char* printCustomized); 22 43 23 44 static void 24 printTeamInfo(team_info *teamInfo, bool printHeader) 45 printTeamInfo(team_info* teamInfo, bool printHeader, 46 const char* printCustomized) 25 47 { 26 // Print team info 48 char buffer[256] = {0}; 49 int bufferPos = 0; 50 int printCustomizedPos = 0; 51 bool foundOption = false; 52 while (printCustomized[printCustomizedPos] != '\0') { // until nothing left 53 if (printCustomized[printCustomizedPos] == ',') 54 printCustomizedPos++; 55 56 for (int i = 0; sOptions[i].id != 0; ++i) { 57 option current = sOptions[i]; 58 int len = strlen(current.name); 59 60 if (0 == strncmp(printCustomized + printCustomizedPos, 61 current.name, len)) { 62 // where to check printCustomized next 63 printCustomizedPos += len; 64 foundOption = true; 65 66 if (printHeader) // output header 67 bufferPos += sprintf(buffer + bufferPos, 68 current.outputHeader, current.name); 69 else 70 switch (current.id) { // output line 71 case 1: 72 bufferPos += sprintf(buffer + bufferPos, 73 current.outputLine, teamInfo->args); 74 break; 75 case 2: 76 bufferPos += sprintf(buffer + bufferPos, 77 current.outputLine, teamInfo->team); 78 break; 79 case 3: 80 bufferPos += sprintf(buffer + bufferPos, 81 current.outputLine, teamInfo->thread_count); 82 break; 83 case 4: 84 bufferPos += sprintf(buffer + bufferPos, 85 current.outputLine, teamInfo->gid); 86 break; 87 case 5: 88 bufferPos += sprintf(buffer + bufferPos, 89 current.outputLine, teamInfo->uid); 90 break; 91 } 92 bufferPos += sprintf(buffer + bufferPos, " "); 93 break; 94 } 95 } 96 97 // check whether option was recognized 98 if (!foundOption) { 99 printf("option not recognized\n"); 100 exit(1); 101 } 102 else 103 foundOption = false; // reset the flag 104 }; 105 106 printf(buffer); 107 printf("\n"); 108 27 109 if (printHeader) 28 printf("%-50s %5s %8s %4s %4s\n", "Team", "Id", "#Threads", "Gid", \ 29 "Uid"); 30 31 printf("%-50s %5" B_PRId32 " %8" B_PRId32 " %4d %4d\n", teamInfo->args, 32 teamInfo->team, teamInfo->thread_count, teamInfo->gid, teamInfo->uid); 110 printTeamInfo(teamInfo, false, printCustomized); 33 111 } 34 112 35 113 36 114 static void 37 115 printTeamThreads(team_info *teamInfo, bool printSemaphoreInfo) 38 116 { 39 c har *threadState;117 const char *threadState; 40 118 int32 threadCookie = 0; 41 119 sem_info semaphoreInfo; 42 120 thread_info threadInfo; … … printTeamThreads(team_info *teamInfo, bool printSemaphoreInfo) 73 151 } 74 152 } 75 153 154 76 155 int 77 156 main(int argc, char **argv) 78 157 { … … main(int argc, char **argv) 83 162 bool printThreads = false; 84 163 bool printHeader = true; 85 164 bool printSemaphoreInfo = false; 165 const char* printCustomized = printCustomizedDefault; 86 166 // match this in team name 87 167 char *string_to_match; 88 168 89 169 int c; 90 170 91 while ((c = getopt(argc, argv,"ihas ")) != EOF) {92 switch (c) {171 while ((c = getopt(argc, argv,"ihaso:")) != EOF) { 172 switch (c) { 93 173 case 'i': 94 174 printSystemInfo = true; 95 175 break; 96 176 case 'h': 97 printf( "usage: ps [-hais] [team]\n" 98 "-h : show help\n" 99 "-i : show system info\n" 100 "-s : show semaphore info\n" 101 "-a : show threads too (by default only teams are " \ 102 "displayed)\n"); 177 printf( "usage: ps [-hais] [-o options] [team]\n" 178 "-h : show help\n" 179 "-i : show system info\n" 180 "-s : show semaphore info\n" 181 "-o : customizes the program output\n" 182 " possible options: Team,Id,Threads,Gid,Uid\n" 183 " seperate with comma - no spaces! (like above)\n" 184 "-a : show threads too (by default only teams are " \ 185 "displayed)\n"); 103 186 return 0; 104 187 break; 105 188 case 'a': … … main(int argc, char **argv) 108 191 case 's': 109 192 printSemaphoreInfo = true; 110 193 break; 194 case 'o': 195 printCustomized = optarg; 196 break; 111 197 } 112 198 } 113 199 114 200 // TODO: parse command line 115 201 // Possible command line options: 116 // -tpstree like output202 // -t pstree like output 117 203 118 204 if (argc == 2 && (printSystemInfo||printThreads)) 119 205 string_to_match = NULL; 120 206 else 121 string_to_match = (argc >= 2) ? argv[argc-1] : NULL; 207 string_to_match = (argc >= 2 && 208 printCustomized == printCustomizedDefault) ? argv[argc-1] : NULL; 122 209 123 210 if (!string_to_match) { 124 211 while (get_next_team_info(&teamCookie, &teamInfo) >= B_OK) { 125 212 126 printTeamInfo(&teamInfo,printHeader );213 printTeamInfo(&teamInfo,printHeader,printCustomized); 127 214 printHeader = false; 128 215 if (printThreads) { 129 216 printf("\n%-37s %5s %8s %4s %8s %8s\n", "Thread", "Id", \ … … main(int argc, char **argv) 145 232 p = teamInfo.args; 146 233 if (strstr(p, string_to_match) == NULL) 147 234 continue; 148 printTeamInfo(&teamInfo,true );235 printTeamInfo(&teamInfo,true, printCustomized); 149 236 printf("\n%-37s %5s %8s %4s %8s %8s\n", "Thread", "Id", "State", \ 150 237 "Prio", "UTime", "KTime"); 151 238 printTeamThreads(&teamInfo,printSemaphoreInfo);