aboutsummaryrefslogtreecommitdiff
path: root/gnu/lib/libdialog/tree.c
blob: ceacf05f0e59713a219904e26a0d47d144c3f67f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
/*
 * tree.c -- implements the 'tree' interface element for libdialog
 *
 * Author: Anatoly A. Orehovsky (tolik@mpeks.tomsk.su)
 *
 * Copyright (c) 1997, Anatoly A. Orehovsky
 * 09/28/98 - patched by Anatoly A. Orehovsky (smart_tree())
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <stdlib.h>
#include <strings.h>
#include <stdio.h>
#include <dialog.h>
#include "dialog.priv.h"
#include <ncurses.h>

/* static utils for make tree */
struct leaf {
	unsigned char *name;		/* name of leaf */
	unsigned char *branches;	/* branches that going by leaf */
	unsigned char slip;		/* slip of leaf*/
	int shift;			/* shift relative root of tree */
};

static int	mk_slip(struct leaf array[], int arr_size, 
			int number, int shift);

/* make tree from file
 *
 * filename	- name of file with like find(1) output
 * p_names	- pointer to array of strings
 * p_size	- pointer to size of array
 * FS		- fields separator
 * p_array	- pointer to array of leafs
 *
 * return values:
 * 0		- ok and names by p_names, size by p_size, array by p_array set
 * -1		- memory allocation error (errno set)
 */  

static int	mk_ftree(char *filename, 
		unsigned char ***p_names, int *p_size, unsigned char FS, 
			struct leaf **p_array);
		
/* make tree from array
 *
 * names	- array of strings
 * size		- size of array
 * FS		- fields separator
 * p_array	- pointer to array of leafs
 *
 * return values:
 * 0		- ok and array by p_array set
 * -1		- memory allocation error (errno set)
 */  
 
static int	mk_tree(unsigned char **names, int size, unsigned char FS, 
			struct leaf **p_array);

/* free memory from tree (leafs)
 *
 * return values:
 * nothing
 */

static void	free_leafs(struct leaf *array, int size);

/* free memory from source data for tree (names)
 *
 * return values:
 * if 0 <= choice <= size - pointer to name from names, 
 *	and memory for name not released (must be freed later)
 * else - NULL (recomended choice -1 for it)
 */

static unsigned char	*free_names(unsigned char **names, 
					int size, int choice);

/* end of static utils for make tree */

/* static utils for ftree */

/* control struct for queue */
struct queue {
	int size;			/* size of queue */
	struct m_queue *first;		/* begin of queue */
	struct m_queue *last;		/* end of queue */
};

/* queue member */
struct m_queue {
	void *pointer;			/* queue member */
	struct m_queue *next;		/* next queue member */
};

/* init struct queue by zeros */
static void	init_queue(struct queue *queue);

/* add pointer to queue */
/* return - pointer or NULL if error */
static void	*p2_queue(struct queue *queue, void *pointer);

/* get first from queue */
/* return - pointer or NULL if queue is empty */
static void	*first_queue(struct queue *queue);

/* make zero terminated array from queue */
/* return - pointer to array or NULL if error */
static void	**q2arr(struct queue *queue, int depth);

/* smart_tree (for like find(1) with -d flag output compliance) */
/* return - not NULL or NULL if malloc error */
static unsigned char	*smart_tree(struct queue *queue, unsigned char FS,
					unsigned char *current,
					unsigned char *prev);

/* end of static utils for ftree */

/* static utils for saved_tree */

/* saved values for unique tree */
struct saved_tree {
	unsigned char **names;	/* names + */ 
	int size;		/* size + */
	unsigned char FS;	/* FS + */
	int height;		/* height + */
	int width;		/* width + */
	int menu_height;	/* menu_height - unique for treebox ? */
	int ch;			/* saved ch - choice */
	int sc;			/* saved sc - scroll */
};

/* search saved tree within queue */
/* return - struct saved_tree * or NULL if not found */
static struct saved_tree *search_saved_tree(struct queue *queue, 
					unsigned char **names,
					int size,
					unsigned char FS,
					int height,
					int width,
					int menu_height);

/* end of static utils for saved_tree */

static void print_item(WINDOW *win, struct leaf item, int choice, int selected);

static void print_position(WINDOW *win, int x, int y,
				int cur_pos, int size);

