summaryrefslogtreecommitdiff
path: root/stream-ui.c
blob: 364ad0a1f64ce7fc43244635ac0e58e5b46cfdda (plain)
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
/*
 * nvidia-installer: A tool for installing NVIDIA software packages on
 * Unix and Linux systems.
 *
 * Copyright (C) 2003 NVIDIA Corporation
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 * 
 * 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, see <http://www.gnu.org/licenses>.
 *
 *
 * stream_ui.c - implementation of the nvidia-installer ui using printf
 * and friends.  This user interface is compiled into nvidia-installer to
 * ensure that we always have a ui available.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>

#include "nvidia-installer.h"
#include "nvidia-installer-ui.h"
#include "misc.h"
#include "files.h"
#include "common-utils.h"
#include "msg.h"

/* prototypes of each of the stream ui functions */

int   stream_detect              (Options*);
int   stream_init                (Options*, FormatTextRows format_text_rows);
void  stream_set_title           (Options*, const char*);
char *stream_get_input           (Options*, const char*, const char*);
int   stream_display_license     (Options*, const char*);
void  stream_message             (Options*, int level, const char*);
void  stream_command_output      (Options*, const char*);
int   stream_approve_command_list(Options*, CommandList*, const char*);
int   stream_yes_no              (Options*, const int, const char*);
int   stream_multiple_choice     (Options *op, const char *, const char * const *,
                                  int, int);
int   stream_paged_prompt        (Options *op, const char *, const char *,
                                  const char *, const char * const *, int, int);
void  stream_status_begin        (Options*, const char*, const char*);
void  stream_status_update       (Options*, const float, const char*);
void  stream_status_end          (Options*, const char*);
void  stream_close               (Options*);

InstallerUI stream_ui_dispatch_table = {
    stream_detect,
    stream_init,
    stream_set_title,
    stream_get_input,
    stream_display_license,
    stream_message,
    stream_command_output,
    stream_approve_command_list,
    stream_yes_no,
    stream_multiple_choice,
    stream_paged_prompt,
    stream_status_begin,
    stream_status_update,
    stream_status_end,
    stream_close
};


typedef struct {
    int status_active;
    char *status_label;
} Data;



#define STATUS_BEGIN 0
#define STATUS_UPDATE 1
#define STATUS_END 2

#define STATUS_BAR_WIDTH 30

/*
 * print_status_bar() -
 */

static void print_status_bar(Data *d, int status, float percent)
{
    int i;
    float val;
    
    if (status != STATUS_BEGIN) printf("\r");
    
    if (d->status_label) {
        printf("  %s: [", d->status_label);
    } else {
        printf("  [");
    }

    val = ((float) STATUS_BAR_WIDTH * percent);

    for (i = 0; i < STATUS_BAR_WIDTH; i++) {
        printf("%c", ((float) i < val) ? '#' : ' ');
    }

    printf("] %3d%%", (int) (percent * 100.0));

    if (status == STATUS_END) printf("\n");
    
    fflush(stdout);

} /* print_status_bar() */



static void sigwinch_handler(int n)
{
    reset_current_terminal_width(0);
}


/*
 * stream_detect - returns TRUE if the user interface is present; the
 * stream_ui is always present, so this always returns TRUE.
 */

int stream_detect(Options *op)
{
    return TRUE;

} /* stream_detect() */



/*
 * stream_init - initialize the ui and print a welcome message.
 */

int stream_init(Options *op, FormatTextRows format_text_rows)
{
    Data *d = nvalloc(sizeof(Data));

    d->status_active = FALSE;
    op->ui_priv = d;

    if (!op->silent) {

        nv_info_msg(NULL, "");
        nv_info_msg(NULL, "Welcome to the NVIDIA Software Installer for "
                          "Unix/Linux");
        nv_info_msg(NULL, "");
    
        /* register the SIGWINCH signal handler */

        signal(SIGWINCH, sigwinch_handler);
    }

    return TRUE;
    
} /* stream_init() */



/*
 * stream_set_title() - other ui's might use this to update a welcome
 * banner, window title, etc, but it doesn't make sense for this ui.
 */

void stream_set_title(Options *op, const char *title)
{
    return;

} /* stream_set_title() */



/*
 * stream_get_input - prompt for user input with the given msg;
 * returns the user inputed string.
 */

char *stream_get_input(Options *op, const char *def, const char *msg)
{
    char *buf;
    
    nv_info_msg(NULL, "");
    nv_info_msg(NULL, "%s", msg);
    fprintf(stdout, "  [default: '%s']: ", def);
    fflush(stdout);
    
    buf = fget_next_line(stdin, NULL);
    
    if (!buf || !buf[0]) {
        if (buf) free(buf);
        buf = nvstrdup(def);
    }

    return buf;

} /* stream_get_input() */




