OpenCoverage

unexpand.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/unexpand.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* unexpand - convert blanks to tabs-
2 Copyright (C) 1989-2018 Free Software Foundation, Inc.-
3-
4 This program is free software: you can redistribute it and/or modify-
5 it under the terms of the GNU General Public License as published by-
6 the Free Software Foundation, either version 3 of the License, or-
7 (at your option) any later version.-
8-
9 This program is distributed in the hope that it will be useful,-
10 but WITHOUT ANY WARRANTY; without even the implied warranty of-
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
12 GNU General Public License for more details.-
13-
14 You should have received a copy of the GNU General Public License-
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
16-
17/* By default, convert only maximal strings of initial blanks and tabs-
18 into tabs.-
19 Preserves backspace characters in the output; they decrement the-
20 column count for tab calculations.-
21 The default action is equivalent to -8.-
22-
23 Options:-
24 --tabs=tab1[,tab2[,...]]-
25 -t tab1[,tab2[,...]]-
26 -tab1[,tab2[,...]] If only one tab stop is given, set the tabs tab1-
27 columns apart instead of the default 8. Otherwise,-
28 set the tabs at columns tab1, tab2, etc. (numbered from-
29 0); preserve any blanks beyond the tab stops given.-
30 --all-
31 -a Use tabs wherever they would replace 2 or more blanks,-
32 not just at the beginnings of lines.-
33-
34 David MacKenzie <djm@gnu.ai.mit.edu> */-
35-
36#include <config.h>-
37-
38#include <stdio.h>-
39#include <getopt.h>-
40#include <sys/types.h>-
41#include "system.h"-
42#include "die.h"-
43#include "xstrndup.h"-
44-
45#include "expand-common.h"-
46-
47/* The official name of this program (e.g., no 'g' prefix). */-
48#define PROGRAM_NAME "unexpand"-
49-
50#define AUTHORS proper_name ("David MacKenzie")-
51-
52-
53-
54/* For long options that have no equivalent short option, use a-
55 non-character as a pseudo short option, starting with CHAR_MAX + 1. */-
56enum-
57{-
58 CONVERT_FIRST_ONLY_OPTION = CHAR_MAX + 1-
59};-
60-
61static struct option const longopts[] =-
62{-
63 {"tabs", required_argument, NULL, 't'},-
64 {"all", no_argument, NULL, 'a'},-
65 {"first-only", no_argument, NULL, CONVERT_FIRST_ONLY_OPTION},-
66 {GETOPT_HELP_OPTION_DECL},-
67 {GETOPT_VERSION_OPTION_DECL},-
68 {NULL, 0, NULL, 0}-
69};-
70-
71void-
72usage (int status)-
73{-
74 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 6 times by 1 test
Evaluated by:
  • unexpand
3-6
75 emit_try_help ();
executed 3 times by 1 test: end of block
Executed by:
  • unexpand
3
76 else-
77 {-
78 printf (_("\-
79Usage: %s [OPTION]... [FILE]...\n\-
80"),-
81 program_name);-
82 fputs (_("\-
83Convert blanks in each FILE to tabs, writing to standard output.\n\-
84"), stdout);-
85-
86 emit_stdin_note ();-
87 emit_mandatory_arg_note ();-
88-
89 fputs (_("\-
90 -a, --all convert all blanks, instead of just initial blanks\n\-
91 --first-only convert only leading sequences of blanks (overrides -a)\n\-
92 -t, --tabs=N have tabs N characters apart instead of 8 (enables -a)\n\-
93"), stdout);-
94 emit_tab_list_info ();-
95 fputs (HELP_OPTION_DESCRIPTION, stdout);-
96 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
97 emit_ancillary_info (PROGRAM_NAME);-
98 }
executed 6 times by 1 test: end of block
Executed by:
  • unexpand
6
99 exit (status);
executed 9 times by 1 test: exit (status);
Executed by:
  • unexpand
9
100}-
101-
102/* Change blanks to tabs, writing to stdout.-
103 Read each file in 'file_list', in order. */-
104-
105static void-
106unexpand (void)-
107{-
108 /* Input stream. */-
109 FILE *fp = next_file (NULL);-
110-
111 /* The array of pending blanks. In non-POSIX locales, blanks can-
112 include characters other than spaces, so the blanks must be-
113 stored, not merely counted. */-
114 char *pending_blank;-
115-
116 if (!fp)
!fpDescription
TRUEnever evaluated
FALSEevaluated 59 times by 1 test
Evaluated by:
  • unexpand
0-59
117 return;
never executed: return;
0
118-
119 /* The worst case is a non-blank character, then one blank, then a-
120 tab stop, then MAX_COLUMN_WIDTH - 1 blanks, then a non-blank; so-
121 allocate MAX_COLUMN_WIDTH bytes to store the blanks. */-
122 pending_blank = xmalloc (max_column_width);-
123-
124 while (true)-
125 {-
126 /* Input character, or EOF. */-
127 int c;-
128-
129 /* If true, perform translations. */-
130 bool convert = true;-
131-
132-
133 /* The following variables have valid values only when CONVERT-
134 is true: */-
135-
136 /* Column of next input character. */-
137 uintmax_t column = 0;-
138-
139 /* Column the next input tab stop is on. */-
140 uintmax_t next_tab_column = 0;-
141-
142 /* Index in TAB_LIST of next tab stop to examine. */-
143 size_t tab_index = 0;-
144-
145 /* If true, the first pending blank came just before a tab stop. */-
146 bool one_blank_before_tab_stop = false;-
147-
148 /* If true, the previous input character was a blank. This is-
149 initially true, since initial strings of blanks are treated-
150 as if the line was preceded by a blank. */-
151 bool prev_blank = true;-
152-
153 /* Number of pending columns of blanks. */-
154 size_t pending = 0;-
155-
156-
157 /* Convert a line of text. */-
158-
159 do-
160 {-
161 while ((c = getc (fp)) < 0 && (fp = next_file (fp)))
(c = getc_unlocked (fp)) < 0Description
TRUEevaluated 59 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 443 times by 1 test
Evaluated by:
  • unexpand
(fp = next_file (fp))Description
TRUEnever evaluated
FALSEevaluated 59 times by 1 test
Evaluated by:
  • unexpand
0-443
162 continue;
never executed: continue;
0
163-
164 if (convert)
convertDescription
TRUEevaluated 444 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 58 times by 1 test
Evaluated by:
  • unexpand
58-444
165 {-
166 bool blank = !! isblank (c);-
167-
168 if (blank)
blankDescription
TRUEevaluated 220 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 224 times by 1 test
Evaluated by:
  • unexpand
220-224
169 {-
170 bool last_tab IF_LINT (=0);-
171-
172 next_tab_column = get_next_tab_column (column, &tab_index,-
173 &last_tab);-
174-
175 if (last_tab)
last_tabDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 211 times by 1 test
Evaluated by:
  • unexpand
9-211
176 convert = false;
executed 9 times by 1 test: convert = 0 ;
Executed by:
  • unexpand
9
177-
178 if (convert)
convertDescription
TRUEevaluated 211 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 9 times by 1 test
Evaluated by:
  • unexpand
9-211
179 {-
180 if (next_tab_column < column)
next_tab_column < columnDescription
TRUEnever evaluated
FALSEevaluated 211 times by 1 test
Evaluated by:
  • unexpand
0-211
181 die (EXIT_FAILURE, 0, _("input line is too long"));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, dcgettext (((void *)0), \"input line is too long\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, dcgettext (((void *)0), "input line is too long" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, dcgettext (((void *)0), "input line is too long" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
182-
183 if (c == '\t')
c == '\t'Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 199 times by 1 test
Evaluated by:
  • unexpand
12-199
184 {-
185 column = next_tab_column;-
186-
187 if (pending)
pendingDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 7 times by 1 test
Evaluated by:
  • unexpand
5-7
188 pending_blank[0] = '\t';
executed 5 times by 1 test: pending_blank[0] = '\t';
Executed by:
  • unexpand
5
189 }
executed 12 times by 1 test: end of block
Executed by:
  • unexpand
12
190 else-
191 {-
192 column++;-
193-
194 if (! (prev_blank && column == next_tab_column))
prev_blankDescription
TRUEevaluated 153 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 46 times by 1 test
Evaluated by:
  • unexpand
column == next_tab_columnDescription
TRUEevaluated 45 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 108 times by 1 test
Evaluated by:
  • unexpand
45-153
195 {-
196 /* It is not yet known whether the pending blanks-
197 will be replaced by tabs. */-
198 if (column == next_tab_column)
column == next_tab_columnDescription
TRUEevaluated 20 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 134 times by 1 test
Evaluated by:
  • unexpand
20-134
199 one_blank_before_tab_stop = true;
executed 20 times by 1 test: one_blank_before_tab_stop = 1 ;
Executed by:
  • unexpand
20
200 pending_blank[pending++] = c;-
201 prev_blank = true;-
202 continue;
executed 154 times by 1 test: continue;
Executed by:
  • unexpand
154
203 }-
204-
205 /* Replace the pending blanks by a tab or two. */-
206 pending_blank[0] = c = '\t';-
207 }
executed 45 times by 1 test: end of block
Executed by:
  • unexpand
45
208-
209 /* Discard pending blanks, unless it was a single-
210 blank just before the previous tab stop. */-
211 pending = one_blank_before_tab_stop;-
212 }
executed 57 times by 1 test: end of block
Executed by:
  • unexpand
57
213 }
executed 66 times by 1 test: end of block
Executed by:
  • unexpand
66
214 else if (c == '\b')
c == '\b'Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 221 times by 1 test
Evaluated by:
  • unexpand
3-221
215 {-
216 /* Go back one column, and force recalculation of the-
217 next tab stop. */-
218 column -= !!column;-
219 next_tab_column = column;-
220 tab_index -= !!tab_index;-
221 }
executed 3 times by 1 test: end of block
Executed by:
  • unexpand
3
222 else-
223 {-
224 column++;-
225 if (!column)
!columnDescription
TRUEnever evaluated
FALSEevaluated 221 times by 1 test
Evaluated by:
  • unexpand
0-221
226 die (EXIT_FAILURE, 0, _("input line is too long"));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, dcgettext (((void *)0), \"input line is too long\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, dcgettext (((void *)0), "input line is too long" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, dcgettext (((void *)0), "input line is too long" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
227 }
executed 221 times by 1 test: end of block
Executed by:
  • unexpand
221
228-
229 if (pending)
pendingDescription
TRUEevaluated 37 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 253 times by 1 test
Evaluated by:
  • unexpand
37-253
230 {-
231 if (pending > 1 && one_blank_before_tab_stop)
pending > 1Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 23 times by 1 test
Evaluated by:
  • unexpand
one_blank_before_tab_stopDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 12 times by 1 test
Evaluated by:
  • unexpand
2-23
232 pending_blank[0] = '\t';
executed 2 times by 1 test: pending_blank[0] = '\t';
Executed by:
  • unexpand
2
233 if (fwrite (pending_blank, 1, pending, stdout) != pending)
never executed: break;
(__extension__...))) != pendingDescription
TRUEnever evaluated
FALSEevaluated 37 times by 1 test
Evaluated by:
  • unexpand
(__builtin_exp...r++))) == (-1)Description
TRUEnever evaluated
FALSEnever evaluated
__cnt > 0Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_expe...write_end), 0)Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( 1 )Description
TRUEevaluated 37 times by 1 test
Evaluated by:
  • unexpand
FALSEnever evaluated
__builtin_cons..._p ( pending )Description
TRUEnever evaluated
FALSEevaluated 37 times by 1 test
Evaluated by:
  • unexpand
(size_t) ( 1 )...pending ) <= 8Description
TRUEnever evaluated
FALSEnever evaluated
(size_t) ( 1 ) != 0Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( 1 )Description
TRUEevaluated 37 times by 1 test
Evaluated by:
  • unexpand
FALSEnever evaluated
(size_t) ( 1 ) == 0Description
TRUEnever evaluated
FALSEevaluated 37 times by 1 test
Evaluated by:
  • unexpand
__builtin_cons..._p ( pending )Description
TRUEnever evaluated
FALSEevaluated 37 times by 1 test
Evaluated by:
  • unexpand
(size_t) ( pending ) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-37
234 die (EXIT_FAILURE, errno, _("write error"));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"write error\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "write error" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "write error" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
235 pending = 0;-
236 one_blank_before_tab_stop = false;-
237 }
executed 37 times by 1 test: end of block
Executed by:
  • unexpand
37
238-
239 prev_blank = blank;-
240 convert &= convert_entire_line || blank;
convert_entire_lineDescription
TRUEevaluated 264 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 26 times by 1 test
Evaluated by:
  • unexpand
blankDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 23 times by 1 test
Evaluated by:
  • unexpand
3-264
241 }
executed 290 times by 1 test: end of block
Executed by:
  • unexpand
290
242-
243 if (c < 0)
c < 0Description
TRUEevaluated 59 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 289 times by 1 test
Evaluated by:
  • unexpand
59-289
244 {-
245 free (pending_blank);-
246 return;
executed 59 times by 1 test: return;
Executed by:
  • unexpand
59
247 }-
248-
249 if (putchar (c) < 0)
putchar_unlocked (c) < 0Description
TRUEnever evaluated
FALSEevaluated 289 times by 1 test
Evaluated by:
  • unexpand
0-289
250 die (EXIT_FAILURE, errno, _("write error"));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"write error\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "write error" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "write error" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
251 }
executed 289 times by 1 test: end of block
Executed by:
  • unexpand
289
252 while (c != '\n');
c != '\n'Description
TRUEevaluated 396 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 47 times by 1 test
Evaluated by:
  • unexpand
47-396
253 }
executed 47 times by 1 test: end of block
Executed by:
  • unexpand
47
254}
never executed: end of block
0
255-
256int-
257main (int argc, char **argv)-
258{-
259 bool have_tabval = false;-
260 uintmax_t tabval IF_LINT ( = 0);-
261 int c;-
262-
263 /* If true, cancel the effect of any -a (explicit or implicit in -t),-
264 so that only leading blanks will be considered. */-
265 bool convert_first_only = false;-
266-
267 initialize_main (&argc, &argv);-
268 set_program_name (argv[0]);-
269 setlocale (LC_ALL, "");-
270 bindtextdomain (PACKAGE, LOCALEDIR);-
271 textdomain (PACKAGE);-
272-
273 atexit (close_stdout);-
274-
275 while ((c = getopt_long (argc, argv, ",0123456789at:", longopts, NULL))
(c = getopt_lo... *)0) )) != -1Description
TRUEevaluated 108 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 59 times by 1 test
Evaluated by:
  • unexpand