static int menu_width, item_x;

static int dialog_treemenu(unsigned char *title, unsigned char *prompt, 
			int height, int width, int menu_height, 
				int item_no, struct leaf items[], 
					int *result, 
						int *ch, int *sc);

/*
 * Display a menu for choosing among a number of options
 */
static
int dialog_treemenu(unsigned char *title, unsigned char *prompt, 
			int height, int width, int menu_height, 
				int item_no, struct leaf items[], 
					int *result, 
						int *ch, int *sc)
{
  int i, j, x, y, cur_x, cur_y, box_x, box_y, key = 0, button = 0, choice = 0,
      l, scroll = 0, max_choice, redraw_menu = FALSE;
  WINDOW *dialog, *menu;

  if (ch)  /* restore menu item info */
      choice = *ch;
  if (sc)
      scroll = *sc;

  max_choice = MIN(menu_height, item_no);

  item_x = 0;
  /* Find length of longest item in order to center menu */
  for (i = 0; i < item_no; i++) {
    l = strlen(items[i].name) + strlen(items[i].branches) * 4 + 4;
    item_x = MAX(item_x, l);
  }
  
  if (height < 0)
	height = strheight(prompt)+menu_height+4+2;
  if (width < 0) {
	i = strwidth(prompt);
	j = ((title != NULL) ? strwidth(title) : 0);
	width = MAX(i,j);
	width = MAX(width,item_x+4)+4;
  }
  width = MAX(width,24);

  if (width > COLS)
	width = COLS;
  if (height > LINES)
	height = LINES;
  /* center dialog box on screen */
  x = (COLS - width)/2;
  y = (LINES - height)/2;

#ifdef HAVE_NCURSES
  if (use_shadow)
    draw_shadow(stdscr, y, x, height, width);
#endif
  dialog = newwin(height, width, y, x);
  if (dialog == NULL) {
    endwin();
    fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
    exit(1);
  }
  keypad(dialog, TRUE);

  draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
  wattrset(dialog, border_attr);
  wmove(dialog, height-3, 0);
  waddch(dialog, ACS_LTEE);
  for (i = 0; i < width-2; i++)
    waddch(dialog, ACS_HLINE);
  wattrset(dialog, dialog_attr);
  waddch(dialog, ACS_RTEE);
  wmove(dialog, height-2, 1);
  for (i = 0; i < width-2; i++)
    waddch(dialog, ' ');

  if (title != NULL) {
    wattrset(dialog, title_attr);
    wmove(dialog, 0, (width - strlen(title))/2 - 1);
    waddch(dialog, ' ');
    waddstr(dialog, title);
    waddch(dialog, ' ');
  }
  wattrset(dialog, dialog_attr);
  wmove(dialog, 1, 2);
  print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE);

  menu_width = width-6;
  getyx(dialog, cur_y, cur_x);
  box_y = cur_y + 1;
  box_x = (width - menu_width)/2 - 1;

  /* create new window for the menu */
  menu = subwin(dialog, menu_height, menu_width, y + box_y + 1, x + box_x + 1);
  if (menu == NULL) {
    endwin();
    fprintf(stderr, "\nsubwin(dialog,%d,%d,%d,%d) failed, maybe wrong dims\n", menu_height,menu_width,y+box_y+1,x+box_x+1);
    exit(1);
  }
  keypad(menu, TRUE);

  /* draw a box around the menu items */
  draw_box(dialog, box_y, box_x, menu_height+2, menu_width+2, menubox_border_attr, menubox_attr);

  item_x = 1;

  /* Print the menu */
  for (i = 0; i < max_choice; i++)
    print_item(menu, items[(scroll+i)], i, i == choice);
  wnoutrefresh(menu);
  print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, item_x, cur_x, cur_y);
  print_position(dialog, box_x+menu_width, box_y+menu_height, scroll+choice, item_no);

  display_helpline(dialog, height-1, width);

  x = width/2-11;
  y = height-2;
  print_button(dialog, "Cancel", y, x+14, FALSE);
  print_button(dialog, "  OK  ", y, x, TRUE);

  wrefresh(dialog);

  while (key != ESC) {
    key = wgetch(dialog);
    /* Check if key pressed matches first character of any item tag in menu */

    if (key == KEY_UP || key == KEY_DOWN || key == '-' || key == '+') {
     if (key == KEY_UP || key == '-') {
        if (!choice) {
          if (scroll) {
#ifdef BROKEN_WSCRL
    /* wscrl() in ncurses 1.8.1 seems to be broken, causing a segmentation
       violation when scrolling windows of height = 4, so scrolling is not
       used for now */
            scroll--;
            getyx(dialog, cur_y, cur_x);    /* Save cursor position */
            /* Reprint menu to scroll down */
            for (i = 0; i < max_choice; i++)
              print_item(menu, items[(scroll+i)], i, i == choice);

#else

            /* Scroll menu down */
            getyx(dialog, cur_y, cur_x);    /* Save cursor position */
            if (menu_height > 1) {
              /* De-highlight current first item before scrolling down */
              print_item(menu, items[scroll], 0, FALSE);
              scrollok(menu, TRUE);
              wscrl(menu, -1);
              scrollok(menu, FALSE);
            }
            scroll--;
            print_item(menu, items[scroll], 0, TRUE);
#endif
            wnoutrefresh(menu);
	    print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, item_x, cur_x, cur_y);
  	    print_position(dialog, box_x+menu_width, box_y+menu_height, scroll+choice, item_no);	    
  	    wmove(dialog, cur_y, cur_x);  /* Restore cursor to previous position */        
            wrefresh(dialog);
          }
          continue;    /* wait for another key press */
        }
        else
          i = choice - 1;
      }
      else if (key == KEY_DOWN || key == '+') {
        if (choice == max_choice - 1) {
          if (scroll+choice < item_no-1) {
#ifdef BROKEN_WSCRL
    /* wscrl() in ncurses 1.8.1 seems to be broken, causing a segmentation
       violation when scrolling windows of height = 4, so scrolling is not
       used for now */
            scroll++;
            getyx(dialog, cur_y, cur_x);    /* Save cursor position */
            /* Reprint menu to scroll up */
            for (i = 0; i < max_choice; i++)
              print_item(menu, items[(scroll+i)], i, i == choice);

#else

            /* Scroll menu up */
            getyx(dialog, cur_y, cur_x);    /* Save cursor position */
            if (menu_height > 1) {
              /* De-highlight current last item before scrolling up */
              print_item(menu, items[(scroll+max_choice-1)], max_choice-1, FALSE);
              scrollok(menu, TRUE);
              scroll(menu);
              scrollok(menu, FALSE);
            }
            scroll++;
              print_item(menu, items[(scroll+max_choice-1)], max_choice-1, TRUE);
#endif
            wnoutrefresh(menu);
	    print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, item_x, cur_x, cur_y);
  	    print_position(dialog, box_x+menu_width, box_y+menu_height, scroll+choice, item_no);	    
  	    wmove(dialog, cur_y, cur_x);  /* Restore cursor to previous position */        
            wrefresh(dialog);
          }
          continue;    /* wait for another key press */
        }
        else
          i = choice + 1;
      }

      if (i != choice) {
        /* De-highlight current item */
        getyx(dialog, cur_y, cur_x);    /* Save cursor position */
        print_item(menu, items[(scroll+choice)], choice, FALSE);

        /* Highlight new item */
        choice = i;
        print_item(menu, items[(scroll+choice)], choice, TRUE);
        wnoutrefresh(menu);
        print_position(dialog, box_x+menu_width, box_y+menu_height, scroll+choice, item_no);
        wmove(dialog, cur_y, cur_x);  /* Restore cursor to previous position */        
        wrefresh(dialog);
      }
      continue;    /* wait for another key press */
    }

    /* save info about menu item position */
    if (ch)
	*ch = choice;
    if (sc)
	*sc = scroll;

    switch (key) {
    case KEY_PPAGE:
    case 'B' :
    case 'b' :
	if (scroll > menu_height) {	/* can we go up? */
	    scroll -= (menu_height);
	} else {
	    scroll = 0;
	}
	redraw_menu = TRUE;
	break;
    case KEY_NPAGE:
    case 'F' :
    case 'f' :
	if (scroll + menu_height >= item_no-1 - menu_height) { /* can we go down a full page? */
	    scroll = item_no - menu_height;
	    if (scroll < 0) scroll = 0;
	} else {
	    scroll += menu_height;
	}
	redraw_menu = TRUE;
	break;
    case KEY_HOME:
    case 'g' :
	scroll = 0;
	choice = 0;
	redraw_menu = TRUE;
	break;
    case KEY_END:
    case 'G' :
	scroll = item_no - menu_height;
	if (scroll < 0) scroll = 0;
	choice = max_choice - 1;
	redraw_menu = TRUE;
	break;
    case 'O':
    case 'o':
        delwin(dialog);
	*result = scroll+choice;
        return 0;
    case 'C':
    case 'c':
        delwin(dialog);
        return 1;
    case KEY_BTAB:
    case TAB:
    case KEY_LEFT:
    case KEY_RIGHT:
        if (!button) {
          button = 1;    /* Indicates "Cancel" button is selected */
          print_button(dialog, "  OK  ", y, x, FALSE);
          print_button(dialog, "Cancel", y, x+14, TRUE);
        }
        else {
          button = 0;    /* Indicates "OK" button is selected */
          print_button(dialog, "Cancel", y, x+14, FALSE);
          print_button(dialog, "  OK  ", y, x, TRUE);
        }
        wrefresh(dialog);
        break;
    case ' ':
    case '\r':
    case '\n':
        delwin(dialog);
        if (!button)
	  *result = scroll+choice;
        return button;
    case ESC:
        break;
    case KEY_F(1):
    case '?':
	display_helpfile();
	break;
    }
    if (redraw_menu) {
	for (i = 0; i < max_choice; i++) {
	    print_item(menu, items[(scroll+i)],
		       i, i == choice);
	}
	wnoutrefresh(menu);
        getyx(dialog, cur_y, cur_x);    /* Save cursor position */	
	print_arrows(dialog, scroll, menu_height, item_no, box_x, box_y, item_x, cur_x, cur_y);
  	print_position(dialog, box_x+menu_width, box_y+menu_height, scroll+choice, item_no);	
  	wmove(dialog, cur_y, cur_x);  /* Restore cursor to previous position */        
	wrefresh(dialog);
	redraw_menu = FALSE;
    }
  }

  delwin(dialog);
  return -1;    /* ESC pressed */
}
/* End of dialog_treemenu() */


