rpc.cmsd: use TIRPC on Linux
[oweals/cde.git] / cde / programs / nsgmls / ParserEventGeneratorKit.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: ParserEventGeneratorKit.C /main/1 1996/07/29 17:00:49 cde-hp $ */
24 // Copyright (c) 1995 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifdef __GNUG__
28 #pragma implementation
29 #endif
30
31 #include "splib.h"
32 #include "Boolean.h"
33 #include "ParserApp.h"
34 #include "macros.h"
35 #include "SGMLApplication.h"
36 #include "ParserEventGeneratorKit.h"
37 #include "GenericEventHandler.h"
38
39 #ifdef SP_NAMESPACE
40 using namespace SP_NAMESPACE;
41 #endif
42
43 class ParserEventGeneratorKitImpl : public ParserApp {
44 public:
45   ParserOptions &options() { return options_; }
46   bool generalEntities;
47   unsigned refCount;
48 private:
49   ErrorCountEventHandler *makeEventHandler() { return 0; }
50 };
51
52 class ParserEventGenerator : public EventGenerator {
53 public:
54   ParserEventGenerator(SgmlParser &,
55                        bool generalEntities,
56                        ParserEventGeneratorKitImpl *kit_);
57   ParserEventGenerator(const SgmlParser &,
58                        const SGMLApplication::Char *,
59                        size_t n,
60                        bool generalEntities,
61                        bool messagesInhibited,
62                        ParserEventGeneratorKitImpl *kit_);
63   ~ParserEventGenerator();
64   unsigned run(SGMLApplication &);
65   void inhibitMessages(bool);
66   void halt();
67   EventGenerator *
68     makeSubdocEventGenerator(const SGMLApplication::Char *systemId,
69                              size_t systemIdLength);
70 private:
71   SgmlParser parser_;
72   bool generalEntities_;
73   bool messagesInhibited_;
74   sig_atomic_t cancel_;
75   ParserEventGeneratorKitImpl *kit_;
76 };
77
78 ParserEventGeneratorKit::ParserEventGeneratorKit()
79 {
80   impl_ = new ParserEventGeneratorKitImpl;
81   impl_->refCount = 1;
82   impl_->generalEntities = 0;
83 }
84
85 ParserEventGeneratorKit::~ParserEventGeneratorKit()
86 {
87   impl_->refCount -= 1;
88   if (impl_->refCount == 0)
89     delete impl_;
90 }
91
92 EventGenerator *
93 ParserEventGeneratorKit::makeEventGenerator(int nFiles,
94                                             AppChar *const *files)
95 {
96   StringC sysid;
97   if (impl_->makeSystemId(nFiles, files, sysid))
98     impl_->initParser(sysid);
99   return new ParserEventGenerator(impl_->parser(),
100                                   impl_->generalEntities,
101                                   impl_);
102 }
103
104 void ParserEventGeneratorKit::setProgramName(const AppChar *prog)
105 {
106   if (prog)
107     impl_->setProgramName(impl_->convertInput(prog));
108 }
109
110 void ParserEventGeneratorKit::setOption(Option opt)
111 {
112   switch (opt) {
113   case showOpenEntities:
114     impl_->processOption('e', 0);
115     break;
116   case showOpenElements:
117     impl_->processOption('g', 0);
118     break;
119   case outputCommentDecls:
120     impl_->options().eventsWanted.addCommentDecls();
121     break;
122   case outputMarkedSections:
123     impl_->options().eventsWanted.addMarkedSections();
124     break;
125   case outputGeneralEntities:
126     impl_->generalEntities = 1;
127     break;
128   case mapCatalogDocument:
129     impl_->processOption('C', 0);
130     break;
131   }
132 }
133
134 void ParserEventGeneratorKit::setOption(OptionWithArg opt,
135                                         const AppChar *arg)
136 {
137   switch (opt) {
138   case addCatalog:
139     impl_->processOption('c', arg);
140     break;
141   case includeParam:
142     impl_->processOption('i', arg);
143     break;
144   case enableWarning:
145     impl_->processOption('w', arg);
146     break;
147   case addSearchDir:
148     impl_->processOption('D', arg);
149     break;
150   case activateLink:
151     impl_->processOption('a', arg);
152     break;
153   case architecture:
154     impl_->processOption('A', arg);
155     break;
156   }
157 }
158
159 ParserEventGenerator::ParserEventGenerator(SgmlParser &parser,
160                                            bool generalEntities,
161                                            ParserEventGeneratorKitImpl *kit)
162 : generalEntities_(generalEntities),
163   messagesInhibited_(0),
164   cancel_(0),
165   kit_(kit)
166 {
167   parser_.swap(parser);
168   kit_->refCount += 1;
169 }
170
171 ParserEventGenerator::ParserEventGenerator(const SgmlParser &parser,
172                                            const SGMLApplication::Char *s,
173                                            size_t n,
174                                            bool generalEntities,
175                                            bool messagesInhibited,
176                                            ParserEventGeneratorKitImpl *kit)
177 : generalEntities_(generalEntities),
178   messagesInhibited_(messagesInhibited),
179   cancel_(0),
180   kit_(kit)
181 {
182   kit_->refCount += 1;
183   SgmlParser::Params params;
184   params.parent = &parser;
185   params.sysid.assign(s, n);
186   params.entityType = SgmlParser::Params::subdoc;
187   parser_.init(params);
188 }
189
190 void ParserEventGenerator::halt()
191 {
192   cancel_ = 1;
193 }
194
195 ParserEventGenerator::~ParserEventGenerator()
196 {
197   kit_->refCount -= 1;
198   if (kit_->refCount == 0)
199     delete kit_;
200 }
201
202 unsigned ParserEventGenerator::run(SGMLApplication &app)
203 {
204   MsgGenericEventHandler handler(app, generalEntities_,
205                                  *kit_, &messagesInhibited_);
206   kit_->parseAll(parser_, handler, &cancel_);
207   return handler.errorCount();
208 }
209
210 void ParserEventGenerator::inhibitMessages(bool b)
211 {
212   messagesInhibited_ = b;
213 }
214
215 EventGenerator *
216 ParserEventGenerator::makeSubdocEventGenerator(const SGMLApplication::Char *s,
217                                                size_t n)
218 {
219   return new ParserEventGenerator(parser_, s, n, generalEntities_,
220                                   messagesInhibited_, kit_);
221 }