Link with C++ linker
[oweals/cde.git] / cde / programs / nsgmls / EntityApp.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 librararies 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: EntityApp.C /main/1 1996/07/29 16:49:57 cde-hp $ */
24 // Copyright (c) 1996 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 "EntityApp.h"
33 #include "sptchar.h"
34
35 #include <stdlib.h>
36
37 #ifdef SP_MULTI_BYTE
38 #include "ISO8859InputCodingSystem.h"
39 #endif
40
41 #include "PosixStorage.h"
42 #include "URLStorage.h"
43 #include "LiteralStorage.h"
44 #include "ExtendEntityManager.h"
45 #include "SOEntityCatalog.h"
46 #include "CodingSystem.h"
47 #include "macros.h"
48
49 #ifndef SGML_SEARCH_PATH_DEFAULT
50 #define SGML_SEARCH_PATH_DEFAULT SP_T("")
51 #endif
52
53 #ifndef SGML_CATALOG_FILES_DEFAULT
54 #define SGML_CATALOG_FILES_DEFAULT SP_T("")
55 #endif /* not SGML_CATALOG_FILES_DEFAULT */
56
57 #ifdef SP_NAMESPACE
58 namespace SP_NAMESPACE {
59 #endif
60
61 #ifdef MSDOS_FILENAMES
62 const Char FILE_SEP = ';';
63 #else
64 const Char FILE_SEP = ':';
65 #endif
66
67 #ifdef SP_MULTI_BYTE
68 static ISO8859InputCodingSystem iso8859_2InputCodingSystem(2);
69 static ISO8859InputCodingSystem iso8859_3InputCodingSystem(3);
70 static ISO8859InputCodingSystem iso8859_4InputCodingSystem(4);
71 static ISO8859InputCodingSystem iso8859_5InputCodingSystem(5);
72 static ISO8859InputCodingSystem iso8859_6InputCodingSystem(6);
73 static ISO8859InputCodingSystem iso8859_7InputCodingSystem(7);
74 static ISO8859InputCodingSystem iso8859_8InputCodingSystem(8);
75 static ISO8859InputCodingSystem iso8859_9InputCodingSystem(9);
76
77 static struct {
78   const char *name;
79   const InputCodingSystem *cs;
80 } inputCodingSystems[] = {
81  { "IS8859-2", &iso8859_2InputCodingSystem },
82  { "IS8859-3", &iso8859_3InputCodingSystem },
83  { "IS8859-4", &iso8859_4InputCodingSystem },
84  { "IS8859-5", &iso8859_5InputCodingSystem },
85  { "IS8859-6", &iso8859_6InputCodingSystem },
86  { "IS8859-7", &iso8859_7InputCodingSystem },
87  { "IS8859-8", &iso8859_8InputCodingSystem },
88  { "IS8859-9", &iso8859_9InputCodingSystem },
89 };
90
91 #endif /* SP_MULTI_BYTE */
92
93 #ifdef SP_MULTI_BYTE
94 static UnivCharsetDesc::Range range = { 0, 65536, 0 };
95 #else
96 static UnivCharsetDesc::Range range = { 0, 256, 0 };
97 #endif
98
99 EntityApp::EntityApp()
100 : mapCatalogDocument_(0),
101   systemCharset_(UnivCharsetDesc(&range, 1))
102 {
103   registerOption('c', SP_T("catalog_sysid"));
104   registerOption('C');
105   registerOption('D', SP_T("dir"));
106 }
107
108 void EntityApp::processOption(AppChar opt, const AppChar *arg)
109 {
110   switch (opt) {
111   case 'c':
112     catalogSysids_.push_back(arg);
113     break;
114   case 'C':
115     mapCatalogDocument_ = 1;
116     break;
117   case 'D':
118     searchDirs_.push_back(arg);
119     break;
120   default:
121     CmdLineApp::processOption(opt, arg);
122     break;
123   }
124 }
125
126 int EntityApp::processArguments(int argc, AppChar **argv)
127 {
128   StringC sysid;
129   if (!makeSystemId(argc, argv, sysid))
130     return 1;
131   return processSysid(sysid);
132 }
133
134 Boolean EntityApp::makeSystemId(int nFiles, AppChar *const *files,
135                                         StringC &result)
136 {
137   Vector<StringC> filenames(nFiles == 0 ? 1 : nFiles);
138   int i;
139   for (i = 0; i < nFiles; i++)
140     filenames[i] = convertInput(tcscmp(files[i], SP_T("-")) == 0
141                                 ? SP_T("<OSFD>0")
142                                 : files[i]);
143   if (nFiles == 0)
144     filenames[0] = convertInput(SP_T("<OSFD>0"));
145   return entityManager()->mergeSystemIds(filenames,
146                                          mapCatalogDocument_,
147                                          systemCharset_,
148                                          *this,
149                                          result);
150 }
151
152
153 Ptr<ExtendEntityManager> &EntityApp::entityManager()
154 {
155   if (!entityManager_.isNull())
156     return entityManager_;
157   PosixStorageManager *sm
158     = new PosixStorageManager("OSFILE",
159                               systemCharset_.desc(),
160 #ifndef SP_WIDE_SYSTEM
161                               codingSystem(),
162 #endif
163                               5);
164   size_t i;
165   for (i = 0; i < searchDirs_.size(); i++)
166     sm->addSearchDir(convertInput(searchDirs_[i]));
167   {
168     const AppChar *e = tgetenv(SP_T("SGML_SEARCH_PATH"));
169     if (!e)
170       e = SGML_SEARCH_PATH_DEFAULT;
171     if (*e) {
172       StringC str(convertInput(e));
173       size_t i = 0;
174       size_t start = 0;
175       for (;;) {
176         if (i == str.size() || str[i] == FILE_SEP) {
177           sm->addSearchDir(StringC(str.data() + start,
178                                    i - start));
179           if (i == str.size())
180             break;
181           start = ++i;
182         }
183         else
184           i++;
185       }
186     }
187   }
188
189   entityManager_ = ExtendEntityManager::make(sm, codingSystem());
190   entityManager_
191   ->registerStorageManager(new PosixFdStorageManager("OSFD",
192                                                      systemCharset_.desc()));
193   entityManager_->registerStorageManager(new URLStorageManager("URL"));
194   entityManager_->registerStorageManager(new LiteralStorageManager("LITERAL"));
195   for (i = 0;; i++) {
196     const char *s;
197     const CodingSystem *p = codingSystem(i, s);
198     if (!p)
199       break;
200     entityManager_->registerCodingSystem(s, p);
201   }
202 #ifdef SP_MULTI_BYTE
203   for (i = 0; i < SIZEOF(inputCodingSystems); i++)
204     entityManager_->registerCodingSystem(inputCodingSystems[i].name,
205                                          inputCodingSystems[i].cs);
206 #endif
207   Vector<StringC> v;
208   for (i = 0; i < catalogSysids_.size(); i++)
209     // filenames specified on command-line must exist
210     v.push_back(convertInput(catalogSysids_[i]));
211   {
212     const AppChar *e = tgetenv(SP_T("SGML_CATALOG_FILES"));
213     if (!e)
214       e = SGML_CATALOG_FILES_DEFAULT;
215     if (*e) {
216       StringC str(convertInput(e));
217       size_t i = 0;
218       size_t start = 0;
219       for (;;) {
220         if (i == str.size() || str[i] == FILE_SEP) {
221           v.push_back(StringC(str.data() + start,
222                               i - start));
223           if (i == str.size())
224             break;
225           start = ++i;
226         }
227         else
228           i++;
229       }
230     }
231   }
232   entityManager_->setCatalogManager(SOCatalogManager::make(v,
233                                                            catalogSysids_.size(),
234                                                            systemCharset_,
235                                                            systemCharset_));
236   return entityManager_;
237 }
238
239
240 #ifdef SP_NAMESPACE
241 }
242 #endif