/*
 * Print menu item
 */
static void print_item(WINDOW *win, struct leaf item, int choice, int selected)
{
  int i, j = menu_width - 2;
  char *branches = item.branches;

  /* Clear 'residue' of last item */
  wattrset(win, menubox_attr);
  wmove(win, choice, 0);
  for (i = 0; i < menu_width; i++)
    waddch(win, ' ');
  wmove(win, choice, item_x);

  while(*branches && j)
  {
  	switch (*branches++) {
  	case ' ' : waddch(win, ' ');
  		break;
  	case '|' : waddch(win, ACS_VLINE);
  	}
  	
  	j--;
  	i = 3;
  	while(i-- && j)
  	{
	  	waddch(win, ' ');
	  	j--;
	}
  }	
  
  if (j)
  {
	  switch (item.slip) {
	  case '+' : waddch(win, ACS_LTEE);
  		break;
	  case '`' : waddch(win, ACS_LLCORNER);
	  }
	  j--;
  }

  i = 3;
  while(i-- && j)
  {
  	waddch(win, ACS_HLINE);
  	j--;
  }
  
  wattrset(win, selected ? item_selected_attr : item_attr);
  if (j)
	  waddnstr(win, item.name, j);
}
/* End of print_item() */

/*
 * Print current position
 */
