Revert bs-a and bs-b patch.
[mpsl.git] / mpsl_m.c
blobddeb2ca957dd42d9da74cd31dbfb029d15ed497a
1 /*
3 MPSL - Minimum Profit Scripting Language
4 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
6 mpsl_m.c - Minimum Profit Scripting Language main()
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <wchar.h>
31 #include "mpdm.h"
32 #include "mpsl.h"
35 /** code **/
37 mpdm_t trap_func(mpdm_t args, mpdm_t ctxt)
39 mpdm_t c = mpdm_aget(args, 0);
40 mpdm_t r = mpdm_aget(args, 2);
42 printf("-- Code ---------------\n");
43 mpdm_dump(c);
44 printf("-- Ret ---------------\n");
45 mpdm_dump(r);
47 printf("Press ENTER\n");
48 getchar();
50 return NULL;
54 int mpsl_main(int argc, char *argv[])
56 mpdm_t v = NULL;
57 mpdm_t w = NULL;
58 char *immscript = NULL;
59 FILE *script = stdin;
60 int ret = 0;
61 int dump_only = 0;
62 int install_trap = 0;
63 int ok = 0;
65 /* skip the executable */
66 argv++;
67 argc--;
69 while (!ok && argc > 0) {
70 if (strcmp(argv[0], "-v") == 0 || strcmp(argv[0], "--help") == 0) {
71 printf("MPSL %s - Minimum Profit Scripting Language\n",
72 VERSION);
73 printf("Copyright (C) 2003-2012 Angel Ortega <angel@triptico.com>\n");
74 printf("This software is covered by the GPL license. NO WARRANTY.\n\n");
76 printf("Usage: mpsl [-d | -s] [-e 'script' | script.mpsl ]\n\n");
78 return 0;
80 else
81 if (strcmp(argv[0], "-d") == 0)
82 dump_only = 1;
83 else
84 if (strcmp(argv[0], "-s") == 0)
85 install_trap = 1;
86 else
87 if (strcmp(argv[0], "-e") == 0) {
88 argv++;
89 argc--;
90 immscript = argv[0];
91 ok = 1;
93 else {
94 /* next argument is a script name; open it */
95 if ((script = fopen(argv[0], "r")) == NULL) {
96 fprintf(stderr, "Can't open '%s'\n", argv[0]);
97 return 1;
99 ok = 1;
102 argv++;
103 argc--;
106 mpsl_startup();
108 /* set arguments */
109 mpsl_argv(argc, argv);
111 if (install_trap)
112 mpsl_trap(MPDM_X(trap_func));
114 /* compile */
115 if (immscript != NULL) {
116 w = mpdm_ref(MPDM_MBS(immscript));
117 v = mpsl_compile(w);
118 mpdm_unref(w);
120 else {
121 int c;
123 /* if line starts with #!, discard it */
124 if ((c = getc(script)) == '#' && (c = getc(script)) == '!')
125 while ((c = getc(script)) != EOF && c != '\n');
126 else
127 ungetc(c, script);
129 if (c != EOF) {
130 w = mpdm_ref(MPDM_F(script));
131 v = mpsl_compile_file(w, NULL);
132 mpdm_close(w);
133 mpdm_unref(w);
137 if (v != NULL) {
138 mpdm_ref(v);
140 if (dump_only)
141 mpdm_dump(v);
142 else
143 mpdm_void(mpdm_exec(v, NULL, NULL));
145 mpdm_unref(v);
148 /* prints the error, if any */
149 if ((w = mpdm_hget_s(mpdm_root(), L"ERROR")) != NULL) {
150 mpdm_write_wcs(stderr, mpdm_string(w));
151 fprintf(stderr, "\n");
153 ret = 1;
156 mpsl_shutdown();
158 return ret;
162 int main(int argc, char *argv[])
164 return mpsl_main(argc, argv);