59-108
276 != -1)
(c = getopt_lo... *)0) )) != -1Description
TRUEevaluated 108 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 59 times by 1 test
Evaluated by:
  • unexpand
59-108
277 {-
278 switch (c)-
279 {-
280 case '?':
executed 3 times by 1 test: case '?':
Executed by:
  • unexpand
3
281 usage (EXIT_FAILURE);-
282 case 'a':
code before this statement never executed: case 'a':
executed 27 times by 1 test: case 'a':
Executed by:
  • unexpand
0-27
283 convert_entire_line = true;-
284 break;
executed 27 times by 1 test: break;
Executed by:
  • unexpand
27
285 case 't':
executed 31 times by 1 test: case 't':
Executed by:
  • unexpand
31
286 convert_entire_line = true;-
287 parse_tab_stops (optarg);-
288 break;
executed 27 times by 1 test: break;
Executed by:
  • unexpand
27
289 case CONVERT_FIRST_ONLY_OPTION:
executed 2 times by 1 test: case CONVERT_FIRST_ONLY_OPTION:
Executed by:
  • unexpand
2
290 convert_first_only = true;-
291 break;
executed 2 times by 1 test: break;
Executed by:
  • unexpand
2
292 case ',':
executed 3 times by 1 test: case ',':
Executed by:
  • unexpand
3
293 if (have_tabval)
have_tabvalDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • unexpand
FALSEnever evaluated
0-3
294 add_tab_stop (tabval);
executed 3 times by 1 test: add_tab_stop (tabval);
Executed by:
  • unexpand
3
295 have_tabval = false;-
296 break;
executed 3 times by 1 test: break;
Executed by:
  • unexpand
3
297 case_GETOPT_HELP_CHAR;
never executed: break;
executed 6 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • unexpand
0-6
298 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 4 times by 1 test: exit ( 0 );
Executed by:
  • unexpand
never executed: break;
executed 4 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • unexpand
0-4
299 default:
executed 32 times by 1 test: default:
Executed by:
  • unexpand
32
300 if (!have_tabval)
!have_tabvalDescription
TRUEevaluated 12 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 20 times by 1 test
Evaluated by:
  • unexpand
12-20
301 {-
302 tabval = 0;-
303 have_tabval = true;-
304 }
executed 12 times by 1 test: end of block
Executed by:
  • unexpand
12
305 if (!DECIMAL_DIGIT_ACCUMULATE (tabval, c - '0', uintmax_t))
!( (void) (&(t... '0')), 1 )) )Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 31 times by 1 test
Evaluated by:
  • unexpand
