xref: /original-bsd/contrib/ed/l.c (revision 58272d20)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rodney Ruddock of the University of Guelph.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)l.c	5.5 (Berkeley) 03/08/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 
17 #include <regex.h>
18 #include <setjmp.h>
19 #include <stdio.h>
20 #include <string.h>
21 
22 #ifdef DBI
23 #include <db.h>
24 #endif
25 
26 #include "ed.h"
27 #include "extern.h"
28 
29 /*
30  * This is the list command. It's not wrapped in with n and p because
31  * of the unambiguous printing need.
32  */
33 void
34 l(inputt, errnum)
35 	FILE *inputt;
36 	int *errnum;
37 {
38 	int l_cnt, l_len = 1;
39 
40 	if (Start_default && End_default)
41 		Start = End = current;
42 	else
43 		if (Start_default)
44 			Start = End;
45 
46 	if (Start == NULL) {
47 		strcpy(help_msg, "empty buffer");
48 		*errnum = -1;
49 		return;
50 	}
51 	Start_default = End_default = 0;
52 
53 	if (rol(inputt, errnum))	/* For "command-suffix pairs". */
54 		return;
55 
56 	current = Start;
57 	for (;;) {
58 		/*
59 		 * Print out the line character-by-character and split the
60 		 * line when line length is at line_length.
61 		 */
62 		if (current == NULL)
63 			break;
64 		get_line(current->handle, current->len);
65 		if (sigint_flag && (!sigspecial))
66 			SIGINT_ACTION;
67 		for (l_cnt = 0; l_cnt < current->len; l_cnt++, l_len += 2) {
68 			/* Check if line needs to be broken first. */
69 			if ((l_len) % line_length == 0)
70 				putchar('\n');
71 			else switch (text[l_cnt]) {
72 			case '\b':	/* backspace (cntl-H) */
73 				fwrite("\\b", sizeof(char), 2, stdout);
74 				break;
75 			case '\t':	/* horizontal tab */
76 				fwrite("\\t", sizeof(char), 2, stdout);
77 				break;
78 			case '\n':	/* newline (not that there is one). */
79 				fwrite("\\n", sizeof(char), 2, stdout);
80 				break;
81 			case '\v':	/* vertical tab */
82 				fwrite("\\v", sizeof(char), 2, stdout);
83 				break;
84 			case '\f':	/* form feed */
85 				fwrite("\\f", sizeof(char), 2, stdout);
86 				break;
87 			case '\r':	/* return */
88 				fwrite("\\r", sizeof(char), 2, stdout);
89 				break;
90 			default:
91 				if ((text[l_cnt] < 32) ||
92 				    (text[l_cnt] > 126)) {
93 					putchar('\\');
94 					putchar(text[l_cnt] / 64 + '0');
95 					putchar(text[l_cnt] / 8 + '0');
96 					putchar(text[l_cnt] % 8 + '0');
97 					l_len += 2;
98 				} else if (text[l_cnt] == '\\')
99 					fwrite("\\\\", sizeof(char), 2, stdout);
100 				else {
101 					l_len--;
102 					putchar(text[l_cnt]);
103 				}
104 				break;
105 			}
106 		}
107 		l_len = 1;
108 		putchar('\n');
109 		if (current == End)
110 			break;
111 		current = current->below;
112 	}
113 	*errnum = 1;
114 }
115