File: slstkchk

package info (click to toggle)
slang2 2.3.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,488 kB
  • sloc: ansic: 101,756; sh: 3,435; makefile: 1,046; pascal: 440
file content (119 lines) | stat: -rwxr-xr-x 2,917 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env slsh
require ("cmdopt");
require ("stkcheck");

private variable Version = "0.1.0-0";
private variable Output_Fp;

private define _slstkchk_version ()
{
   () = fprintf (stderr, "slstkchk version %s; S-Lang version: %s\n",
		 Version, _slang_version_string);
}

private define _slstkchk_usage ()
{
   variable fp = stderr;
   () = fprintf (fp, "Usage: %s [options] script args...\n", path_basename (__argv[0]));
   () = fprintf (fp, "Options:\n");
   variable opts =
     [
      " -f|--func <function>       Name of Function to call [default: slsh_main]\n",
      " -o|--output <file>         Name of output file [default: write to stderr]\n",
      " -v|--version               Print version information\n",
      " -h|--help                  Print this message\n"
     ];
   variable opt;
   foreach opt (opts)
     () = fputs (opt, fp);
   exit (1);
}

private define _slstkchk_cmdopt_error (msg)
{
   () = fprintf (stderr, "%s\n", msg);
   _slstkchk_usage ();
}

private define _slstkchk_output_hook (fp, str)
{
   () = fputs (str, fp);
}

private define _slstkchk_main ()
{
   variable output = NULL;
   variable func = "slsh_main";

   variable opts = cmdopt_new (&_slstkchk_cmdopt_error);
   opts.add("f|funct", &func; type="string");
   opts.add("o|output",&output; type="string");
   opts.add("v|version", &_slstkchk_version),
   opts.add("h|help", &_slstkchk_usage);
   variable i = opts.process (__argv, 1);

   if (func == "")
     _slstkchk_usage ();

   if (i >= __argc)
     _slstkchk_usage ();

   variable main = strtok (func, " \t(;")[0];
   variable main_args = strtrim (substrbytes (func, 1+strlen(main), -1));

   variable depth = _stkdepth();
   try
     {
	if (strlen (main)) eval (main_args);
     }
   catch AnyError:
     {
	() = fprintf (stderr, "Error parsing args of %s\n", func);
	exit (1);
     }
   main_args = __pop_list (_stkdepth ()-depth);

   variable script = __argv[i];

   if (not path_is_absolute (script))
     script = path_concat (getcwd (), script);

   variable st = stat_file (script);
   if (st == NULL)
     {
	() = fprintf (stderr, "Unable to stat %s -- did you specify its path?\n", script);
	exit (1);
     }

   if (not stat_is ("reg", st.st_mode))
     () = fprintf (stderr, "*** Warning %s is not a regular file\n", script);

   variable fp = NULL;
   if (output != NULL)
     {
	fp = fopen (output, "w");
	if (fp == NULL)
	  {
	     () = fprintf (stderr, "Unable to open output file %s\n");
	     exit (1);
	  }
	stkcheck_set_output_hook (&_slstkchk_output_hook, fp);
     }

   __set_argc_argv (__argv[[i:]]);

   enable_stack_check ();
   () = evalfile (script);

   variable ref = __get_reference (main);
   if (ref != NULL)
     (@ref) (__push_list (main_args));
   else if (main != "slsh_main")
     () = fprintf (stderr, "*** Warning: %s is not defined\n", main);

   if (fp != NULL)
     () = fclose (fp);
}

_slstkchk_main ();
exit (0);