1a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel/* 2a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz> 3a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * 4a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * This program is free software: you can redistribute it and/or modify 5a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * it under the terms of the GNU General Public License as published by 6a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * the Free Software Foundation, either version 2 of the License, or 7a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * (at your option) any later version. 8a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * 9a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * This program is distributed in the hope that it will be useful, 10a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * GNU General Public License for more details. 13a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * 14a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * You should have received a copy of the GNU General Public License 15a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel */ 17a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 18a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel#include <unistd.h> 19a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel#include <stdlib.h> 20a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel#include <string.h> 21a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 22a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel#include "tst_res_flags.h" 233a0ef86ac1449ba6fd938c44e28e6e48bbda408cPetr Vorel#include "tst_ansi_color.h" 24a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 25a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorelchar* tst_ttype2color(int ttype) 26a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel{ 27a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel switch (TTYPE_RESULT(ttype)) { 28a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel case TPASS: 29a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel return ANSI_COLOR_GREEN; 30a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel break; 31a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel case TFAIL: 32a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel return ANSI_COLOR_RED; 33a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel break; 34a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel case TBROK: 35a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel return ANSI_COLOR_RED; 36a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel break; 37a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel case TCONF: 38a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel return ANSI_COLOR_YELLOW; 39a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel break; 40a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel case TWARN: 41a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel return ANSI_COLOR_MAGENTA; 42a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel break; 43a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel case TINFO: 44a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel return ANSI_COLOR_BLUE; 45a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel break; 46a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel default: 47a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel return ""; 48a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel } 49a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel} 50a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 51047c72769fb51db35c55b45d06ef214cfd9f9167Cyril Hrubisint tst_color_enabled(int fd) 52a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel{ 53a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel static int color; 54a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 55a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel if (color) 56a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel return color - 1; 57a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 58a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel char *env = getenv("LTP_COLORIZE_OUTPUT"); 59a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 60a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel if (env) { 61a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel if (!strcmp(env, "n") || !strcmp(env, "0")) 62a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel color = 1; 63a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 64a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel if (!strcmp(env, "y") || !strcmp(env, "1")) 65a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel color = 2; 66a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 67a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel return color - 1; 68a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel } 69a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 70047c72769fb51db35c55b45d06ef214cfd9f9167Cyril Hrubis if (isatty(fd) == 0) 71a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel color = 1; 72a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel else 73a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel color = 2; 74a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel 75a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel return color - 1; 76a7f6133726a55fe8a094b8a14a8cb80101d0ff81Petr Vorel} 77