1 /* Copyright 1996-1997,2000-2001,2006,2008,2011,2013,2018,2021
2      Free Software Foundation, Inc.
3 
4    This file is part of Guile.
5 
6    Guile is free software: you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as published
8    by the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10 
11    Guile is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14    License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with Guile.  If not, see
18    <https://www.gnu.org/licenses/>.  */
19 
20 /* This is the 'main' function for the `guile' executable.  It is not
21    included in libguile.a.
22 
23    Eventually, we hope this file will be automatically generated,
24    based on the list of installed, statically linked libraries on the
25    system.  For now, please don't put interesting code in here.  */
26 
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30 
31 #include <locale.h>
32 #include <stdio.h>
33 
34 #ifdef HAVE_WINSOCK2_H
35 #include <winsock2.h>
36 #endif
37 
38 #include <libguile.h>
39 
40 static void
inner_main(void * closure SCM_UNUSED,int argc,char ** argv)41 inner_main (void *closure SCM_UNUSED, int argc, char **argv)
42 {
43 #ifdef __MINGW32__
44   /* This is necessary to startup the Winsock API under Win32. */
45   WSADATA WSAData;
46   WSAStartup (0x0202, &WSAData);
47 #endif /* __MINGW32__ */
48 
49   /* module initializations would go here */
50   scm_shell (argc, argv);
51 
52 #ifdef __MINGW32__
53   WSACleanup ();
54 #endif /* __MINGW32__ */
55 }
56 
57 static int
get_integer_from_environment(const char * var,int def)58 get_integer_from_environment (const char *var, int def)
59 {
60   char *end = 0;
61   char *val = getenv (var);
62   long res = def;
63   if (!val)
64     return def;
65   res = strtol (val, &end, 10);
66   if (end == val)
67     {
68       fprintf (stderr, "guile: warning: invalid %s: %s\n", var, val);
69       return def;
70     }
71   return res;
72 }
73 
74 static int
should_install_locale(void)75 should_install_locale (void)
76 {
77   /* If the GUILE_INSTALL_LOCALE environment variable is unset,
78      or set to a nonzero value, we should install the locale via
79      setlocale().  */
80   return get_integer_from_environment ("GUILE_INSTALL_LOCALE", 1);
81 }
82 
83 int
main(int argc,char ** argv)84 main (int argc, char **argv)
85 {
86   /* If we should install a locale, do it right at the beginning so that
87      string conversion for command-line arguments, along with possible
88      error messages, use the right locale.  See
89      <https://lists.gnu.org/archive/html/guile-devel/2011-11/msg00041.html>
90      for the rationale.  */
91   if (should_install_locale () && setlocale (LC_ALL, "") == NULL)
92     fprintf (stderr, "guile: warning: failed to install locale\n");
93 
94   scm_boot_guile (argc, argv, inner_main, 0);
95   return 0; /* never reached */
96 }
97