"Fossies" - the Fresh Open Source Software Archive

Member "monit-5.28.0/src/terminal/TextColor.c" (28 Mar 2021, 3631 Bytes) of package /linux/privat/monit-5.28.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "TextColor.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Tildeslash Ltd. All rights reserved.
    3  *
    4  * This program is free software: you can redistribute it and/or modify
    5  * it under the terms of the GNU Affero General Public License version 3.
    6  *
    7  * This program is distributed in the hope that it will be useful,
    8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10  * GNU Affero General Public License for more details.
   11  *
   12  * You should have received a copy of the GNU Affero General Public License
   13  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   14  *
   15  * In addition, as a special exception, the copyright holders give
   16  * permission to link the code of portions of this program with the
   17  * OpenSSL library under certain conditions as described in each
   18  * individual source file, and distribute linked combinations
   19  * including the two.
   20  *
   21  * You must obey the GNU Affero General Public License in all respects
   22  * for all of the code used other than OpenSSL.
   23  */
   24 
   25 
   26 #include "config.h"
   27 
   28 #ifdef HAVE_STDLIB_H
   29 #include <stdlib.h>
   30 #endif
   31 
   32 #ifdef HAVE_UNISTD_H
   33 #include <unistd.h>
   34 #endif
   35 
   36 #include "monit.h"
   37 #include "TextColor.h"
   38 
   39 // libmonit
   40 #include "util/Str.h"
   41 
   42 
   43 /**
   44  * Implementation of the Terminal color interface
   45  *
   46  * @author http://www.tildeslash.com/
   47  * @see http://www.mmonit.com/
   48  * @file
   49  */
   50 
   51 
   52 /* -------------------------------------------------------- Public Methods */
   53 
   54 
   55 bool TextColor_support() {
   56         if (! (Run.flags & Run_Batch) && isatty(STDOUT_FILENO)) {
   57                 if (getenv("COLORTERM")) {
   58                         return true;
   59                 } else {
   60                         char *term = getenv("TERM");
   61                         if (term && (Str_startsWith(term, "screen") || Str_startsWith(term, "xterm") || Str_startsWith(term, "vt100") || Str_startsWith(term, "ansi") || Str_startsWith(term, "linux") || Str_startsWith(term, "rxvt") || Str_sub(term, "color")))
   62                                 return true;
   63                 }
   64         }
   65         return false;
   66 }
   67 
   68 
   69 int TextColor_length(char *s) {
   70         if (STR_DEF(s)) {
   71                 int length = 0;
   72                 bool ansi = false;
   73                 for (int i = 0; s[i]; i++) {
   74                         if (s[i] == '\033' && s[i + 1] == '[') {
   75                                 // Escape sequence start
   76                                 ansi = true;
   77                                 length += 2;
   78                                 i++;
   79                         } else if (ansi) {
   80                                 length++;
   81                                 // Escape sequence stop
   82                                 if (s[i] >= 64 && s[i] <= 126)
   83                                         ansi = false;
   84                         }
   85                 }
   86                 return length;
   87         }
   88         return 0;
   89 }
   90 
   91 
   92 char *TextColor_strip(char *s) {
   93         if (STR_DEF(s)) {
   94                 int x, y;
   95                 bool ansi = false;
   96                 for (x = 0, y = 0; s[y]; y++) {
   97                         if (s[y] == '\033' && s[y + 1] == '[') {
   98                                 // Escape sequence start
   99                                 ansi = true;
  100                                 y++; // ++ to skip 'ESC['
  101                         } else if (ansi) {
  102                                 // Escape sequence stop
  103                                 if (s[y] >= 64 && s[y] <= 126)
  104                                         ansi = false;
  105                         } else {
  106                                 s[x++] = s[y];
  107                         }
  108                 }
  109                 s[x] = 0;
  110         }
  111         return s;
  112 }
  113