File: evmsn.c

package info (click to toggle)
evms 1.0.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,168 kB
  • ctags: 5,853
  • sloc: ansic: 87,317; makefile: 691; sh: 238
file content (421 lines) | stat: -rw-r--r-- 14,325 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
/*----------------------------------------------------------------------
 *
 *   Copyright (c) International Business Machines  Corp., 2001
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Module: evmsn.c
 *---------------------------------------------------------------------*/

/*----------------------------------------------------------------------
 * Change History:
 *
 * 9/2001  John Stiles   Initial version.
 *
 *---------------------------------------------------------------------*/


/* Identify this file. */
#define EVMSN_C    1

/*----------------------------------------------------------------------
 * Necessary include files
 *---------------------------------------------------------------------*/
#include <sys/types.h>
#include <string.h>        /* strcat, strcpy    */
#include <stdlib.h>        /* malloc, free      */
#include <ctype.h>         /* toupper           */
#include <stdio.h>         /* printf            */
#include <unistd.h>        /* geteuid           */
#include <getopt.h>        /* getopt_long       */


#include "evmsn.h"

/*----------------------------------------------------------------------
 * Private Global Variables
 *---------------------------------------------------------------------*/
static engine_mode_t open_mode   = ENGINE_READWRITE;
static debug_level_t debug_level = DEFAULT;
static char          *log_name   = NULL;
static engine_message_functions_t  message_calls;


/*----------------------------------------------------------------------
 * There are No Private Constants
 * There are No Private Type Definitions
 * There are No Global Variables.
 *---------------------------------------------------------------------*/


/*----------------------------------------------------------------------
 * Local Functions
 *---------------------------------------------------------------------*/

static void show_help (char **argv)
{
    printf ("\nEnterprise Volume Management System %s\n", VERSION);
    printf ("International Business Machines  %s\n\n", DATE);
   
    printf ("Usage: %s [OPTION...] \n\n", argv[0]);

    printf ("  -d, --log-level=LEVEL\t\tLogging output level where LEVEL is one of the following:\n");
    printf ("  \t\t\t\t\tcritical | serious | error | warning | default |\n"); 
    printf ("  \t\t\t\t\tdetails | debug | extra | entry-exit | everything\n");
    printf ("  -?, --help\t\t\tShow this help message\n");
    printf ("  -m, --mode=ENGINE-MODE\tEngine mode where ENGINE-MODE is one of the following:\n");
    printf ("  \t\t\t\t\treadwrite | readonly\n");
    printf ("  -l, --log-name=FILENAME\tFile to send logging output to\n\n");
}

/*
 *
 *   static int parse_options (int argc, char **argv)
 *
 *   Description:
 *      This routine parses the command line options. It then sets the
 *      appropriate program variables accordingly.
 *       
 *   Entry:
 *      argc - count of command line arguments
 *      argv - array of program command line argument strings
 *
 *   Exit:
 *      Returns 0 is parsing was successful or -1 if a problem was encountered.
 *
 */
static int parse_options (int argc, char **argv)
{

    int                  c;
    int                  rc=0;
    char                *short_opts = "h?d:m:l:";
    struct option        long_opts[] =
    {
           {"help",      no_argument,       NULL, 'h'},
           {"log-level", required_argument, NULL, 'd'},
           {"mode",      required_argument, NULL, 'm'},
           {"log-name",  required_argument, NULL, 'l'},
           {NULL,        0,                 NULL,  0 }
    };

    while (rc == 0 && (c = getopt_long (argc, argv, short_opts, long_opts, NULL)) != EOF)
    {
           switch (c)
           {
               case 'm':
                   if (strcasecmp (optarg, "readwrite") == 0)
                       open_mode = ENGINE_READWRITE;
                   else if (strcasecmp (optarg, "readonly") == 0)
                       open_mode = ENGINE_READONLY;
                   else
                   {
                       printf (_("Unrecognized mode argument -- \"%s\"\n\n"), optarg);
                       rc = -1;
                   }

                   break;
                          
               case 'd':                    
                   if (strcasecmp (optarg, "critical") == 0)
                       debug_level = CRITICAL;
                   else if (strcasecmp (optarg, "serious") == 0)
                       debug_level = SERIOUS;
                   else if (strcasecmp (optarg, "error") == 0)
                       debug_level = ERROR;
                   else if (strcasecmp (optarg, "warning") == 0)
                       debug_level = WARNING;
                   else if (strcasecmp (optarg, "default") == 0)
                       debug_level = DEFAULT;
                   else if (strcasecmp (optarg, "details") == 0)
                       debug_level = DETAILS;
                   else if (strcasecmp (optarg, "debug") == 0)
                       debug_level = DEBUG;
                   else if (strcasecmp (optarg, "extra") == 0)
                       debug_level = EXTRA;
                   else if (strcasecmp (optarg, "entry-exit") == 0)
                       debug_level = ENTRY_EXIT;
                   else if (strcasecmp (optarg, "everything") == 0)
                       debug_level = EVERYTHING;
                   else
                   {
                       printf (_("Unrecognized debug level argument -- \"%s\"\n\n"), optarg);
                       rc = -1;
                   }

                   break;

               case 'l':
                   log_name = strdup (optarg);
                   break;

               default:
                   printf (_("Unrecognized option -- \"%c\"\n\n"), c);
               case 'h':
               case '?':
                   rc = -1;
                   break;
           }
    }

    if (rc != 0)
        show_help (argv);
 
    return rc;
}

