Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / dtinfogen / infolib / etc / Dispatch.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: Dispatch.cc /main/2 1996/06/11 16:49:03 cde-hal $
24  *
25  * (c) Copyright 1996 Digital Equipment Corporation.
26  * (c) Copyright 1996 Hewlett-Packard Company.
27  * (c) Copyright 1996 International Business Machines Corp.
28  * (c) Copyright 1996 Sun Microsystems, Inc.
29  * (c) Copyright 1996 Novell, Inc. 
30  * (c) Copyright 1996 FUJITSU LIMITED.
31  * (c) Copyright 1996 Hitachi.
32  */
33
34 /* exported interfaces... */
35 #include "Dispatch.h"
36
37 /* imported interfaces... */
38 #include <assert.h>
39 #include <string.h>
40
41 #include "SGMLName.h"
42 #include "EntityScope.h"
43 #include "Token.h"
44 #include "FlexBuffer.h"
45 #include "SGMLDefn.h"
46 #include "SearchPath.h"
47
48 #include "OLAF.h"
49
50 Task *Dispatch::TaskObject = NULL;
51 Stack<int> *Dispatch::IgnoreStack = NULL;
52 int Dispatch::level = 0;
53 static EntityScope entity_stack;
54 Token*  Dispatch::tok = new Token();
55
56 char* Dispatch::f_file = NULL;
57 int   Dispatch::f_line = 0;
58
59 const char *Dispatch::tmpdir = NULL;
60 const char *Dispatch::srcdir = NULL;
61 SearchPath *Dispatch::search_path_table = NULL;
62
63 int Dispatch::tocgen_only = 0;
64
65 //---------------------------------------------------------------------
66 void
67 Dispatch::token(TOKEN_TYPE tokType, unsigned char *Name )
68 {
69
70   tok->setFileLine(f_file, f_line);
71
72   switch(tokType){
73   case START:
74     level++;
75     tok->StoreStartTag( Name, level );
76
77     if ( tok->LookupAttr( OLAF::OL_Ignore ) 
78          || tok->LookupAttr( OLAF::OL_ShortTitle ) )  {
79       IgnoreStack->push ( level );
80     }
81
82     break;
83
84   case END:
85     tok->StoreEndTag ( Name, level );
86     level--;
87     break;
88     
89   case INTERNAL_ENTITY:
90     tok->StoreEntity( Name , INTERNAL_ENTITY);
91     break;
92     
93   case EXTERNAL_ENTITY:
94     tok->StoreEntity( Name , EXTERNAL_ENTITY);
95
96     SGMLDefn *sgml_rec;
97     sgml_rec = entity_stack.LookupEntity( SGMLName::intern((char*)Name) );
98
99     if(sgml_rec){
100       tok->SetEntityValue( sgml_rec );
101     }else{
102       tok->reportError(Token::Internal, Token::Fatal,
103                       "Unable to find entity definition for %.50s", Name);
104     }
105     break;
106   }
107   
108
109   TaskObject->markup( *tok );
110
111   if ( tokType == END ) {
112     if ( !IgnoreStack->empty() ) {
113
114       int topelement = IgnoreStack->top();
115       if ( topelement == tok->level() ) {
116         topelement = IgnoreStack->pop();
117       }
118     }
119   }
120
121   delete tok;
122   tok = new Token();
123 }
124
125
126
127 //---------------------------------------------------------------------
128 void Dispatch::setRoot( Task *t, Stack<int> *istack)
129 {
130   assert(TaskObject == NULL);
131   TaskObject = t;
132
133   assert(IgnoreStack == NULL);
134   IgnoreStack = istack;
135   
136 }
137
138 //---------------------------------------------------------------------
139 SGMLDefn *
140 Dispatch::entity_ref( const char *ent_name )
141 {
142   int ent_num;
143   ent_num = SGMLName::intern(ent_name);
144   SGMLDefn *sgml_rec = entity_stack.LookupEntity( ent_num );
145   return sgml_rec;
146 }
147
148   
149 //---------------------------------------------------------------------
150 void
151 Dispatch::entity_decl( SGMLDefn *defn )
152 {
153   EntityList *escope = entity_stack.GetTopEntities();
154
155   assert(escope != NULL);
156
157   /*
158    * first clone up an SGMLDefn record for storage purposes
159    */
160   
161   SGMLDefn *sgmlRec = new SGMLDefn();
162   *sgmlRec = *defn;
163
164   escope->insert( sgmlRec );
165 }
166
167 //---------------------------------------------------------------------
168 void
169 Dispatch::file(const char *f)
170 {
171   delete f_file;
172   f_file = new char[strlen(f)+1];
173   strcpy(f_file, f);
174
175   
176   /*
177    * put directory of f_file into the search path also 
178    */
179   if(search_path_table){
180     // perform dirname first
181     char *dirname = strdup( f_file );
182     char *p = strrchr( dirname, '/' );
183     if ( p ) {
184       *p = '\0';
185     }
186     else {
187       strcpy( dirname,"." );
188     }
189     
190     search_path_table->replace_file_scope( dirname );
191     delete dirname;
192   }
193 }
194
195 //---------------------------------------------------------------------
196 void
197 Dispatch::subdoc_start()
198 {
199   EntityList *newrec = new EntityList();
200   entity_stack.push ( newrec );
201 }
202
203 //---------------------------------------------------------------------
204 void
205 Dispatch::subdoc_end()
206 {
207   EntityList *rec = entity_stack.pop();
208     
209   if ( !rec ) {
210     throw(Unexpected("SUBDOC end tag is not matched"));
211   }
212
213   delete rec;
214
215 }