root/orte/tools/orte-info/output.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. orte_info_out
  2. orte_info_out_int

   1 /*
   2  * Copyright (c) 2004-2009 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2006 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2010      Cisco Systems, Inc.  All rights reserved.
  13  * Copyright (c) 2018      Intel, Inc.  All rights reserved.
  14  * $COPYRIGHT$
  15  *
  16  * Additional copyrights may follow
  17  *
  18  * $HEADER$
  19  */
  20 
  21 #include "orte_config.h"
  22 
  23 #include <stdio.h>
  24 #include <string.h>
  25 #ifdef HAVE_UNISTD_H
  26 #include <unistd.h>
  27 #endif
  28 #include <signal.h>
  29 #ifdef HAVE_TERMIOS_H
  30 #include <termios.h>
  31 #endif
  32 #ifdef HAVE_SYS_IOCTL_H
  33 #include <sys/ioctl.h>
  34 #endif
  35 #include <ctype.h>
  36 
  37 #include "orte/tools/orte-info/orte-info.h"
  38 
  39 #include "opal/util/show_help.h"
  40 
  41 #define OMPI_max(a,b) (((a) > (b)) ? (a) : (b))
  42 
  43 
  44 /*
  45  * Private variables - set some reasonable screen size defaults
  46  */
  47 
  48 static int centerpoint = 24;
  49 static int screen_width = 78;
  50 
  51 /*
  52  * Prints the passed integer in a pretty or parsable format.
  53  */
  54 void orte_info_out(const char *pretty_message, const char *plain_message, const char *value)
  55 {
  56     size_t i, len, max_value_width;
  57     char *spaces = NULL;
  58     char *filler = NULL;
  59     char *pos, *v, savev, *v_to_free;
  60 
  61 #ifdef HAVE_ISATTY
  62     /* If we have isatty(), if this is not a tty, then disable
  63      * wrapping for grep-friendly behavior
  64      */
  65     if (0 == isatty(STDOUT_FILENO)) {
  66         screen_width = INT_MAX;
  67     }
  68 #endif
  69 
  70 #ifdef TIOCGWINSZ
  71     if (screen_width < INT_MAX) {
  72         struct winsize size;
  73         if (ioctl(STDOUT_FILENO, TIOCGWINSZ, (char*) &size) >= 0) {
  74             screen_width = size.ws_col;
  75         }
  76     }
  77 #endif
  78 
  79     /* Strip leading and trailing whitespace from the string value */
  80     v = v_to_free = strdup(value);
  81     len = strlen(v);
  82     if (isspace(v[0])) {
  83         char *newv;
  84         i = 0;
  85         while (isspace(v[i]) && i < len) {
  86             ++i;
  87         }
  88         newv = strdup(v + i);
  89         free(v_to_free);
  90         v_to_free = v = newv;
  91         len = strlen(v);
  92     }
  93     if (len > 0 && isspace(v[len - 1])) {
  94         i = len - 1;
  95         /* Note that i is size_t (unsigned), so we can't check for i
  96            >= 0.  But we don't need to, because if the value was all
  97            whitespace, stripping whitespace from the left (above)
  98            would have resulted in an empty string, and we wouldn't
  99            have gotten into this block. */
 100         while (isspace(v[i]) && i > 0) {
 101             --i;
 102         }
 103         v[i] = '\0';
 104     }
 105 
 106     if (orte_info_pretty && NULL != pretty_message) {
 107         if (centerpoint > (int)strlen(pretty_message)) {
 108             opal_asprintf(&spaces, "%*s", centerpoint -
 109                      (int)strlen(pretty_message), " ");
 110         } else {
 111             spaces = strdup("");
 112 #if OPAL_ENABLE_DEBUG
 113             if (centerpoint < (int)strlen(pretty_message)) {
 114                 opal_show_help("help-orte-info.txt",
 115                                "developer warning: field too long", false,
 116                                pretty_message, centerpoint);
 117             }
 118 #endif
 119         }
 120         max_value_width = screen_width - strlen(spaces) - strlen(pretty_message) - 2;
 121         if (0 < strlen(pretty_message)) {
 122             opal_asprintf(&filler, "%s%s: ", spaces, pretty_message);
 123         } else {
 124             opal_asprintf(&filler, "%s  ", spaces);
 125         }
 126         free(spaces);
 127         spaces = NULL;
 128 
 129         while (true) {
 130             if (strlen(v) < max_value_width) {
 131                 printf("%s%s\n", filler, v);
 132                 break;
 133             } else {
 134                 opal_asprintf(&spaces, "%*s", centerpoint + 2, " ");
 135 
 136                 /* Work backwards to find the first space before
 137                  * max_value_width
 138                  */
 139                 savev = v[max_value_width];
 140                 v[max_value_width] = '\0';
 141                 pos = (char*)strrchr(v, (int)' ');
 142                 v[max_value_width] = savev;
 143                 if (NULL == pos) {
 144                     /* No space found < max_value_width.  Look for the first
 145                      * space after max_value_width.
 146                      */
 147                     pos = strchr(&v[max_value_width], ' ');
 148 
 149                     if (NULL == pos) {
 150 
 151                         /* There's just no spaces.  So just print it and be done. */
 152 
 153                         printf("%s%s\n", filler, v);
 154                         break;
 155                     } else {
 156                         *pos = '\0';
 157                         printf("%s%s\n", filler, v);
 158                         v = pos + 1;
 159                     }
 160                 } else {
 161                     *pos = '\0';
 162                     printf("%s%s\n", filler, v);
 163                     v = pos + 1;
 164                 }
 165 
 166                 /* Reset for the next iteration */
 167                 free(filler);
 168                 filler = strdup(spaces);
 169                 free(spaces);
 170                 spaces = NULL;
 171             }
 172         }
 173         if (NULL != filler) {
 174             free(filler);
 175         }
 176         if (NULL != spaces) {
 177             free(spaces);
 178         }
 179     } else {
 180         if (NULL != plain_message && 0 < strlen(plain_message)) {
 181             printf("%s:%s\n", plain_message, value);
 182         } else {
 183             printf("  %s\n", value);
 184         }
 185     }
 186     if (NULL != v_to_free) {
 187         free(v_to_free);
 188     }
 189 }
 190 
 191 void orte_info_out_int(const char *pretty_message,
 192                        const char *plain_message,
 193                        int value)
 194 {
 195     char *valstr;
 196 
 197     opal_asprintf(&valstr, "%d", (int)value);
 198     orte_info_out(pretty_message, plain_message, valstr);
 199     free(valstr);
 200 }

/* [<][>][^][v][top][bottom][index][help] */