1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 /* Lefteris Koutsofios - AT&T Bell Laboratories */
15 
16 #include <windows.h>
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 static char *shellpath;
25 
26 static char *buildpath (char *);
27 static void panic (char *, int, char *, char *, ...);
28 
WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)29 int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
30         LPSTR lpCmdLine, int nCmdShow) {
31     HANDLE handle;
32     char cmd[256];
33     char *path;
34     char *s;
35 
36     shellpath = getenv ("PATH");
37     if (!shellpath || !(path = buildpath ("lefty"))) {
38         if (!GetModuleFileName (hInstance, cmd, 256) ||
39                 !(s = strrchr (cmd, '\\')))
40             exit (1);
41         *s = 0;
42         shellpath = &cmd[0];
43         if (!(path = buildpath ("lefty")))
44             exit (1);
45     }
46     if (lpCmdLine[0] == 0)
47         sprintf (cmd, "%s -e \"load('dotty.lefty');dotty.protogt.lserver='neato';dotty.simple(null);\"", path);
48     else
49         sprintf (cmd, "%s -e \"load('dotty.lefty');dotty.protogt.lserver='neato';dotty.simple('%Ns');\"", path, lpCmdLine);
50 
51     handle = WinExec (cmd, SW_SHOW);
52     exit (0);
53 }
54 
55 #define PATHDEL '\\'
56 #define PATHSEP ';'
57 
58 static char pathbuf[1024];
59 static char commandbuf[1024];
60 
buildpath(char * file)61 static char *buildpath (char *file) {
62     struct stat statbuf;
63     char *s1, *s2;
64     int mode, pathi;
65 
66     if (file && file[0] && (file[0] == '.' || file[0] == PATHDEL))
67         return file;
68     mode = ~0;
69     s1 = shellpath;
70     while (*s1) {
71         pathi = 0;
72         while (*s1 && *s1 != PATHSEP)
73             pathbuf[pathi++] = *s1++;
74         if (*s1)
75             s1++;
76         pathbuf[pathi++] = PATHDEL;
77         for (s2 = file; *s2; s2++)
78             pathbuf[pathi++] = *s2;
79         pathbuf[pathi++] = '.';
80         pathbuf[pathi++] = 'e';
81         pathbuf[pathi++] = 'x';
82         pathbuf[pathi++] = 'e';
83         pathbuf[pathi] = '\000';
84         if (stat (pathbuf, &statbuf) == 0 && (statbuf.st_mode & mode))
85             return pathbuf;
86     }
87     return NULL;
88 }
89 
panic(char * file,int line,char * func,char * fmt,...)90 static void panic (char *file, int line, char *func, char *fmt, ...) {
91     va_list args;
92 
93     va_start(args, fmt);
94     {
95         char buf[256];
96         vsprintf (buf, fmt, args);
97         MessageBox ((HWND) NULL, buf, "dotty PANIC", MB_APPLMODAL);
98     }
99     abort ();
100 }
101