summaryrefslogtreecommitdiff
path: root/stream-ui.c
blob: be0326c32cb818184192d80fc0a9699fc4c2434b (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
/*
 * 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"

/* 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*);
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_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) {

        fmtout("");
        fmtout("Welcome to the NVIDIA Software Installer for Unix/Linux");
        fmtout("");
    
        /* 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;
    
    fmt(stdout, NULL, "");
    fmt(stdout, NULL, 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;
    
    fmtout("");
    fmtout("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.");
    fmtout("");
    
    fmtout("________");
    fmtout("");
    printf("%s", license);
    fmtout("________");
    fmtout("");
    
    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) fmt(msg_attrs[level].stream, NULL, "");
    fmt(msg_attrs[level].stream, msg_attrs[level].prefix, msg);
    if (msg_attrs[level].newline) fmt(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;

    fmtoutp("   ", 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 = " --> ";

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

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

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

          default:

            fmterr("Error in CommandList! (cmd: %d; s0: '%s';"
                   "s1: '%s'; s2: '%s'; mode: %04o)",
                   c->cmd, c->s0, c->s1, c->s2, c->mode);
            fmterr("Aborting installation.");
            return FALSE;
            break;
        }
    }
    
    fflush(stdout);
    
    if (!stream_yes_no(op, TRUE, "\nIs this acceptable? (answering 'no' will "
                       "abort installation)")) {
        fmterr("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;

    fmt(stdout, NULL, "");
    fmt(stdout, NULL, 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() */



/*
 * 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;
    
    fmtout(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() */