File: Bastille_Curses.pm

package info (click to toggle)
bastille 1:1.3.0-2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,576 kB
  • ctags: 397
  • sloc: perl: 8,407; sh: 1,879; ansic: 638; makefile: 203; csh: 17
file content (696 lines) | stat: -rw-r--r-- 19,147 bytes parent folder | download
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
#!/usr/bin/perl

##########################################################################
#
# This is the code implementing the Bastille-1.1.1 curses interface.
#
# In Bastille-1.1.1, this code was in the InteractiveBastille.pl script
# and carried the following copyright:
#
# Copyright (C) 2000 Jay Beale
# Licensed under the GNU General Public License
#
# The do_Bastille function is a wrapper around the main program logic
# from the InteractiveBastille.pl script.  When it is called, the
# Curses interface runs.  Other than turning this code into a perl
# module and adding the do_Bastille wrapper, there have been no
# changes.

##########################################
## Bastille Linux Text User Interface   ##
##########################################

#
# TO DO:
#
#  1) Use the newly-release Curses::Widgets 1.1 to get better text windows
#  2) Finish documenting program
#  3) Rewrite &Ask subroutine: either use Curses::Forms or just make cleaner

# This use is needed to give the TUI/GUI access to the GLOBAL_ hashes.

use API;   

sub do_Bastille {

    use Curses;
    use Curses::Widgets;

    # Number_Modules is the number of modules loaded in by Load_Questions
    $Number_Modules=0;

    #
    # Highlighted button is the button currently chosen in the button bar
    #     We preserve this from question to question...
    #
    $highlighted_button=1;

    # These variables contain the state of and text for the detail button
    $detail_toggle="high";
    $detail_button{"low"}="< Explain More >";
    $detail_button{"high"}="< Explain Less >";

    # Load in the questions database
    $Number_Modules=&Load_Questions;
    #&Load_Questions_Stub;

    # Check for an existing config file and populate the default answers from it
    &Populate_Answers;

    # Set up the Curses environment
    $window = new Curses;

    # Confirm that we have a full 80 columns...
    if ($COLS < 80) {
        endwin;
        print "\n\n**************************************************************\n";
        print "Please run this in an 80 column environment, preferably from a\n";
        print "text console! You can get a text console by hitting\n";
        print "\n            <CTRL>-<ALT>-<F2>\n";
        print "\nand can return to your graphical display, usually, by hitting\n";
        print "\n            <CTRL>-<ALT>-<F7>";
        print "\n\n**************************************************************\n";
        die "Not in 80 columns...";
    }

    start_color;

    # Increment determines spacing between buttons
    $increment=$COLS/4;

    # Get to the real work of asking questions...
    &Draw_Title;
    # Draw instructions
    # $window->addstr(24,25,"TAB to switch between windows");
    # $window->refresh;

    $next_question_index="Title_Screen";
    while ($next_question_index ne "RUN_SCRIPT") {
        $next_question_index=&Ask($next_question_index);    
    }

    # Output answers to the script and display

    &Output_Config_Files;

    # Run Bastille

    &Run_Bastille_with_Config;


    # Display Credits

    open CREDITS,"/usr/share/Bastille/Credits";
    while (my $creditline=<CREDITS>) {
        $credits .= $creditline;
    }
    close CREDITS;
    $window->erase;

    txt_field( 'window'       => $window,
    	   'ypos'         => 0,
    	   'xpos'         => 0,
    	   'lines'        => $LINES - 2, 
    	   'cols'         => $COLS - 2,
    	   'content'      => $credits,
    	   'border'       => 'blue',
    	   'draw_only'    => 0,
    	   'title'        => "Bastille Linux Credits     (press TAB to go on)",
    	   'pos'          => 1,
    	   'regex'	  => "\n\t"
    	   );

    endwin;

}

