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
|
/* $Id: common.c,v 1.3 2005/10/18 19:05:50 ellson Exp $ $Revision: 1.3 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2004 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
/* Lefteris Koutsofios - AT&T Labs Research */
#include "common.h"
int warnflag;
char *leftypath, *leftyoptions, *shellpath;
jmp_buf exitljbuf;
int idlerunmode;
fd_set inputfds;
static int innetscape;
static char *nswin;
#ifndef FEATURE_MS
#define PATHDEL '/'
#define PATHSEP ':'
#define PATHSEPSTR ":"
#define PATHLEFTY "/../lib/lefty"
#else
#define PATHDEL '\\'
#define PATHSEP ';'
#define PATHSEPSTR ";"
#define PATHLEFTY "\\..\\lib\\lefty"
#endif
#ifdef FEATURE_X11
#define WINSYS "LEFTYWINSYS=X11"
#else
#define WINSYS "LEFTYWINSYS=mswin"
#endif
#include "g.h"
#include "gcommon.h"
#ifdef FEATURE_MS
extern int Gnocallbacks;
#else
static int Gnocallbacks;
#endif
static char *pathp;
#define PATHINCR 10240
#define PATHSIZE sizeof (char)
static char *cmdp;
#define CMDINCR 4096
#define CMDSIZE sizeof (char)
static char *lpathp;
int init (char *aout) {
char *s1, *s2, c;
int k;
#ifdef FEATURE_WIN32
extern HANDLE hinstance;
char buf[260];
#endif
#ifdef LEFTYDATADIR
char *leftdatadir = LEFTYDATADIR;
#else
char *leftdatadir = NULL;
#endif
c = 0;
if (getenv ("INNETSCAPE"))
innetscape = TRUE, nswin = getenv ("NSWIN");
if (!(pathp = malloc (PATHINCR * PATHSIZE)))
panic (POS, "init", "pathp malloc failed");
if (!(cmdp = malloc (CMDINCR * CMDSIZE)))
panic (POS, "init", "cmdp malloc failed");
shellpath = getenv ("PATH");
#ifdef FEATURE_WIN32
GetModuleFileName (hinstance, buf, 260);
aout = buf;
#else
if (!strchr (aout, PATHDEL)) {
leftypath = "";
if ((s1 = buildpath (aout, TRUE)))
aout = strdup (s1);
} else
aout = strdup (aout);
#endif
if (!(s1 = strrchr (aout, PATHDEL)))
s1 = aout;
*s1 = 0;
if (!(leftypath = malloc (PATHINCR * PATHSIZE)))
panic (POS, "init", "leftypath malloc failed");
leftypath[0] = 0;
if ((s1 = getenv ("LEFTYPATH"))) {
strcat (leftypath, s1);
strcat (leftypath, PATHSEPSTR);
}
if (*aout) {
strcat (leftypath, aout);
strcat (leftypath, PATHSEPSTR);
}
for (k = 0; k < 2; k++) {
if (k == 0)
s1 = aout;
else
s1 = shellpath;
while (s1) {
if ((s2 = strchr (s1, PATHSEP)))
c = *s2, *s2 = 0;
strcat (leftypath, s1);
strcat (leftypath, PATHLEFTY);
if (s2) {
*s2 = c, s1 = s2 + 1;
strcat (leftypath, PATHSEPSTR);
} else
s1 = NULL;
}
if (leftypath[0])
strcat (leftypath, PATHSEPSTR);
}
if (leftdatadir) { /* support a compile-time path as last resort */
strcat (leftypath, leftdatadir);
strcat (leftypath, PATHSEPSTR);
}
if (!(leftyoptions = getenv ("LEFTYOPTIONS")))
leftyoptions = "";
putenv (WINSYS);
return 0;
}
void term (void) {
if (lpathp)
free (lpathp);
if (pathp)
free (pathp);
if (cmdp)
free (cmdp);
}
/* given a file name, it looks for this file in LEFTYPATH
(and if flag == TRUE in PATH)
returns the first occurance of that file or NULL
*/
char *buildpath (char *file, int flag) {
struct stat statbuf;
char *s1, *s2;
int mode, pathi, i;
#ifdef FEATURE_NETSCAPE
if (flag == FALSE && innetscape) {
#ifdef FEATURE_WIN32
HWND hwnd;
char *s;
if (!nswin) {
MessageBox (
(HWND) NULL, "error: no peer window",
"Lefty Warning", MB_APPLMODAL
);
return NULL;
}
hwnd = atol (nswin);
fprintf (stdout, "file %s\n", file);
if (fflush (stdout) == -1) {
MessageBox (
(HWND) NULL, "Lost Connection to Netscape",
"Lefty Warning", MB_APPLMODAL
);
return NULL;
}
SendMessage (hwnd, WM_USER, 12, 34);
fgets (pathp, PATHINCR - 1, stdin);
pathp[strlen (pathp) - 1] = 0;
if (pathp[0] == 0)
return NULL;
return pathp;
#else
Window window;
static XButtonEvent ev;
if (!nswin) {
fprintf (stderr, "error: no peer window\n");
return NULL;
}
window = strtol (nswin, NULL, 16);
fprintf (stdout, "file %s\n", file);
if (fflush (stdout) == -1) {
fprintf (stderr, "Lost Connection to Netscape\n");
return NULL;
}
ev.type = ButtonPress;
ev.window = window;
ev.x = -123, ev.y = -123;
XSendEvent (Gdisplay, window, False, 0, (XEvent *) &ev);
XFlush (Gdisplay);
fgets (pathp, PATHINCR - 1, stdin);
pathp[strlen (pathp) - 1] = 0;
if (pathp[0] == 0)
return NULL;
return pathp;
#endif
}
#endif
#ifndef FEATURE_MS
if (file && file[0] && strchr (file, PATHDEL))
return file;
mode = S_IRUSR | (flag ? S_IXUSR : 0);
for (i = 0; i < 2; i++) {
if (i == 0)
s1 = leftypath;
else
s1 = shellpath;
while (*s1) {
pathi = 0;
while (*s1 && *s1 != PATHSEP)
if (pathi < PATHINCR)
pathp[pathi++] = *s1++;
if (*s1)
s1++;
if (pathi + 3 + strlen (file) >= PATHINCR)
continue;
pathp[pathi++] = PATHDEL;
for (s2 = file; *s2; s2++)
pathp[pathi++] = *s2;
pathp[pathi] = '\000';
if (stat (pathp, &statbuf) == 0 && (statbuf.st_mode & mode))
return pathp;
}
}
#else
if (file && file[0] && strchr (file, PATHDEL))
return file;
mode = ~0;
for (i = 0; i < 2; i++) {
if (i == 0)
s1 = leftypath;
else
s1 = shellpath;
while (*s1) {
pathi = 0;
while (*s1 && *s1 != PATHSEP)
if (pathi < PATHINCR)
pathp[pathi++] = *s1++;
if (*s1)
s1++;
if (pathi + 7 + strlen (file) >= PATHINCR)
continue;
pathp[pathi++] = PATHDEL;
for (s2 = file; *s2; s2++)
pathp[pathi++] = *s2;
if (flag) {
pathp[pathi++] = '.';
pathp[pathi++] = 'e';
pathp[pathi++] = 'x';
pathp[pathi++] = 'e';
}
pathp[pathi] = '\000';
if (stat (pathp, &statbuf) == 0 && (statbuf.st_mode & mode))
return pathp;
}
}
#endif
return NULL;
}
/* given a file name (path) and an optional format (fmt)
it builds a shell command.
%e is replaced by the command path
%i ... the input channel descriptor
%o ... the output channel descriptor
%h ... the hostname
returns the complete command string or NULL
*/
char *buildcommand (char *path, char *host, int infd, int outfd, char *fmt) {
char buf[10];
char *s1, *s2;
int bufi;
cmdp[0] = '\000';
for (bufi = 0, s1 = fmt; *s1; s1++) {
if (*s1 == '%') {
if (*(s1 + 1) == 'e') {
s1++;
if (bufi + strlen (path) >= CMDINCR)
return NULL;
for (s2 = path; *s2; s2++)
cmdp[bufi++] = *s2;
continue;
} else if (*(s1 + 1) == 'i') {
if (infd == -1)
buf[0] = '%', buf[1] = 'd', buf[2] = '\000';
else
sprintf (buf, "%d", infd);
s1++;
if (bufi + strlen (buf) >= CMDINCR)
return NULL;
for (s2 = buf; *s2; s2++)
cmdp[bufi++] = *s2;
continue;
} else if (*(s1 + 1) == 'o') {
if (outfd == -1)
buf[0] = '%', buf[1] = 'd', buf[2] = '\000';
else
sprintf (buf, "%d", outfd);
s1++;
if (bufi + strlen (buf) >= CMDINCR)
return NULL;
for (s2 = buf; *s2; s2++)
cmdp[bufi++] = *s2;
continue;
} else if (*(s1 + 1) == 'h') {
s1++;
if (bufi + strlen (host) >= CMDINCR)
return NULL;
for (s2 = host; *s2; s2++)
cmdp[bufi++] = *s2;
continue;
}
}
if (bufi + 1 >= CMDINCR)
return NULL;
cmdp[bufi++] = *s1;
}
if (bufi + 1 >= CMDINCR)
return NULL;
cmdp[bufi] = '\000';
return &cmdp[0];
}
/* varargs function for printing a warning */
void warning (char *file, int line, char *func, char *fmt, ...) {
va_list args;
#ifndef FEATURE_MS
if (!warnflag)
return;
va_start(args, fmt);
Gnocallbacks = TRUE;
fprintf (stderr, "warning: (file %s, line %d, func %s) ", file, line, func);
vfprintf (stderr, fmt, args);
fprintf (stderr, "\n");
Gnocallbacks = FALSE;
va_end(args);
#else
char buf[256];
if (!warnflag)
return;
va_start(args, fmt);
vsprintf (buf, fmt, args);
Gnocallbacks = TRUE;
MessageBox ((HWND) NULL, buf, "Lefty Warning", MB_APPLMODAL);
Gnocallbacks = FALSE;
va_end(args);
#endif
}
/* varargs function for printing an error message and aborting */
void panic (char *file, int line, char *func, char *fmt, ...) {
va_list args;
#ifndef FEATURE_MS
va_start(args, fmt);
Gnocallbacks = TRUE;
fprintf (stderr, "panic: (file %s, line %d, func %s) ", file, line, func);
vfprintf (stderr, fmt, args);
fprintf (stderr, "\n");
fflush (stdout);
Gnocallbacks = FALSE;
va_end(args);
#else
char buf[256];
va_start(args, fmt);
vsprintf (buf, fmt, args);
Gnocallbacks = TRUE;
MessageBox ((HWND) NULL, buf, "Lefty PANIC", MB_APPLMODAL);
Gnocallbacks = FALSE;
va_end(args);
#endif
abort ();
}
/* varargs function for printing an error message, and the
error message corresponding to errno and aborting
*/
void panic2 (char *file, int line, char *func, char *fmt, ...) {
va_list args;
#ifndef FEATURE_MS
va_start(args, fmt);
Gnocallbacks = TRUE;
fprintf (stderr, "panic: (file %s, line %d, func %s) ", file, line, func);
vfprintf (stderr, fmt, args);
fprintf (stderr, "\n");
perror ("");
fflush (stdout);
Gnocallbacks = FALSE;
va_end(args);
#else
char buf[256];
va_start(args, fmt);
vsprintf (buf, fmt, args);
Gnocallbacks = TRUE;
MessageBox ((HWND) NULL, buf, "Lefty PANIC", MB_APPLMODAL);
Gnocallbacks = FALSE;
va_end(args);
#endif
abort ();
}
#ifdef FEATURE_MS
int printf (const char *fmt, ...) {
va_list args;
char buf[10240];
int l;
va_start(args, fmt);
vsprintf (buf, fmt, args);
l = strlen (buf);
if (buf[l - 1] == '\n')
buf[l - 1] = 0;
if (buf[0]) {
Gnocallbacks = TRUE;
MessageBox ((HWND) NULL, buf, "Lefty printf", MB_APPLMODAL);
Gnocallbacks = FALSE;
}
va_end(args);
}
#endif
|