Debian Codegraph @

libidu/fnprint.c

raw symbols SCIP
1 /* fnprint.c -- print a list of file names
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <alloca.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <pathmax.h>
26 #include <error.h>
27
28 #include "idfile.h"
29 #include "xnls.h"
30 #include "iduglobal.h"
31
32 static char const *root_name (char const *path);
33 static char const *suff_name (char const *path);
34 static int common_prefix_suffix (struct file_link const *flink_1,
35 struct file_link const *flink_2);
36
37 extern void usage (void);
38 static struct file_link *cw_dlink;
39
40 /* Return the file name with the suffix stripped off. */
41
42 static char const *
43 root_name (char const *file_name)
44 {
45 static char file_name_buffer[BUFSIZ];
46 char const *dot = strrchr (file_name, '.');
47
48 if (dot)
49 stzncpy (file_name_buffer, file_name, dot - file_name);
50 else
51 strcpy (file_name_buffer, file_name);
52 return file_name_buffer;
53 }
54
55 /* Return the suffix, including the dot, or an empty string if there
56 is no suffix. */
57
58 static char const *
59 suff_name (char const *file_name)
60 {
61 char const *dot = strrchr (file_name, '.');
62 return dot ? dot : "";
63 }
64
65 /* common_prefix_suffix returns non-zero if two file names have a
66 fully common directory prefix and a common suffix (i.e., they're
67 eligible for coalescing with brace notation. */
68
69 static int
70 common_prefix_suffix (struct file_link const *flink_1, struct file_link const *flink_2)
71 {
72 return (flink_1->fl_parent == flink_2->fl_parent
73 && strequ (suff_name (flink_1->fl_name), suff_name (flink_2->fl_name)));
74 }
75
76 void
77 print_filenames (struct file_link **flinkv, enum separator_style separator_style)
78 {
79 struct file_link const *arg;
80 struct file_link const *dlink;
81 int brace_is_open = 0;
82 char *file_name = alloca (sizeof(char) * PATH_MAX);
83
84 while (*flinkv)
85 {
86 arg = *flinkv++;
87 if (*flinkv && (separator_style == ss_braces)
88 && common_prefix_suffix (arg, *flinkv))
89 {
90 if (brace_is_open)
91 printf (",%s", root_name (arg->fl_name));
92 else
93 {
94 dlink = arg->fl_parent;
95 if (dlink && dlink != cw_dlink)
96 {
97 maybe_relative_file_name (file_name, dlink, cw_dlink);
98 fputs (file_name, stdout);
99 putchar ('/');
100 }
101 printf ("{%s", root_name (arg->fl_name));
102 }
103 brace_is_open = 1;
104 }
105 else
106 {
107 if (brace_is_open)
108 printf (",%s}%s", root_name (arg->fl_name), suff_name (arg->fl_name));
109 else
110 {
111 maybe_relative_file_name (file_name, arg, cw_dlink);
112 fputs (file_name, stdout);
113 }
114 brace_is_open = 0;
115 if (*flinkv)
116 {
117 if (separator_style == ss_newline)
118 putchar ('\n');
119 else
120 putchar (' ');
121 }
122 }
123 }
124 putchar ('\n');
125 }
126
127 enum separator_style
128 parse_separator_style (char const *arg)
129 {
130 MAYBE_RETURN_PREFIX_MATCH (arg, "braces", ss_braces);
131 MAYBE_RETURN_PREFIX_MATCH (arg, "space", ss_space);
132 MAYBE_RETURN_PREFIX_MATCH (arg, "newline", ss_newline);
133 error (0, 0, _("invalid `--separator' style: `%s'"), arg);
134 usage ();
135 return ss_bogus;
136 }