source: trunk/minix/commands/simple/pr.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 12.0 KB
Line 
1/* pr - print files Author: Michiel Huisjes */
2
3/* Pr - print files
4 *
5 * Author: Michiel Huisjes.
6 * Modified: Jacob P. Bunschoten. (30 nov 87)
7 * When "columns" is not given and numbering is on:
8 * line numbers are correlated with input lines.
9 * (try pr [-1] -n file )
10 * tabs are accounted for.
11 * When numbering is turned on, width know this.
12 * automatic line-folding. -f to get the original program.
13 * backspaces are accounted for. -b to disable this.
14 * multi-column mode changed.
15 * header can be given and used.
16 * format changed may occur between printing of several files:
17 * pr -l30 file1 -w75 file2
18 *
19 * Modified: Rick Thomas. (Sept 12, 1988)
20 * added "-M" option to cover functionality of old "-n" option,
21 * and made "-n" option behavior compatible with system V.
22 *
23 * Usage: pr [+page] [-columns] [-h header] [-wwidth] [-llength] [-ntm] [files]
24 * -t : Do not print the 5 line header and trailer at the page.
25 * -n : Turn on line numbering.
26 * -M : Use "Minix" style line numbering -- Each page begins at
27 * a line number that is an even multiple of the page length.
28 * Like the listings in Appendix E of the book.
29 * +page : Start printing at page n.
30 * -columns : Print files in n-columns.
31 * -l length: Take the length of the page to be n instead of 66
32 * -h header: Take next argument as page header.
33 * -w width : Take the width of the page to be n instead of default 79
34 * -f : do not fold lines.
35 *
36 * Modified: Lars Fredriksen (Jan 19, 1990)
37 * fixed the program so that
38 * pr -n *.c
39 * would work. The clobal variable 'width' was decremented
40 * by NUM_WIDTH, for each file, resulting in width finally
41 * being so small that nothing was printed. Used the local
42 * variable 'w' for the width adjustment (in print())
43 *
44 * Modified: Kenneth J. Hendrickson (10 April 1991)
45 * date in header should be last modification date for files,
46 * and the current time for stdin.
47 *
48 * Modified: Kees J. Bot (5 October 1992)
49 * Use localtime(3) to get the date, it knows TZ.
50 */
51
52#include <sys/types.h>
53#include <sys/stat.h>
54#include <string.h>
55#include <time.h>
56#include <stdlib.h>
57#include <stdio.h>
58
59
60#define DEF_LENGTH 66
61#define DEF_WIDTH 79
62#define NUM_WIDTH 8
63#define TAB_WIDTH 8 /* fixed tab_width */
64
65/* Used to compute next (fixed) tabstop */
66#define TO_TAB(x) (( (x) + TAB_WIDTH ) & ~07 )
67
68typedef char BOOL;
69
70#define FALSE 0
71#define TRUE 1
72
73/* EAT: eat rest of input line */
74#define EAT(fp) while((c=getc(fp))!='\n' && c!=EOF)
75
76/* L_BUF: calculate address of pointer to char (string) used in format */
77#define L_BUF(i,j) * (char **) (line_buf + (i + j*length)*sizeof(char *))
78
79char *header;
80BOOL no_header;
81BOOL number = FALSE;
82BOOL minix_number = FALSE;
83BOOL ext_header_set = FALSE; /* external header given */
84BOOL back_space = TRUE; /* back space correction in line width */
85BOOL dont_fold = FALSE; /* original. If the line does not fit eat it. */
86short columns;
87short cwidth;
88short start_page = 1;
89short width = DEF_WIDTH;
90short length = DEF_LENGTH;
91short linenr;
92char *line_buf; /* used in format for multi-column output */
93
94char output[1024];
95
96_PROTOTYPE(int main, (int argc, char **argv));
97_PROTOTYPE(static char *myalloc, (size_t size));
98_PROTOTYPE(char skip_page, (int lines, int width, FILE * filep));
99_PROTOTYPE(void format, (FILE * filep));
100_PROTOTYPE(void print_page, (int pagenr, int maxcol));
101_PROTOTYPE(void print, (FILE * filep));
102_PROTOTYPE(void out_header, (int page));
103_PROTOTYPE(void print_time, (time_t t));
104
105int main(argc, argv)
106int argc;
107char *argv[];
108{
109 FILE *file;
110 char *ptr;
111 int index = 1; /* index is one ahead of argc */
112 int line, col;
113
114 setbuf(stdout, output);
115 do {
116 if (argc == index) /* No arguments (left) */
117 goto pr_files;
118
119 ptr = argv[index++];
120 if (*ptr == '+') {
121 start_page = atoi(++ptr);
122 continue;
123 }
124 if (*ptr != '-') { /* no flags */
125 index--;
126 goto pr_files;
127 }
128 if (*++ptr >= '0' && *ptr <= '9') {
129 columns = atoi(ptr);
130 if (columns <= 0) columns = 1;
131 continue; /* Fetch next flag */
132 }
133 while (*ptr) switch (*ptr++) {
134 case 't': no_header = TRUE; break;
135 case 'n':
136 number = TRUE;
137 minix_number = FALSE;
138 break;
139 case 'M':
140 number = TRUE;
141 minix_number = TRUE;
142 break;
143 case 'h':
144 header = argv[index++];
145 ext_header_set = TRUE;
146 break;
147 case 'w':
148 if ((width = atoi(ptr)) <= 0) width = DEF_WIDTH;
149 *ptr = '\0';
150 break;
151 case 'l':
152 if ((length = atoi(ptr)) <= 0) length = DEF_LENGTH;
153 *ptr = '\0';
154 break;
155 case 'b': /* back_space correction off */
156 back_space = FALSE;
157 break;
158 case 'f': /* do not fold lines */
159 dont_fold = TRUE;
160 break;
161 default:
162 fprintf(stderr, "Usage: %s [+page] [-columns] [-h header] [-w<width>] [-l<length>] [-nMt] [files]\n", argv[0]);
163 exit(1);
164 }
165 continue; /* Scan for next flags */
166
167
168 /* ============== flags are read. Print the file(s) ========= */
169
170pr_files:
171
172 if (!no_header) length -= 10;
173
174 if (columns) {
175 cwidth = width / columns + 1;
176 if (columns > width) {
177 fprintf(stderr, "Too many columns for page width.\n");
178 exit(1);
179 }
180
181 /* Allocate piece of mem to hold some pointers */
182 line_buf = myalloc(length * columns * sizeof(char *));
183 }
184 for (line = 0; line < length; line++)
185 for (col = 0; col < columns; col++)
186 L_BUF(line, col) = NULL;
187
188 if (length <= 0) {
189 fprintf(stderr, "Minimal length should be %d\n", no_header ?
190 1 : 11);
191 exit(1);
192 }
193 while (index <= argc) { /* print all files, including stdin */
194 if (index < argc && (*argv[index] == '-' || *argv[index] == '+'))
195 break; /* Format change */
196
197 if (argc == index) { /* no file specified, so stdin */
198 if (!ext_header_set) header = "";
199 file = stdin;
200 } else {
201 if ((file = fopen(argv[index], "r")) == (FILE *) 0) {
202 fprintf(stderr, "Cannot open %s\n", argv[index++]);
203 continue;
204 }
205 if (!ext_header_set) header = argv[index];
206 }
207 if (columns)
208 format(file);
209 else
210 print(file);
211 fclose(file);
212 if (++index >= argc)
213 break; /* all files (including stdin) done */
214 }
215 if (index >= argc) break;
216 /* When control comes here. format changes are to be done.
217 * reinitialize some variables */
218 if (!no_header) length += 10;
219
220 start_page = 1;
221 ext_header_set = FALSE;
222 if (columns) free(line_buf);
223 } while (index <= argc); /* "pr -l60" should work too */
224
225 (void) fflush(stdout);
226 return(0);
227}
228
229char skip_page(lines, width, filep)
230int lines, width;
231FILE *filep;
232{
233 short c;
234 int char_cnt;
235 int w;
236
237 do {
238 w = width;
239 if (number) /* first lines are shorter */
240 if (!columns || /* called from print(file) */
241 !(lines % columns)) /* called from format(file) */
242 w -= NUM_WIDTH;
243
244 char_cnt = 0;
245 while ((c = getc(filep)) != '\n' && c != EOF && char_cnt < w) {
246 /* Calculate if this line is longer than "width (w)"
247 * characters */
248 if (c == '\b' && back_space) {
249 if (--char_cnt < 0) char_cnt = 0;
250 } else if (c == '\t')
251 char_cnt = TO_TAB(char_cnt);
252 else
253 char_cnt++;
254 }
255 if (dont_fold && c != '\n' && c != EOF) EAT(filep);
256 lines--;
257 if (c == '\n') linenr++;
258 } while (lines > 0 && c != EOF);
259
260 return c; /* last char read */
261}
262
263void format(filep)
264FILE *filep;
265{
266 char buf[512];
267 short c = '\0';
268 short index, lines, i;
269 short page_number = 0;
270 short maxcol = columns;
271 short wdth;
272 short line, col;
273
274 do {
275 /* Check printing of page */
276 page_number++;
277
278 if (page_number < start_page && c != EOF) {
279 c = (char) skip_page(columns * length, cwidth, filep);
280 continue;
281 }
282 if (c == EOF) return;
283
284 lines = columns * length;
285 for (line = 0; line < length; line++)
286 for (col = 0; col < columns; col++) {
287 if (L_BUF(line, col) != NULL)
288 free(L_BUF(line, col));
289 L_BUF(line, col) = (char *) NULL;
290 }
291 line = 0;
292 col = 0;
293 do {
294 index = 0;
295 wdth = cwidth - 1;
296 if (number && !col) /* need room for numbers */
297 wdth -= NUM_WIDTH;
298
299 /* Intermidiate colums are shortened by 1 char */
300 /* Last column not */
301 if (col + 1 == columns) wdth++;
302 for (i = 0; i < wdth - 1; i++) {
303 c = getc(filep);
304 if (c == '\n' || c == EOF) break;
305
306 if (c == '\b' && back_space) {
307 buf[index++] = '\b';
308 if (--i < 0) { /* just in case ... */
309 i = 0;
310 index = 0;
311 }
312 } else if (c == '\t') {
313 int cnt, max;
314
315 max = TO_TAB(i);
316 for (cnt = i; cnt < max; cnt++)
317 buf[index++] = ' ';
318 i = max - 1;
319 } else
320 buf[index++] = (char) c;
321 }
322 buf[index++] = '\0';
323 /* Collected enough chars (or eoln, or EOF) */
324
325 /* First char is EOF */
326 if (i == 0 && lines == columns * length && c == EOF) return;
327
328 /* Alloc mem to hold this (sub) string */
329 L_BUF(line, col) = myalloc(index * sizeof(char));
330 strcpy(L_BUF(line, col), buf);
331
332 line++;
333 line %= length;
334 if (line == 0) {
335 col++;
336 col %= columns;
337 }
338 if (dont_fold && c != '\n' && c != EOF) EAT(filep);
339 lines--; /* line ready for output */
340 if (c == EOF) {
341 maxcol = columns - lines / length;
342 }
343 } while (c != EOF && lines);
344 print_page(page_number, maxcol);
345 } while (c != EOF);
346}
347
348void print_page(pagenr, maxcol)
349short pagenr, maxcol;
350{
351 short pad, i, j;
352 short width;
353 char *p;
354
355 if (minix_number)
356 linenr = (pagenr - 1) * length + 1;
357 else
358 linenr = 1;
359
360 if (!no_header) out_header(pagenr);
361
362 for (i = 0; i < length; i++) {
363 for (j = 0; j < maxcol; j++) {
364 width = cwidth;
365 if (number && j == 0) { /* first columns */
366 printf("%7.7d ", linenr++); /* 7 == NUM_WIDTH-1 */
367 width -= NUM_WIDTH;
368 }
369 pad = 0;
370 if (p = (char *) L_BUF(i, j))
371 for (; pad < width - 1 && *p; pad++) putchar(*p++);
372 if (j < maxcol - 1) while (pad++ < width - 1)
373 putchar(' ');
374 }
375 putchar('\n');
376 }
377 if (!no_header) printf("\n\n\n\n\n");
378}
379
380void print(filep)
381FILE *filep;
382{
383 short c = '\0';
384 short page_number = 0;
385 short lines;
386 short cnt;
387 short w = width;
388 BOOL pr_number = TRUE; /* only real lines are numbered, not folded
389 * parts */
390
391 linenr = 1;
392 if (number) w -= NUM_WIDTH;
393
394 do {
395 /* Check printing of page */
396 page_number++;
397
398 if (page_number < start_page && c != EOF) {
399 pr_number = FALSE;
400 c = skip_page(length, w, filep);
401 if (c == '\n') pr_number = TRUE;
402 continue;
403 }
404 if (c == EOF) return;
405
406 if (minix_number) linenr = (page_number - 1) * length + 1;
407
408 if (page_number == start_page) c = getc(filep);
409
410 /* Print the page */
411 lines = length;
412 while (lines && c != EOF) {
413 if (lines == length && !no_header) out_header(page_number);
414 if (number)
415 if (pr_number)
416 printf("%7.7d ", linenr++); /* 7 == NUM_WIDTH-1 */
417 else
418 printf("%7c ", ' '); /* 7 == NUM_WIDTH-1 */
419 pr_number = FALSE;
420 cnt = 0;
421 while (c != '\n' && c != EOF && cnt < w) {
422 if (c == '\t') {
423 int i, max;
424 max = TO_TAB(cnt);
425 for (i = cnt; i < max; i++) putchar(' ');
426 cnt = max - 1;
427 } else if (c == '\b' && back_space) {
428 putchar('\b');
429 cnt--;
430 } else
431 putchar(c);
432 c = getc(filep);
433 cnt++;
434 }
435 putchar('\n');
436 if (dont_fold && c != '\n' && c != EOF) EAT(filep);
437 lines--;
438 if (c == '\n') {
439 c = getc(filep);
440 pr_number = TRUE;
441 }
442 }
443 if (lines == length) /* We never printed anything on this
444 * page -- */
445 return; /* even the header, so dont try to fill it up */
446 if (!no_header) /* print the trailer -- 5 blank lines */
447 printf("\n\n\n\n\n");
448 } while (c != EOF);
449
450 /* Fill last page */
451 if (page_number >= start_page) {
452 while (lines--) putchar('\n');
453 }
454}
455
456static char *myalloc(size)
457size_t size; /* How many bytes */
458{
459 void *ptr;
460
461 ptr = malloc(size);
462 if (ptr == NULL) {
463 fprintf(stderr, "malloc returned NULL\n");
464 exit(1);
465 }
466 return(char *) ptr;
467}
468
469void out_header(page)
470short page;
471{
472 time_t t;
473 struct stat buf;
474
475 if (strlen(header)) {
476 stat(header, &buf); /* use last modify time for file */
477 t = buf.st_mtime;
478 } else
479 (void) time(&t); /* use current time for stdin */
480 print_time(t);
481 printf(" %s Page %d\n\n\n", header, page);
482}
483
484char *moname[] = {
485 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
486 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
487};
488
489/* Print the date. */
490void print_time(t)
491time_t t;
492{
493 struct tm *tm;
494
495 tm = localtime(&t);
496
497 printf("\n\n%s %2d %2d:%02d %d",
498 moname[tm->tm_mon],
499 tm->tm_mday,
500 tm->tm_hour,
501 tm->tm_min,
502 1900 + tm->tm_year
503 );
504}
Note: See TracBrowser for help on using the repository browser.