static void print_position(WINDOW *win, int x, int y, 
					int cur_pos, int size)
{
  int percent;

  wattrset(win, position_indicator_attr);
  percent = cur_pos == size - 1 ? 100 : (cur_pos * 100)/(size - 1);
  wmove(win, y + 1, x - 6);
  wprintw(win, "(%3d%%)", percent);
}
/* End of print_position() */

/*
 * Display a tree menu from file
 *
 * filename	- file with like find(1) output
 * FS		- fields separator
 * title	- title of dialog box
 * prompt	- prompt text into dialog box
 * height	- height of dialog box
 * width	- width of dialog box
 * menu_height	- height of menu box
 * result	- pointer to char array
 *
 * return values:
 * -1		- ESC pressed
 * 0		- Ok, result set (must be freed later)
 * 1		- Cancel
 */

int dialog_ftree(unsigned char *filename, unsigned char FS,
		unsigned char *title, unsigned char *prompt, 
			int height, int width, int menu_height, 
					unsigned char **result)
{
	int retcode, choice, size;
	struct leaf *items;
	unsigned char **names;
	
	if (mk_ftree(filename, &names, &size, FS, &items))
	{
		perror("dialog_ftree");
		end_dialog();
		exit(-1);
	}
	
	if (!size)
	{
		fprintf(stderr, "\ndialog_ftree: file %s is empty\n", filename);
		end_dialog();
		exit(-1);
	}
	
	retcode = dialog_treemenu(title, prompt, height, width, menu_height,
					size, items, &choice, NULL, NULL);
					
	free_leafs(items, size);
	
	if (!retcode)
		*result = free_names(names, size, choice);
	else
		(void)free_names(names, size, -1);	
					
	return retcode;
}
/* End of dialog_ftree() */

