| 1 |
/* |
| 2 |
* SPDX-License-Identifier: BSD-3-Clause |
| 3 |
* Copyright © 2009 CNRS |
| 4 |
* Copyright © 2009-2021 Inria. All rights reserved. |
| 5 |
* Copyright © 2009-2012, 2020, 2025 Université Bordeaux |
| 6 |
* Copyright © 2009-2018 Cisco Systems, Inc. All rights reserved. |
| 7 |
* See COPYING in top-level directory. |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
* Pretty text output |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "private/autogen/config.h" |
| 15 |
#include "hwloc.h" |
| 16 |
|
| 17 |
#ifdef HAVE_NL_LANGINFO |
| 18 |
#include <langinfo.h> |
| 19 |
#endif /* HAVE_NL_LANGINFO */ |
| 20 |
|
| 21 |
#ifdef HAVE_PUTWC |
| 22 |
#include <wchar.h> |
| 23 |
#endif /* HAVE_PUTWC */ |
| 24 |
|
| 25 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 26 |
#ifdef HWLOC_USE_NCURSES |
| 27 |
# include <ncurses.h> |
| 28 |
#else |
| 29 |
# include <curses.h> |
| 30 |
#endif |
| 31 |
#include <term.h> |
| 32 |
#endif /* HWLOC_HAVE_LIBTERMCAP */ |
| 33 |
|
| 34 |
#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_ISATTY) |
| 35 |
#include <sys/wait.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include "lstopo.h" |
| 39 |
|
| 40 |
#define TERM_COLOR_START 16 |
| 41 |
|
| 42 |
/* Uses unicode bars if available */ |
| 43 |
#ifdef HAVE_PUTWC |
| 44 |
typedef wchar_t character; |
| 45 |
#define PRIchar "lc" |
| 46 |
#define putcharacter(c,f) putwc(c,f) |
| 47 |
#else /* HAVE_PUTWC */ |
| 48 |
typedef unsigned char character; |
| 49 |
#define PRIchar "c" |
| 50 |
#define putcharacter(c,f) putc(c,f) |
| 51 |
#endif /* HAVE_PUTWC */ |
| 52 |
|
| 53 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 54 |
static int myputchar(int c) { |
| 55 |
return putcharacter(c, stdout); |
| 56 |
} |
| 57 |
#endif /* HWLOC_HAVE_LIBTERMCAP */ |
| 58 |
|
| 59 |
/* Off-screen rendering buffer */ |
| 60 |
struct cell { |
| 61 |
character c; |
| 62 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 63 |
const struct lstopo_color *fcolor, *bcolor; |
| 64 |
#endif /* HWLOC_HAVE_LIBTERMCAP */ |
| 65 |
}; |
| 66 |
|
| 67 |
struct lstopo_ascii_output { |
| 68 |
struct lstopo_output *loutput; |
| 69 |
struct cell **cells; |
| 70 |
int utf8; |
| 71 |
/* loutput->width and ->height converted to array of chars to simplify internal management below*/ |
| 72 |
int width; |
| 73 |
int height; |
| 74 |
}; |
| 75 |
|
| 76 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 77 |
/* Standard terminfo strings */ |
| 78 |
static char *initc = NULL, *initp = NULL; |
| 79 |
|
| 80 |
/* Set text color to bright white or black according to the background */ |
| 81 |
static int set_textcolor(int rr, int gg, int bb) |
| 82 |
{ |
| 83 |
if (!initc && !initp && rr + gg + bb < 2) { |
| 84 |
if (enter_bold_mode) |
| 85 |
tputs(enter_bold_mode, 1, myputchar); |
| 86 |
return 7; |
| 87 |
} else { |
| 88 |
if (exit_attribute_mode) |
| 89 |
tputs(exit_attribute_mode, 1, myputchar); |
| 90 |
return 0; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
set_color(const struct lstopo_color *fcolor, const struct lstopo_color *bcolor) |
| 96 |
{ |
| 97 |
char *toput; |
| 98 |
int color, textcolor; |
| 99 |
|
| 100 |
if (initc || initp) { |
| 101 |
/* Can set rgb color, easy */ |
| 102 |
textcolor = fcolor->private.ascii.color; |
| 103 |
color = bcolor->private.ascii.color; |
| 104 |
} else { |
| 105 |
/* Magic trigger: it seems to separate colors quite well */ |
| 106 |
int brr = bcolor->r >= 0xe0; |
| 107 |
int bgg = bcolor->g >= 0xe0; |
| 108 |
int bbb = bcolor->b >= 0xe0; |
| 109 |
|
| 110 |
if (set_a_background) |
| 111 |
/* ANSI colors */ |
| 112 |
color = (brr << 0) | (bgg << 1) | (bbb << 2); |
| 113 |
else |
| 114 |
/* Legacy colors */ |
| 115 |
color = (brr << 2) | (bgg << 1) | (bbb << 0); |
| 116 |
textcolor = set_textcolor(brr, bgg, bbb); |
| 117 |
} |
| 118 |
|
| 119 |
/* And now output magic string to TTY */ |
| 120 |
if (set_a_foreground) { |
| 121 |
/* foreground */ |
| 122 |
if ((toput = tparm(set_a_foreground, textcolor, 0, 0, 0, 0, 0, 0, 0, 0))) |
| 123 |
tputs(toput, 1, myputchar); |
| 124 |
/* background */ |
| 125 |
if ((toput = tparm(set_a_background, color, 0, 0, 0, 0, 0, 0, 0, 0))) |
| 126 |
tputs(toput, 1, myputchar); |
| 127 |
} else if (set_foreground) { |
| 128 |
/* foreground */ |
| 129 |
if ((toput = tparm(set_foreground, textcolor, 0, 0, 0, 0, 0, 0, 0, 0))) |
| 130 |
tputs(toput, 1, myputchar); |
| 131 |
/* background */ |
| 132 |
if ((toput = tparm(set_background, color, 0, 0, 0, 0, 0, 0, 0, 0))) |
| 133 |
tputs(toput, 1, myputchar); |
| 134 |
} else if (set_color_pair) { |
| 135 |
/* pair */ |
| 136 |
if ((toput = tparm(set_color_pair, color, 0, 0, 0, 0, 0, 0, 0, 0))) |
| 137 |
tputs(toput, 1, myputchar); |
| 138 |
} |
| 139 |
} |
| 140 |
#endif /* HWLOC_HAVE_LIBTERMCAP */ |
| 141 |
|
| 142 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 143 |
static int ascii_color_index = TERM_COLOR_START, ascii_color_index_step = 1; |
| 144 |
#endif |
| 145 |
static struct lstopo_color *default_color = NULL; |
| 146 |
|
| 147 |
/* When we can, allocate rgb colors */ |
| 148 |
static int |
| 149 |
ascii_declare_color(struct lstopo_output *loutput __hwloc_attribute_unused, struct lstopo_color *lcolor) |
| 150 |
{ |
| 151 |
int r = lcolor->r, g = lcolor->g, b = lcolor->b; |
| 152 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 153 |
int rr, gg, bb; |
| 154 |
char *toput; |
| 155 |
|
| 156 |
lcolor->private.ascii.color = ascii_color_index; |
| 157 |
ascii_color_index += ascii_color_index_step; |
| 158 |
|
| 159 |
/* Yes, values seem to range from 0 to 1000 inclusive */ |
| 160 |
rr = (r * 1001) / 256; |
| 161 |
gg = (g * 1001) / 256; |
| 162 |
bb = (b * 1001) / 256; |
| 163 |
|
| 164 |
if (initc) { |
| 165 |
if ((toput = tparm(initc, lcolor->private.ascii.color, rr, gg, bb, 0, 0, 0, 0, 0))) |
| 166 |
tputs(toput, 1, myputchar); |
| 167 |
} else if (initp) { |
| 168 |
if ((toput = tparm(initp, lcolor->private.ascii.color, 0, 0, 0, rr, gg, bb, 0, 0))) |
| 169 |
tputs(toput, 1, myputchar); |
| 170 |
} |
| 171 |
#endif /* HWLOC_HAVE_LIBTERMCAP */ |
| 172 |
|
| 173 |
if (!default_color && !r && !g && !b) |
| 174 |
default_color = lcolor; |
| 175 |
|
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
/* output text, erasing any previous content */ |
| 180 |
static void |
| 181 |
put(struct lstopo_ascii_output *disp, int x, int y, character c, const struct lstopo_color *fcolor __hwloc_attribute_unused, const struct lstopo_color *bcolor __hwloc_attribute_unused) |
| 182 |
{ |
| 183 |
if (x >= disp->width || y >= disp->height) { |
| 184 |
/* fprintf(stderr, "%"PRIchar" overflowed to (%d,%d)\n", c, x, y); */ |
| 185 |
return; |
| 186 |
} |
| 187 |
disp->cells[y][x].c = c; |
| 188 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 189 |
if (fcolor) |
| 190 |
disp->cells[y][x].fcolor = fcolor; |
| 191 |
if (bcolor) |
| 192 |
disp->cells[y][x].bcolor = bcolor; |
| 193 |
#endif /* HWLOC_HAVE_LIBTERMCAP */ |
| 194 |
} |
| 195 |
|
| 196 |
/* Where bars of a character go to */ |
| 197 |
enum { |
| 198 |
up = (1<<0), |
| 199 |
down = (1<<1), |
| 200 |
left = (1<<2), |
| 201 |
right = (1<<3) |
| 202 |
}; |
| 203 |
|
| 204 |
/* Convert a bar character into its directions */ |
| 205 |
static int |
| 206 |
to_directions(struct lstopo_ascii_output *disp, character c) |
| 207 |
{ |
| 208 |
#ifdef HAVE_PUTWC |
| 209 |
if (disp->utf8) { |
| 210 |
switch (c) { |
| 211 |
case L'\x250c': return down|right; |
| 212 |
case L'\x2510': return down|left; |
| 213 |
case L'\x2514': return up|right; |
| 214 |
case L'\x2518': return up|left; |
| 215 |
case L'\x2500': return left|right; |
| 216 |
case L'\x2502': return down|up; |
| 217 |
case L'\x2577': return down; |
| 218 |
case L'\x2575': return up; |
| 219 |
case L'\x2576': return right; |
| 220 |
case L'\x2574': return left; |
| 221 |
case L'\x251c': return down|up|right; |
| 222 |
case L'\x2524': return down|up|left; |
| 223 |
case L'\x252c': return down|left|right; |
| 224 |
case L'\x2534': return up|left|right; |
| 225 |
case L'\x253c': return down|up|left|right; |
| 226 |
default: return 0; |
| 227 |
} |
| 228 |
} else |
| 229 |
#endif /* HAVE_PUTWC */ |
| 230 |
{ |
| 231 |
switch (c) { |
| 232 |
case L'-': return left|right; |
| 233 |
case L'|': return down|up; |
| 234 |
case L'/': |
| 235 |
case L'\\': |
| 236 |
case L'+': return down|up|left|right; |
| 237 |
default: return 0; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/* Produce a bar character given the wanted directions */ |
| 243 |
static character |
| 244 |
from_directions(struct lstopo_ascii_output *disp, int direction) |
| 245 |
{ |
| 246 |
#ifdef HAVE_PUTWC |
| 247 |
if (disp->utf8) { |
| 248 |
switch (direction) { |
| 249 |
case down|right: return L'\x250c'; |
| 250 |
case down|left: return L'\x2510'; |
| 251 |
case up|right: return L'\x2514'; |
| 252 |
case up|left: return L'\x2518'; |
| 253 |
case left|right: return L'\x2500'; |
| 254 |
case down|up: return L'\x2502'; |
| 255 |
case down: return L'\x2577'; |
| 256 |
case up: return L'\x2575'; |
| 257 |
case right: return L'\x2576'; |
| 258 |
case left: return L'\x2574'; |
| 259 |
case down|up|right: return L'\x251c'; |
| 260 |
case down|up|left: return L'\x2524'; |
| 261 |
case down|left|right: return L'\x252c'; |
| 262 |
case up|left|right: return L'\x2534'; |
| 263 |
case down|up|left|right: return L'\x253c'; |
| 264 |
default: return L' '; |
| 265 |
}; |
| 266 |
} else |
| 267 |
#endif /* HAVE_PUTWC */ |
| 268 |
{ |
| 269 |
switch (direction) { |
| 270 |
case down|right: return '/'; |
| 271 |
case down|left: return '\\'; |
| 272 |
case up|right: return '\\'; |
| 273 |
case up|left: return '/'; |
| 274 |
case left|right: return '-'; |
| 275 |
case down|up: return '|'; |
| 276 |
case down: return '|'; |
| 277 |
case up: return '|'; |
| 278 |
case right: return '-'; |
| 279 |
case left: return '-'; |
| 280 |
case down|up|right: return '+'; |
| 281 |
case down|up|left: return '+'; |
| 282 |
case down|left|right: return '+'; |
| 283 |
case up|left|right: return '+'; |
| 284 |
case down|up|left|right: return '+'; |
| 285 |
default: return ' '; |
| 286 |
}; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/* output bars, merging with existing bars: `andnot' are removed, `or' are added */ |
| 291 |
static void |
| 292 |
merge(struct lstopo_ascii_output *disp, int x, int y, int or, int andnot, const struct lstopo_color *color) |
| 293 |
{ |
| 294 |
character current; |
| 295 |
int directions; |
| 296 |
if (x >= disp->width || y >= disp->height) { |
| 297 |
/* fprintf(stderr, "|%x &~%x overflowed to (%d,%d)\n", or, andnot, x, y); */ |
| 298 |
return; |
| 299 |
} |
| 300 |
current = disp->cells[y][x].c; |
| 301 |
directions = (to_directions(disp, current) & ~andnot) | or; |
| 302 |
put(disp, x, y, from_directions(disp, directions), NULL, color); |
| 303 |
} |
| 304 |
|
| 305 |
/* 10 is the only allowed gridsize/fontsize/linespacing (enforced in output_ascii()). |
| 306 |
* vertically, it's a line of text. |
| 307 |
* horizontally, it's 2 chars (so that we get a space between text and boxes |
| 308 |
*/ |
| 309 |
#define SCALE 10 |
| 310 |
/* convert generic coordinates to ASCII coordinates */ |
| 311 |
#define XSCALE(x) ((x)*2/SCALE) |
| 312 |
#define YSCALE(y) ((y)/SCALE) |
| 313 |
/* reverse conversion from text to generic x coordinate */ |
| 314 |
#define XSCALE_REVERSE(x) ((x)*SCALE/2) |
| 315 |
|
| 316 |
/* Now we can implement the standard drawing helpers */ |
| 317 |
static void |
| 318 |
ascii_box(struct lstopo_output *loutput, const struct lstopo_color *lcolor, unsigned depth __hwloc_attribute_unused, unsigned x1, unsigned width, unsigned y1, unsigned height, hwloc_obj_t obj __hwloc_attribute_unused, unsigned box_id __hwloc_attribute_unused) |
| 319 |
{ |
| 320 |
struct lstopo_ascii_output *disp = loutput->backend_data; |
| 321 |
unsigned i, j; |
| 322 |
unsigned x2, y2; |
| 323 |
|
| 324 |
x1 = XSCALE(x1); |
| 325 |
width = XSCALE(width); |
| 326 |
y1 = YSCALE(y1); |
| 327 |
height = YSCALE(height); |
| 328 |
x2 = x1 + width - 1; |
| 329 |
y2 = y1 + height - 1; |
| 330 |
|
| 331 |
/* Corners */ |
| 332 |
merge(disp, x1, y1, down|right, 0, lcolor); |
| 333 |
merge(disp, x2, y1, down|left, 0, lcolor); |
| 334 |
merge(disp, x1, y2, up|right, 0, lcolor); |
| 335 |
merge(disp, x2, y2, up|left, 0, lcolor); |
| 336 |
|
| 337 |
for (i = 1; i < width - 1; i++) { |
| 338 |
/* upper line */ |
| 339 |
merge(disp, x1 + i, y1, left|right, down, lcolor); |
| 340 |
/* lower line */ |
| 341 |
merge(disp, x1 + i, y2, left|right, up, lcolor); |
| 342 |
} |
| 343 |
for (j = 1; j < height - 1; j++) { |
| 344 |
/* left line */ |
| 345 |
merge(disp, x1, y1 + j, up|down, right, lcolor); |
| 346 |
/* right line */ |
| 347 |
merge(disp, x2, y1 + j, up|down, left, lcolor); |
| 348 |
} |
| 349 |
for (j = y1 + 1; j < y2; j++) { |
| 350 |
for (i = x1 + 1; i < x2; i++) { |
| 351 |
put(disp, i, j, ' ', NULL, lcolor); |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
static void |
| 357 |
ascii_line(struct lstopo_output *loutput, unsigned depth __hwloc_attribute_unused, unsigned x1, unsigned y1, unsigned x2, unsigned y2, hwloc_obj_t obj __hwloc_attribute_unused, unsigned line_id __hwloc_attribute_unused) |
| 358 |
{ |
| 359 |
struct lstopo_ascii_output *disp = loutput->backend_data; |
| 360 |
unsigned i, j, z; |
| 361 |
|
| 362 |
x1 = XSCALE(x1); |
| 363 |
y1 = YSCALE(y1); |
| 364 |
x2 = XSCALE(x2); |
| 365 |
y2 = YSCALE(y2); |
| 366 |
|
| 367 |
/* Canonicalize coordinates */ |
| 368 |
if (x1 > x2) { |
| 369 |
z = x1; |
| 370 |
x1 = x2; |
| 371 |
x2 = z; |
| 372 |
} |
| 373 |
if (y1 > y2) { |
| 374 |
z = y1; |
| 375 |
y1 = y2; |
| 376 |
y2 = z; |
| 377 |
} |
| 378 |
|
| 379 |
/* vertical/horizontal should be enough, but should mix with existing |
| 380 |
* characters for better output ! */ |
| 381 |
|
| 382 |
if (x1 == x2) { |
| 383 |
/* Vertical */ |
| 384 |
if (y1 == y2) { |
| 385 |
/* Hu ?! That's a point, let's do nothing... */ |
| 386 |
} else { |
| 387 |
/* top */ |
| 388 |
merge(disp, x1, y1, down, 0, NULL); |
| 389 |
/* bottom */ |
| 390 |
merge(disp, x1, y2, up, 0, NULL); |
| 391 |
} |
| 392 |
for (j = y1 + 1; j < y2; j++) |
| 393 |
merge(disp, x1, j, down|up, 0, NULL); |
| 394 |
} else if (y1 == y2) { |
| 395 |
/* Horizontal */ |
| 396 |
/* left */ |
| 397 |
merge(disp, x1, y1, right, 0, NULL); |
| 398 |
/* right */ |
| 399 |
merge(disp, x2, y1, left, 0, NULL); |
| 400 |
for (i = x1 + 1; i < x2; i++) |
| 401 |
merge(disp, i, y1, left|right, 0, NULL); |
| 402 |
} else { |
| 403 |
/* Unsupported, sorry */ |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
static void |
| 408 |
ascii_text(struct lstopo_output *loutput, const struct lstopo_color *lcolor, int size __hwloc_attribute_unused, unsigned depth __hwloc_attribute_unused, unsigned x, unsigned y, const char *text, hwloc_obj_t obj __hwloc_attribute_unused, unsigned text_id __hwloc_attribute_unused) |
| 409 |
{ |
| 410 |
struct lstopo_ascii_output *disp = loutput->backend_data; |
| 411 |
|
| 412 |
x = XSCALE(x); |
| 413 |
y = YSCALE(y); |
| 414 |
|
| 415 |
#if defined(HAVE_PUTWC) && !defined(__MINGW32__) && !defined(_MSC_VER) |
| 416 |
{ |
| 417 |
size_t len = strlen(text) + 1; |
| 418 |
wchar_t *wbuf = malloc(len * sizeof(wchar_t)), *wtext; |
| 419 |
swprintf(wbuf, len, L"%s", text); |
| 420 |
for (wtext = wbuf ; *wtext; wtext++) |
| 421 |
put(disp, x++, y, *wtext, lcolor, NULL); |
| 422 |
free(wbuf); |
| 423 |
} |
| 424 |
#else |
| 425 |
for ( ; *text; text++) |
| 426 |
put(disp, x++, y, *text, lcolor, NULL); |
| 427 |
#endif |
| 428 |
} |
| 429 |
|
| 430 |
static void |
| 431 |
ascii_textsize(struct lstopo_output *loutput __hwloc_attribute_unused, const char *text __hwloc_attribute_unused, unsigned textlength, unsigned fontsize __hwloc_attribute_unused, unsigned *width) |
| 432 |
{ |
| 433 |
*width = XSCALE_REVERSE(textlength); |
| 434 |
} |
| 435 |
|
| 436 |
static struct draw_methods ascii_draw_methods = { |
| 437 |
ascii_declare_color, |
| 438 |
NULL, |
| 439 |
ascii_box, |
| 440 |
ascii_line, |
| 441 |
ascii_text, |
| 442 |
ascii_textsize, |
| 443 |
}; |
| 444 |
|
| 445 |
#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_ISATTY) |
| 446 |
static int start_pager(pid_t *child, int *pager_output) { |
| 447 |
const char *pager; |
| 448 |
int fd[2]; |
| 449 |
pid_t pid; |
| 450 |
|
| 451 |
pager = getenv("LSTOPO_PAGER"); |
| 452 |
if (!pager) |
| 453 |
pager = "less"; |
| 454 |
|
| 455 |
if (!*pager || !strcmp(pager, "cat")) |
| 456 |
return 0; |
| 457 |
|
| 458 |
if (pipe(fd) < 0) |
| 459 |
return 0; |
| 460 |
|
| 461 |
pid = fork(); |
| 462 |
if (pid < 0) { |
| 463 |
close(fd[0]); |
| 464 |
close(fd[1]); |
| 465 |
return 0; |
| 466 |
} |
| 467 |
|
| 468 |
if (pid > 0) { |
| 469 |
/* father */ |
| 470 |
close(fd[0]); |
| 471 |
close(STDIN_FILENO); |
| 472 |
|
| 473 |
*child = pid; |
| 474 |
|
| 475 |
*pager_output = fd[1]; |
| 476 |
if (isatty(STDERR_FILENO)) |
| 477 |
dup2(fd[1], STDERR_FILENO); |
| 478 |
|
| 479 |
return 1; |
| 480 |
} else { |
| 481 |
/* child */ |
| 482 |
char options[] = "LESS=SFRX"; |
| 483 |
putenv(options); |
| 484 |
|
| 485 |
dup2(fd[0], STDIN_FILENO); |
| 486 |
close(fd[0]); |
| 487 |
close(fd[1]); |
| 488 |
|
| 489 |
execlp(pager, pager, NULL); |
| 490 |
|
| 491 |
/* less not available, just output */ |
| 492 |
execlp("cat", "cat", NULL); |
| 493 |
|
| 494 |
/* Oops */ |
| 495 |
fprintf(stderr, "Failed to execute both pager %s and cat\n", pager); |
| 496 |
exit(1); |
| 497 |
} |
| 498 |
} |
| 499 |
#endif |
| 500 |
|
| 501 |
int |
| 502 |
output_ascii(struct lstopo_output *loutput, const char *filename) |
| 503 |
{ |
| 504 |
FILE *output; |
| 505 |
struct lstopo_ascii_output disp; |
| 506 |
int i, j; |
| 507 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 508 |
const struct lstopo_color *lfcolor = NULL; /* Last foreground color */ |
| 509 |
const struct lstopo_color *lbcolor = NULL; /* Last background color */ |
| 510 |
int term = 0; |
| 511 |
char *tmp; |
| 512 |
#endif |
| 513 |
int use_pager = 0; |
| 514 |
#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_ISATTY) |
| 515 |
pid_t pager_pid = -1; |
| 516 |
int pager_output = -1; |
| 517 |
#endif |
| 518 |
int width, height; |
| 519 |
|
| 520 |
output = open_output(filename, loutput->overwrite); |
| 521 |
if (!output) { |
| 522 |
fprintf(stderr, "Failed to open %s for writing (%s)\n", filename, strerror(errno)); |
| 523 |
return -1; |
| 524 |
} |
| 525 |
|
| 526 |
#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_ISATTY) |
| 527 |
use_pager = (!filename || !strcmp(filename, "-")) && isatty(STDOUT_FILENO); |
| 528 |
if (use_pager) |
| 529 |
use_pager = start_pager(&pager_pid, &pager_output); |
| 530 |
#endif |
| 531 |
|
| 532 |
loutput->gridsize = 10; |
| 533 |
loutput->fontsize = 10; |
| 534 |
loutput->linespacing = 10; |
| 535 |
|
| 536 |
/* cannot write between lines of the terminal */ |
| 537 |
loutput->backend_flags |= LSTOPO_BACKEND_FLAG_NO_HALF_LINES; |
| 538 |
|
| 539 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 540 |
/* If we are outputing to a tty, use colors */ |
| 541 |
if (use_pager || (output == stdout && isatty(STDOUT_FILENO))) { |
| 542 |
term = !setupterm(NULL, STDOUT_FILENO, NULL); |
| 543 |
|
| 544 |
if (term) { |
| 545 |
/* reset colors */ |
| 546 |
if (orig_colors) |
| 547 |
tputs(orig_colors, 1, myputchar); |
| 548 |
|
| 549 |
/* Get terminfo(5) strings */ |
| 550 |
initp = initialize_pair; |
| 551 |
if (max_pairs <= TERM_COLOR_START || !initp || !set_color_pair) { |
| 552 |
/* Can't use max_pairs to define our own colors */ |
| 553 |
initp = NULL; |
| 554 |
if (max_colors > TERM_COLOR_START) { |
| 555 |
/* Better start overwriting last colors */ |
| 556 |
ascii_color_index = max_colors-1; |
| 557 |
ascii_color_index_step = -1; |
| 558 |
if (can_change) |
| 559 |
initc = initialize_color; |
| 560 |
} |
| 561 |
} |
| 562 |
/* Prevent a trivial compiler warning because the param of |
| 563 |
tgetflag is (char*), not (const char*). */ |
| 564 |
tmp = strdup("lhs"); |
| 565 |
if (tgetflag(tmp)) { |
| 566 |
/* Sorry, I'm lazy to convert colors and I don't know any terminal |
| 567 |
* using LHS anyway */ |
| 568 |
initc = initp = 0; |
| 569 |
} |
| 570 |
free(tmp); |
| 571 |
} |
| 572 |
} |
| 573 |
#endif /* HWLOC_HAVE_LIBTERMCAP */ |
| 574 |
|
| 575 |
disp.loutput = loutput; |
| 576 |
loutput->backend_data = &disp; |
| 577 |
loutput->methods = &ascii_draw_methods; |
| 578 |
|
| 579 |
/* recurse once for preparing sizes and positions */ |
| 580 |
loutput->drawing = LSTOPO_DRAWING_PREPARE; |
| 581 |
output_draw(loutput); |
| 582 |
width = disp.width = XSCALE(loutput->width + 1); |
| 583 |
height = disp.height = YSCALE(loutput->height + 1); |
| 584 |
loutput->drawing = LSTOPO_DRAWING_DRAW; |
| 585 |
|
| 586 |
/* prepare colors */ |
| 587 |
declare_colors(loutput); |
| 588 |
lstopo_prepare_custom_styles(loutput); |
| 589 |
|
| 590 |
/* terminals usually have narrow characters, so let's make them wider */ |
| 591 |
disp.cells = malloc(height * sizeof(*disp.cells)); |
| 592 |
for (j = 0; j < height; j++) { |
| 593 |
disp.cells[j] = calloc(width, sizeof(**disp.cells)); |
| 594 |
for (i = 0; i < width; i++) { |
| 595 |
disp.cells[j][i].c = ' '; |
| 596 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 597 |
disp.cells[j][i].fcolor = default_color; |
| 598 |
disp.cells[j][i].bcolor = default_color; |
| 599 |
#endif |
| 600 |
} |
| 601 |
} |
| 602 |
#ifdef HAVE_NL_LANGINFO |
| 603 |
disp.utf8 = !strcmp(nl_langinfo(CODESET), "UTF-8"); |
| 604 |
#endif /* HAVE_NL_LANGINFO */ |
| 605 |
|
| 606 |
#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_ISATTY) |
| 607 |
if (use_pager) { |
| 608 |
fflush(stdout); |
| 609 |
|
| 610 |
/* Now that we have set up colors on the tty (which the pager doesn't |
| 611 |
* understand), we can send the text to the pager. */ |
| 612 |
dup2(pager_output, STDOUT_FILENO); |
| 613 |
close(pager_output); |
| 614 |
} |
| 615 |
#endif |
| 616 |
|
| 617 |
/* ready */ |
| 618 |
output_draw(loutput); |
| 619 |
|
| 620 |
for (j = 0; j < disp.height; j++) { |
| 621 |
for (i = 0; i < disp.width; i++) { |
| 622 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 623 |
if (term) { |
| 624 |
/* TTY output, use colors */ |
| 625 |
const struct lstopo_color *fcolor = disp.cells[j][i].fcolor; |
| 626 |
const struct lstopo_color *bcolor = disp.cells[j][i].bcolor; |
| 627 |
|
| 628 |
/* Avoid too much work for the TTY */ |
| 629 |
if (fcolor != lfcolor || bcolor != lbcolor) { |
| 630 |
set_color(fcolor, bcolor); |
| 631 |
lfcolor = fcolor; |
| 632 |
lbcolor = bcolor; |
| 633 |
} |
| 634 |
} |
| 635 |
#endif /* HWLOC_HAVE_LIBTERMCAP */ |
| 636 |
putcharacter(disp.cells[j][i].c, output); |
| 637 |
} |
| 638 |
#ifdef HWLOC_HAVE_LIBTERMCAP |
| 639 |
/* Keep the rest of the line as default */ |
| 640 |
if (term && orig_pair) { |
| 641 |
lfcolor = NULL; |
| 642 |
lbcolor = NULL; |
| 643 |
tputs(orig_pair, 1, myputchar); |
| 644 |
} |
| 645 |
#endif /* HWLOC_HAVE_LIBTERMCAP */ |
| 646 |
putcharacter('\n', output); |
| 647 |
} |
| 648 |
|
| 649 |
for (j = 0; j < disp.height; j++) |
| 650 |
free(disp.cells[j]); |
| 651 |
free(disp.cells); |
| 652 |
|
| 653 |
if (output != stdout) |
| 654 |
fclose(output); |
| 655 |
|
| 656 |
destroy_colors(loutput); |
| 657 |
|
| 658 |
#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_ISATTY) |
| 659 |
if (use_pager) { |
| 660 |
/* Tell pager that we are finished */ |
| 661 |
fflush(stdout); |
| 662 |
fflush(stderr); |
| 663 |
close(STDOUT_FILENO); |
| 664 |
close(STDERR_FILENO); |
| 665 |
/* And wait for it */ |
| 666 |
waitpid(pager_pid, NULL, 0); |
| 667 |
} |
| 668 |
#endif |
| 669 |
|
| 670 |
return 0; |
| 671 |
} |