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
|
/*
* nvidia-xconfig: A tool for manipulating X config files,
* specifically for use by the NVIDIA Linux graphics driver.
*
* Copyright (C) 2005 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>.
*
*
* Util.c
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include "xf86Parser.h"
#include "Configint.h"
void *xconfigAlloc(size_t size)
{
void *m = malloc(size);
if (!m) {
fprintf(stderr, "memory allocation failure (%s)! \n", strerror(errno));
exit(1);
}
memset((char *) m, 0, size);
return m;
} /* xconfigAlloc() */
/*
* xconfigStrdup() - wrapper for strdup() that checks the return
* value; if an error occurs, an error is printed to stderr and exit
* is called -- this function will only return on success.
*/
char *xconfigStrdup(const char *s)
{
char *m;
if (!s) return NULL;
m = strdup(s);
if (!m) {
fprintf(stderr, "memory allocation failure during strdup (%s)! \n",
strerror(errno));
exit(1);
}
return m;
} /* xconfigStrdup() */
/*
* xconfigStrcat() - allocate a new string, copying all given strings
* into it. taken from glib
*/
char *xconfigStrcat(const char *str, ...)
{
unsigned int l;
va_list args;
char *s;
char *concat;
l = 1 + strlen(str);
va_start(args, str);
s = va_arg(args, char *);
while (s) {
l += strlen(s);
s = va_arg(args, char *);
}
va_end(args);
concat = xconfigAlloc(l);
concat[0] = 0;
strcat(concat, str);
va_start(args, str);
s = va_arg(args, char *);
while (s) {
strcat(concat, s);
s = va_arg(args, char *);
}
va_end(args);
return concat;
} /* xconfigStrcat() */
#define NV_FMT_BUF_LEN 64
extern int configLineNo;
extern char *configSection;
extern char *configPath;
void xconfigErrorMsg(MsgType t, char *fmt, ...)
{
va_list ap;
int len, current_len = NV_FMT_BUF_LEN;
char *b, *pre = NULL, *msg;
char scratch[64];
b = xconfigAlloc(current_len);
while (1) {
va_start(ap, fmt);
len = vsnprintf(b, current_len, fmt, ap);
va_end(ap);
if ((len > -1) && (len < current_len)) {
break;
} else if (len > -1) {
current_len = len + 1;
} else {
current_len += NV_FMT_BUF_LEN;
}
free(b);
b = xconfigAlloc(current_len);
}
switch (t) {
case ParseErrorMsg:
sprintf(scratch, "%d", configLineNo);
pre = xconfigStrcat("Parse error on line ", scratch, " of section ",
configSection, " in file ", configPath, ".\n", NULL);
break;
case ParseWarningMsg:
sprintf(scratch, "%d", configLineNo);
pre = xconfigStrcat("Parse warning on line ", scratch, " of section ",
configSection, " in file ", configPath, ".\n", NULL);
break;
case ValidationErrorMsg:
pre = xconfigStrcat("Data incomplete in file ", configPath, ".\n", NULL);
break;
case InternalErrorMsg: break;
case WriteErrorMsg: break;
case WarnMsg: break;
case ErrorMsg: break;
case DebugMsg: break;
case UnknownMsg: break;
}
if (pre) {
msg = xconfigStrcat(pre, b, NULL);
} else {
msg = strdup(b);
}
/* call back into the host to print the message */
xconfigPrint(t, msg);
free(b);
free(msg);
if (pre) free(pre);
}
|