GnuCOBOL  2.0
A free COBOL compiler
cobcrun.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004-2012, 2014-2016 Free Software Foundation, Inc.
3  Written by Roger While, Simon Sobisch
4 
5  This file is part of GnuCOBOL.
6 
7  The GnuCOBOL compiler is free software: you can redistribute it
8  and/or modify it under the terms of the GNU General Public License
9  as published by the Free Software Foundation, either version 3 of the
10  License, or (at your option) any later version.
11 
12  GnuCOBOL is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with GnuCOBOL. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "config.h"
22 #include "defaults.h"
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stddef.h>
27 #include <string.h>
28 #include "libcob.h"
29 #include "tarstamp.h"
30 
31 #include "libcob/cobgetopt.h"
32 
33 #ifdef HAVE_LOCALE_H
34 #include <locale.h>
35 #endif
36 
37 static int arg_shift = 1;
38 
39 static const char short_options[] = "+hirc:V";
40 
41 #define CB_NO_ARG no_argument
42 #define CB_RQ_ARG required_argument
43 #define CB_OP_ARG optional_argument
44 
45 static const struct option long_options[] = {
46  {"help", CB_NO_ARG, NULL, 'h'},
47  {"info", CB_NO_ARG, NULL, 'i'},
48  {"runtime-env", CB_NO_ARG, NULL, 'r'},
49  {"config", CB_RQ_ARG, NULL, 'C'},
50  {"version", CB_NO_ARG, NULL, 'V'},
51  {NULL, 0, NULL, 0}
52 };
53 
54 #if defined(ENABLE_NLS) && defined(COB_NLS_RUNTIME)
55 #include "lib/gettext.h"
56 #define _(s) gettext(s)
57 #define N_(s) gettext_noop(s)
58 #else
59 #define _(s) s
60 #define N_(s) s
61 #endif
62 
63 
64 static void
66 {
67  char cob_build_stamp[COB_MINI_BUFF];
68  char month[64];
69  int status, day, year;
70 
71  /* Set up build time stamp */
72  memset (cob_build_stamp, 0, (size_t)COB_MINI_BUFF);
73  memset (month, 0, sizeof(month));
74  day = 0;
75  year = 0;
76  status = sscanf (__DATE__, "%s %d %d", month, &day, &year);
77  if (status == 3) {
78  snprintf (cob_build_stamp, (size_t)COB_MINI_MAX,
79  "%s %2.2d %4.4d %s", month, day, year, __TIME__);
80  } else {
81  snprintf (cob_build_stamp, (size_t)COB_MINI_MAX,
82  "%s %s", __DATE__, __TIME__);
83  }
84 
85  printf ("cobcrun (%s) %s.%d\n",
87  puts ("Copyright (C) 2004-2012, 2014-2016 Free Software Foundation, Inc.");
88  printf (_("Written by %s\n"), "Roger While, Simon Sobisch");
89  puts (_("This is free software; see the source for copying conditions. There is NO\n"\
90  "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."));
91  printf (_("Built %s"), cob_build_stamp);
92  putchar ('\n');
93  printf (_("Packaged %s"), COB_TAR_DATE);
94  putchar ('\n');
95 }
96 
97 static void
98 cobcrun_print_usage (char * prog)
99 {
100  puts (_("COBOL driver program for GnuCOBOL modules"));
101  putchar ('\n');
102  printf (_("usage: %s [options] PROGRAM [param ...]"), prog);
103  putchar ('\n');
104  printf (_(" or: %s options"), prog);
105  putchar ('\n');
106  putchar ('\n');
107  puts (_("options:"));
108  puts (_(" -h, -help display this help and exit"));
109  puts (_(" -V, -version display cobcrun and runtime version and exit"));
110  puts (_(" -i, -info display runtime information (build/environment)"));
111  puts (_(" -c <file>, -config=<file> set runtime configuration from <file>"));
112  puts (_(" -r, -runtime-env display current runtime configuration\n"
113  " (value and origin for all settings)"));
114 }
115 
116 /* Set current argument from getopt as environment value */
117 static int
118 cobcrun_setenv (const char * environment)
119 {
120 #if !HAVE_SETENV
121  int len;
122  char * p;
123 
124  len = strlen (environment) + strlen (cob_optarg) + 2U;
125  p = cob_fast_malloc (len);
126  sprintf (p, "%s=%s", environment, cob_optarg);
127  return putenv (p);
128 #else
129  return setenv(environment, cob_optarg, 1);
130 #endif
131 }
132 
133 static void
134 process_command_line (int argc, char *argv[])
135 {
136  int c, idx;
137 #ifdef _WIN32
138  int argnum;
139 
140  /* Translate command line arguments from WIN to UNIX style */
141  argnum = 1;
142  while (++argnum <= argc) {
143  if (strrchr(argv[argnum - 1], '/') == argv[argnum - 1]) {
144  argv[argnum - 1][0] = '-';
145  }
146  }
147 #endif
148 
149  /* c = -1 if idx > argc or argv[idx] has non-option */
150  while ((c = cob_getopt_long_long (argc, argv, short_options,
151  long_options, &idx, 1)) >= 0) {
152  switch (c) {
153  case '?':
154  /* Unknown option or ambiguous */
155  exit (1);
156 
157  case 'c':
158  case 'C':
159  /* --config=<file> */
160  if (strlen (cob_optarg) > COB_SMALL_MAX) {
161  fputs (_("invalid configuration file name"), stderr);
162  putc ('\n', stderr);
163  fflush (stderr);
164  exit (1);
165  }
166  arg_shift++;
167  cobcrun_setenv ("COB_RUNTIME_CONFIG");
168  /* shift argument again if two part argument was used */
169  if (c == 'c') {
170  arg_shift++;
171  }
172  break;
173 
174  case 'h':
175  /* --help */
176  cobcrun_print_usage (argv[0]);
177  exit (0);
178 
179  case 'i':
180  /* --info */
181  print_info ();
182  exit (0);
183 
184  case 'r':
185  /* --runtime-env */
186  cob_init (0, &argv[0]);
188  exit (0);
189 
190  case 'V':
191  /* --version */
193  putchar ('\n');
194  print_version();
195  exit (0);
196  }
197  }
198 }
199 
200 int
201 main (int argc, char **argv)
202 {
203  cob_call_union unifunc;
204 
205 #ifdef HAVE_SETLOCALE
206  setlocale (LC_ALL, "");
207 #endif
208 
209  process_command_line (argc, argv);
210 
211  /* At least one option or module name needed */
212  if (argc <= arg_shift) {
213  cobcrun_print_usage (argv[0]);
214  exit (1);
215  }
216 
217  if (strlen (argv[arg_shift]) > 31) {
218  fputs (_("PROGRAM name exceeds 31 characters"), stderr);
219  putc ('\n', stderr);
220  return 1;
221  }
222  cob_init (argc - arg_shift, &argv[arg_shift]);
223  unifunc.funcvoid = cob_resolve (argv[arg_shift]);
224  if (unifunc.funcvoid == NULL) {
225  cob_call_error ();
226  }
227  cob_stop_run ( unifunc.funcint() );
228 }
int cob_getopt_long_long(const int argc, char *const *argv, const char *optstring, const struct option *longopts, int *longind, const int long_only)
Definition: cobgetopt.c:321
static void cobcrun_print_usage(char *prog)
Definition: cobcrun.c:98
void print_version(void)
Definition: common.c:5119
static int arg_shift
Definition: cobcrun.c:37
#define CB_RQ_ARG
Definition: cobcrun.c:42
void print_info(void)
Definition: common.c:5153
void * cob_resolve(const char *name)
Definition: call.c:908
#define COB_MINI_BUFF
Definition: common.h:539
static int cobcrun_setenv(const char *environment)
Definition: cobcrun.c:118
#define COB_SMALL_MAX
Definition: common.h:546
void * funcvoid
Definition: common.h:1010
#define PACKAGE_NAME
Definition: config.h:300
#define _(s)
Definition: cobcrun.c:59
EC ARGUMENT EC EC BOUND EC BOUND EC BOUND EC BOUND TABLE EC DATA EC DATA EC DATA PTR NULL
Definition: exception.def:95
void cob_init(const int argc, char **argv)
Definition: common.c:5390
void print_runtime_env()
Definition: common.c:5234
int(* funcint)()
Definition: common.h:1009
static void cobcrun_print_version(void)
Definition: cobcrun.c:65
#define CB_NO_ARG
Definition: cobcrun.c:41
#define COB_TAR_DATE
Definition: tarstamp.h:1
int main(int argc, char **argv)
Definition: cobcrun.c:201
void cob_call_error(void)
Definition: call.c:878
static void process_command_line(int argc, char *argv[])
Definition: cobcrun.c:134
static const char short_options[]
Definition: cobcrun.c:39
void * cob_fast_malloc(const size_t size)
Definition: common.c:1296
#define PACKAGE_VERSION
Definition: config.h:312
#define PATCH_LEVEL
Definition: config.h:315
static const struct option long_options[]
Definition: cobcrun.c:45
char * cob_optarg
Definition: cobgetopt.c:77
#define COB_MINI_MAX
Definition: common.h:545
void cob_stop_run(const int status)
Definition: common.c:1524