int simple_message( char *msg_text, int *msg_answer, char * * choices )
{
  int i, j, startx, starty, screen_cols, screen_lines, keyval = 0,
      start_index = 0, select_index = 0, num_objects = 0, item_count = 0,
      msg_lines;
  char *holdbuf, *curtext;
  size_t msg_length;

  log_debug( "%s: got message: %s\n", __FUNCTION__, msg_text );

  if ( GL_screen_context == SC_NONE ) {
    screen_cols = GL_screen_cols - XPOS_BOX2TEXT - XPOS_BOX2RIGHT - 2;
    screen_lines = CALC_SCREEN2_LINES;
    startx = XPOS_BOX2TEXT - 1;
    starty = YPOS_BOX2TOP + 1;
  }
  else {
    screen_cols = GL_screen_cols - XPOS_BOX3TEXT - XPOS_BOX3RIGHT - 2;
    screen_lines = CALC_SCREEN3_LINES;
    startx = XPOS_BOX3TEXT - 1;
    starty = YPOS_BOX3TOP + 1;
  }

  holdbuf = malloc( screen_cols + 3 );
  if ( !holdbuf ) {
    log_error( "%s: %s\n", __FUNCTION__, err_msg_malloc_blanks );
    return ENOMEM;
  }

  // first blank the lines
  BLANK_BUFFER( holdbuf, screen_cols + 2 )
  for ( i = starty; i < starty + 1 + screen_lines; i++ )
    print_string( startx, i, COLOR_DEFAULT, holdbuf );

  startx++;
  starty++;
  screen_lines--;
  print_string( startx, starty, COLOR_DEFAULT, _( "Message:  " ));
  starty++;
  screen_lines--;

  msg_lines = screen_lines - 1;
  curtext = msg_text;
  while ( msg_lines ) {
    BLANK_BUFFER( holdbuf, screen_cols )
    msg_length = strlen( curtext );
    if ( msg_length > screen_cols - 2 )
      msg_length = screen_cols - 2;

    for ( i = 0; i < msg_length; i++ ) {
      if ( curtext[i] == '\n' ) {
        i++;
        if ( curtext[i] == '\0' )
          msg_lines = 1;
        break;
      }
      else  if ( curtext[i] == '\0' ) {
        msg_lines = 1;
        break;
      }
      else
        holdbuf[i] = curtext[i];
    }

    curtext += i;
    print_string( startx, starty, COLOR_DEFAULT, holdbuf );
    starty++;
    screen_lines--;
    msg_lines--;
  }
  screen_lines += 2;

  if ( msg_answer != NULL && choices != NULL ) {
    log_debug( "%s: message has answer, initially %i\n", __FUNCTION__, *msg_answer );
    while ( choices[num_objects] != NULL )
      num_objects++;
    log_debug( "%s: counted %i choices\n", __FUNCTION__, num_objects );

    do {

      // first blank the lines
      BLANK_BUFFER( holdbuf, screen_cols )
      for ( i = starty; i < starty + screen_lines - 1; i++ )
        print_string( startx, i, COLOR_DEFAULT, holdbuf );

      if ( start_index + screen_lines > num_objects )
        item_count = num_objects - start_index + starty;
      else
        item_count = screen_lines - 1 + starty;

      for ( i = starty, j = start_index; i < item_count; i++, j++ ) {

        BLANK_BUFFER( holdbuf, screen_cols )
        if ( strlen( choices[j] ) > screen_cols - 1 )
          memcpy( holdbuf, choices[j], screen_cols - 2 );
        else
          memcpy( holdbuf, choices[j], strlen( choices[j] ));

        if ( select_index == j )
          print_string( startx, i, COLOR_BAR, holdbuf );
        else
          print_string( startx, i, COLOR_TITLE, holdbuf );
      }

      draw_status_line( _( "Select a choice and press enter." ));

      keyval = key_scroller( &start_index, &select_index, num_objects, screen_lines - 2 );

      switch ( keyval ) {

        case KEY_ENTER :
          *msg_answer = select_index;
          break;

        default:
          keyval = KEY_CURSORACT;
          break;
      }

    }  while ( keyval == KEY_CURSORACT );
  }
  else {
    starty += 2;
    print_string( startx, starty, COLOR_DEFAULT, info_press_any_keyc );
    get_inputc();
  }

  free( holdbuf );
  return 0;
}


