Debian Codegraph @

texk/web2c/cwebdir/examples/treeprint.w

raw
1 \def\covernote{Copyright 1987 Norman Ramsey -- Princeton University}
2
3 \def\vbar{\.{|}}
4 @*Directory Trees.
5 Our object is to print out a directory hierarchy in some pleasant way.
6 The program takes output from {\tt find * -type d -print \vbar\ sort}
7 @^system dependencies@>
8 and produces a nicer-looking listing.
9 More precisely, our input, which is the output of {\tt find} followed
10 by {\tt sort}, is a list of fully qualified directory names (parent
11 and child separated by slashes |'/'|); everything has already been
12 sorted nicely into lexicographic order.
13
14 The {\tt treeprint} routine takes one option, |"-p"|, which tells it
15 to use the printer's line-drawing set, rather than the terminal's.
16
17 @c
18 @<Global definitions@>@;
19 @<Global include files@>@;
20 @<Global declarations@>@;
21
22 @#
23 int main(
24 int argc,
25 char **argv)
26 {
27 @<|main| variable declarations@>;
28 @<Search for options and set special characters on |"-p"|@>;
29 @<Read output from find and enter into tree@>;
30 @<Write tree on standard output@>@;
31 exit(0);
32 }
33
34 @
35 We make all the siblings of a directory a linked list off of its left child,
36 and the offspring a linked list off the right side.
37 Data are just directory names.
38 @d sibling left
39 @d child right
40
41 @<Global decl...@>=
42 typedef struct tnode {
43 struct tnode *left, *right;
44 char *data;
45 } TNODE;
46 @ @<|main| variable...@>=
47 struct tnode *root=NULL;
48
49
50
51 @*Input.
52 Reading the tree is simple---we read one line at a time, and call on the
53 recursive |add_tree| procedure.
54
55 @c
56 void read_tree (
57 FILE *fp,
58 struct tnode **rootptr)
59 {
60 char buf[255], *p;
61
62 while ((fgets(buf, 255, fp))!=NULL) {
63 @<If |buf| contains a newline, make it end there@>;
64 add_tree(rootptr, buf);
65 }
66 }
67 @ @<Global include...@>=
68 #include <stdio.h>
69
70 @ Depending what system you're on, you may or may not get a newline in |buf|.
71 @<If |buf| contains a newline...@>=
72 p=buf; while (*p!='\0'&&*p!='\n') p++;
73 @^system dependencies@>
74 *p='\0';
75
76 @
77 To add a string, we split off the first part of the name and insert it into
78 the sibling list. We then do the rest of the string as a child of the new node.
79
80 @c
81 void add_tree(
82 struct tnode **rootptr,
83 char *p)
84 {
85 char *s;
86 int slashed;
87
88 if (*p=='\0') return;
89
90 @<Break up the string so |p| is the first word,
91 |s| points at null-begun remainder,
92 and |slashed| tells whether |*s=='/'| on entry@>;
93
94 if (*rootptr==NULL) {
95 @<Allocate new node to hold string of size |strlen(p)|@>;
96 strcpy((*rootptr)->data,p);
97 }
98 if (strcmp((*rootptr)->data,p)==0) {
99 if (slashed) ++s;
100 add_tree(&((*rootptr)->child),s);
101 }
102 else {
103 if (slashed) *s='/';
104 add_tree(&((*rootptr)->sibling),p);
105 }
106 }
107
108 @ We perform some nonsense to cut off the string |p| so that |p| just
109 holds the first word of a multiword name. Variable |s| points at what
110 was either the end of |p| or a slash delimiting names. In either case
111 |*s| is made |'\0'|. Later, depending on whether we want to pass the
112 whole string or the last piece, we will restore the slash or advance
113 |s| one character to the right.
114
115 @<Break up...@>=
116 for (s=p;*s!='\0'&&*s!='/';) s++;
117 if (*s=='/') {
118 slashed=1;
119 *s='\0';
120 } else slashed=0;
121
122 @ Node allocation is perfectly standard \dots
123 @<Allocate new node...@>=
124 *rootptr=(struct tnode *) malloc (sizeof(struct tnode));
125 (*rootptr)->left = (*rootptr)->right = NULL;
126 (*rootptr)->data = malloc (strlen(p)+1);
127
128 @
129 @<Global incl...@>=
130 #include <stdlib.h>
131 #include <string.h>
132
133 @ In this simple implementation, we just read from standard input.
134 @<Read...@>= read_tree(stdin,&root);
135
136 @*Output.
137 We begin by defining some lines, tees, and corners.
138 The |s| stands for screen and the |p| for printer.
139 You will have to change this for your line-drawing set.
140 @^system dependencies@>
141
142 @<Global definitions@>=
143 #define svert '|'
144 #define shoriz '-'
145 #define scross '+'
146 #define scorner '\\' /* lower left corner */
147
148 #define pvert '|'
149 #define phoriz '-'
150 #define pcross '+'
151 #define pcorner '\\' /* lower left corner */
152
153 @ The default is to use the terminal's line drawing set.
154 @<Global declarations@>=
155 char vert=svert;
156 char horiz=shoriz;
157 char cross=scross;
158 char corner=scorner;
159
160 @ With option |"-p"| use the printer character set.
161 @<Search for options...@>=
162 while (--argc>0) {
163 if (**++argv=='-') {
164 switch (*++(*argv)) {
165 case 'p':
166 vert=pvert;
167 horiz=phoriz;
168 cross=pcross;
169 corner=pcorner;
170 break;
171 default:
172 fprintf(stderr,"treeprint: bad option -%c\n",**argv);
173 break;
174 }
175 }
176 }
177
178 @ We play games with a character stack to figure out when to put in vertical
179 bars.
180 A vertical bar connects every sibling with its successor, but the last sibling
181 in a list is followed by blanks, not by vertical bars. The state of
182 bar-ness or space-ness for each preceding sibling is recorded in the
183 |indent_string| variable, one character (bar or blank) per sibling.
184
185 @<Global decl...@>=
186 char indent_string[100]="";
187
188 @ Children get printed
189 before siblings.
190 We don't bother trying to bring children up to the same line as their parents,
191 because the \UNIX/ filenames are so long.
192
193 We define a predicate telling us when a sibling is the last in a series.
194 @d is_last(S) (S->sibling==NULL)
195
196 @c
197 void print_node(
198 FILE *fp,
199 char *indent_string,
200 struct tnode *node)
201 {
202 char string[255];
203 int i;
204 char *p, *is;
205
206 if (node==NULL) {
207 }
208 else {
209 *string='\0';
210 for (i=strlen(indent_string); i>0; i--)
211 strcat(string,@, " | ");
212 strcat(string,@t\ \ @> " +--");
213 @<Replace chars in |string| with chars from
214 line-drawing set and from |indent_string|@>;
215 fprintf(fp,"%s%s\n",string,node->data);
216
217 @#
218 /* Add vertical bar or space for this sibling (claim |*is=='\0'|) */
219 *is++ = (is_last(node) ? ' ' : vert);
220 *is='\0';
221
222 print_node(fp, indent_string, node->child); /* extended |indent_string| */
223 *--is='\0';
224 print_node(fp, indent_string, node->sibling); /* original |indent_string| */
225 }
226
227 }
228 @ For simplicity, we originally wrote connecting lines with |'|'|, |'+'|, and
229 |'-'|.
230 Now we replace those characters with appropriate characters from the
231 line-drawing set.
232 We take the early vertical bars and replace them with characters from
233 |indent_string|, and we replace the other characters appropriately.
234 We are sure to put a |corner|, not a |cross|, on the last sibling in
235 a group.
236 @<Replace chars...@>=
237 is=indent_string;
238 for (p=string; *p!='\0'; p++) switch(*p) {
239 case '|': *p=*is++; break;
240 case '+': *p=(is_last(node) ? corner : cross); break;
241 case '-': *p=horiz; break;
242 default: break;
243 }
244
245
246 @ For this simple implementation, we just write on standard output.
247
248 @<Write...@>= print_node(stdout, indent_string, root);
249
250 @ @<Global decl...@>=
251 void read_tree (
252 FILE *fp,
253 struct tnode **rootptr);
254
255 void print_node(
256 FILE *fp,
257 char *indent_string,
258 struct tnode *node);
259
260 void add_tree(
261 struct tnode **rootptr,
262 char *p);
263
264 @*Index.