Remove UXPDS support
[oweals/cde.git] / cde / programs / dtinfo / dtinfogen / infolib / etc / NCFGen.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: NCFGen.C /main/9 1996/08/21 15:47:02 drk $ */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #if !defined(USL)
28 #include <strings.h>
29 #endif
30 #include <sstream>
31 using namespace std;
32
33 #include "Exceptions.hh"
34 #include "DataBase.h"
35 #include "BookCaseDB.h"
36 #include "Task.h"
37 #include "Handler.h"
38 #include "StyleValidate.h"
39 #include "Token.h"
40
41 /* MMDB interfaces */
42 #include "oliasdb/mmdb.h"
43 #include "oliasdb/asciiIn_filters.h"
44 #include "oliasdb/olias_consts.h"
45 #include "oliasdb/stylesheet_hd.h"
46
47
48 /* Hash table interfaces */
49 #include "dti_cc/CC_String.h"
50 #include "dti_cc/cc_hdict.h"
51 #include "BTCollectable.h"
52
53 #ifdef FISH_DEBUG
54 #include "dbug.h"
55 #endif
56
57 #define SKIP_CODE -1
58 static unsigned hash_func(const CC_String &str)
59 {
60   return str.hash();
61 }
62
63 //-------------------------------------------------------------------------
64 static void
65 writeStyleSheets(BookCaseDB& db)
66 {
67   DBTable *out = db.DB::table(DATABASE_STDIO,
68                               STYLESHEET_CODE, BT_NUM_STYLESHEET_FIELDS,
69                               DB::CREATE);
70
71   DBTable *in = db.table(BookCaseDB::StyleSheet, DB::READ);
72   DBCursor cursor(*in);
73
74   const char *name;
75   const char *online;
76   int len_o;
77   const char *print;
78   int len_p;
79
80   int statusO = 0;
81   int statusP = 0;
82
83   
84   while(cursor.next(STRING_CODE, &name,
85                     -STRING_CODE, &online, &len_o,
86                     -STRING_CODE, &print, &len_p,
87                     NULL)){
88     if( (statusO=validate_stylesheet( online, len_o, ONLINE ))){
89       Token::signalError(Token::User, Token::Continuable, 0, 0,
90                          "Online style sheet for `%s' is invalid.", name);
91     }
92     
93     if( (statusP=validate_stylesheet( print, len_p, PRINT ))){
94       Token::signalError(Token::User, Token::Continuable, 0, 0,
95                          "Print style sheet for `%s' is invalid.", name);
96     }
97
98     if ( statusO || statusP ) {
99       throw(Unexpected("Style sheet validation failed\n"));
100     }
101     
102     out->insert(STRING_CODE, name,
103                 -STRING_CODE, online, (size_t)len_o,
104                 -STRING_CODE, print, (size_t)len_p,
105                 NULL);
106   }
107
108   delete out;
109 }
110
111
112 //-------------------------------------------------------------------------
113 static void
114 buildNCF(BookCaseDB& db, const char *base_name, int compressed)
115 {
116   DBTable *ncf = db.DB::table(DATABASE_STDIO,
117                               OLIAS_NODE_CODE, BT_NUM_OLIAS_NODE_FIELDS,
118                               DB::CREATE);
119
120   DBTable *nodeMeta = db.table(BookCaseDB::NodeMeta, DB::READ);
121   DBCursor cursor(*nodeMeta);
122
123
124   const char *bookLocator;
125   const char *nodeLocator;
126   const char *filename;
127   int         line_num;
128   const char *title;
129   const char *stitle;
130   const char *style;
131   int dupID = 0;
132   string outstr;
133
134   OLIAS_DB mmdb_handle;
135   info_lib *mmdb = 
136     mmdb_handle.openInfoLib(getenv("MMDB_PATH"), (char*)base_name);
137   info_base *base_ptr = mmdb->get_info_base(base_name);
138
139   const int BUFSIZE=30;
140
141   hashTable<CC_String,BTCollectable> node_dict(hash_func);    // Hash table...
142   
143   if ( compressed ) {
144
145     // 30 will be enough for now.    
146     const int COMPRESSED_AGENT_SIZE=30;
147     
148     char comp_agent[COMPRESSED_AGENT_SIZE];
149
150     // was bzero before, but unable to find bzero on solaris
151     for ( int i = 0; i < COMPRESSED_AGENT_SIZE; i++ ) {
152         comp_agent[i] = 0;
153     }
154
155     ostringstream str_buf( comp_agent );
156     handler *x = (base_ptr->get_obj_dict()).get_handler(
157       form("%s.%s", base_name, "sgml.dict"));
158     x->its_oid().asciiOut(str_buf);
159     memcpy(comp_agent, str_buf.str().c_str(), COMPRESSED_AGENT_SIZE);
160     
161
162     while(cursor.next(STRING_CODE, &bookLocator,
163                       STRING_CODE, &nodeLocator,
164                       STRING_CODE, &filename,
165                       INTEGER_CODE, &line_num,
166                       SKIP_CODE, /* TOC num */
167                       STRING_CODE, &title,
168                       STRING_CODE, &stitle,
169                       STRING_CODE, &style,
170                       NULL)){
171
172
173       CC_String *key = new CC_String(nodeLocator);
174
175       // check for duplicate node locator 
176       BTCollectable *val = node_dict.findValue( key );
177       if ( val ) {
178         delete key;
179         dupID++;
180         
181         cerr << "(ERROR) Duplicate section ID    = " << nodeLocator << endl
182              << "               found in file    = " << filename << endl
183              << "                     at line    = " << line_num << endl
184              << "         is in conflict with    " << endl
185              << "                  section ID    = " << nodeLocator << endl
186              << "                     in file    = " << val->filename() << endl
187              << "                     at line    = " << val->linenum() << "\n\n";
188
189
190       }
191       else {
192         
193         BTCollectable *val = new BTCollectable( filename, 
194                                                 line_num,
195                                                 bookLocator);
196         node_dict.insertKeyAndValue( key, val );
197
198       }
199
200       stylesheet_smart_ptr sheet(base_ptr, style);
201       ostringstream strout;
202       sheet.its_oid().asciiOut(strout);
203       outstr = strout.str();
204
205       ncf->insert(STRING_CODE, nodeLocator,
206                   STRING_CODE, title,
207                   STRING_CODE, stitle,
208                   COMPRESSED_STRING_CODE, comp_agent, "",
209                   STRING_CODE, bookLocator,
210                   OID_CODE, "0.0", /* pointer to Book/CCF/DOC object */
211                   OID_CODE, (char *)outstr.c_str(),
212                   NULL);
213     }
214
215   }
216   else {
217     
218     while(cursor.next(STRING_CODE, &bookLocator,
219                       STRING_CODE, &nodeLocator,
220                       STRING_CODE, &filename,
221                       INTEGER_CODE, &line_num,
222                       SKIP_CODE, /* TOC num */
223                       STRING_CODE, &title,
224                       STRING_CODE, &stitle,
225                       STRING_CODE, &style,
226                       NULL)){
227
228       CC_String *key = new CC_String(nodeLocator);
229
230       // check for duplicate node locator 
231       BTCollectable *val = node_dict.findValue( key );
232       if ( val ) {
233         delete key;
234         dupID++;
235
236         
237         cerr << "(ERROR) Duplicate section ID    = " << nodeLocator << endl
238              << "               found in file    = " << filename << endl
239              << "                     at line    = " << line_num << endl
240              << "         is in conflict with    " << endl
241              << "                  section ID    = " << nodeLocator << endl
242              << "                     in file    = " << val->filename() << endl
243              << "                     at line    = " << val->linenum() << "\n\n";
244
245       }
246       else {
247         
248         BTCollectable *val = new BTCollectable( filename, 
249                                                 line_num, 
250                                                 bookLocator );
251         node_dict.insertKeyAndValue( key , val );
252
253       }
254       
255       
256       stylesheet_smart_ptr sheet(base_ptr, style);
257       char oid_buf[BUFSIZE];
258       ostringstream strout(oid_buf);
259       sheet.its_oid().asciiOut(strout);
260       strout << ends;
261       memcpy(oid_buf, strout.str().c_str(), BUFSIZE);
262       
263       ncf->insert(STRING_CODE, nodeLocator,
264                   STRING_CODE, title,
265                   STRING_CODE, stitle,
266                   STRING_CODE, "",
267                   STRING_CODE, bookLocator,
268                   OID_CODE, "0.0", /* pointer to Book/CCF/DOC object */
269                   OID_CODE, oid_buf,
270                   NULL);
271     }
272     
273   }
274
275   if ( dupID ) {
276     throw(Unexpected(
277       form("Number of duplicated section ID found = %d", dupID)
278       ));
279   }
280
281   delete ncf;
282   node_dict.clearAndDestroy();
283 }
284
285 //-------------------------------------------------------------------------
286 static void
287 usage(const char *progname)
288 {
289   fprintf(stderr, "usage: %s [-compressed] [-load-style] <bookcasename> <bookcasedir>\n", progname);
290   exit(1);
291 }
292
293 //-------------------------------------------------------------------------
294 int main(int argc, char **argv)
295 {
296   INIT_EXCEPTIONS();
297
298   set_new_handler( FreeStoreException );
299   
300   int ret = 1;
301   const char *progname = argv[0];
302   int compressed = 0;
303   int load_style_only = 0;
304
305 #ifdef FISH_DEBUG
306   DBUG_PROCESS(argv[0]);
307   if(getenv("FISH_DBUG")) DBUG_PUSH(getenv("FISH_DBUG"));
308 #endif
309   
310   argv++;
311   argc--;
312   
313   while(argc > 0 && argv[0][0] == '-'){
314     const char *opt = argv[0];
315     argv++;
316     argc--;
317     
318     if(strcmp(opt, "-compressed") == 0){
319       compressed = 1;
320     }
321     else if ( strcmp(opt, "-load-style") == 0 ) {
322       load_style_only = 1;
323     }
324     else {
325       usage(progname);
326     }
327   }
328   
329   if(argc == 2){
330     const char *base_name = argv[0];
331     const char *bookcaseDir = argv[1];
332
333     mtry{
334       BookCaseDB db(bookcaseDir);
335
336       if ( load_style_only ) {
337         writeStyleSheets(db);
338       }
339       else {
340         buildNCF(db, base_name, compressed);
341       }
342       ret = 0;
343     }
344
345     mcatch(PosixError&, pe){
346       fprintf(stderr, "%s: error on %s: %s\n",
347               progname, bookcaseDir, pe.msg());
348     }
349
350     mcatch(Unexpected&, pe) {
351       fprintf(stderr, "(ERROR) %s\n\n", pe.msg() );
352     }
353     
354     mcatch(mmdbException&, e) {
355       cerr << e;
356     }
357     
358     mcatch_any() {
359       fprintf(stderr, "*** Internal Error ***: unexpected exception\n");
360       abort();
361     }end_try;
362
363   }else{
364     usage(progname);
365   }
366
367   return ret;
368 }
369