/*
 * stream_display_license() - print the text from the license file,
 * prompt for acceptance from the user, and return whether or not the
 * user accepted.
 */

int stream_display_license(Options *op, const char *license)
{
    char *str;
    
    nv_info_msg(NULL, "");
    nv_info_msg(NULL, "Please read the following LICENSE and type \""
                      "accept\" followed by the Enter key to accept the "
                      "license, or type anything else to not accept and "
                      "exit nvidia-installer.");
    nv_info_msg(NULL, "");
    
    nv_info_msg(NULL, "________");
    nv_info_msg(NULL, "");
    printf("%s", license);
    nv_info_msg(NULL, "________");
    nv_info_msg(NULL, "");
    
    printf("Accept? (Type \"Accept\" to accept, or anything else to abort): ");
    fflush(stdout);

    str = fget_next_line(stdin, NULL);
    if (strlen(str) < 6) {
        free(str);
        return FALSE;
    }
    
    str[7] = '\0';
    
    if (strcasecmp(str, "ACCEPT") == 0) {
        free(str);
        return TRUE;
    } else {
        free(str);
        return FALSE;
    }
    
} /* stream_display_license() */



/*
 * stream_message() - print a message
 */

void stream_message(Options *op, const int level, const char *msg)
{
    typedef struct {
        char *prefix;
        FILE *stream;
        int newline;
    } MessageLevelAttributes;

    const MessageLevelAttributes msg_attrs[] = {
        { NULL,        stdout, FALSE }, /* NV_MSG_LEVEL_LOG */
        { NULL,        stdout, TRUE  }, /* NV_MSG_LEVEL_MESSAGE */
        { "WARNING: ", stderr, TRUE  }, /* NV_MSG_LEVEL_WARNING */
        { "ERROR: ",   stderr, TRUE  }  /* NV_MSG_LEVEL_ERROR */
    };
    
    Data *d = op->ui_priv;

    /* don't print log messages if we're currently displaying a status */

    if ((level == NV_MSG_LEVEL_LOG) && (d->status_active)) return;

    if (msg_attrs[level].newline) {
        nv_info_msg_to_file(msg_attrs[level].stream, NULL, "");
    }
    nv_info_msg_to_file(msg_attrs[level].stream,
                        msg_attrs[level].prefix,
                        "%s", msg);
    if (msg_attrs[level].newline) {
        nv_info_msg_to_file(msg_attrs[level].stream, NULL, "");
    }

} /* stream_message() */



/* 
 * stream_command_output() - if in expert mode, print output from
 * executing a command.
 *
 * XXX Should this output be formatted?
 */

void stream_command_output(Options *op, const char *msg)
{
    Data *d = op->ui_priv;
    
    if ((!op->expert) || (d->status_active)) return;

    nv_info_msg("   ", "%s", msg);
    
} /* stream_command_output() */



/*
 * stream_approve_command_list() - list all the commands that will be
 * executed, and ask for approval.
 */

int stream_approve_command_list(Options *op, CommandList *cl,
                                const char *descr)
{
    int i;
    Command *c;
    char *perms;
    const char *prefix = " --> ";

    nv_info_msg(NULL, "");
    nv_info_msg(NULL, "The following operations will be performed to install the %s:",
                descr);
    nv_info_msg(NULL, "");
    
    for (i = 0; i < cl->num; i++) {
        c = &cl->cmds[i];

        switch (c->cmd) {
        
          case INSTALL_CMD:
            perms = mode_to_permission_string(c->mode);
            nv_info_msg(prefix, "install the file '%s' as '%s' with "
                        "permissions '%s'", c->s0, c->s1, perms);
            free(perms);
            if (c->s2) {
                nv_info_msg(prefix, "execute the command `%s`", c->s2);
            }
            break;
            
          case RUN_CMD:
            nv_info_msg(prefix, "execute the command `%s`", c->s0);
            break;
            
          case SYMLINK_CMD:
            nv_info_msg(prefix, "create a symbolic link '%s' to '%s'",
                        c->s0, c->s1);
            break;
            
          case BACKUP_CMD:
            nv_info_msg(prefix, "back up the file '%s'", c->s0);
            break;

          case DELETE_CMD:
            nv_info_msg(prefix, "delete file '%s'", c->s0);
            break;

          default:

            nv_error_msg("Error in CommandList! (cmd: %d; s0: '%s';"
                         "s1: '%s'; s2: '%s'; mode: %04o)",
                         c->cmd, c->s0, c->s1, c->s2, c->mode);
            nv_error_msg("Aborting installation.");
            return FALSE;
            break;
        }
    }
    
    fflush(stdout);
    
    if (!stream_yes_no(op, TRUE, "\nIs this acceptable? (answering 'no' will "
                       "abort installation)")) {
        nv_error_msg("Command list not accepted; exiting installation.");
        return FALSE;
    }

    return TRUE;
    
} /* stream_approve_command_list() */