sub Ask {
# sub Ask (index to Question{} record)

##############################################################################
#
# More comments coming here, from Design Doc...
#
# WEIRD CASES:
#
#  1 If a record has no short_exp explanation, none is shown.  This is bad?
#  2 If a record has no question, no question is asked, but the explanation
#    is still displayed.  If this is the case, the default_answer is still
#    entered into the configuration, if it exists.
#  3 If a question has no answer, it doesn't create any blank lines or such
#    in the output file, as it will be skipped in &Output_Config_Files.  For
#    this reason, &Prompt_Input, which is only called when the record contains
#    a user-answerable question, pads a space to the end of any 0-length input.
#    Not to worry: Output_Config_Files replaces said space with 0-length input
#    NOTE: we couldn't just only print the answer field when a real question
#          existed -- this would improperly handle case 2.
#    
###############################################################################

    #
    # Load question into local variables
    #
    my $index=$_[0];
    my $short_exp      =$Question{$index}{"short_exp"};
    my $long_exp       =$Question{$index}{"long_exp"};
    my $question       =$Question{$index}{"question"};
    my $toggle_confirm = $Question{$index}{"toggle_confirm"};
    my $toggle_yn      =$Question{$index}{"toggle_yn"};
    my $yes_epilogue   =$Question{$index}{"yes_epilogue"};
    my $no_epilogue    =$Question{$index}{"no_epilogue"};
    my $yes_child      =$Question{$index}{"yes_child"};
    my $no_child       =$Question{$index}{"no_child"};
    my $proper_parent  =$Question{$index}{"proper_parent"};
    my $answer         =$Question{$index}{"answer"};
    my $module         =$Question{$index}{"module"};

    
    #
    # Set up other local variables
    #

    # Index to next question -- returned by this routine -- loop until we
    # have an index
    my $return_index="";

    # Highlighted button stores the button currently hightlighted -- set this
    # to 1 whenever we enter a question
    # my $highlighted_button=1;


    # Widgets 1.1 feature
    # Current position in the Scrollable explanation box
    #my $current_scroll_position=0;

    while (not $return_index) {

	# Button pressed at end
	my $button_pressed="";
	
	# Input window exited via enter key instead of tab key
	my $exited_with_enter=0;

	#
	# Explanation to display in routines -- we use short explanation if 
	# long is empty, since long explanation is optional
	
	my $explanation;
	if (($detail_toggle eq "high") and ($long_exp)) {
	    if ($question) {
		$explanation="Q: $question\n$long_exp\n\n$question";
	    }
	    else {
		$explanation="$long_exp\n\n$question";
	    }
	}
	elsif ($short_exp) {
	    if ($question) {
		$explanation="Q: $question\n$short_exp\n\n$question";
	    }
	    else {
		$explanation="$short_exp\n\n$question";
	    }
	}
	else {
	    # Special case: when there is no explanation, this is a question
	    # used either to show an epilogue or to (more likely) get some
	    # default answer into the data stream

	    $explanation="";
	}

	#
	# Draw the screen before accepting input
	#
	# This basically does the following, making it easier to continue
	# redrawing the screen on every window switch...
	# &Draw_Explanation($explanation);
	# if ($question) { 
	#     &Draw_Prompt($answer,$toggle_yn);
	# }
	# &Draw_Buttons;

	&Redraw_Screen($module,$explanation,$question,$answer,$toggle_yn);

	#
	# Redraw windows with user input allowed and accept input
	#

	# Change from 1.0.4.pre1 -> 1.0.4.pre2  -- switched window order
	#if ($explanation) {
	#    &Scroll_Explanation($module,$explanation);
	#    &Redraw_Screen($module,$explanation,$question,$answer,$toggle_yn);
	#}
	if ($question) {
	    ($answer,$exited_with_enter)=&Prompt_Input($answer,$toggle_yn);
	    &Redraw_Screen($module,$explanation,$question,$answer,$toggle_yn);
	}
	else {
	    if ($answer) {
		$button_pressed="Next";
	    }
	}
	
	# Skip the buttons if we exited the input with the Enter key
	if ($exited_with_enter) {
	    $button_pressed="Next";
	}
	elsif ($question or $explanation)  {
	    $button_pressed=&Allow_Buttons;
	    &Redraw_Screen($module,$explanation,$question,$answer,$toggle_yn);
	}

	# Was the detail level of the explanations toggled?
	if ($button_pressed eq "Detail") {
	    # Switch the detail level
	    if ($detail_toggle eq "high") {
		$detail_toggle="low";
	    }
	    else {
		$detail_toggle="high";
	    }

	    # Reload the explanation
	    if (($detail_toggle eq "high") and ($long_exp)) {
		$explanation="$long_exp\n\n$question";
	    }
	    elsif ($short_exp) {
		$explanation="$short_exp\n\n$question";
	    }

	}
	
	# Did the user hit the Back Button?
	if ($button_pressed eq "Back") {
	    $return_index=$proper_parent;
	}

	# Did the user hit the next button on an answered question?
	# Ahhh, we allow blank responses to questions -- make this less
	# icky later by using radio buttons/lists for yes/no...
	#
#	if (($button_pressed eq "Next") and ($answer)) {
	if  ($button_pressed eq "Next") {

	    # If not a Y/N question, use the Yes Child and Epilogue
	    unless ($toggle_yn) {		
		$return_index=$yes_child;
		
		if ($yes_epilogue) {
		    $window->erase;
		    &Draw_Title;
		    &Display_Epilogue($yes_epilogue);
		}
	    }
	    # Otherwise, use the epilogue appropriate to the answer
	    else {
		if ($answer=~/^\s*Y/i) {
		    $return_index=$yes_child;

		    $window->erase;
		    &Draw_Title;
		    &Display_Epilogue($yes_epilogue);
		}
		elsif ($answer =~/^\s*N/i) {
		    $window->erase;
		    &Draw_Title;
		    $return_index=$no_child;
		    &Display_Epilogue($no_epilogue);
		}
	    }
	}

	# Now, if the user hit Prev/Next, store the answer so it's there
	# when we come back to it...
	if ($return_index) {
	    if ($answer) {
		$Question{$index}{"answer"}=$answer;
	    }
	}
	else {
	    &Scroll_Explanation($module,$explanation);
	    &Redraw_Screen($module,$explanation,$question,$answer,$toggle_yn);
	}

	# Deal with text-screen questions: questions w/ no question and
        # explanation which still have an epilogue

	if ((not $explanation) and (not $question)) {
	    $return_index=$yes_child;
	    &Display_Epilogue($yes_epilogue);
	}

    }
		
    $return_index;
}