/*
 * Display a tree menu from array
 *
 * names	- array with like find(1) output
 * size		- size of array
 * FS		- fields separator
 * title	- title of dialog box
 * prompt	- prompt text into dialog box
 * height	- height of dialog box
 * width	- width of dialog box
 * menu_height	- height of menu box
 * result	- pointer to char array
 *
 * return values:
 * -1		- ESC pressed
 * 0		- Ok, result set
 * 1		- Cancel
 */

int dialog_tree(unsigned char **names, int size, unsigned char FS,
		unsigned char *title, unsigned char *prompt, 
			int height, int width, int menu_height, 
					unsigned char **result)
{
	int retcode, choice;
	struct leaf *items;
	struct saved_tree *st;
	static struct queue *q_saved_tree = NULL;
	
	if (!size)
	{
		fprintf(stderr, "\ndialog_tree: source array is empty\n");
		end_dialog();
		exit(-1);
	}	
	
	if (mk_tree(names, size, FS, &items))
	{
		perror("dialog_tree");
		end_dialog();
		exit(-1);
	}

/* is tree saved ? */
	if (!(st = search_saved_tree(q_saved_tree, names, 
					size, FS,
					height, width, menu_height))) {
		if (!q_saved_tree) {
			if (!(q_saved_tree = 
				calloc(sizeof (struct queue), 1))) {
				perror("dialog_tree");
				end_dialog();
				exit(-1);
			}
		}

		if (!(st = calloc(sizeof (struct saved_tree), 1))) {
			perror("dialog_tree");
			end_dialog();
			exit(-1);
		}
		
		st->names = names;
		st->size = size;
		st->FS = FS;
		st->height = height;
		st->width = width;
		st->menu_height = menu_height;
		
		if (!p2_queue(q_saved_tree, st)) {
			perror("dialog_tree");
			end_dialog();
			exit(-1);
		}
	}
	
	retcode = dialog_treemenu(title, prompt, height, width, menu_height,
					size, items, &choice, 
					&(st->ch), &(st->sc));
		
	free_leafs(items, size);
				
	if (!retcode)
		*result = names[choice];
		
	return retcode;
}
/* End of dialog_tree() */

/* utils for ftree */

/* init struct queue by zeros */
static void
init_queue(struct queue *queue)
{
	bzero((void *)queue, sizeof(struct queue));
}

/* add pointer to queue */
/* return - pointer or NULL if error */
static void	*
p2_queue(struct queue *queue, void *pointer)
{
	if (!queue)
		return NULL;

	if (!queue->first)
	{
		if (!(queue->first = queue->last = 
			calloc(1, sizeof(struct m_queue))))
				return NULL;

	}
	else 
	{
	if (!(queue->last->next = 
			calloc(1, sizeof(struct m_queue))))
				return NULL;	
	
	queue->last = queue->last->next;
	}
		
	queue->size++;
	return queue->last->pointer = pointer;		
}

/* get first from queue */
/* return - pointer or NULL if queue is empty */
static void	*
first_queue(struct queue *queue)
{
	void *retval;
	struct m_queue *new_first;
	
	if (!queue ||
		!queue->first ||
			!queue->size)
				return NULL;
	
	retval = queue->first->pointer;
	new_first = queue->first->next;
	free(queue->first);
	queue->first = new_first;
	queue->size--;
	
	return retval;
		
}

