Debian Codegraph @

read.c

raw
1 /*
2 * $Id: read.c 769 2010-09-11 21:00:16Z dhiebert $
3 *
4 * Copyright (c) 1996-2002, Darren Hiebert
5 *
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
8 *
9 * This module contains low level source and tag file read functions (newline
10 * conversion for source files are performed at this level).
11 */
12
13 /*
14 * INCLUDE FILES
15 */
16 #include "general.h" /* must always come first */
17
18 #include <string.h>
19 #include <ctype.h>
20
21 #define FILE_WRITE
22 #include "read.h"
23 #include "debug.h"
24 #include "entry.h"
25 #include "main.h"
26 #include "routines.h"
27 #include "options.h"
28
29 /*
30 * DATA DEFINITIONS
31 */
32 inputFile File; /* globally read through macros */
33 static fpos_t StartOfLine; /* holds deferred position of start of line */
34
35 /*
36 * FUNCTION DEFINITIONS
37 */
38
39 extern void freeSourceFileResources (void)
40 {
41 if (File.name != NULL)
42 vStringDelete (File.name);
43 if (File.path != NULL)
44 vStringDelete (File.path);
45 if (File.source.name != NULL)
46 vStringDelete (File.source.name);
47 if (File.source.tagPath != NULL)
48 eFree (File.source.tagPath);
49 if (File.line != NULL)
50 vStringDelete (File.line);
51 }
52
53 /*
54 * Source file access functions
55 */
56
57 static void setInputFileName (const char *const fileName)
58 {
59 const char *const head = fileName;
60 const char *const tail = baseFilename (head);
61
62 if (File.name != NULL)
63 vStringDelete (File.name);
64 File.name = vStringNewInit (fileName);
65
66 if (File.path != NULL)
67 vStringDelete (File.path);
68 if (tail == head)
69 File.path = NULL;
70 else
71 {
72 const size_t length = tail - head - 1;
73 File.path = vStringNew ();
74 vStringNCopyS (File.path, fileName, length);
75 }
76 }
77
78 static void setSourceFileParameters (vString *const fileName)
79 {
80 if (File.source.name != NULL)
81 vStringDelete (File.source.name);
82 File.source.name = fileName;
83
84 if (File.source.tagPath != NULL)
85 eFree (File.source.tagPath);
86 if (! Option.tagRelative || isAbsolutePath (vStringValue (fileName)))
87 File.source.tagPath = eStrdup (vStringValue (fileName));
88 else
89 File.source.tagPath =
90 relativeFilename (vStringValue (fileName), TagFile.directory);
91
92 if (vStringLength (fileName) > TagFile.max.file)
93 TagFile.max.file = vStringLength (fileName);
94
95 File.source.isHeader = isIncludeFile (vStringValue (fileName));
96 File.source.language = getFileLanguage (vStringValue (fileName));
97 }
98
99 static boolean setSourceFileName (vString *const fileName)
100 {
101 boolean result = FALSE;
102 if (getFileLanguage (vStringValue (fileName)) != LANG_IGNORE)
103 {
104 vString *pathName;
105 if (isAbsolutePath (vStringValue (fileName)) || File.path == NULL)
106 pathName = vStringNewCopy (fileName);
107 else
108 pathName = combinePathAndFile (
109 vStringValue (File.path), vStringValue (fileName));
110 setSourceFileParameters (pathName);
111 result = TRUE;
112 }
113 return result;
114 }
115
116 /*
117 * Line directive parsing
118 */
119
120 static int skipWhite (void)
121 {
122 int c;
123 do
124 c = getc (File.fp);
125 while (c == ' ' || c == '\t');
126 return c;
127 }
128
129 static unsigned long readLineNumber (void)
130 {
131 unsigned long lNum = 0;
132 int c = skipWhite ();
133 while (c != EOF && isdigit (c))
134 {
135 lNum = (lNum * 10) + (c - '0');
136 c = getc (File.fp);
137 }
138 ungetc (c, File.fp);
139 if (c != ' ' && c != '\t')
140 lNum = 0;
141
142 return lNum;
143 }
144
145 /* While ANSI only permits lines of the form:
146 * # line n "filename"
147 * Earlier compilers generated lines of the form
148 * # n filename
149 * GNU C will output lines of the form:
150 * # n "filename"
151 * So we need to be fairly flexible in what we accept.
152 */
153 static vString *readFileName (void)
154 {
155 vString *const fileName = vStringNew ();
156 boolean quoteDelimited = FALSE;
157 int c = skipWhite ();
158
159 if (c == '"')
160 {
161 c = getc (File.fp); /* skip double-quote */
162 quoteDelimited = TRUE;
163 }
164 while (c != EOF && c != '\n' &&
165 (quoteDelimited ? (c != '"') : (c != ' ' && c != '\t')))
166 {
167 vStringPut (fileName, c);
168 c = getc (File.fp);
169 }
170 if (c == '\n')
171 ungetc (c, File.fp);
172 vStringPut (fileName, '\0');
173
174 return fileName;
175 }
176
177 static boolean parseLineDirective (void)
178 {
179 boolean result = FALSE;
180 int c = skipWhite ();
181 DebugStatement ( const char* lineStr = ""; )
182
183 if (isdigit (c))
184 {
185 ungetc (c, File.fp);
186 result = TRUE;
187 }
188 else if (c == 'l' && getc (File.fp) == 'i' &&
189 getc (File.fp) == 'n' && getc (File.fp) == 'e')
190 {
191 c = getc (File.fp);
192 if (c == ' ' || c == '\t')
193 {
194 DebugStatement ( lineStr = "line"; )
195 result = TRUE;
196 }
197 }
198 if (result)
199 {
200 const unsigned long lNum = readLineNumber ();
201 if (lNum == 0)
202 result = FALSE;
203 else
204 {
205 vString *const fileName = readFileName ();
206 if (vStringLength (fileName) == 0)
207 {
208 File.source.lineNumber = lNum - 1; /* applies to NEXT line */
209 DebugStatement ( debugPrintf (DEBUG_RAW, "#%s %ld", lineStr, lNum); )
210 }
211 else if (setSourceFileName (fileName))
212 {
213 File.source.lineNumber = lNum - 1; /* applies to NEXT line */
214 DebugStatement ( debugPrintf (DEBUG_RAW, "#%s %ld \"%s\"",
215 lineStr, lNum, vStringValue (fileName)); )
216 }
217
218 if (Option.include.fileNames && vStringLength (fileName) > 0 &&
219 lNum == 1)
220 {
221 tagEntryInfo tag;
222 initTagEntry (&tag, baseFilename (vStringValue (fileName)));
223
224 tag.isFileEntry = TRUE;
225 tag.lineNumberEntry = TRUE;
226 tag.lineNumber = 1;
227 tag.kindName = "file";
228 tag.kind = 'F';
229
230 makeTagEntry (&tag);
231 }
232 vStringDelete (fileName);
233 result = TRUE;
234 }
235 }
236 return result;
237 }
238
239 /*
240 * Source file I/O operations
241 */
242
243 /* This function opens a source file, and resets the line counter. If it
244 * fails, it will display an error message and leave the File.fp set to NULL.
245 */
246 extern boolean fileOpen (const char *const fileName, const langType language)
247 {
248 #ifdef VMS
249 const char *const openMode = "r";
250 #else
251 const char *const openMode = "rb";
252 #endif
253 boolean opened = FALSE;
254
255 /* If another file was already open, then close it.
256 */
257 if (File.fp != NULL)
258 {
259 fclose (File.fp); /* close any open source file */
260 File.fp = NULL;
261 }
262
263 File.fp = fopen (fileName, openMode);
264 if (File.fp == NULL)
265 error (WARNING | PERROR, "cannot open \"%s\"", fileName);
266 else
267 {
268 opened = TRUE;
269
270 setInputFileName (fileName);
271 fgetpos (File.fp, &StartOfLine);
272 fgetpos (File.fp, &File.filePosition);
273 File.currentLine = NULL;
274 File.lineNumber = 0L;
275 File.eof = FALSE;
276 File.newLine = TRUE;
277
278 if (File.line != NULL)
279 vStringClear (File.line);
280
281 setSourceFileParameters (vStringNewInit (fileName));
282 File.source.lineNumber = 0L;
283
284 verbose ("OPENING %s as %s language %sfile\n", fileName,
285 getLanguageName (language),
286 File.source.isHeader ? "include " : "");
287 }
288 return opened;
289 }
290
291 extern void fileClose (void)
292 {
293 if (File.fp != NULL)
294 {
295 /* The line count of the file is 1 too big, since it is one-based
296 * and is incremented upon each newline.
297 */
298 if (Option.printTotals)
299 {
300 fileStatus *status = eStat (vStringValue (File.name));
301 addTotals (0, File.lineNumber - 1L, status->size);
302 }
303 fclose (File.fp);
304 File.fp = NULL;
305 }
306 }
307
308 extern boolean fileEOF (void)
309 {
310 return File.eof;
311 }
312
313 /* Action to take for each encountered source newline.
314 */
315 static void fileNewline (void)
316 {
317 File.filePosition = StartOfLine;
318 File.newLine = FALSE;
319 File.lineNumber++;
320 File.source.lineNumber++;
321 DebugStatement ( if (Option.breakLine == File.lineNumber) lineBreak (); )
322 DebugStatement ( debugPrintf (DEBUG_RAW, "%6ld: ", File.lineNumber); )
323 }
324
325 /* This function reads a single character from the stream, performing newline
326 * canonicalization.
327 */
328 static int iFileGetc (void)
329 {
330 int c;
331 readnext:
332 c = getc (File.fp);
333
334 /* If previous character was a newline, then we're starting a line.
335 */
336 if (File.newLine && c != EOF)
337 {
338 fileNewline ();
339 if (c == '#' && Option.lineDirectives)
340 {
341 if (parseLineDirective ())
342 goto readnext;
343 else
344 {
345 fsetpos (File.fp, &StartOfLine);
346 c = getc (File.fp);
347 }
348 }
349 }
350
351 if (c == EOF)
352 File.eof = TRUE;
353 else if (c == NEWLINE)
354 {
355 File.newLine = TRUE;
356 fgetpos (File.fp, &StartOfLine);
357 }
358 else if (c == CRETURN)
359 {
360 /* Turn line breaks into a canonical form. The three commonly
361 * used forms if line breaks: LF (UNIX/Mac OS X), CR (Mac OS 9),
362 * and CR-LF (MS-DOS) are converted into a generic newline.
363 */
364 #ifndef macintosh
365 const int next = getc (File.fp); /* is CR followed by LF? */
366 if (next != NEWLINE)
367 ungetc (next, File.fp);
368 else
369 #endif
370 {
371 c = NEWLINE; /* convert CR into newline */
372 File.newLine = TRUE;
373 fgetpos (File.fp, &StartOfLine);
374 }
375 }
376 DebugStatement ( debugPutc (DEBUG_RAW, c); )
377 return c;
378 }
379
380 extern void fileUngetc (int c)
381 {
382 File.ungetch = c;
383 }
384
385 static vString *iFileGetLine (void)
386 {
387 vString *result = NULL;
388 int c;
389 if (File.line == NULL)
390 File.line = vStringNew ();
391 vStringClear (File.line);
392 do
393 {
394 c = iFileGetc ();
395 if (c != EOF)
396 vStringPut (File.line, c);
397 if (c == '\n' || (c == EOF && vStringLength (File.line) > 0))
398 {
399 vStringTerminate (File.line);
400 #ifdef HAVE_REGEX
401 if (vStringLength (File.line) > 0)
402 matchRegex (File.line, File.source.language);
403 #endif
404 result = File.line;
405 break;
406 }
407 } while (c != EOF);
408 Assert (result != NULL || File.eof);
409 return result;
410 }
411
412 /* Do not mix use of fileReadLine () and fileGetc () for the same file.
413 */
414 extern int fileGetc (void)
415 {
416 int c;
417
418 /* If there is an ungotten character, then return it. Don't do any
419 * other processing on it, though, because we already did that the
420 * first time it was read through fileGetc ().
421 */
422 if (File.ungetch != '\0')
423 {
424 c = File.ungetch;
425 File.ungetch = '\0';
426 return c; /* return here to avoid re-calling debugPutc () */
427 }
428 do
429 {
430 if (File.currentLine != NULL)
431 {
432 c = *File.currentLine++;
433 if (c == '\0')
434 File.currentLine = NULL;
435 }
436 else
437 {
438 vString* const line = iFileGetLine ();
439 if (line != NULL)
440 File.currentLine = (unsigned char*) vStringValue (line);
441 if (File.currentLine == NULL)
442 c = EOF;
443 else
444 c = '\0';
445 }
446 } while (c == '\0');
447 DebugStatement ( debugPutc (DEBUG_READ, c); )
448 return c;
449 }
450
451 extern int fileSkipToCharacter (int c)
452 {
453 int d;
454 do
455 {
456 d = fileGetc ();
457 } while (d != EOF && d != c);
458 return d;
459 }
460
461 /* An alternative interface to fileGetc (). Do not mix use of fileReadLine()
462 * and fileGetc() for the same file. The returned string does not contain
463 * the terminating newline. A NULL return value means that all lines in the
464 * file have been read and we are at the end of file.
465 */
466 extern const unsigned char *fileReadLine (void)
467 {
468 vString* const line = iFileGetLine ();
469 const unsigned char* result = NULL;
470 if (line != NULL)
471 {
472 result = (const unsigned char*) vStringValue (line);
473 vStringStripNewline (line);
474 DebugStatement ( debugPrintf (DEBUG_READ, "%s\n", result); )
475 }
476 return result;
477 }
478
479 /*
480 * Source file line reading with automatic buffer sizing
481 */
482 extern char *readLine (vString *const vLine, FILE *const fp)
483 {
484 char *result = NULL;
485
486 vStringClear (vLine);
487 if (fp == NULL) /* to free memory allocated to buffer */
488 error (FATAL, "NULL file pointer");
489 else
490 {
491 boolean reReadLine;
492
493 /* If reading the line places any character other than a null or a
494 * newline at the last character position in the buffer (one less
495 * than the buffer size), then we must resize the buffer and
496 * reattempt to read the line.
497 */
498 do
499 {
500 char *const pLastChar = vStringValue (vLine) + vStringSize (vLine) -2;
501 fpos_t startOfLine;
502
503 fgetpos (fp, &startOfLine);
504 reReadLine = FALSE;
505 *pLastChar = '\0';
506 result = fgets (vStringValue (vLine), (int) vStringSize (vLine), fp);
507 if (result == NULL)
508 {
509 if (! feof (fp))
510 error (FATAL | PERROR, "Failure on attempt to read file");
511 }
512 else if (*pLastChar != '\0' &&
513 *pLastChar != '\n' && *pLastChar != '\r')
514 {
515 /* buffer overflow */
516 reReadLine = vStringAutoResize (vLine);
517 if (reReadLine)
518 fsetpos (fp, &startOfLine);
519 else
520 error (FATAL | PERROR, "input line too big; out of memory");
521 }
522 else
523 {
524 char* eol;
525 vStringSetLength (vLine);
526 /* canonicalize new line */
527 eol = vStringValue (vLine) + vStringLength (vLine) - 1;
528 if (*eol == '\r')
529 *eol = '\n';
530 else if (*(eol - 1) == '\r' && *eol == '\n')
531 {
532 *(eol - 1) = '\n';
533 *eol = '\0';
534 --vLine->length;
535 }
536 }
537 } while (reReadLine);
538 }
539 return result;
540 }
541
542 /* Places into the line buffer the contents of the line referenced by
543 * "location".
544 */
545 extern char *readSourceLine (
546 vString *const vLine, fpos_t location, long *const pSeekValue)
547 {
548 fpos_t orignalPosition;
549 char *result;
550
551 fgetpos (File.fp, &orignalPosition);
552 fsetpos (File.fp, &location);
553 if (pSeekValue != NULL)
554 *pSeekValue = ftell (File.fp);
555 result = readLine (vLine, File.fp);
556 if (result == NULL)
557 error (FATAL, "Unexpected end of file: %s", vStringValue (File.name));
558 fsetpos (File.fp, &orignalPosition);
559
560 return result;
561 }
562
563 /* vi:set tabstop=4 shiftwidth=4: */