Release 1.3.6.
[fwd] / scan.h
CommitLineData
e82f7154 1/* -*-c-*-
2 *
e82f7154 3 * Character scanners
4 *
77d0e3dc 5 * (c) 1999 Straylight/Edgeware
e82f7154 6 */
7
206212ca 8/*----- Licensing notice --------------------------------------------------*
e82f7154 9 *
9155ea97 10 * This file is part of the `fwd' port forwarder.
e82f7154 11 *
9155ea97 12 * `fwd' is free software; you can redistribute it and/or modify
e82f7154 13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
206212ca 16 *
9155ea97 17 * `fwd' is distributed in the hope that it will be useful,
e82f7154 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
206212ca 21 *
e82f7154 22 * You should have received a copy of the GNU General Public License
9155ea97 23 * along with `fwd'; if not, write to the Free Software Foundation,
e82f7154 24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
e82f7154 27#ifndef SCAN_H
28#define SCAN_H
29
30#ifdef __cplusplus
31 extern "C" {
32#endif
33
34/*----- Header files ------------------------------------------------------*/
35
36#include <stdio.h>
37
38#include <mLib/dstr.h>
39
40/*----- Data structures ---------------------------------------------------*/
41
77d0e3dc 42/* --- A low-level scanner source --- */
43
44typedef struct scansrc {
45 struct scansrc *next; /* Next one in the list */
46 struct scansrc_ops *ops; /* Pointer to operations table */
47 char *src; /* Name of this source */
48 int line; /* Current line number */
7bb7c50b 49 dstr pushback; /* Pushback characters */
77d0e3dc 50 char *tok; /* Token pushback */
51 unsigned t; /* Token type pushback */
52} scansrc;
53
54/* --- Scanner source operations --- */
e82f7154 55
77d0e3dc 56typedef struct scansrc_ops {
57 int (*scan)(scansrc */*ss*/); /* Read another character */
58 void (*destroy)(scansrc */*ss*/); /* Destroy an unwanted source */
59} scansrc_ops;
60
61/* --- A character scanner --- */
e82f7154 62
63typedef struct scanner {
77d0e3dc 64 scansrc *head, **tail; /* Scanner list head and tail */
e82f7154 65 int t; /* Token type */
66 dstr d; /* Current token value */
78111730 67 const char *wbegin, *wcont; /* Parsing exception strings */
e82f7154 68} scanner;
69
77d0e3dc 70/*----- Particular scanner types ------------------------------------------*/
71
72/* --- @scan_file@ --- *
73 *
74 * Arguments: @FILE *fp@ = pointer to file descriptor
7bb7c50b 75 * @const char *name@ = pointer to source file name
77d0e3dc 76 * @unsigned f@ = flags
77 *
78 * Returns: A scanner source.
79 *
80 * Use: Creates a new scanner source for reading from a file.
81 */
82
7bb7c50b 83#define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */
77d0e3dc 84
7bb7c50b 85extern scansrc *scan_file(FILE */*fp*/, const char */*name*/,
86 unsigned /*f*/);
77d0e3dc 87
88/* --- @scan_argv@ --- *
89 *
90 * Arguments: @char **av@ = pointer to argument array (null terminated)
91 *
92 * Returns: A scanner source.
93 *
94 * Use: Creates a new scanner source for reading from an @argv@
95 * array.
96 */
97
98extern scansrc *scan_argv(char **/*av*/);
99
100/*----- General scanner handling ------------------------------------------*/
101
102/* --- @scan@ --- *
103 *
104 * Arguments: @scanner *sc@ = pointer to main scanner context
105 *
106 * Returns: Character read, or end-of-file.
107 *
108 * Use: Scans a character from a source of characters.
109 */
110
206212ca 111extern int scan(scanner */*sc*/);
e82f7154 112
77d0e3dc 113/* --- @unscan@ --- *
114 *
115 * Arguments: @scanner *sc@ = pointer to main scanner context
116 * @int ch@ = character to unscan
117 *
118 * Returns: ---
119 *
120 * Use: Scans a character from a source of characters.
121 */
e82f7154 122
77d0e3dc 123extern void unscan(scanner */*sc*/, int /*ch*/);
e82f7154 124
77d0e3dc 125/* --- @scan_push@ --- *
126 *
127 * Arguments: @scanner *sc@ = pointer to main scanner context
128 * @scansrc *ss@ = souorce to push
129 *
130 * Returns: ---
131 *
132 * Use: Pushes a scanner source onto the front of the queue.
133 */
e82f7154 134
77d0e3dc 135extern void scan_push(scanner */*sc*/, scansrc */*ss*/);
e82f7154 136
77d0e3dc 137/* --- @scan_add@ --- *
138 *
139 * Arguments: @scanner *sc@ = pointer to main scanner context
140 * @scansrc *ss@ = souorce to push
141 *
142 * Returns: ---
143 *
144 * Use: Adds a scanner source onto the end of the queue.
145 */
146
147extern void scan_add(scanner */*sc*/, scansrc */*ss*/);
148
149/* --- @scan_create@ --- *
150 *
151 * Arguments: @scanner *sc@ = scanner context to initialize
152 *
153 * Returns: ---
154 *
155 * Use: Initializes a scanner block ready for use.
156 */
157
158extern void scan_create(scanner */*sc*/);
159
160/* --- @scan_destroy@ --- *
161 *
162 * Arguments: @scanner *sc@ = pointer to scanner context
163 *
164 * Returns: ---
165 *
166 * Use: Destroys a scanner and all the sources attached to it.
167 */
e82f7154 168
77d0e3dc 169extern void scan_destroy(scanner */*sc*/);
e82f7154 170
171/*----- That's all, folks -------------------------------------------------*/
172
173#ifdef __cplusplus
174 }
175#endif
176
177#endif