/* make zero terminated array from queue */
/* return - pointer to array or NULL if error */
static void	**
q2arr(struct queue *queue, int depth)
{
	void **mono, **end;

	if (!queue ||
		!queue->first ||
			!queue->size)
			return NULL;

	/* memory allocation for array */
	if (!(mono = end = malloc(depth * sizeof(void *) + 1)))
		return NULL;
	
	while(depth--)
	{
		if (!(*end++ = first_queue(queue)))
			break;
	}
	
	*end = NULL;
	
	return mono;
	
}

/*
 * smart_tree (for like find(1) with -d flag output compliance)
 *
 * return values:
 * NULL - malloc error
 * not NULL - ok
 *
 */
static
unsigned char *
smart_tree(struct queue *queue,
		unsigned char FS, 
		unsigned char *current, 
		unsigned char *prev) {
	unsigned char *pcurrent = current, *pprev = prev, *toqueue;
	register char break_flag = 0;
	
	while(*pcurrent && *pprev) {
		if (*pcurrent == *pprev) {
			pcurrent++;
			pprev++;
		}
		else {
			break_flag = 1;
			break;
		}
	}

	if (!*pprev || break_flag) {
		if (*pcurrent == FS) {
			pcurrent++;
			
			if ((!*prev) && (*pcurrent)) {
				unsigned char tchar = *pcurrent;
			
				*pcurrent = '\0';
				if (!(toqueue = strdup(current))) {
					*pcurrent = tchar;
					return NULL;
				}
				if (!p2_queue(queue, toqueue)) {
					*pcurrent = tchar;
					return NULL;
				}
				*pcurrent = tchar;			
			}
		}

		while(*pcurrent) {
			if (*pcurrent == FS) {
				*pcurrent = '\0';
				if (!(toqueue = strdup(current))) {
					*pcurrent = FS;
					return NULL;
				}
				if (!p2_queue(queue, toqueue)) {
					*pcurrent = FS;
					return NULL;
				}
				*pcurrent = FS;
			}
			pcurrent++;
		}
		if (!p2_queue(queue, current))
			return NULL;	
	} 
	return current;
}

/* end of utils for ftree */

/* utils for make tree */

/* if error - return -1 */
static
int
mk_slip(struct leaf array[], int arr_size, int number, int shift)
{
	int t_number;
	int t_shift;
	
	if (number > arr_size - 1)
		return number - 1;
	
	t_shift = shift;
	
	if (!(array[number].branches = calloc(1, t_shift + 1)))
			return -1;
			
	(void)memset(array[number].branches, ' ', t_shift);
	
	t_number = number;
	
	while (array[number].shift < array[t_number + 1].shift)
	{
		t_number = mk_slip(array, arr_size, t_number + 1, t_shift + 1);
		if (t_number < 0) 
				return -1;
		if (t_number == arr_size - 1) 
				break;
	}
	
	if (array[number].shift == array[t_number + 1].shift)
		array[number].slip = '+';
	
	if ((array[number].shift > array[t_number + 1].shift) || 
				t_number == arr_size - 1)
		array[number].slip = '`';
	
	return t_number;

} /* mk_slip() */

/* make tree from file
 *
 * filename	- name of file with like find(1) output
 * p_names	- pointer to array of strings
 * p_size	- pointer to size of array
 * FS		- fields separator
 * p_array	- pointer to array of leafs
 *
 * return values:
 * 0		- ok and names by p_names, size by p_size, array by p_array set
 * -1		- memory allocation error (errno set)
 */  

static
int
mk_ftree(char *filename, 
	unsigned char ***p_names, int *p_size, unsigned char FS, 
		struct leaf **p_array)
{
	int NR;	/* number of input records */	
	struct queue queue;
	unsigned char *string, *sstring = "";
	unsigned char **names;
	
	FILE *input_file;
	
	if (!(input_file = fopen(filename, "r")))
				return -1;

	init_queue(&queue);	
	
	if (!(string = malloc(BUFSIZ)))
			return -1;
	
	/* read input file into queue */
	while(fgets(string, BUFSIZ, input_file))
	{
		if (strchr(string, '\n'))
			*strchr(string, '\n') = '\0';
	
		if (!(string = realloc(string, strlen(string) + 1)))
				return -1;
				
		if (!smart_tree(&queue, FS, string, sstring))
				return -1;
		sstring = string;
				
		if (!(string = malloc(BUFSIZ)))
				return -1;
	} /* read input file into queue */	
	
	if (fclose(input_file) == EOF)
			return -1;
	
	if (!(NR = queue.size))
	{
		*p_size = 0;
		return 0;
	}	

	/* make array from queue */
	if (!(names = (unsigned char **)q2arr(&queue, NR)))
			return -1;
			
	*p_names = names;
	*p_size = NR;
	
	/* make tree from array */
	return mk_tree(names, NR, FS, p_array);
	
} /* mk_ftree */

/* make tree from array
 *
 * names	- array of strings
 * size		- size of array
 * FS		- fields separator
 * p_array	- pointer to array of leafs
 *
 * return values:
 * 0		- ok and array by p_array set
 * -1		- memory allocation error (errno set)
 */  
 
static
int
mk_tree(unsigned char **names, int size, unsigned char FS, 
		struct leaf **p_array)
{
	int i;
	struct leaf *array;

	/* make array of leafs */
	if (!(array = calloc(size, sizeof(struct leaf))))
			return -1;
	
	/* init leafs */
	for (i = 0; i < size; i++)
	{
		unsigned char *in_string, *name;
		int shift = 0;
	
		in_string = name = names[i];
		while(*in_string)
		{
			if (*in_string == FS) {
				if (!i && !*(in_string + 1))
					name = in_string;
				else
				{
					shift++;
					name = in_string + 1;
				}
			}
			in_string++;
		}
		array[i].name = name;
		array[i].shift = shift;
		array[i].slip = '\0';
		array[i].branches = NULL;
	} /* init leafs */
	
	/* make slips */
	for (i = 0;i < size; i++)
	{
		i = mk_slip(array, size, i, 0);
		if (i < 0) 
			return -1;
	} /* make slips */

	/* make branches */
	for (i = 1;i < size; i++)
	{
		unsigned char *src = array[i - 1].branches;
		unsigned char *dst = array[i].branches;
	
		while(*src && *dst)
			*dst++ = *src++;
		
		if (*dst)
			switch (array[i - 1].slip) {
			case '+' : *dst = '|'; 
				break;
			case '`' : *dst = ' ';
			}
	} /* make branches */
	
	*p_array = array;
	return 0;

} /* mk_tree() */

/* free memory from tree (leafs)
 *
 * return values:
 * nothing
 */

static
void
free_leafs(struct leaf *array, int size)
{
	struct leaf *p_array = array;
	
	while (size--)
		free(array++->branches);

	free(p_array);
} /* free_leafs() */

/* free memory from source data for tree (names)
 *
 * return values:
 * if 0 <= choice <= size - pointer to name from names, 
 *	and memory for name not released (must be freed later)
 * else - NULL (recomended choice -1 for it)
 */

static
unsigned char *
free_names(unsigned char **names, int size, int choice)
{
	unsigned char *retval = NULL;
	unsigned char **p_names = names;
	
	while (size--)
	{
		if (!choice--)
			retval = *names++;
		else	
			free(*names++);
	}
	free(p_names);
	return retval;
} /* free_names() */

/* end of utils for make tree */

/* static utils for saved_tree */

/* search saved tree within queue */
/* return - struct *saved_tree or NULL if not found */
static 
struct saved_tree *
search_saved_tree(struct queue *queue, unsigned char **names, int size,
					unsigned char FS,
					int height, int width, 
					int menu_height) 
{
	struct m_queue *member;
	struct saved_tree *retval;
	
	if (!queue || !names || !FS || 
		!height || !width || !menu_height)
		return NULL;
	
	if (!(member = queue->first))
		return NULL;

	while (member->next) {
		retval = member->pointer;
		if ((names == retval->names) &&
			(size == retval->size) &&
			(FS == retval->FS) &&
			(height == retval->height) &&
			(width == retval->width) &&
			(menu_height == retval->menu_height))
			return retval;
		member = member->next;
	}
	retval = member->pointer;
	if ((names == retval->names) &&
		(size == retval->size) &&
		(FS == retval->FS) &&
		(height == retval->height) &&
		(width == retval->width) &&
		(menu_height == retval->menu_height))
		return retval;
	return NULL;	
}

/* end of static utils for saved_tree */