summaryrefslogtreecommitdiff
path: root/src/msg.c
blob: 0642a650e4b88feb7d8f15cb717c1b2e57de3ae9 (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
/*
 * nvidia-settings: A tool for configuring the NVIDIA X driver on Unix
 * and Linux systems.
 *
 * Copyright (C) 2004 NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of Version 2 of the GNU General Public
 * License 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 Version 2
 * of 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
 *
 */

#include "msg.h"

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>

static void format(FILE*, const char*, const char *, va_list);
static int get_terminal_width(void);

/*
 * nv_error_msg() - print an error message, nicely formatted using the
 * format() function.
 */

void nv_error_msg(const char *fmt, ...)
{
    va_list ap;

    fprintf(stderr, "\n");
 
    va_start(ap, fmt);
    format(stderr, "ERROR: ", fmt, ap);
    va_end(ap);

    fprintf(stderr, "\n");

} /* nv_error_msg() */



/*
 * nv_warning_msg() - print a warning message, nicely formatted using
 * the format() function.
 */

void nv_warning_msg(const char *fmt, ...)
{
    va_list ap;
    
    fprintf(stdout, "\n");

    va_start(ap, fmt);
    format(stdout, "WARNING: ", fmt, ap);
    va_end(ap);

    fprintf(stdout, "\n");
    
} /* nv_warning_msg() */



/*
 * nv_msg() - print a message, nicely formatted using the format()
 * function.
 */

void nv_msg(const char *prefix, const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    format(stdout, prefix, fmt, ap);
    va_end(ap);

} /* nv_msg() */



/*
 * XXX gcc's '-ansi' option causes vsnprintf to not be defined, so
 * declare the prototype here.
 */

#if defined(__STRICT_ANSI__)
int vsnprintf(char *str, size_t size, const char  *format,
              va_list ap);
#endif



#define FMT_BUF_LEN 256

/*
 * nv_build_vararg_string() - return an alloced string, assembled
 * using vsnprintf().
 */

char *nv_build_vararg_string(const char *fmt, va_list ap)
{
    int len, finished, current_len;
    char *buf;

    finished = 0;
    current_len = FMT_BUF_LEN;
    buf = malloc(current_len);
    do {
        len = vsnprintf(buf, current_len, fmt, ap);
        if ((len == -1) || len > current_len) {
            
            /*
             * if we get in here we know that vsnprintf had to truncate the
             * string to make it fit in the buffer... we need to extend the
             * buffer to encompass the string.  Unfortunately, we have to deal
             * with two different semantics of the return value from
             * (v)snprintf:
             *
             * -1 when the buffer is not long enough (glibc < 2.1)
             * 
             * or
             *
             * the length the string would have been if the buffer had been
             * large enough (glibc >= 2.1)
             */
            
            if (len == -1) current_len += FMT_BUF_LEN;
            else current_len = len+1;
            buf = realloc(buf, current_len);
        }
        else finished = 1;
        
    } while (!finished);

    return buf;

} /* nv_build_vararg_string() */



/*
 * format() - this takes a printf-style format string and a variable
 * list of args.  We use sprintf to generate the desired string, and
 * then format the string so that not more than 80 characters are
 * printed across.
 */

static void format(FILE *stream, const char *prefix,
                   const char *fmt, va_list ap)
{
    int len, prefix_len, z, w, i, max_width;
    char *buf, *line, *local_prefix, *a, *b, *c;
        
    buf = nv_build_vararg_string(fmt, ap);
    
    max_width = get_terminal_width();

    /* loop until we've printed the entire string */
    
    z = strlen(buf);
    a = buf;

    if (prefix) {
        prefix_len = strlen(prefix);
        local_prefix = malloc(prefix_len+1);
        strcpy(local_prefix, prefix);
    } else {
        prefix_len = 0;
        local_prefix = NULL;
    }
    
    do {
        w = max_width;
        
        /* adjust the max width for any prefix */

        w -= prefix_len;
        
        /*
         * if the string will fit on one line, point b to the end of the
         * string
         */
        
        if (z < w) b = a + z;

        /* 
         * if the string won't fit on one line, move b to where the end of
         * the line should be, and then move b back until we find a space;
         * if we don't find a space before we back b all the way up to a, start
         * b at a, and move forward until we do find a space
         */
        
        else {
            b = a + w;
            while ((b >= a) && (!isspace(*b))) b--;
            
            if (b <= a) {
                b = a;
                while (!isspace(*b)) b++;
            }
        }

        /* look for any newline in the line, and move b to it */
        
        for (c = a; c < b; c++) if (*c=='\n') { b = c; break; }
        
        /* print the string that starts at a and ends at b */

        /*
         * XXX this could be done just by temporarily overwriting b
         * with '\0'
         */

        len = b-a;
        line = malloc(len+1);
        strncpy(line, a, len);
        line[len] = '\0';
        if (local_prefix) {
            fprintf(stream, "%s%s\n", local_prefix, line); 
        } else {
            fprintf(stream, "%s\n", line);
        }
        free(line);
        
        /*
         * adjust the length of the string and move the pointer to the
         * beginning of the new line
         */
        
        z -= (b - a + 1);
        a = b + 1;

        /* move to the first non whitespace character */
        
        while ((z > 0) && (isspace(*a))) a++, z--;

        if (local_prefix) {
            for (i = 0; i < prefix_len; i++) local_prefix[i] = ' ';
        }
        
    } while (z > 0);
    
    free (buf);
    if (local_prefix) free (local_prefix);

} /* format() */


#define DEFAULT_MAX_WIDTH 75

static int get_terminal_width(void)
{
    struct winsize ws;
    
    if (ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
        return DEFAULT_MAX_WIDTH;
    } else {
        return (ws.ws_col - 1);
    }
}