Debian Codegraph @

dune/uggrid/low/ugenv.cc

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 /****************************************************************************/
6 /* */
7 /* File: ugenv.c */
8 /* */
9 /* Purpose: implements the ug environment mechanism */
10 /* */
11 /* Author: Peter Bastian */
12 /* Interdisziplinaeres Zentrum fuer Wissenschaftliches Rechnen */
13 /* Universitaet Heidelberg */
14 /* Im Neuenheimer Feld 368 */
15 /* 6900 Heidelberg */
16 /* */
17 /* History: 18.02.92 begin, ug version 2.0 */
18 /* */
19 /* Revision: 08.09.95 */
20 /* */
21 /****************************************************************************/
22
23 /****************************************************************************/
24 /* */
25 /* include files */
26 /* system include files */
27 /* application include files */
28 /* */
29 /****************************************************************************/
30
31 #include <config.h>
32 #include <array>
33 #include <cstdlib>
34 #include <cstring>
35 #include <cstdio>
36
37 #include "ugtypes.h"
38 #include "heaps.h"
39 #include "misc.h"
40 #include "ugenv.h"
41 /** \todo this is a hierarchy conflict, remove. (VR) */
42 #include <dune/uggrid/ugdevices.h>
43
44 USING_UG_NAMESPACE
45
46 /****************************************************************************/
47 /* */
48 /* defines in the following order */
49 /* */
50 /* compile time constants defining static data size (i.e. arrays) */
51 /* other constants */
52 /* macros */
53 /* */
54 /****************************************************************************/
55
56 #define MAXENVPATH 32 /* max depth of the environment tree*/
57
58 /****************************************************************************/
59 /* */
60 /* definition of variables global to this source file only (static!) */
61 /* */
62 /****************************************************************************/
63
64 /** \brief Path to current directory
65
66 We only need the first entry to be zero-initialized. */
67 static std::array<ENVDIR*, MAXENVPATH> path{nullptr};
68 static int pathIndex; /* entry to path array */
69
70
71 /****************************************************************************/
72 /** \brief Initialize the Environment and the heap
73
74 * @param heapSize - size of the heap in bytes
75
76 This function initializes the Environment and the heap.
77
78 @return <ul>
79 * <li> 0 if OK
80 * <li> '__LINE__' if not enough memory, error in initializing or allocating a
81 heap structure
82 </ul>
83 */
84 /****************************************************************************/
85
86 INT NS_PREFIX InitUgEnv ()
87 {
88 ENVDIR *root;
89
90 /* Environment heap already initialized? */
91 if (path[0])
92 return 0;
93
94 /* allocate root directory */
95 if ((root=(ENVDIR*)malloc(sizeof(ENVDIR)))==NULL) return(__LINE__);
96
97 root->type = ROOT_DIR;
98 root->next = root->previous = root->down = NULL;
99 strcpy(root->name,"root");
100
101 /* set path[0] */
102 pathIndex = 0;
103 path[0] = root;
104
105 /* return ok */
106 return(0);
107 }
108
109 /****************************************************************************/
110 /** \brief Change environment directory
111
112 This function changes environment directory.
113
114 @return <ul>
115 * <li> pointer to new environment directory </li>
116 * <li> NULL if directory not found. </li>
117 </ul>
118 */
119 /****************************************************************************/
120
121 ENVDIR * NS_PREFIX ChangeEnvDir (const char *s)
122 {
123 std::array<ENVDIR*, MAXENVPATH> newPath;
124 ENVDIR *theDir;
125 INT i,k,len;
126 char token[NAMESIZE];
127
128 if (s==NULL) return (NULL);
129
130 /* avoid trivial case */
131 if ((len=strlen(s))==0) return(NULL);
132
133 /* check max possible length */
134 if (len>=MAXENVPATH*NAMESIZE)
135 return (NULL);
136
137 /* look at first character */
138 if (strncmp(s,DIRSEP,1)==0)
139 {
140 /* start from root directory */
141 newPath[0] = path[0];
142 i = 0;
143 }
144 else
145 {
146 /* start from current directory */
147 for (k=0; k<=pathIndex; k++) newPath[k] = path[k];
148 i = pathIndex;
149 }
150
151 /* loop through input string */
152 do
153 {
154 if ((s = strntok(s,DIRSEP,NAMELEN,token))==NULL)
155 return (NULL);
156 if (*token=='\0')
157 continue;
158
159 /* process token */
160 if (strcmp(token,"..")==0)
161 {
162 /* one level up */
163 if (i>0) i--;
164 }
165 else
166 {
167 /* find subdirectory */
168 if (i+1>=MAXENVPATH) return(NULL); /* path too long */
169 theDir = (ENVDIR *) newPath[i]->down; /* search next level*/
170 while (theDir!=NULL)
171 {
172 if (theDir->type%2==1) /* is type odd ? */
173 {
174 if (strcmp(token,theDir->name)==0) /* name equal ? */
175 break; /* directory found */
176 }
177 theDir = (ENVDIR *) theDir->next;
178 }
179 if (theDir==NULL) return(NULL); /* not found error */
180 newPath[++i] = theDir; /* extend path */
181 }
182 }
183 while (*s!='\0');
184
185 /* path found, copy to current path */
186 for (k=0; k<=i; k++) path[k] = newPath[k]; /* copy path */
187 pathIndex = i;
188
189 /* return current directory */
190 return(path[pathIndex]);
191 }
192
193
194 /****************************************************************************/
195 /** \brief GetCurrentDir - Get current environment directory
196
197 @return <ul>
198 <li> pointer to current environment directory
199 </ul>
200 */
201 /****************************************************************************/
202
203 ENVDIR * NS_PREFIX GetCurrentDir ()
204 {
205 return(path[pathIndex]);
206 }
207
208 /****************************************************************************/
209 /** \brief Assemble pathname of current directory
210
211 @param s - pointer to buffer for the string
212
213 */
214 /****************************************************************************/
215
216 void NS_PREFIX GetPathName (char *s)
217 {
218 int i;
219
220 strcpy(s,DIRSEP);
221 for (i=1; i<=pathIndex; i++)
222 {
223 strcat(s,path[i]->name); strcat(s,DIRSEP);
224 }
225 }
226
227 /****************************************************************************/
228 /** \brief Allocate a new environment item in the current directory
229 *
230 * @param name - name of the new item
231 * @param type - type of item (5 types possible)
232 * @param size - size of item in bytes
233 *
234 * This function allocates a new environment item in the current directory.
235 *
236 * @return <ul>
237 * <li> pointer to new item
238 * <li> NULL if name too long or already in use, type equal to 'ROOT_DIR',
239 not enough memory
240 * <li> in the environment heap or other error occurs.
241 </ul>
242 */
243 /****************************************************************************/
244
245 ENVITEM * NS_PREFIX MakeEnvItem (const char *name, const INT type, const INT size)
246 {
247 ENVITEM *newItem,*lastItem;
248 ENVDIR *currentDir;
249
250 /* check if name too long */
251 if (strlen(name)+1>NAMESIZE)
252 return(NULL);
253
254 /* check if name not already used in this directory */
255 currentDir = path[pathIndex];
256 lastItem = currentDir->down;
257
258 #ifdef Debug
259 for (ENVITEM* anItem = lastItem; anItem != nullptr; anItem = anItem->v.next)
260 {
261 if ((anItem->v.type==type)&&(strcmp(anItem->v.name,name)==0))
262 {
263 UserWriteF("MakeEnvItem(): item '%s' already defined\n",anItem->v.name);
264 return(NULL);
265 }
266 lastItem = anItem;
267 }
268 #endif
269
270 /* allocate memory from environment heap */
271 switch (type)
272 {
273 case ROOT_DIR :
274 return(NULL);
275
276 default :
277 if (type%2==0)
278 {
279 /* new variable */
280 newItem=(ENVITEM*) malloc(size);
281
282 if (newItem==NULL)
283 {
284 UserWriteF("MakeEnvItem(): envHeap out of memory\n");
285 return(NULL);
286 }
287 memset(newItem,0,size);
288 }
289 else
290 {
291 /* new directory */
292 if (pathIndex+1>=MAXENVPATH) return(NULL);
293 newItem=(ENVITEM*) malloc(size);
294
295 if (newItem==NULL)
296 {
297 UserWriteF("MakeEnvItem(): envHeap out of memory\n");
298 return(NULL);
299 }
300 memset(newItem,0,size);
301 newItem->d.down = NULL;
302 }
303 break;
304 }
305
306 /* initialize item */
307 newItem->v.type = type;
308 newItem->v.locked = 1;
309 strcpy(newItem->v.name,name);
310
311 /* put in list */
312 if (lastItem==NULL)
313 {
314 /* directory is empty */
315 currentDir->down = newItem;
316 newItem->v.next = newItem->v.previous = NULL;
317 }
318 else
319 {
320 /* insert before first item */
321 newItem->v.previous = NULL;
322 currentDir->down->v.previous = newItem;
323 newItem->v.next = currentDir->down;
324 currentDir->down = newItem;
325 }
326
327 /* return pointer to new item */
328 return(newItem);
329 }
330
331
332 /****************************************************************************/
333 /** \brief Deallocate an environment item in the current directory
334
335 @param theItem - pointer to item
336
337 This function deallocates an environment item in the current directory.
338 Directories may only be deleted if they are empty.
339
340 @return <ul>
341 * <li> 0 if OK
342 * <li> 1 if item not found in current directory
343 * <li> 2 if attempt is done to delete non empty directory
344 * <li> 3 if attempt is done to delete locked item.
345 </ul>
346 */
347 /****************************************************************************/
348
349 INT NS_PREFIX RemoveEnvItem (ENVITEM *theItem)
350 {
351 ENVITEM *anItem;
352 ENVDIR *currentDir;
353
354 /* check if item is in current directory */
355 currentDir = path[pathIndex];
356 anItem = currentDir->down;
357 while (anItem!=NULL)
358 {
359 if (anItem==theItem) break;
360 anItem = anItem->v.next;
361 }
362 if (anItem==NULL) return(1);
363
364 /* check if item is allowed to be deleted */
365 if (theItem->v.locked) return(3);
366 if ((theItem->v.type%2==1)&&(theItem->d.down!=NULL)) return(2);
367
368 /* remove item from double linked list */
369 if (theItem->v.previous==NULL)
370 currentDir->down = theItem->v.next;
371 else
372 theItem->v.previous->v.next = theItem->v.next;
373 if (theItem->v.next!=NULL)
374 theItem->v.next->v.previous = theItem->v.previous;
375
376 /* deallocate memory */
377 free(theItem);
378
379 /* return ok */
380 return(0);
381 }
382
383 static
384 INT RemoveEnvDirContent (ENVITEM *theItem)
385 {
386 ENVITEM *Item,*Next;
387
388 for (Item = theItem; Item != NULL; Item = Next) {
389 Next = NEXT_ENVITEM(Item);
390 if (IS_ENVDIR(Item))
391 RemoveEnvDirContent(ENVITEM_DOWN(Item));
392 free(Item);
393 }
394
395 return(0);
396 }
397
398 /****************************************************************************/
399 /** \brief Deallocate an environment directory
400
401 * @param theItem - pointer to item
402
403 This function deallocates an environment directory.
404
405 @return <ul>
406 <li> 0 if OK
407 <li> 1 if item not found in current directory
408 <li> 2 if theItem is not a directory
409 <li> 3 if attempt is done to delete locked item
410 </ul>
411 */
412 /****************************************************************************/
413
414 INT NS_PREFIX RemoveEnvDir (ENVITEM *theItem)
415 {
416 ENVITEM *anItem;
417 ENVDIR *currentDir;
418
419 /* check if item is in current directory */
420 currentDir = path[pathIndex];
421 anItem = currentDir->down;
422 while (anItem!=NULL)
423 {
424 if (anItem==theItem) break;
425 anItem = anItem->v.next;
426 }
427 if (anItem==NULL) return(1);
428 if (!IS_ENVDIR(theItem)) return(2);
429 if (theItem->v.locked) return(3);
430 RemoveEnvDirContent(ENVITEM_DOWN(theItem));
431
432 /* remove item from double linked list */
433 if (theItem->v.previous==NULL)
434 currentDir->down = theItem->v.next;
435 else
436 theItem->v.previous->v.next = theItem->v.next;
437 if (theItem->v.next!=NULL)
438 theItem->v.next->v.previous = theItem->v.previous;
439
440 /* deallocate memory */
441 free(theItem);
442
443 return(0);
444 }
445
446 /****************************************************************************/
447 /** \brief Move an environment item in the tree structure
448
449 * @param item - pointer to item
450 * @param old - directory containing the item
451 * @param new - directory to which item has to be moved (if NULL move to root)
452
453 This function moves an environment item (or subtree) in the tree structure.
454
455 @return <ul>
456 * <li> 0 if OK
457 </ul>
458 */
459 /****************************************************************************/
460
461 INT NS_PREFIX MoveEnvItem (ENVITEM *item, ENVDIR *oldDir, ENVDIR *newDir)
462 {
463 ENVITEM *anItem;
464
465 if (newDir==NULL)
466 newDir = path[0];
467
468 for (anItem=ENVDIR_DOWN(oldDir); anItem!=NULL; anItem=NEXT_ENVITEM(anItem))
469 if (anItem==item) break;
470 if (anItem==NULL) return(1);
471
472 /* remove from old directory */
473 if (PREV_ENVITEM(item)!=NULL)
474 NEXT_ENVITEM(PREV_ENVITEM(item)) = NEXT_ENVITEM(item);
475 else
476 ENVDIR_DOWN(oldDir) = NEXT_ENVITEM(item);
477 if (NEXT_ENVITEM(item)!=NULL)
478 PREV_ENVITEM(NEXT_ENVITEM(item)) = PREV_ENVITEM(item);
479
480 /* insert in new directory */
481 PREV_ENVITEM(item) = NULL;
482 NEXT_ENVITEM(item) = ENVDIR_DOWN(newDir);
483 ENVDIR_DOWN(newDir) = item;
484
485 return (0);
486 }
487
488 /****************************************************************************/
489 /** \brief Search for a given name in the tree
490
491 * @param name - name of item searched for
492 * @param type - one of the five basic types
493 * @param dirtype - directory type to scan or SEARCHALL
494
495 This function searches a given name in the tree, sets current directory
496 to directory of the item found and returns the item.
497
498 @return <ul>
499 * <li> pointer to the item in the tree
500 * <li> NULL if name not found or where not OK.
501 </ul>
502 */
503 /****************************************************************************/
504
505 static ENVITEM *SearchTree (const char *name, INT type, INT dirtype)
506 {
507 ENVDIR *currentDir;
508 ENVITEM *theItem,*result;
509
510 currentDir = path[pathIndex];
511
512 /* first loop in current directory */
513 theItem = currentDir->down;
514 while (theItem!=NULL)
515 {
516 if (theItem->v.type == type)
517 if (strcmp(theItem->v.name,name)==0)
518 return(theItem);
519 theItem = theItem->v.next;
520 }
521
522 /* try recursive search */
523 theItem = currentDir->down;
524 while (theItem!=NULL)
525 {
526 if (theItem->v.type%2 == 1)
527 {
528 /* this is a directory */
529 if ((theItem->d.type==dirtype)||(dirtype==SEARCHALL))
530 {
531 path[++pathIndex] = (ENVDIR *) theItem;
532 if ((result=SearchTree(name,type,dirtype))!=NULL)
533 return(result);
534 pathIndex--;
535 }
536 }
537 theItem = theItem->v.next;
538 }
539
540 /* return not found */
541 return(NULL);
542 }
543
544 /****************************************************************************/
545 /** \brief Search for a given name in the tree
546 *
547 * @param name - name of item searched for
548 * @param type - one of the five basic types
549 * @param where - path to directory where recursive search starts
550 * @param dirtype - directory type to scan or SEARCHALL
551 *
552 * This function searches a given name in the tree, sets current directory
553 * to directory of the item found and returns the item.
554 *
555 * @return <ul>
556 <li> pointer to item in the tree
557 <li> NULL if name not found or where not OK.
558 </ul>
559 */
560 /****************************************************************************/
561
562 ENVITEM * NS_PREFIX SearchEnv (const char *name, const char *where, INT type, INT dirtype)
563 {
564 /* check if search directory is changed */
565 if (strcmp(where,".")!=0)
566 if (ChangeEnvDir(where)==NULL) return(NULL);
567
568 /* recursive search */
569 return(SearchTree(name,type,dirtype));
570 }
571
572 /****************************************************************************/
573 /** \brief For backward compatibility: Print size and used of environment heap to string
574
575 * @param s - string to print on
576
577 This function used to print size and used percentage of the environment heap
578 to a string. Ever since the operating system heap is used this information
579 is not available anymore.
580 */
581 /****************************************************************************/
582
583 void NS_PREFIX EnvHeapInfo (char *s)
584 {
585 sprintf(s,"no heap information available\n");
586 }
587
588 /****************************************************************************/
589 /** \brief Return a unique 'ID' for a new 'ENVDIR' type
590
591 This function returns a unique 'ID' for a new 'ENVDIR' type.
592
593 @return 'ID' for the new 'ENVDIR'
594 */
595 /****************************************************************************/
596
597 INT NS_PREFIX GetNewEnvDirID (void)
598 {
599 /* NB: ENVDIRs have odd types and start with 1 */
600 static INT theNewEnvDirID = 1;
601
602 theNewEnvDirID += 2;
603
604 return (theNewEnvDirID);
605 }
606
607 /****************************************************************************/
608 /** \brief Return a unique 'ID' for a new 'ENVVAR' type
609
610 This function returns a unique 'ID' for a new 'ENVVAR' type.
611
612 @return 'ID' for the new 'ENVVAR'
613 */
614 /****************************************************************************/
615
616 INT NS_PREFIX GetNewEnvVarID ()
617 {
618 /* NB: ENVVARs have even types and start with 2 */
619 static INT theNewEnvVarID = 0;
620
621 theNewEnvVarID += 2;
622
623 return (theNewEnvVarID);
624 }
625
626
627 INT NS_PREFIX ExitUgEnv()
628 {
629 RemoveEnvDirContent((ENVITEM*)path[0]);
630 path[0] = NULL;
631 return 0;
632 }