sub Redraw_Screen {
# sub Redraw_Screen($module,$explanation,$question,$answer,$toggle_yn)
    
    my ($module,$explanation,$question,$answer,$toggle_yn)=@_;

    # Erase the screen.
    $window->erase;

    &Draw_Title;
    &Draw_Key_Instructions;

    if ($explanation) {
	&Draw_Explanation($module,$explanation);
    }
    if ($question) { 
	&Draw_Prompt($answer,$toggle_yn);
    }
    if ($question or $explanation) {
	&Draw_Buttons;
    }
}

# Window drawing functions...

sub Draw_Title {
# Draw the title bar   

    txt_field( 'window' => $window, 
	       'ypos'   => 0, 'xpos'   => 0,
	       'lines'  => 1,
	       'cols'   => $COLS-2, 
	       'content'=> "                                Bastille Linux",
	       'border' => 'blue', 
	       'draw_only' => 1,
	       );
}

sub Draw_Key_Instructions {

    select_colour($window,"yellow");
    $window->addstr(24,0,"TAB to switch Windows                               Arrow Keys to switch Buttons");
    $window->attrset(0);

    1;
}
    
    

sub Draw_Explanation {
# Draw the explanation

    my ($module,$explanation)=@_;
    my $title;

    if ($module) {
	$title=$module . " of $Number_Modules";
    }

    txt_field( 'window'       => $window,
	       'ypos'         => 3,
	       'xpos'         => 0, # $COLS - 5,
	       'lines'        => $LINES - 11, 
	       'cols'         => $COLS - 2,
	       'content'      => $explanation,
	       'border'       => 'blue',
	       'draw_only'    => 1,
	       'title'        => $title,
	       'pos'          => 0,
# Widgets 1.1 feature
#	       'pos'          => $current_scroll_position,
	       # 'function'     => \&clock
	       );
}

