root/src/viewer/nroff.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mcview_nroff_get_char
  2. mcview__get_nroff_real_len
  3. mcview_nroff_seq_new_num
  4. mcview_nroff_seq_new
  5. mcview_nroff_seq_free
  6. mcview_nroff_seq_info
  7. mcview_nroff_seq_next
  8. mcview_nroff_seq_prev

   1 /*
   2    Internal file viewer for the Midnight Commander
   3    Functions for searching in nroff-like view
   4 
   5    Copyright (C) 1994-2024
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Miguel de Icaza, 1994, 1995, 1998
  10    Janne Kukonlehto, 1994, 1995
  11    Jakub Jelinek, 1995
  12    Joseph M. Hinkle, 1996
  13    Norbert Warmuth, 1997
  14    Pavel Machek, 1998
  15    Roland Illig <roland.illig@gmx.de>, 2004, 2005
  16    Slava Zanko <slavazanko@google.com>, 2009
  17    Andrew Borodin <aborodin@vmail.ru>, 2009
  18    Ilia Maslakov <il.smind@gmail.com>, 2009
  19 
  20    This file is part of the Midnight Commander.
  21 
  22    The Midnight Commander is free software: you can redistribute it
  23    and/or modify it under the terms of the GNU General Public License as
  24    published by the Free Software Foundation, either version 3 of the License,
  25    or (at your option) any later version.
  26 
  27    The Midnight Commander is distributed in the hope that it will be useful,
  28    but WITHOUT ANY WARRANTY; without even the implied warranty of
  29    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30    GNU General Public License for more details.
  31 
  32    You should have received a copy of the GNU General Public License
  33    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  34  */
  35 
  36 #include <config.h>
  37 
  38 #include "lib/global.h"
  39 #include "lib/tty/tty.h"
  40 #include "lib/skin.h"
  41 #ifdef HAVE_CHARSET
  42 #include "lib/charsets.h"
  43 #endif
  44 
  45 #include "internal.h"
  46 
  47 /*** global variables ****************************************************************************/
  48 
  49 /*** file scope macro definitions ****************************************************************/
  50 
  51 /*** file scope type declarations ****************************************************************/
  52 
  53 /*** forward declarations (file scope functions) *************************************************/
  54 
  55 /*** file scope variables ************************************************************************/
  56 
  57 /* --------------------------------------------------------------------------------------------- */
  58 /*** file scope functions ************************************************************************/
  59 /* --------------------------------------------------------------------------------------------- */
  60 
  61 static gboolean
  62 mcview_nroff_get_char (mcview_nroff_t *nroff, int *ret_val, off_t nroff_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
  63 {
  64     int c = 0;
  65 
  66 #ifdef HAVE_CHARSET
  67     if (nroff->view->utf8)
  68     {
  69         if (!mcview_get_utf (nroff->view, nroff_index, &c, &nroff->char_length))
  70         {
  71             /* we need got symbol in any case */
  72             nroff->char_length = 1;
  73             if (!mcview_get_byte (nroff->view, nroff_index, &c) || !g_ascii_isprint (c))
  74                 return FALSE;
  75         }
  76     }
  77     else
  78 #endif
  79     {
  80         nroff->char_length = 1;
  81         if (!mcview_get_byte (nroff->view, nroff_index, &c))
  82             return FALSE;
  83     }
  84 
  85     *ret_val = c;
  86 
  87     return g_unichar_isprint (c);
  88 }
  89 
  90 /* --------------------------------------------------------------------------------------------- */
  91 /*** public functions ****************************************************************************/
  92 /* --------------------------------------------------------------------------------------------- */
  93 
  94 int
  95 mcview__get_nroff_real_len (WView *view, off_t start, off_t length)
     /* [previous][next][first][last][top][bottom][index][help]  */
  96 {
  97     mcview_nroff_t *nroff;
  98     int ret = 0;
  99     off_t i = 0;
 100 
 101     if (!view->mode_flags.nroff)
 102         return 0;
 103 
 104     nroff = mcview_nroff_seq_new_num (view, start);
 105     if (nroff == NULL)
 106         return 0;
 107     while (i < length)
 108     {
 109         switch (nroff->type)
 110         {
 111         case NROFF_TYPE_BOLD:
 112             ret += 1 + nroff->char_length;      /* real char length and 0x8 */
 113             break;
 114         case NROFF_TYPE_UNDERLINE:
 115             ret += 2;           /* underline symbol and ox8 */
 116             break;
 117         default:
 118             break;
 119         }
 120         i += nroff->char_length;
 121         mcview_nroff_seq_next (nroff);
 122     }
 123 
 124     mcview_nroff_seq_free (&nroff);
 125     return ret;
 126 }
 127 
 128 /* --------------------------------------------------------------------------------------------- */
 129 
 130 mcview_nroff_t *
 131 mcview_nroff_seq_new_num (WView *view, off_t lc_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
 132 {
 133     mcview_nroff_t *nroff;
 134 
 135     nroff = g_try_malloc0 (sizeof (mcview_nroff_t));
 136     if (nroff != NULL)
 137     {
 138         nroff->index = lc_index;
 139         nroff->view = view;
 140         mcview_nroff_seq_info (nroff);
 141     }
 142     return nroff;
 143 }
 144 
 145 /* --------------------------------------------------------------------------------------------- */
 146 
 147 mcview_nroff_t *
 148 mcview_nroff_seq_new (WView *view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 149 {
 150     return mcview_nroff_seq_new_num (view, (off_t) 0);
 151 
 152 }
 153 
 154 /* --------------------------------------------------------------------------------------------- */
 155 
 156 void
 157 mcview_nroff_seq_free (mcview_nroff_t **nroff)
     /* [previous][next][first][last][top][bottom][index][help]  */
 158 {
 159     if (nroff == NULL || *nroff == NULL)
 160         return;
 161     MC_PTR_FREE (*nroff);
 162 }
 163 
 164 /* --------------------------------------------------------------------------------------------- */
 165 
 166 nroff_type_t
 167 mcview_nroff_seq_info (mcview_nroff_t *nroff)
     /* [previous][next][first][last][top][bottom][index][help]  */
 168 {
 169     int next, next2;
 170 
 171     if (nroff == NULL)
 172         return NROFF_TYPE_NONE;
 173     nroff->type = NROFF_TYPE_NONE;
 174 
 175     if (!mcview_nroff_get_char (nroff, &nroff->current_char, nroff->index))
 176         return nroff->type;
 177 
 178     if (!mcview_get_byte (nroff->view, nroff->index + nroff->char_length, &next) || next != '\b')
 179         return nroff->type;
 180 
 181     if (!mcview_nroff_get_char (nroff, &next2, nroff->index + 1 + nroff->char_length))
 182         return nroff->type;
 183 
 184     if (nroff->current_char == '_' && next2 == '_')
 185     {
 186         nroff->type = (nroff->prev_type == NROFF_TYPE_BOLD)
 187             ? NROFF_TYPE_BOLD : NROFF_TYPE_UNDERLINE;
 188 
 189     }
 190     else if (nroff->current_char == next2)
 191     {
 192         nroff->type = NROFF_TYPE_BOLD;
 193     }
 194     else if (nroff->current_char == '_')
 195     {
 196         nroff->current_char = next2;
 197         nroff->type = NROFF_TYPE_UNDERLINE;
 198     }
 199     else if (nroff->current_char == '+' && next2 == 'o')
 200     {
 201         /* ??? */
 202     }
 203     return nroff->type;
 204 }
 205 
 206 /* --------------------------------------------------------------------------------------------- */
 207 
 208 int
 209 mcview_nroff_seq_next (mcview_nroff_t *nroff)
     /* [previous][next][first][last][top][bottom][index][help]  */
 210 {
 211     if (nroff == NULL)
 212         return -1;
 213 
 214     nroff->prev_type = nroff->type;
 215 
 216     switch (nroff->type)
 217     {
 218     case NROFF_TYPE_BOLD:
 219         nroff->index += 1 + nroff->char_length;
 220         break;
 221     case NROFF_TYPE_UNDERLINE:
 222         nroff->index += 2;
 223         break;
 224     default:
 225         break;
 226     }
 227 
 228     nroff->index += nroff->char_length;
 229 
 230     mcview_nroff_seq_info (nroff);
 231     return nroff->current_char;
 232 }
 233 
 234 /* --------------------------------------------------------------------------------------------- */
 235 
 236 int
 237 mcview_nroff_seq_prev (mcview_nroff_t *nroff)
     /* [previous][next][first][last][top][bottom][index][help]  */
 238 {
 239     int prev;
 240     off_t prev_index, prev_index2;
 241 
 242     if (nroff == NULL)
 243         return -1;
 244 
 245     nroff->prev_type = NROFF_TYPE_NONE;
 246 
 247     if (nroff->index == 0)
 248         return -1;
 249 
 250     prev_index = nroff->index - 1;
 251 
 252     while (prev_index != 0)
 253     {
 254         if (mcview_nroff_get_char (nroff, &nroff->current_char, prev_index))
 255             break;
 256         prev_index--;
 257     }
 258     if (prev_index == 0)
 259     {
 260         nroff->index--;
 261         mcview_nroff_seq_info (nroff);
 262         return nroff->current_char;
 263     }
 264 
 265     prev_index--;
 266 
 267     if (!mcview_get_byte (nroff->view, prev_index, &prev) || prev != '\b')
 268     {
 269         nroff->index = prev_index;
 270         mcview_nroff_seq_info (nroff);
 271         return nroff->current_char;
 272     }
 273     prev_index2 = prev_index - 1;
 274 
 275     while (prev_index2 != 0)
 276     {
 277         if (mcview_nroff_get_char (nroff, &prev, prev_index))
 278             break;
 279         prev_index2--;
 280     }
 281 
 282     nroff->index = (prev_index2 == 0) ? prev_index : prev_index2;
 283     mcview_nroff_seq_info (nroff);
 284     return nroff->current_char;
 285 }
 286 
 287 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */