(root)/
texinfo-7.1/
tp/
Texinfo/
XS/
parsetexi/
debug.c
       1  /* Copyright 2014-2023 Free Software Foundation, Inc.
       2  
       3     This program is free software: you can redistribute it and/or modify
       4     it under the terms of the GNU General Public License as published by
       5     the Free Software Foundation, either version 3 of the License, or
       6     (at your option) any later version.
       7  
       8     This program is distributed in the hope that it will be useful,
       9     but WITHOUT ANY WARRANTY; without even the implied warranty of
      10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      11     GNU General Public License for more details.
      12  
      13     You should have received a copy of the GNU General Public License
      14     along with this program.  If not, see <http://www.gnu.org/licenses/>. */
      15  
      16  #include <config.h>
      17  #include <stdarg.h>
      18  #include <stdio.h>
      19  #include <string.h>
      20  
      21  #include "debug.h"
      22  #include "commands.h"
      23  
      24  /* Whether to dump debugging output on stderr. */
      25  int debug_output = 0;
      26  
      27  void
      28  debug (char *s, ...)
      29  {
      30    va_list v;
      31  
      32    if (!debug_output)
      33      return;
      34    va_start (v, s);
      35    vfprintf (stderr, s, v);
      36    fputc ('\n', stderr);
      37  }
      38  
      39  void
      40  debug_nonl (char *s, ...)
      41  {
      42    va_list v;
      43  
      44    if (!debug_output)
      45      return;
      46    va_start (v, s);
      47    vfprintf (stderr, s, v);
      48  }
      49  
      50  char *
      51  debug_command_name (enum command_id cmd)
      52  {
      53    if (cmd == CM_TAB)
      54      return "\\t";
      55    else if (cmd == CM_NEWLINE)
      56      return "\\n";
      57    else
      58      return command_name(cmd);
      59  }
      60  
      61  char *
      62  debug_protect_eol (char *input_string, int *allocated)
      63  {
      64    char *end_of_line = strchr (input_string, '\n');
      65    char *protected_string = input_string;
      66    *allocated = 0;
      67    if (end_of_line) {
      68      char *p;
      69      protected_string = malloc ((strlen(input_string) + 2) * sizeof(char));
      70      *allocated = 1;
      71      memcpy (protected_string, input_string, strlen(input_string));
      72      p = protected_string + (end_of_line - input_string);
      73      *p = '\\';
      74      *(p+1) = 'n';
      75      *(p+2) = '\0';
      76    }
      77    return protected_string;
      78  }
      79  
      80  char *
      81  print_element_debug (ELEMENT *e, int print_parent)
      82  {
      83    TEXT text;
      84    char *result;
      85  
      86    text_init (&text);
      87    text_append (&text, "");
      88    if (e->cmd)
      89      text_printf (&text, "@%s", debug_command_name(e->cmd));
      90    if (e->type)
      91      text_printf (&text, "(%s)", element_type_names[e->type]);
      92    if (e->text.end > 0)
      93      {
      94        int allocated = 0;
      95        char *element_text = debug_protect_eol (e->text.text, &allocated);
      96        text_printf (&text, "[T: %s]", element_text);
      97        if (allocated)
      98          free (element_text);
      99      }
     100    if (e->args.number)
     101      text_printf (&text, "[A%d]", e->args.number);
     102    if (e->contents.number)
     103      text_printf (&text, "[C%d]", e->contents.number);
     104    if (print_parent && e->parent)
     105      {
     106        text_append (&text, " <- ");
     107        if (e->parent->cmd)
     108          text_printf (&text, "@%s", command_name(e->parent->cmd));
     109        if (e->parent->type)
     110          text_printf (&text, "(%s)", element_type_names[e->parent->type]);
     111      }
     112    result = strdup (text.text);
     113    free (text.text);
     114    return result;
     115  }
     116  
     117  void
     118  debug_print_element (ELEMENT *e, int print_parent)
     119  {
     120    if (debug_output)
     121      {
     122        char *result;
     123        result = print_element_debug (e, print_parent);
     124        fputs (result, stderr);
     125        free (result);
     126      }
     127  }
     128  
     129  void
     130  debug_print_protected_string (char *input_string)
     131  {
     132    if (debug_output)
     133      {
     134        int allocated = 0;
     135        char *result;
     136        if (!input_string)
     137          result = "[NULL]";
     138        else
     139          result = debug_protect_eol (input_string, &allocated);
     140        fputs (result, stderr);
     141        if (allocated)
     142          free (result);
     143      }
     144  }
     145