sub Draw_Prompt {
# Draw the Answer area

    my ($current_answer,$toggle_yn)=@_;    
    my %yes_no_list =("0","Yes","1","No");
    my $default_entry=0;

    # Is this a yes/no question or not? If yes, use a button list...
    if ($toggle_yn) {
	if ($current_answer=~/^\s*Y/i) {
	    $default_entry=0;
	}
	elsif ($current_answer=~/^\s*N/i) {
	    $default_entry=1;
	}
	    
	my ($key, $answer)=list_box( 'window'    => $window,
				     'ypos'     => $LINES-6,
				     'xpos'     => 36,
				     'lines'     => 2,
				     'cols'      => 5,
				     'border'    => 'blue',
				     'selected'  => $default_entry,
				     'list'      => \%yes_no_list,
				     'draw_only' => 0
				     );

    }
    # If it's not a yes/no question, create an answer blank...
    else {

	txt_field( 'window'        => $window,
		   'ypos'          => $LINES-6,
		   'xpos'          => 1,
		   'lines'         => 2,
		   'cols'          => $COLS-4,
		   'content'       => "Answer: $current_answer",
		   'border'        => 'blue',
		   'draw_only'     => 1,
		   'pos'           => 8,
		   );
    }
}

sub Draw_Buttons {

# Use of the widget removed for now, since draw_only doesn't work here
#($input,$selected) = buttons(    'window'       => $window,
#                                 'buttons'       => ["Back","Next",
#					$detail_button{$detail_toggle}],
#				 'ypos'          => $LINES-2,
#				 'xpos'          => ($increment)-2,
#				 'spacing'       => $increment/4,
#				 'active_button' => 1,
#				 'draw-only'     => 1,
#				 );
# Emulate!
    select_colour($window,"blue");
    $window->addstr($LINES-2,($increment)-4,"< Back >");
    $window->addstr($LINES-2,($increment*2)-11,"< Next >");
    $window->addstr($LINES-2,($increment*3)-18,$detail_button{$detail_toggle});
    $window->attrset(0);
    $window->refresh;
}

sub Scroll_Explanation {
# Allow user to scroll the explanation

    my ($module,$explanation)=@_;
    my $title;

    if ($module) {
	$title=$module . " of $Number_Modules";
    }

    noecho;
    # Widgets 1.1 feature
    #($key, $rtrnd_note,$current_scroll_position) = 
    ($key,$rtrnd_note) =  txt_field( 'window'       => $window,
				     'ypos'         => 3,
				     'xpos'         => 0, # $COLS - 5,
				     'lines'        => $LINES - 11, 
				     'cols'         => $COLS - 2,
				     'content'      => $explanation,
				     'border'       => 'red',
				     'draw_only'    => 0,
                                     # Widgets 1.1 only -- no edit in window
				     #'edit'         => 0,
				     #'cursor_disable' => 0,
				     'title'        => $title,
				     'pos'          => $current_scroll_position,
				     # 'function'     => \&clock
				     );
}

sub Display_Epilogue {
    ###
#######  See about using text widget, but blanking the buttons and answer for
#######  this...
    ###

    my $text=$_[0];
    
    if ($text) {
	$text = " "x27 . "<Press TAB to go on>\n\n" . $text;

	($key, $rtrnd_note) = txt_field( 'window'       => $window,
					 'ypos'         => 3,
					 'xpos'         => 0,
					 'lines'        => $LINES-5,
					 'cols'         => $COLS-2,
					 'content'      => $text,
					 'border'       => 'red',
					 'pos'          => 1,
					 'regex'        => "\n\t",
					 );
#    	msg_box('message' => $text);
    }
}

