Fix missing @clicksequence problem with older Texinfo versions.
[pspp-builds.git] / src / language / syntax-string-source.c
1 /* PSPPIRE - a graphical interface for PSPP.
2    Copyright (C) 2007 Free Software Foundation, Inc.
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 by
6    the Free Software Foundation, either version 3 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 <http://www.gnu.org/licenses/>. */
16
17
18 #include <config.h>
19
20 #include <libpspp/getl.h>
21 #include <libpspp/compiler.h>
22 #include <libpspp/str.h>
23
24 #include <stdlib.h>
25
26 #include "syntax-string-source.h"
27
28 #include "xalloc.h"
29
30 struct syntax_string_source
31   {
32     struct getl_interface parent;
33     struct string buffer;
34     size_t posn;
35   };
36
37
38 static bool
39 always_false (const struct getl_interface *i UNUSED)
40 {
41   return false;
42 }
43
44 /* Returns the name of the source */
45 static const char *
46 name (const struct getl_interface *i UNUSED)
47 {
48   return NULL;
49 }
50
51
52 /* Returns the location within the source */
53 static int
54 location (const struct getl_interface *i UNUSED)
55 {
56   return -1;
57 }
58
59
60 static void
61 do_close (struct getl_interface *i )
62 {
63   struct syntax_string_source *sss = (struct syntax_string_source *) i;
64
65   ds_destroy (&sss->buffer);
66
67   free (sss);
68 }
69
70
71
72 static bool
73 read_single_line (struct getl_interface *i,
74                   struct string *line)
75 {
76   struct syntax_string_source *sss = (struct syntax_string_source *) i;
77
78   size_t next;
79
80   if ( sss->posn == -1)
81     return false;
82
83   next = ss_find_char (ds_substr (&sss->buffer,
84                                   sss->posn, -1), '\n');
85
86   ds_assign_substring (line,
87                        ds_substr (&sss->buffer,
88                                   sss->posn,
89                                   next)
90                        );
91
92   if ( next != -1 )
93     sss->posn += next + 1; /* + 1 to skip newline */
94   else
95     sss->posn = -1; /* End of file encountered */
96
97   return true;
98 }
99
100 struct getl_interface *
101 create_syntax_string_source (const char *format, ...)
102 {
103   va_list args;
104
105   struct syntax_string_source *sss = xzalloc (sizeof *sss);
106
107   sss->posn = 0;
108
109   ds_init_empty (&sss->buffer);
110
111   va_start (args, format);
112   ds_put_vformat (&sss->buffer, format, args);
113   va_end (args);
114
115   sss->parent.interactive = always_false;
116   sss->parent.close = do_close;
117   sss->parent.read = read_single_line;
118
119   sss->parent.name = name;
120   sss->parent.location = location;
121
122
123   return (struct getl_interface *) sss;
124 }
125
126 /* Return the syntax currently contained in S.
127    Primarily usefull for debugging */
128 const char *
129 syntax_string_source_get_syntax (const struct syntax_string_source *s)
130 {
131   return ds_cstr (&s->buffer);
132 }