Repository navigation
Expand file tree
/
Copy pathfmt.c
More file actions
266 lines (224 loc) · 4.96 KB
/
Copy pathfmt.c
File metadata and controls
266 lines (224 loc) · 4.96 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* fmt.c */
/* usage: fmt [-width] [files]...
*
* Fmt rearrages text in order to make each line have roughly the
* same width. Indentation and word spacing is preserved.
*
* The default width is 72 characters, but you can override that via -width.
* If no files are given on the command line, then it reads stdin.
*/
#include <stdio.h>
#ifndef TRUE
# define TRUE 1
# define FALSE 0
#endif
int width = 72; /* the desired line width */
int isblank; /* is the current output line blank? */
int indent; /* width of the indentation */
char ind[512]; /* indentation text */
char word[1024]; /* word buffer */
/* This function displays a usage message and quits */
void usage()
{
fprintf(stderr, "usage: fmt [-width] [files]...\n");
exit(2);
}
/* This function outputs a single word. It takes care of spacing and the
* newlines within a paragraph.
*/
void putword()
{
int i; /* index into word[], or whatever */
int ww; /* width of the word */
int sw; /* width of spacing after word */
static int psw; /* space width of previous word */
static int tab; /* the width of text already written */
/* separate the word and its spacing */
for (ww = 0; word[ww] && word[ww] != ' '; ww++)
{
}
sw = strlen(word) - ww;
word[ww] = '\0';
/* if no spacing (that is, the word was at the end of the line) then
* assume 1 space unless the last char of the word was punctuation
*/
if (sw == 0)
{
sw = 1;
if (word[ww - 1] == '.' || word[ww - 1] == '?' || word[ww - 1] == '!')
sw = 2;
}
/* if this is the first word on the line... */
if (isblank)
{
/* output the indentation first */
fputs(ind, stdout);
tab = indent;
}
else /* text has already been written to this output line */
{
/* will the word fit on this line? */
if (psw + ww + tab <= width)
{
/* yes - so write the previous word's spacing */
for (i = 0; i < psw; i++)
{
putchar(' ');
}
tab += psw;
}
else
{
/* no, so write a newline and the indentation */
putchar('\n');
fputs(ind, stdout);
tab = indent;
}
}
/* write the word itself */
fputs(word, stdout);
tab += ww;
/* remember this word's spacing */
psw = sw;
/* this output line isn't blank anymore. */
isblank = FALSE;
}
/* This function reformats text. */
void fmt(in)
FILE *in; /* the name of the input stream */
{
int ch; /* character from input stream */
int prevch; /* the previous character in the loop */
int i; /* index into ind[] or word[] */
int inword; /* boolean: are we between indent & newline? */
/* for each character in the stream... */
for (indent = -1, isblank = TRUE, inword = FALSE, i = 0, prevch = '\n';
(ch = getc(in)) != EOF;
prevch = ch)
{
/* is this the end of a line? */
if (ch == '\n')
{
/* if end of last word in the input line */
if (inword)
{
/* if it really is a word */
if (i > 0)
{
/* output it */
word[i] = '\0';
putword();
}
}
else /* blank line in input */
{
/* finish the previous paragraph */
if (!isblank)
{
putchar('\n');
isblank = TRUE;
}
/* output a blank line */
putchar('\n');
}
/* continue with next input line... */
indent = -1;
i = 0;
inword = FALSE;
continue;
}
/* if we're expecting indentation now... */
if (indent < 0)
{
/* if this is part of the indentation... */
if (ch == ' ' || ch == '\t')
{
/* remember it */
ind[i++] = ch;
}
else /* end of indentation */
{
/* mark the end of the indentation string */
ind[i] = '\0';
/* calculate the width of the indentation */
for (i = indent = 0; ind[i]; i++)
{
if (ind[i] == '\t')
indent = (indent | 7) + 1;
else
indent++;
}
/* reset the word index */
i = 0;
/* reprocess that last character */
ungetc(ch, in);
}
/* continue in the for-loop */
continue;
}
/* if we get here, we're either in a word or in the space
* after a word.
*/
inword = TRUE;
/* is this the start of a new word? */
if (ch != ' ' && prevch == ' ')
{
/* yes! output the previous word */
word[i] = '\0';
putword();
/* reset `i' to the start of the word[] buffer */
i = 0;
}
word[i++] = ch;
}
/* if necessary, write a final newline */
if (!isblank)
{
putchar('\n');
isblank = TRUE;
}
}
int main(argc, argv)
int argc;
char **argv;
{
FILE *in; /* an input stream */
int error; /* if non-zero, then an error occurred */
int i;
/* handle the -width flag, if given */
if (argc > 1 && argv[1][0] == '-')
{
width = atoi(argv[1] + 1);
if (width <= 0)
{
usage();
}
argc--;
argv++;
}
/* if no filenames given, then process stdin */
if (argc == 1)
{
fmt(stdin);
}
else /* one or more filenames given */
{
for (error = 0, i = 1; i < argc; i++)
{
in = fopen(argv[i], "r");
if (!in)
{
perror(argv[i]);
error = 3;
}
else
{
fmt(in);
fclose(in);
}
}
}
/* exit, possibly indicating an error */
exit(error);
/*NOTREACHED*/
}