Remove Unixware and openserver support
[oweals/cde.git] / cde / lib / tt / bin / tttar / tttar_spec.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 //%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                  
24 //%%  (c) Copyright 1993, 1994 International Business Machines Corp.    
25 //%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                   
26 //%%  (c) Copyright 1993, 1994 Novell, Inc.                             
27 //%%  $XConsortium: tttar_spec.C /main/3 1995/10/20 17:00:42 rswiston $                                                         
28 /*
29  * tttar_spec.cc - Implementation of specs for Link Service/ToolTalk archiving
30  *
31  * Copyright (c) 1990 by Sun Microsystems, Inc.
32  *
33  */
34
35 #if defined(__osf__) || defined(linux) || defined(CSRG_BASED) || defined(sun)
36 #include <unistd.h>
37 #else
38 #include <osfcn.h>
39 #endif /* __osf__ */
40 #include "api/c/tt_c.h"
41 #include "util/tt_iostream.h"
42 #include "tttar_utils.h"
43 #include "tttar_spec.h"
44
45 /*
46  * Lstar_spec::Lstar_spec()
47  */
48 Lstar_spec::
49 Lstar_spec()
50 {
51         _props = new Lstar_spec_prop_list();
52 }
53
54 /*
55  * Lstar_spec::Lstar_spec() - 
56  */
57 Lstar_spec::
58 Lstar_spec(  _Tt_string id, _Tt_string path )
59 {
60         _id = id;
61         _path = path;
62         _props = new Lstar_spec_prop_list();
63 }
64
65 /*
66  * Lstar_spec::~Lstar_spec()
67  */
68 Lstar_spec::
69 ~Lstar_spec()
70 {
71 }
72
73 /*
74  * Lstar_spec::xdr()
75  */
76 bool_t Lstar_spec::
77 xdr( XDR *xdrs )
78 {
79         if (! this->_id.xdr(xdrs)) {
80                 return FALSE;
81         }
82         if (! this->_path.xdr(xdrs)) {
83                 return FALSE;
84         }
85         if (! this->_type.xdr(xdrs)) {
86                 return FALSE;
87         }
88         if (! this->_props.xdr(xdrs)) {
89                 return FALSE;
90         }
91         return TRUE;
92 }
93
94 /*
95  * Lstar_spec::read_self() - Read from ToolTalk everything we need to know
96  *      about ourselves in order to archive ourself.
97  */
98 Tt_status Lstar_spec::
99 read_self()
100 {
101         /*
102          * Get the spec's type.
103          */
104         note_ptr_err( tt_spec_type( (char *)_id ));
105         if (IS_TT_ERR(err_noted)) {
106                 return err_noted;
107         }
108         _type = ptr_returned;
109         /*
110          * Get how many properties are on the spec.
111          */
112         note_int_err( tt_spec_propnames_count( (char *)_id ));
113         if (IS_TT_ERR(err_noted)) {
114                 return err_noted;
115         }
116         int num_props = int_returned;
117         /*
118          * Push the spec's properties onto our list in reverse order,
119          * to preserve the admittedly meaningless order they had.
120          */
121         for (int n = num_props - 1; n >= 0; n--) {
122                 note_ptr_err( tt_spec_propname( (char *)_id, n ));
123                 switch (err_noted) {
124                     case TT_ERR_OBJID:
125                     case TT_ERR_DBAVAIL:
126                         return err_noted;
127                     case TT_ERR_NUM:
128                         continue;
129                     case TT_ERR_DBEXIST:
130                     default:
131                         if (IS_TT_ERR(err_noted)) {
132                                 return err_noted;
133                         }
134                 }
135                 _Tt_string              propname = ptr_returned;
136                 Lstar_spec_prop_ptr     prop_ptr;
137
138                 prop_ptr = new Lstar_spec_prop( _id, propname );
139                 this->_props->push( prop_ptr );
140         }
141         return err_noted;
142 }
143
144 /*
145  * Lstar_spec::write_self() - Recreate a spec like the one we are, returning
146  *      the id of the spec created.  The string returned must be freed
147  *      using tt_free().
148  */
149 char * Lstar_spec::
150 write_self( _Tt_string where, bool_t preserve__props, Tt_status *err )
151 {
152         char           *spec_created = NULL;
153         _Tt_string      path;
154
155         if (this->_path.len() <= 0) {
156                 *err = TT_ERR_PATH;
157                 return NULL;
158         }
159         /*
160          * TO_DO: tt_spec_create() won't convert /./ to / in a path
161          * if the path doesn't exist.
162          */
163         if (this->_path.left(2) == "./") {
164                 this->_path = this->_path.right( _path.len() - 2 );
165         }
166         /*
167          * If the archived path is absolute, ignore <where>.
168          */
169         if (this->_path[0] == '/') {
170                 path = this->_path;
171         } else {
172                 path = where.cat( "/" ).cat( this->_path );
173         }
174         note_ptr_err( tt_spec_create( (char *)path ));
175         *err = err_noted;
176         spec_created = ptr_returned;
177         if (IS_TT_ERR(err_noted)) {
178                 return spec_created;
179         }
180         note_err( tt_spec_type_set( spec_created, (char *)this->_type ));
181         *err = err_noted;
182         if (IS_TT_ERR(err_noted)) {
183                 return spec_created;
184         }
185         Lstar_spec_prop_list_cursor prop_cursor( this->_props );
186         while (prop_cursor.next()) {
187                 *err = prop_cursor->write_self( spec_created, preserve__props );
188         }
189         note_err( tt_spec_write( spec_created ));
190         *err = err_noted;
191         return spec_created;
192 }
193
194 /*
195  * Lstar_spec::print()
196  */
197 void Lstar_spec::
198 print( FILE *fs ) const
199 {
200         fprintf( fs, "spec id: "  );
201         this->_id.print( fs );
202         fprintf( fs, "\nspec type: " );
203         this->_type.print( fs );
204         fprintf( fs, "\nspec path: " );
205         this->_path.print( fs );
206         fprintf( fs, "\n" );
207         this->_props->print(Lstar_spec::do_print, fs );
208         fprintf( fs, "\n" );
209 }
210
211 implement_list_of(Lstar_spec_prop)
212
213 /*
214  * Lstar_spec_prop::Lstar_spec_prop()
215  */
216 Lstar_spec_prop::
217 Lstar_spec_prop()
218 {
219 }
220
221 /*
222  * Lstar_spec_prop::Lstar_spec_prop()
223  */
224 Lstar_spec_prop::
225 Lstar_spec_prop( _Tt_string id, _Tt_string propname )
226 {
227         _propname = propname;
228         _values = new _Tt_string_list();
229         /*
230          * Get how many values are in the spec's property.
231          */
232         note_int_err( tt_spec_prop_count( (char *)id, (char *)propname ));
233         switch (err_noted) {
234             case TT_ERR_PROPNAME:
235             case TT_ERR_OBJID:
236             case TT_ERR_DBAVAIL:
237                 return;
238             case TT_ERR_DBEXIST:
239             default:
240                 if (IS_TT_ERR(err_noted)) {
241                         return;
242                 }
243         }
244         int num_values = int_returned;
245         /*
246          * Push the property's values onto our list in reverse order,
247          * to preserve the order they had.
248          */
249         for (int n = num_values - 1; n >= 0; n--) {
250                 int             len;
251                 unsigned char  *value;
252
253                 note_err( tt_spec_bprop( (char *)id, (char *)propname, n, &value, &len ));
254                 switch (err_noted) {
255                     case TT_ERR_PROPNAME:
256                     case TT_ERR_OBJID:
257                     case TT_ERR_DBAVAIL:
258                         return;
259                     case TT_ERR_NUM:
260                         continue;
261                     case TT_ERR_DBEXIST:
262                     default:
263                         if (IS_TT_ERR(err_noted)) {
264                                 return;
265                         }
266                 }
267                 _Tt_string val( value, len );
268                 this->_values->push( val );
269         }
270 }
271
272 /*
273  * Lstar_spec_prop::~Lstar_spec_prop()
274  */
275 Lstar_spec_prop::
276 ~Lstar_spec_prop()
277 {
278 }
279
280 /*
281  * Lstar_spec_prop::xdr()
282  */
283 bool_t Lstar_spec_prop::
284 xdr( XDR *xdrs )
285 {
286         if (! this->_propname.xdr(xdrs)) {
287                 return FALSE;
288         }
289         if (! this->_values.xdr(xdrs)) {
290                 return FALSE;
291         }
292         return TRUE;
293 }
294
295 /*
296  * Lstar_spec_prop::write_self() - Write this prop onto the given spec.
297  */
298 Tt_status Lstar_spec_prop::
299 write_self( char *spec_id, bool_t preserve__props )
300 {
301         /*
302          * If we're not root and this is a blessed property,
303          * do not attempt to write it.
304          * Insert link here to policy statement about prop names in tt_c.h.
305          */
306         if (    (_propname[0] == '_')
307              && (    (! preserve__props)
308                   || (getuid() != 0)))
309         {
310                 return TT_OK;
311         }
312         _Tt_string_list_cursor value_cursor( this->_values );
313         char    *val;
314         int     len;
315         while (value_cursor.next()) {
316                 val = (char *)(*value_cursor);
317                 len = (*value_cursor).len();
318                 note_err( tt_spec_bprop_add( spec_id, (char *)_propname,
319                                             (unsigned char *)val, len));
320                 if (IS_TT_ERR(err_noted)) {
321                         return err_noted;
322                 }
323         }
324         return err_noted;
325 }
326
327 /*
328  * Lstar_spec_prop::print()
329  */
330 void Lstar_spec_prop::
331 print( FILE *fs ) const
332 {
333         this->_propname.print( fs );
334         fprintf( fs, ": " );
335         this->_values->print(_tt_string_print,fs);
336         fprintf( fs, "\n" );
337 }
338
339 void Lstar_spec::
340 do_print(const _Tt_ostream &os, const _Tt_object *obj)
341 {
342         ((Lstar_spec *)obj)->print(os.theFILE());
343 }
344
345 void Lstar_spec_prop::
346 do_print(FILE *fs, const _Tt_object *obj)
347 {
348         ((Lstar_spec_prop *)obj)->print(fs);
349 }
350