summaryrefslogtreecommitdiffstats
path: root/lib/sh/strvis.c
blob: 7a11d5799ff0014b7d4a08aed7a31bc6190f0a08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* strvis.c - make unsafe graphical characters in a string visible. */

/* Copyright (C) 2022 Free Software Foundation, Inc.

   This file is part of GNU Bash, the Bourne Again SHell.
   
   Bash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Bash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
*/

/* This is a stripped-down version suitable for the shell's use. */
#include <config.h>

#include <unistd.h>

#include "bashansi.h"
#include <stdio.h>

#include "chartypes.h"
#include "bashintl.h"
#include "shmbutil.h"

#define SAFECHAR(c)   ((c) == ' ' || (c) == '\t')

#ifndef RUBOUT
#define RUBOUT 0x7f
#endif

#ifndef CTRL_CHAR
#define CTRL_CHAR(c)	((c) < 0x20)
#endif

#ifndef META_CHAR
#define META_CHAR(c)	((c) > 0x7f && (c) <= UCHAR_MAX)
#endif

#ifndef UNCTRL
#define UNCTRL(c)	(TOUPPER ((c) | 0x40))
#endif

#ifndef UNMETA
#define UNMETA(c)	((c) & 0x7f)
#endif

int
sh_charvis (s, sindp, slen, ret, rindp)
     const char *s;
     size_t *sindp;
     size_t slen;
     char *ret;
     size_t *rindp;
{
  unsigned char c;
  size_t si, ri;
  const char *send;
  DECLARE_MBSTATE;

  si = *sindp;
  ri = *rindp;
  c = s[*sindp];

#if defined (HANDLE_MULTIBYTE)
  send = (locale_mb_cur_max > 1) ? s + slen : 0;
#else
  send = 0;
#endif

  if (SAFECHAR (c))
    {
      ret[ri++] = c;
      si++;
    }
  else if (c == RUBOUT)
    {
      ret[ri++] = '^';
      ret[ri++] = '?';
      si++;
    }
  else if (CTRL_CHAR (c))
    {
      ret[ri++] = '^';
      ret[ri++] = UNCTRL (c);
      si++;
    }
#if defined (HANDLE_MULTIBYTE)
  else if (locale_utf8locale && (c & 0x80))
    COPY_CHAR_I (ret, ri, s, send, si);
  else if (locale_mb_cur_max > 1 && is_basic (c) == 0)
    COPY_CHAR_I (ret, ri, s, send, si);
#endif
  else if (META_CHAR (c))
    {
      ret[ri++] = 'M';
      ret[ri++] = '-';
      ret[ri++] = UNMETA (c);
      si++;
    }
  else
    ret[ri++] = s[si++];
  
  *sindp = si;
  *rindp = ri;

  return si;    
}

/* Return a new string with `unsafe' non-graphical characters in S rendered
   in a visible way. */
char *
sh_strvis (string)
     const char *string;
{
  size_t slen, sind;
  char *ret;
  size_t retind, retsize;
  unsigned char c;
  DECLARE_MBSTATE;

  if (string == 0)
    return 0;
  if (*string == '\0')
    {
      if ((ret = (char *)malloc (1)) == 0)
	return 0;
      ret[0] = '\0';
      return ret;
    }

  slen = strlen (string);
  retsize = 3 * slen + 1;

  ret = (char *)malloc (retsize);
  if (ret == 0)
    return 0;

  retind = 0;
  sind = 0;

  while (string[sind])
    sind = sh_charvis (string, &sind, slen, ret, &retind);

  ret[retind] = '\0';
  return ret;
}