forked from jhenin/spiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.c
169 lines (146 loc) · 3.49 KB
/
line.c
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
/* Copyright (c) 1988 Bellcore
** All Rights Reserved
** Permission is granted to copy or use this program, EXCEPT that it
** may not be sold for profit, the copyright notice must be reproduced
** on copies, and credit should be given to Bellcore where it is due.
** BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
*/
#ifndef lint
static char rcsid[]= "$Header: line.c,v 1.1 88/09/15 11:34:00 daniel Rel $";
#endif
#include <stdio.h>
#include "misc.h"
#include "token.h"
#include "line.h"
char *_L_al[_L_MAXLINES]; /* storage for lines */
char *_L_bl[_L_MAXLINES];
int _L_ai[_L_MAXLINES]; /* index from token line number to first token */
int _L_bi[_L_MAXLINES];
int _L_ac[_L_MAXLINES]; /* count of tokens on this token line */
int _L_bc[_L_MAXLINES];
int _L_arlm; /* count of real lines in the file */
int _L_brlm;
int _L_aclm; /* count of content lines in the file */
int _L_bclm;
int _L_atlm; /* count of token lines in the file */
int _L_btlm;
int _L_aclindex[_L_MAXLINES]; /* mapping from content lines to real lines*/
int _L_bclindex[_L_MAXLINES];
int _L_atlindex[_L_MAXLINES]; /*mapping from token lines to content lines */
int _L_btlindex[_L_MAXLINES];
static void
_L_setrline(file,X,str)
int file;
int X;
char *str;
{
if (file)
{
S_savestr(&_L_bl[X],str);
}
else
{
S_savestr(&_L_al[X],str);
}
return;
}
/*
** returns 1 if we reached the end of file
** returns 0 if there is more to do
**
** stores data and sets maximum counts
*/
L_init_file(fnumber,fname)
int fnumber;
char *fname;
{
extern char *fgets();
FILE *fp;
static char buf[Z_LINELEN+2]; /* +2 is to leave room for us to add
a newline if we need to */
int ret_val = 1;
int tmplen;
if ((fp = fopen(fname,"r")) == (FILE*) NULL)
{
(void) sprintf(Z_err_buf, "Cannot open file %s.\n",fname);
Z_fatal(Z_err_buf);
}
/*
** clear the line count
*/
_L_setrlmx(fnumber,0);
/*
** read in the entire file
*/
while (fgets(buf,Z_LINELEN+1,fp) != (char *) NULL)
{
tmplen = strlen(buf);
if (tmplen <= 0)
{
(void) sprintf(Z_err_buf,
"fatal error -- got 0 length line %d in file %s\n",
L_getrlmax(fnumber)+1,
fname);
Z_fatal(Z_err_buf);
}
else if (tmplen > Z_LINELEN)
{
(void) sprintf(Z_err_buf,
"got fatally long line %d in file %s length is %d, must be a bug\n",
L_getrlmax(fnumber)+1,
fname,tmplen);
Z_fatal(Z_err_buf);
}
/*
** look for newline as last character
*/
if ('\n' != buf[tmplen-1])
{
/*
** did we run out room in the buffer?
*/
if (tmplen == Z_LINELEN)
{
(void) sprintf(Z_err_buf,
"line %d too long in file %s, newline added after %d characters\n",
L_getrlmax(fnumber)+1,
fname,Z_LINELEN);
Z_complain(Z_err_buf);
}
else
{
(void) sprintf(Z_err_buf,
"didn't find a newline at end of line %d in file %s, added one\n",
L_getrlmax(fnumber)+1,
fname);
Z_complain(Z_err_buf);
}
buf[tmplen] = '\n';
buf[tmplen+1] = '\0';
}
_L_setrline(fnumber,L_getrlmax(fnumber),buf);
if (L_getrlmax(fnumber) >= _L_MAXLINES-1)
{
(void) sprintf(Z_err_buf,
"warning -- ran out of space reading %s, truncated to %d lines\n",
fname,_L_MAXLINES);
Z_complain(Z_err_buf);
ret_val= 0;
break;
}
else
{
/*
** increment the line count
*/
_L_incrlmx(fnumber);
}
}
(void) fclose(fp);
/*
** reset line numbers
*/
L_setclmax(fnumber,0);
L_settlmax(fnumber,0);
return(ret_val);
}