1 /* Copyright (C) 1996-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
16
17 #ifndef _LINEREADER_H
18 #define _LINEREADER_H 1
19
20 #include <ctype.h>
21 #include <libintl.h>
22 #include <stdint.h>
23 #include <stdio.h>
24
25 #include "charmap.h"
26 #include "error.h"
27 #include "locfile-token.h"
28 #include "repertoire.h"
29 #include "record-status.h"
30
31 typedef const struct keyword_t *(*kw_hash_fct_t) (const char *, size_t);
32 struct charset_t;
33 struct localedef_t;
34
35 struct token
36 {
37 enum token_t tok;
38 union
39 {
40 struct
41 {
42 char *startmb;
43 size_t lenmb;
44 uint32_t *startwc;
45 size_t lenwc;
46 } str;
47 unsigned long int num;
48 struct
49 {
50 /* This element is sized on the safe expectation that no single
51 character in any character set uses more than 16 bytes. */
52 unsigned char bytes[16];
53 int nbytes;
54 } charcode;
55 uint32_t ucs4;
56 } val;
57 };
58
59
60 struct linereader
61 {
62 FILE *fp;
63 const char *fname;
64 char *buf;
65 size_t bufsize;
66 size_t bufact;
67 size_t lineno;
68
69 size_t idx;
70
71 char comment_char;
72 char escape_char;
73
74 struct token token;
75
76 int translate_strings;
77 int return_widestr;
78
79 kw_hash_fct_t hash_fct;
80 };
81
82
83 /* Functions defined in linereader.c. */
84 extern struct linereader *lr_open (const char *fname, kw_hash_fct_t hf);
85 extern struct linereader *lr_create (FILE *fp, const char *fname,
86 kw_hash_fct_t hf);
87 extern int lr_eof (struct linereader *lr);
88 extern void lr_close (struct linereader *lr);
89 extern int lr_next (struct linereader *lr);
90 extern struct token *lr_token (struct linereader *lr,
91 const struct charmap_t *charmap,
92 struct localedef_t *locale,
93 const struct repertoire_t *repertoire,
94 int verbose);
95 extern void lr_ignore_rest (struct linereader *lr, int verbose);
96
97
98 static inline void
99 __attribute__ ((__format__ (__printf__, 2, 3), nonnull (1, 2)))
lr_error(struct linereader * lr,const char * fmt,...)100 lr_error (struct linereader *lr, const char *fmt, ...)
101 {
102 char *str;
103 va_list arg;
104 struct locale_state ls;
105 int ret;
106
107 va_start (arg, fmt);
108 ls = push_locale ();
109
110 ret = vasprintf (&str, fmt, arg);
111 if (ret == -1)
112 abort ();
113
114 pop_locale (ls);
115 va_end (arg);
116
117 error_at_line (0, 0, lr->fname, lr->lineno, "%s", str);
118
119 free (str);
120 }
121
122
123 static inline int
124 __attribute ((always_inline))
lr_getc(struct linereader * lr)125 lr_getc (struct linereader *lr)
126 {
127 if (lr->idx == lr->bufact)
128 {
129 if (lr->bufact != 0)
130 if (lr_next (lr) < 0)
131 return EOF;
132
133 if (lr->bufact == 0)
134 return EOF;
135 }
136
137 return lr->buf[lr->idx++] & 0xff;
138 }
139
140
141 static inline int
142 __attribute ((always_inline))
lr_ungetc(struct linereader * lr,int ch)143 lr_ungetc (struct linereader *lr, int ch)
144 {
145 if (lr->idx == 0)
146 return -1;
147
148 if (ch != EOF)
149 lr->buf[--lr->idx] = ch;
150 return 0;
151 }
152
153
154 static inline int
lr_ungetn(struct linereader * lr,size_t n)155 lr_ungetn (struct linereader *lr, size_t n)
156 {
157 if (lr->idx < n)
158 return -1;
159
160 lr->idx -= n;
161 return 0;
162 }
163
164
165 #endif /* linereader.h */
166