/*
 * stream_yes_no() - ask the yes/no question 'msg' and return TRUE for
 * yes, and FALSE for no.
 */

int stream_yes_no(Options *op, const int def, const char *msg)
{
    char *buf;
    int eof, ret = def;

    nv_info_msg(NULL, "");
    nv_info_msg(NULL, "%s", msg);
    if (def) fprintf(stdout, "  [default: (Y)es]: ");
    else fprintf(stdout, "  [default: (N)o]: ");
    fflush(stdout);
    
    buf = fget_next_line(stdin, &eof);
    
    if (!buf) return FALSE;
 
    /* key off the first letter; otherwise, just use the default */

    if (tolower(buf[0]) == 'y') ret = TRUE;
    if (tolower(buf[0]) == 'n') ret = FALSE;
    
    free(buf);
    
    return ret;

} /* stream_yes_no() */


/*
 * select_option - given a list of options and a user-provided selection, check
 * to see if the selection matches any of the available options. Options can be
 * selected either by index or by option name. Returns the caller-provided
 * default option if the user's selection is an empty string, the index of the
 * first (case-insensitive) matching option, or a negative number if no option
 * matched the user's input.
 */

static int select_option(const char * const *options, int num_options,
                         int default_option, const char *selection)
{
    int i;

    if (strlen(selection) == 0) {
        return default_option;
    }

    if (isdigit(selection[0])) {
        int index = atoi(selection);

        if (index < 1 || index > num_options) {
            return -2;
        }

        return index - 1;
    }

    for (i = 0; i < num_options; i++) {
        if (strcasecmp(selection, options[i]) == 0) {
            return i;
        }
    }

    return -1;
}



/*
 * stream_multiple_choice() - display a question with multiple-choice answers
 * allowing the user to select a button corresponding to the desired response.
 * Returns the index answer selected from the passed-in answers array.
 */

int stream_multiple_choice(Options *op, const char *question,
                           const char * const *answers, int num_answers,
                           int default_answer)
{
    int ret = default_answer;

    do {
        char *str;
        int i;

        if (ret < 0) {
            nv_error_msg("Invalid response!");
        }

        nv_info_msg(NULL, "");
        nv_info_msg(NULL, "%s", question);
        nv_info_msg(NULL, "Valid responses are: ");

        for (i = 0; i < num_answers; i++) {
            nv_info_msg(NULL, " (%d)\t\"%s\"%s", i + 1, answers[i],
                        i == default_answer ? " [ default ]" : "");
        }

        nv_info_msg(NULL, "Please select your response by number or name:");
        str = fget_next_line(stdin, NULL);

        ret = select_option(answers, num_answers, default_answer, str);
        free(str);
    } while (ret < 0);

    return ret;
}



/*
 * stream_paged_prompt() - display a question with multiple-choice answers
 * along with a text area (not scrollable), allowing the user to review the text
 * and select a button corresponding to the desired response. Returns the index
 * answer selected from the passed-in answers array.
 */

int stream_paged_prompt(Options *op, const char *question,
                        const char *pager_title, const char *pager_text,
                        const char * const *answers, int num_answers,
                        int default_answer)
{
    nv_info_msg(NULL, "");
    nv_info_msg(NULL, "________");
    nv_info_msg(NULL, "");
    nv_info_msg(NULL, "%s", pager_title);
    nv_info_msg(NULL, "");
    nv_info_msg(NULL, "%s", pager_text);
    nv_info_msg(NULL, "");
    nv_info_msg(NULL, "________");

    return stream_multiple_choice(op, question, answers, num_answers,
                                  default_answer);
}



/*
 * stream_status_begin() - we assume that title is always passed, but
 * sometimes msg will be NULL.
 */

void stream_status_begin(Options *op, const char *title, const char *msg)
{
    Data *d = op->ui_priv;
    
    d->status_active = TRUE;
    
    nv_info_msg(NULL, "%s", title);
    d->status_label = nvstrdup(msg);
    
    print_status_bar(d, STATUS_BEGIN, 0.0);
    
} /* stream_status_begin() */



/*
 * stream_status_update() - 
 */

void stream_status_update(Options *op, const float percent, const char *msg)
{
    print_status_bar(op->ui_priv, STATUS_UPDATE, percent);

} /* stream_status_update() */



/*
 * stream_status_end() - 
 */

void stream_status_end(Options *op, const char *msg)
{
    Data *d = op->ui_priv;
    
    print_status_bar(op->ui_priv, STATUS_END, 1.0);
    
    nvfree(d->status_label);
    d->status_active = FALSE;

} /* stream_status_end() */



/*
 * stream_close() - close the ui
 */

void stream_close(Options *op)
{
    return;
    
} /* stream_close() */