Updated TODO.
[mpsl.git] / mpsl_m.c
blob87cb5fca881e56bce3fee72b50187d4a8e2a8d92
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)
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 char *immscript = NULL;
58 FILE *script = stdin;
59 int ret = 0;
60 int dump_only = 0;
61 int install_trap = 0;
63 /* skip the executable */
64 argv++;
65 argc--;
67 while (argc > 0) {
68 if (strcmp(argv[0], "-v") == 0 || strcmp(argv[0], "--help") == 0) {
69 printf("MPSL %s - Minimum Profit Scripting Language\n", VERSION);
70 printf("Copyright (C) 2003-2010 Angel Ortega <angel@triptico.com>\n");
71 printf
72 ("This software is covered by the GPL license. NO WARRANTY.\n\n");
74 printf("Usage: mpsl [-d | -s] [-e 'script' | script.mpsl ]\n\n");
76 return 0;
78 else
79 if (strcmp(argv[0], "-d") == 0)
80 dump_only = 1;
81 else
82 if (strcmp(argv[0], "-s") == 0)
83 install_trap = 1;
84 else
85 if (strcmp(argv[0], "-e") == 0) {
86 argv++;
87 argc--;
88 immscript = argv[0];
90 else {
91 /* next argument is a script name; open it */
92 if ((script = fopen(argv[0], "r")) == NULL) {
93 fprintf(stderr, "Can't open '%s'\n", argv[0]);
94 return 1;
98 argv++;
99 argc--;
102 mpsl_startup();
104 /* set arguments */
105 mpsl_argv(argc, argv);
107 if (install_trap)
108 mpsl_trap(MPDM_X(trap_func));
110 /* compile */
111 if (immscript != NULL)
112 v = mpsl_compile(MPDM_MBS(immscript));
113 else {
114 int c;
116 /* if line starts with #!, discard it */
117 if ((c = getc(script)) == '#' && (c = getc(script)) == '!')
118 while ((c = getc(script)) != EOF && c != '\n');
119 else
120 ungetc(c, script);
122 if (c != EOF)
123 v = mpsl_compile_file(MPDM_F(script));
126 if (v != NULL) {
127 if (dump_only)
128 mpdm_dump(v);
129 else
130 mpdm_exec(v, NULL);
133 /* prints the error, if any */
134 if ((v = mpsl_error(NULL)) != NULL) {
135 mpdm_write_wcs(stderr, mpdm_string(v));
136 fprintf(stderr, "\n");
138 ret = 1;
141 mpsl_shutdown();
143 return ret;
147 int main(int argc, char *argv[])
149 return mpsl_main(argc, argv);