#include #include #include #define VERSION "1.1" #define PRINT_FLAG_NONE 0 #define PRINT_FLAG_BOURNE_SYNTAX 1 #define PRINT_FLAG_ELISP_SYNTAX 2 int print_flag = PRINT_FLAG_NONE; static const char * convertToElispString (const char *str) { char buf[strlen (str) * 2 + 1]; char *ptr = buf; while (*str) { *ptr++ = *str; if (*str == '\\') *ptr++ = '\\'; str++; } *ptr = '\0'; return strdup (buf); } static void storevar (const char *name, const char *val) { if (print_flag == PRINT_FLAG_BOURNE_SYNTAX) printf ("export %s='%s'\n", name, val); else if (print_flag == PRINT_FLAG_ELISP_SYNTAX) printf ("(setenv \"%s\" \"%s\")\n", name, convertToElispString (val)); else setenv (name, val, 1); } static const char * convertToUnixPath (const char *dosPath) { int len = strlen (dosPath); char *unixPath = malloc (len + 1); int i; if (unixPath == NULL) abort (); for (i = 0; i < len; i++) if (dosPath[i] == '\\') unixPath[i] = '/'; else unixPath[i] = dosPath[i]; unixPath[i] = '\0'; return unixPath; } static const char * copyValue (char *ptr, int keyLen) { char *buf = ptr + keyLen + 1; char *ret; int bufLen = strlen (buf) - 1; if (buf[bufLen] == '\n') buf[bufLen] = '\0'; ret = malloc (bufLen); if (ret == NULL) abort (); strcpy (ret, buf); return ret; } main (int argc, const char **argv) { const char *exec_prefix = "\\H-i386-cygwin32\\lib\\gcc-lib\\"; const char *share_tcl8 = "\\share\\tcl8.0\\"; const char *share_gdbtcl = "/share/gdbtcl"; const char *cyg_path_subdir = "\\H-i386-cygwin32\\bin"; const char *cygRoot = NULL; const char *swarmRoot = NULL; const char *unixSwarmRoot; const char *cygUnixRoot; int cygRootLen; int nextarg = 1; if (argc > 1) { if (strcmp (argv[nextarg], "-b") == 0) { nextarg++; print_flag = PRINT_FLAG_BOURNE_SYNTAX; } else if (strcmp (argv[nextarg], "-e") == 0) { nextarg++; print_flag = PRINT_FLAG_ELISP_SYNTAX; } } { FILE *fp; char buf[80]; char *ptr; const char *setKey = "SET "; int setKeyLen = strlen (setKey); const char *cygRootKey = "CYGROOT"; int cygRootKeyLen = strlen (cygRootKey); const char *swarmRootKey = "SWARMROOT"; int swarmRootKeyLen = strlen (swarmRootKey); const char *varsbat = "vars.bat"; if (argc > nextarg) { const char *path = argv[nextarg]; char buf[strlen (path) + 1 + strlen (varsbat) + 1]; strcpy (buf, convertToUnixPath (path)); strcat (buf, "/"); strcat (buf, varsbat); if ((fp = fopen (buf, "r")) == NULL) { fprintf (stderr, "Cannot open base path file: %s\n", buf); exit (1); } } else if ((fp = fopen (varsbat, "r")) == NULL) { fprintf (stderr, "Cannot open base path file: %s\n", buf); exit (1); } while ((ptr = fgets (buf, sizeof (buf), fp)) != NULL) { if (strncmp (ptr, setKey, setKeyLen) == 0) { ptr += setKeyLen; if (strncmp (ptr, cygRootKey, cygRootKeyLen) == 0) cygRoot = copyValue (ptr, cygRootKeyLen); else if (strncmp (ptr, swarmRootKey, swarmRootKeyLen) == 0) swarmRoot = copyValue (ptr, swarmRootKeyLen); } } } if (cygRoot == NULL) { fprintf (stderr, "Unable to get Cygnus base path\n"); exit (1); } if (swarmRoot == NULL) { fprintf (stderr, "Unable to get SWARMROOT\n"); exit (1); } cygUnixRoot = convertToUnixPath (cygRoot); cygRootLen = strlen (cygRoot); if (cygUnixRoot[1] == ':') cygUnixRoot += 2; storevar ("CYGFS", cygUnixRoot); storevar ("CYGROOT", cygRoot); storevar ("CYGREL", "B19"); storevar ("MAKE_MODE", "unix"); { char buf[cygRootLen + strlen (exec_prefix) + 1]; strcpy (buf, cygRoot); strcat (buf, exec_prefix); storevar ("GCC_EXEC_PREFIX", buf); } { char buf[cygRootLen + strlen (share_tcl8) + 1]; strcpy (buf, cygRoot); strcat (buf, share_tcl8); storevar ("TCL_LIBRARY", buf); } { char buf[strlen (cygUnixRoot) + strlen (share_gdbtcl) + 1]; strcpy (buf, cygUnixRoot); strcat (buf, share_gdbtcl); storevar ("GDBTK_LIBRARY", buf); } unixSwarmRoot = convertToUnixPath (swarmRoot); if (unixSwarmRoot[1] == ':') unixSwarmRoot += 2; if (print_flag == PRINT_FLAG_BOURNE_SYNTAX) printf ("PATH=%s:%s%s:$PATH\n", unixSwarmRoot, cygUnixRoot, convertToUnixPath (cyg_path_subdir)); else if (print_flag == PRINT_FLAG_ELISP_SYNTAX) { printf ("(setenv \"PATH\" (concat \"%s;%s%s;\" (getenv \"PATH\")))\n", convertToElispString (swarmRoot), convertToElispString (cygRoot), convertToElispString (cyg_path_subdir)); printf ("(setq explicit-shell-file-name \"%s%s/bash\")\n", cygUnixRoot, convertToUnixPath (cyg_path_subdir)); printf ("(setq explicit-bash-args '(\"--rcfile\" \"%s/.bash_login\" \"-i\"))\n", unixSwarmRoot); } else { const char *path = getenv ("PATH"); char buf[strlen (swarmRoot) + 1 + cygRootLen + strlen (cyg_path_subdir) + strlen (path) + 2]; strcpy (buf, swarmRoot[1] == ':' ? swarmRoot + 2 : swarmRoot); strcat (buf, ";"); strcat (buf, cygRoot[1] == ':' ? cygRoot + 2 : cygRoot); strcat (buf, cyg_path_subdir); strcat (buf, ";"); strcat (buf, path); storevar ("PATH", buf); } if (print_flag != PRINT_FLAG_ELISP_SYNTAX) storevar ("HOME", unixSwarmRoot); storevar ("SWARMROOT", unixSwarmRoot); storevar ("SWARMSETUP", "SFI-NT"); { const char *packages_subdir = "/packages"; char pbuf[strlen (unixSwarmRoot) + strlen (packages_subdir) + 1]; strcpy (pbuf, unixSwarmRoot); strcat (pbuf, packages_subdir); storevar ("P", pbuf); { const char *blt8_subdir = "/blt8.0-unoff/library"; char bltbuf[strlen (pbuf) + strlen (blt8_subdir) + 1]; strcpy (bltbuf, pbuf); strcat (bltbuf, blt8_subdir); storevar ("BLT_LIBRARY", bltbuf); } } { const char *swarm_subdir = "/swarm-" VERSION; char buf[strlen (unixSwarmRoot) + strlen (swarm_subdir) + 1]; strcpy (buf, unixSwarmRoot); strcat (buf, swarm_subdir); storevar ("SWARMHOME", buf); } storevar ("PS1", "\\u@\\h[\\w] $ "); if (print_flag == PRINT_FLAG_NONE) { const char *bash = "bash"; char buf[strlen (cygRoot) + strlen (cyg_path_subdir) + 1 + strlen (bash) + 1]; const char *ubuf; strcpy (buf, cygRoot); strcat (buf, cyg_path_subdir); strcat (buf, "\\"); strcat (buf, "bash"); ubuf = convertToUnixPath (buf); execlp (ubuf, bash, "--login", NULL); } }