(uintmax_t) -1 / 10 < (tabval)Description
TRUEnever evaluated
FALSEevaluated 32 times by 1 test
Evaluated by:
  • unexpand
(uintmax_t) ((...')) < (tabval)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 31 times by 1 test
Evaluated by:
  • unexpand
0-32
306 die (EXIT_FAILURE, 0, _("tab stop value is too large"));
executed 1 time by 1 test: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, dcgettext (((void *)0), \"tab stop value is too large\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, dcgettext (((void *)0), "tab stop value is too large" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, dcgettext (((void *)0), "tab stop value is too large" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
Executed by:
  • unexpand
1
307 break;
executed 31 times by 1 test: break;
Executed by:
  • unexpand
31
308 }-
309 }-
310-
311 if (convert_first_only)
convert_first_onlyDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 58 times by 1 test
Evaluated by:
  • unexpand
1-58
312 convert_entire_line = false;
executed 1 time by 1 test: convert_entire_line = 0 ;
Executed by:
  • unexpand
1
313-
314 if (have_tabval)
have_tabvalDescription
TRUEevaluated 8 times by 1 test
Evaluated by:
  • unexpand
FALSEevaluated 51 times by 1 test
Evaluated by:
  • unexpand
8-51
315 add_tab_stop (tabval);
executed 8 times by 1 test: add_tab_stop (tabval);
Executed by:
  • unexpand
8
316-
317 finalize_tab_stops ();-
318-
319 set_file_list ( (optind < argc) ? &argv[optind] : NULL);-
320-
321 unexpand ();-
322-
323 cleanup_file_list_stdin ();-
324-
325 return exit_status;
executed 59 times by 1 test: return exit_status;
Executed by:
  • unexpand
59
326}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2