Ticket #10156: 0001-ps-Support-o-option.patch

File 0001-ps-Support-o-option.patch, 6.7 KB (added by ThomasCompix, 10 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  
    55 * Authors:
    66 *      Francois Revol (mmu_man)
    77 *      Salvatore Benedetto <salvatore.benedetto@gmail.com>
     8 *      Thomas Schmidt <thomas.compix@googlemail.com>
    89 *      Bjoern Herzig (xRaich[o]2x)
    910 */
     11#include <cstdlib>
    1012#include <stdio.h>
    11 #include <unistd.h>
    1213#include <string.h>
     14#include <unistd.h>
    1315
    1416#include <OS.h>
    1517
    1618#define SNOOZE_TIME 100000
    1719
    18 char *sStates[] = {"run", "rdy", "msg", "zzz", "sus", "wait" };
     20struct option
     21{
     22    int         id;
     23    const char* name;
     24    const char* outputHeader;
     25    const char* outputLine;
     26};
     27
     28option 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
     37const char* sStates[] = {"run", "rdy", "msg", "zzz", "sus", "wait"};
     38const char* printCustomizedDefault = "Team,Id,Threads,Gid,Uid";
    1939
    20 static void printTeamThreads(team_info *teamInfo, bool printSemaphoreInfo);
    21 static void printTeamInfo(team_info *teamInfo, bool printHeader);
     40static void printTeamThreads(team_info* teamInfo, bool printSemaphoreInfo);
     41static void printTeamInfo(team_info* teamInfo, bool printHeader,
     42    const char* printCustomized);
    2243
    2344static void
    24 printTeamInfo(team_info *teamInfo, bool printHeader)
     45printTeamInfo(team_info* teamInfo, bool printHeader,
     46    const char* printCustomized)
    2547{
    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
    27109    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);
    33111}
    34112
    35113
    36114static void
    37115printTeamThreads(team_info *teamInfo, bool printSemaphoreInfo)
    38116{
    39     char *threadState;
     117    const char *threadState;
    40118    int32 threadCookie = 0;
    41119    sem_info semaphoreInfo;
    42120    thread_info threadInfo;
    printTeamThreads(team_info *teamInfo, bool printSemaphoreInfo)  
    73151    }
    74152}
    75153
     154
    76155int
    77156main(int argc, char **argv)
    78157{
    main(int argc, char **argv)  
    83162    bool printThreads = false;
    84163    bool printHeader = true;
    85164    bool printSemaphoreInfo = false;
     165    const char* printCustomized = printCustomizedDefault;
    86166    // match this in team name
    87167    char *string_to_match;
    88168   
    89169    int c;
    90170   
    91     while ((c = getopt(argc, argv,"ihas")) != EOF) {
    92         switch(c) {
     171    while ((c = getopt(argc, argv,"ihaso:")) != EOF) {
     172        switch (c) {
    93173            case 'i':
    94174                printSystemInfo = true;
    95175                break;
    96176            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");
    103186                return 0;
    104187                break;
    105188            case 'a':
    main(int argc, char **argv)  
    108191            case 's':
    109192                printSemaphoreInfo = true;
    110193                break;
     194            case 'o':
     195                printCustomized = optarg;   
     196                break;
    111197        }
    112198    }
    113199
    114200    // TODO: parse command line
    115201    // Possible command line options:
    116     //      -t  pstree like output
     202    //      -t  pstree like output
    117203   
    118204    if (argc == 2 && (printSystemInfo||printThreads))
    119205        string_to_match = NULL;
    120206    else
    121         string_to_match = (argc >= 2) ? argv[argc-1] : NULL;
     207        string_to_match = (argc >= 2 &&
     208            printCustomized == printCustomizedDefault) ? argv[argc-1] : NULL;
    122209   
    123210    if (!string_to_match) {
    124211        while (get_next_team_info(&teamCookie, &teamInfo) >= B_OK) {
    125212           
    126             printTeamInfo(&teamInfo,printHeader);
     213            printTeamInfo(&teamInfo,printHeader,printCustomized);
    127214            printHeader = false;
    128215            if (printThreads) {
    129216                printf("\n%-37s %5s %8s %4s %8s %8s\n", "Thread", "Id", \
    main(int argc, char **argv)  
    145232                p = teamInfo.args;
    146233            if (strstr(p, string_to_match) == NULL)
    147234                continue;
    148             printTeamInfo(&teamInfo,true);
     235            printTeamInfo(&teamInfo,true, printCustomized);
    149236            printf("\n%-37s %5s %8s %4s %8s %8s\n", "Thread", "Id", "State", \
    150237                "Prio", "UTime", "KTime");
    151238            printTeamThreads(&teamInfo,printSemaphoreInfo);