/*----------------------------------------------------------------------
 * Public Functions
 *---------------------------------------------------------------------*/

/*----------------------------------------------------------------------
 *
 *   Function Name:  main
 *
 *   Descriptive Name:  guess
 *
 *   Input:  int argc    - The number of parameters on the command line.
 *           char * argv - An array of pointers to the text of each
 *                          parameter found on the command line.
 *
 *   Output: If Success :  The program's exit code will be 0.
 *
 *           If Failure :  The program's exit code will be the failing
 *                          function's return code.
 *
 *   Error Handling:  If an error occurs, all memory allocated by this
 *                     program is freed, and an error message is output
 *                     to the user.
 *
 *   Side Effects:  The state of the disks, partitions, and/or volumes
 *                   in the system may be altered.  Nodes may be
 *                   created or removed from the /dev tree.
 *
 *   Notes:
 *
 *---------------------------------------------------------------------*/
int main( int argc, char * argv[] )
{
  int  keyval, rc = 0;
  char buf[] = "                                                                         ";

  rc = parse_options (argc, argv);
  
  if (rc == 0)
  {    
      // Initialize Globals
      GL_use_color = 0;
      GL_screen_cols  = 0;
      GL_screen_lines  = 0;
      GL_screen_context = SC_NONE;
    
      // Start up the screen environment
      kb_video_setup();
      update_screen_info();                  /* set up for initial screen size.*/
    
      // Set up the messaging functions
      message_calls.user_message = &simple_message;
      message_calls.user_communication = NULL;
    
      // Draw initial screen
      draw_frame();
      draw_fnkeys( KEYS_DEFAULT );
      keyval = KEY_VOLUMES;                     /* initial view.  */
    
      // Presently, only allow root access
      if ( geteuid() == 0) {
         evms_version_t version;
          
         draw_status_line ( _("EVMS is examining your system. Please wait..."));
     
         evms_get_api_version (&version);
          
         if (REQUIRED_ENGINE_VER_MAJOR == version.major &&        
             ((version.minor > REQUIRED_ENGINE_VER_MINOR) ||
             (REQUIRED_ENGINE_VER_MINOR == version.minor && REQUIRED_ENGINE_VER_PATCHLEVEL <= version.patchlevel))) {
          
             if ( (rc = evms_open_engine( open_mode, &message_calls, debug_level, log_name )) == 0 ) {
        
                rc = control_loop( keyval );
        
                evms_close_engine();
             }
             else
             {
                sprintf( buf, _( "Open EVMS Engine failed, rc == %i " ), rc );
                print_string( 4, 13, COLOR_DEFAULT, buf );
                print_string( 6, 14, COLOR_DEFAULT, info_press_any_keyx );
                draw_status_line( info_press_any_keyx );
                keyval = get_inputc();             
             }
         }
         else
         {
             sprintf( buf, _( "Version %u.%u.%u or later of the EVMS Engine API required. Found version %u.%u.%u." ), 
                               REQUIRED_ENGINE_VER_MAJOR, REQUIRED_ENGINE_VER_MINOR, REQUIRED_ENGINE_VER_PATCHLEVEL,
                               version.major, version.minor, version.patchlevel);
             print_string( 2, 13, COLOR_DEFAULT, buf );
             print_string( 6, 14, COLOR_DEFAULT, info_press_any_keyx );
             draw_status_line( info_press_any_keyx );
             keyval = get_inputc();
             rc = EPERM;
         }
      }
      else {  // Not root...
          print_string( 4, 13, COLOR_DEFAULT, _( "This command must be run with root privilege." ));
          print_string( 6, 14, COLOR_DEFAULT, info_press_any_keyx );
          draw_status_line( info_press_any_keyx );
          keyval = get_inputc();
      }
    
      // Terminate the screen environment
      kb_video_shutdown();
  }
  
  return  rc;
}