sub Prompt_Input {
# sub Prompt_Input($current_answer, $toggle_yn)
#
# $current_answer -- the answer to place in the window for user to edit
# $toggle_yn      -- 1 if this is a y/n question, 0 otherwise

###############################################################################
# WEIRD CASES:
#
#  1 If a record has no short_exp explanation, none is shown.  This is bad?
#  2 If a record has no question, no question is asked, but the explanation
#    is still displayed.  If this is the case, the default_answer is still
#    entered into the configuration, if it exists.
#  3 If a question has no answer, it doesn't create any blank lines or such
#    in the output file, as it will be skipped in &Output_Config_Files.  For
#    this reason, &Prompt_Input, which is only called when the record contains
#    a user-answerable question, pads a space to the end of any 0-length input.
#    Not to worry: Output_Config_Files replaces said space with 0-length input
#    NOTE: we couldn't just only print the answer field when a real question
#          existed -- this would improperly handle case 2.
#    
###############################################################################

# Allow user to input an answer

    my ($current_answer,$toggle_yn)=@_;

    # Default item to have highlighted
    my $default_entry=0;

    # List of buttons for the y/n listbox widget
    my %yes_no_list =(0,"Yes",1,"No");

    # Input variables for the widgets
    my ($key,$answer);

    # Used to keep track of how widget was exited
    my $exited_with_enter=0;

    # Is this a yes/no question or not? If yes, use a button list...
    if ($toggle_yn) {
	if ($current_answer=~/^\s*Y/i) {
	    $default_entry="0";
	}
	elsif ($current_answer=~/^\s*N/i) {
	    $default_entry="1";
	}
	    
	($key, $answer)=list_box(    'window'    => $window,
				     'ypos'      => $LINES-6,
				     'xpos'      => 36,
				     'lines'     => 2,
				     'cols'      => 5,
				     'border'    => 'red',
				     'selected'  => $default_entry,
				     'list'      => \%yes_no_list,
				     );
	if ($answer==0) {
	    $answer="Y";
	}
	else {
	    $answer="N";
	}

	if ($key eq "\n") {
	    $exited_with_enter=1;
	}
    }
    # If it's not a yes/no question, create an answer blank...
    else {
	($key, $answer) = txt_field( 'window'        => $window,
					'ypos'          => $LINES-6,
					'xpos'          => 1,
					'lines'         => 2,
					'cols'          => $COLS-4,
					'content'       => "Answer: $current_answer",
					'border'        => 'red',
					'draw_only'     => 0,
					'pos'           => 8,
					);

	# If exited with enter, set flag
	if ($key eq "\n") {
	    $exited_with_enter=1;
	}

	# Prune off the prompt which is retained by the txt_field widget
	if ($answer =~ /^Answer:\s*(.*)/) {
	    $answer=$1;
	}

	# If answer is only a comment, blank it.
	if ($answer =~ /^\s*\#/ ) {
	    $answer=" ";
	}

	# If the $answer field is zero-length, add a space...  Read about
	# this above...

	if ($answer =~ /^$/) {
	    $answer=" ";
	}

	# If the entered answer is multi-lined, this won't do...  We prune
	# out all but the first non-blank line (plus any comment lines)
#	if ($answer =~ /\n/) {
#	    my $line,$pruned_answer;
#	    foreach $line (split($answer,)) {
#		if ($line =~ 
#
#	    }
#	}
    }

    return ($answer,$exited_with_enter);
}

sub Allow_Buttons {

    my ($input,$selected);

    while ($input !~ /[\t \n]/) {
	($input,$selected) = buttons(    'window'       => $window,
				     'buttons'       => ["< Back >","< Next >",
					       $detail_button{$detail_toggle}],
				     'ypos'          => $LINES-2,
				     'xpos'          => ($increment)-4,
				     'spacing'       => $increment/4,
				     'active_button' => $highlighted_button,
				     'draw-only'     => 0,
				 );
    }

    $highlighted_button=$selected;

    if ($selected == 2) {
	$selected="Detail";
    }
    elsif ($selected == 0) {
	$selected="Back";
    }
    else {
	$selected="Next";
    }

    # But wait! If we exited button widget via Tab, return none of these b/c
    # the user didn't SELECT a button!
    if ($input eq "\t") {
	$selected="Repeat_Scroll";
    }

    $selected;
}

1;