-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindent.c
More file actions
executable file
·219 lines (198 loc) · 5.43 KB
/
indent.c
File metadata and controls
executable file
·219 lines (198 loc) · 5.43 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
/****************************************************************************
* indent.c
* Author Joel Welling
* Copyright 1989, Pittsburgh Supercomputing Center, Carnegie Mellon University
*
* Permission use, copy, and modify this software and its documentation
* without fee for personal use or use within your organization is hereby
* granted, provided that the above copyright notice is preserved in all
* copies and that that copyright and this permission notice appear in
* supporting documentation. Permission to redistribute this software to
* other organizations or individuals is not granted; that must be
* negotiated with the PSC. Neither the PSC nor Carnegie Mellon
* University make any representations about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*****************************************************************************/
/*
This package provides an 'indented writing' capability.
*/
/* If __STDC__ is defined, this package uses stdarg.h-type variable length
* parameter lists. If not, and if USE_VARARGS is defined (it's set below),
* varargs.h-type parameter lists are used. Otherwise, a system which
* 'usually' works is used.
*/
#define USE_VARARGS 1
#include <stdio.h>
#ifdef SUN_SPARC
#include <varargs.h>
#else
#ifdef __STDC__
#include <stdarg.h>
#else
#ifdef USE_VARARGS
#include <varargs.h>
#endif
#endif
#endif
#include "ge_error.h"
#include "indent.h"
/*
Potential bug: no length checking is done on strings written to convbuf,
so overflows are possible.
*/
#define convbufsize 512
#define maxline 129
#define tabstep 3
static char buffer[maxline], convbuf[convbufsize],
*startpoint, *inpoint, *endpoint;
void ind_setup()
/* This routine initializes the indentation package */
{
startpoint= buffer;
inpoint= startpoint;
endpoint= buffer+maxline-2;
}
void ind_push()
/* This routine advances the start point */
{
if ( startpoint < endpoint - tabstep )
startpoint= startpoint + tabstep;
else
ger_error("ind_push: too far right to indent further");
ind_write(" ");
inpoint= startpoint;
}
void ind_pop()
/* This routine retreates the start point */
{
if ( startpoint >= buffer + tabstep - 1)
startpoint= startpoint - tabstep;
else
ger_error("ind_pop: this pop doesn't match an indent push");
inpoint= startpoint;
}
#ifdef __STDC__
void ind_write(char *string, ...)
/* This routine adds text to the line buffer. Alas, at the moment it
* does not support the full functionality of sprintf. See the very similar
* routine in K&R C Second Edition, section 7.3, for a functional description.
*/
{
va_list ap;
char *p, *sval;
int ival;
double dval;
char cval;
va_start( ap, string );
for (p = string; *p; p++) {
if (*p != '%') {
if (inpoint<endpoint) *inpoint++= *p;
continue;
}
switch (*++p) {
case 'd':
ival= va_arg(ap,int);
sprintf(convbuf,"%d",ival);
break;
case 'f':
dval= va_arg(ap,double);
sprintf(convbuf,"%f",dval);
break;
case 's':
sval= va_arg(ap,char*);
strncpy( convbuf, sval, convbufsize-1 );
convbuf[convbufsize-1]= '\0';
break;
case 'c':
cval= va_arg(ap,int);
sprintf(convbuf,"%c",cval);
break;
default:
convbuf[0]= *p;
convbuf[1]= '\0';
break;
}
if (inpoint<endpoint) {
(void)strncpy( inpoint, convbuf, (int)(endpoint-inpoint+1) );
inpoint= inpoint + strlen(inpoint);
}
}
va_end(ap);
}
#else
#ifdef USE_VARARGS
void ind_write(string, va_alist)
char *string;
va_dcl /* declare the va_list parameter */
/* This routine adds text to the line buffer. Alas, at the moment it
* does not support the full functionality of sprintf.
*/
{
va_list ap;
char *p, *sval;
int ival;
double dval;
char cval;
va_start( ap );
for (p = string; *p; p++) {
if (*p != '%') {
if (inpoint<endpoint) *inpoint++= *p;
continue;
}
switch (*++p) {
case 'd':
ival= va_arg(ap,int);
sprintf(convbuf,"%d",ival);
break;
case 'f':
dval= va_arg(ap,double);
sprintf(convbuf,"%f",dval);
break;
case 's':
sval= va_arg(ap,char*);
strncpy( convbuf, sval, convbufsize-1 );
convbuf[convbufsize-1]= '\0';
break;
case 'c':
cval= va_arg(ap,char);
sprintf(convbuf,"%c",cval);
break;
default:
convbuf[0]= *p;
convbuf[1]= '\0';
break;
}
if (inpoint<endpoint) {
(void)strncpy( inpoint, convbuf, (int)(endpoint-inpoint+1) );
inpoint= inpoint + strlen(inpoint);
}
}
va_end(ap);
}
#else
void ind_write( string, p1, p2, p3, p4, p5, p6 )
char *string;
int p1, p2, p3, p4, p5, p6;
/* This routine adds text to the line buffer */
{
sprintf(convbuf, string, p1, p2, p3, p4, p5, p6 );
if (inpoint<endpoint) {
(void)strncpy( inpoint, convbuf, (int)(endpoint-inpoint+1) );
inpoint= inpoint + strlen(inpoint);
};
}
#endif
#endif
void ind_eol()
/*
This routine causes the line to be emitted, and moves back to the
start point for the beginning of the next line.
*/
{
register char *i;
fprintf(stdout,"%s\n",buffer);
for ( i=buffer; i<startpoint; i++) *i= ' ';
for ( i=startpoint; i<=endpoint+1; i++) *i= '\0';
inpoint= startpoint;
}