File: vcvars.c

package info (click to toggle)
allegro4.2 2:4.2.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 24,436 kB
  • ctags: 14,714
  • sloc: ansic: 126,425; asm: 17,011; cpp: 3,846; sh: 2,120; objc: 925; makefile: 715; python: 216; pascal: 179; perl: 73
file content (147 lines) | stat: -rw-r--r-- 3,604 bytes parent folder | download | duplicates (16)
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
/*
 *  VCVARS - sets up a command prompt with suitable options for using MSVC.
 *
 *  This program tries to locate the vcvars32.bat file, first by looking in
 *  the registry, and then if that fails, by asking the user. It then sets
 *  the environment variable VCVARS to the full path of this file.
 *
 *  If it was given a commandline argument, it then runs that program,
 *  or otherwise it opens up a new command prompt. In either case it will
 *  adjust the environment size to make sure there is enough space for
 *  setting the compiler variables.
 */


#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>



/* try to read a value from the registry */
int read_registry(char *path, char *var, char *val, int size)
{
   HKEY key;

   if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &key) != ERROR_SUCCESS)
      return 0;

   if (RegQueryValueEx(key, var, NULL, NULL, val, &size) != ERROR_SUCCESS) {
      RegCloseKey(key);
      return 0;
   }

   RegCloseKey(key);
   return 1;
}



/* locate vcvars32.bat */
void find_vcvars()
{
   char data[256], name[256];
   int i;

   /* look in the registry (try VC versions 4 through 9, just in case :-) */
   for (i=9; i>=4; i--) {
      sprintf(name, "Software\\Microsoft\\DevStudio\\%d.0\\Products\\Microsoft Visual C++", i);

      if (read_registry(name, "ProductDir", data, sizeof(data))) {
	 strcat(data, "\\bin\\vcvars32.bat");

	 if (access(data, 4) == 0) {
	    printf("Found %s\n", data);
	    break;
	 }
      }

      data[0] = 0;
   }

   /* oh dear, have to ask the user where they put it */
   if (!data[0]) {
      printf("\n  Unable to find MSVC ProductDir information in your registry!\n\n");
      printf("  To install Allegro, I need to know the path where your compiler is\n");
      printf("  installed. Somewhere in your MSVC installation directory there will\n");
      printf("  be a file called vcvars32.bat, which contains this information.\n");
      printf("  Please enter the full path to where I can find that file, for example\n");
      printf("  c:\\Program Files\\Microsoft Visual Studio\\VC98\\bin\\vcvars32.bat\n");

      for (;;) {
	 printf("\n> ");
	 fflush(stdout);

	 if (gets(data)) {
	    i = strlen(data) - 12;
	    if (i < 0)
	       i = 0;

	    if (stricmp(data+i, "vcvars32.bat") != 0)
	       printf("\nError: that path doesn't end in vcvars32.bat!\n");
	    else if (access(data, 4) != 0)
	       printf("\nError: can't find a vcvars32.bat file there!\n");
	    else {
	       printf("\nUsing %s\n", data);
	       break;
	    }
	 }

	 data[0] = 0;
      }
   }

   /* put it in the environment */
   strcpy(name, "VCVARS=");
   strcat(name, data);

   putenv(name);
}



/* the main program */
int main(int argc, char *argv[])
{
   char cmd[256];
   int i;

   find_vcvars();

   if ((getenv("OS")) && (stricmp(getenv("OS"), "Windows_NT") == 0)) {
      if (argc > 1) {
	 /* run program using cmd.exe */
	 strcpy(cmd, "cmd.exe /e:8192 /c");
      }
      else {
	 /* TSR using cmd.exe */
	 sprintf(cmd, "cmd.exe /e:8192 /k \"%s\"", getenv("VCVARS"));
      }
   }
   else {
      if (argc > 1) {
	 /* run program using command.com */
	 strcpy(cmd, "command.com /e:8192 /c");
      }
      else {
	 /* TSR using command.com */
	 sprintf(cmd, "command.com /e:8192 /k \"%s\"", getenv("VCVARS"));
      }
   }

   /* what program do we want to invoke? */
   for (i=1; i<argc; i++) {
      strcat(cmd, " ");
      strcat(cmd, argv[i]);
   }

   /* do it... */
   printf("Invoking nested command interpreter\n");

   system(cmd);

   return 0;
}