• R/O
  • SSH
  • HTTPS

svardos: Commit


Commit MetaInfo

Revision532 (tree)
Time2021-11-26 02:53:37
Authormateuszviste

Log Message

work in progress "IF" implementation

Change Summary

Incremental Difference

--- svarcom/trunk/cmd/if.c (nonexistent)
+++ svarcom/trunk/cmd/if.c (revision 532)
@@ -0,0 +1,99 @@
1+/* This file is part of the SvarCOM project and is published under the terms
2+ * of the MIT license.
3+ *
4+ * Copyright (C) 2021 Mateusz Viste
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a
7+ * copy of this software and associated documentation files (the "Software"),
8+ * to deal in the Software without restriction, including without limitation
9+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+ * and/or sell copies of the Software, and to permit persons to whom the
11+ * Software is furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in
14+ * all copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+ * DEALINGS IN THE SOFTWARE.
23+ */
24+
25+/*
26+ * if [not] exists
27+ * if [not] errorlevel 1
28+ * if [not] string ==string (string==string and string == string works, too)
29+ * if [not] errorlevel == 1 <-- I do NOT support this one, even though
30+ * MSDOS 5 and 6 considers it equivalent to
31+ * IF ERRORLEVEL 1. This is a misleading and
32+ * undocumented syntax (does not actually
33+ * check for equality).
34+ */
35+
36+
37+#define JMP_NEXT_ARG(s) while ((*s != ' ') && (*s != 0)) s++; while (*s == ' ') s++;
38+
39+
40+static int cmd_if(struct cmd_funcparam *p) {
41+ unsigned char negflag = 0;
42+ unsigned short i;
43+ const char *s = p->cmdline + p->argoffset;
44+
45+ /* help screen ONLY if /? is the only argument - I do not want to output
46+ * help for ex. for "if %1 == /? echo ..." */
47+ if ((p->argc == 1) && (imatch(p->argv[0], "/?"))) {
48+ outputnl("Performs conditional processing in batch programs.");
49+ outputnl("");
50+ outputnl("IF [NOT] ERRORLEVEL num command");
51+ outputnl("IF [NOT] string1==string2 command");
52+ outputnl("IF [NOT] EXIST filename command");
53+ outputnl("");
54+ outputnl("NOT command is executed only if condition is NOT met");
55+ outputnl("ERRORLEVEL num condition: last program returned an exit code >= num");
56+ outputnl("string1==string2 condition: both strings must be equal");
57+ outputnl("EXIST filename condition: filename exists");
58+ outputnl("command command to carry out if condition is met.");
59+ return(-1);
60+ }
61+
62+ /* negation? */
63+ if (imatchlim(s, "NOT ", 4)) {
64+ negflag = 1;
65+ JMP_NEXT_ARG(s);
66+ }
67+
68+ /* IF ERRORLEVEL x cmd */
69+ if (imatchlim(s, "ERRORLEVEL ", 11)) {
70+ unsigned char far *rmod_exitcode = MK_FP(p->rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
71+ JMP_NEXT_ARG(s);
72+ if (*s == 0) goto SYNTAX_ERR;
73+ /* convert errorlevel to an uint */
74+ if ((*s < '0') || (*s > '9')) {
75+ i = 0xffff;
76+ } else {
77+ atous(&i, s);
78+ }
79+ JMP_NEXT_ARG(s);
80+ if (*s == 0) goto SYNTAX_ERR;
81+ /* is errorlevel matching? */
82+ if (i <= *rmod_exitcode) negflag ^= 1;
83+ if (negflag) {
84+ output("EXEC (TO BE IMPLEMENTED): ");
85+ outputnl(s);
86+ }
87+ return(-1);
88+ }
89+
90+ /* TODO IF EXISTS fname */
91+ /* TODO IF str1==str2 */
92+
93+ SYNTAX_ERR:
94+
95+ /* invalid syntax */
96+ outputnl("Syntax error");
97+
98+ return(-1);
99+}
--- svarcom/trunk/cmd.c (revision 531)
+++ svarcom/trunk/cmd.c (revision 532)
@@ -70,6 +70,7 @@
7070 #include "cmd/copy.c"
7171 #include "cmd/date.c"
7272 #include "cmd/del.c"
73+#include "cmd/if.c"
7374 #include "cmd/vol.c" /* must be included before dir.c due to dependency */
7475 #include "cmd/dir.c"
7576 #include "cmd/echo.c"
@@ -110,6 +111,7 @@
110111 {"ECHO", cmd_echo},
111112 {"ERASE", cmd_del},
112113 {"EXIT", cmd_exit},
114+ {"IF", cmd_if},
113115 {"LH", cmd_notimpl},
114116 {"LOADHIGH",cmd_notimpl},
115117 {"MD", cmd_mkdir},
Show on old repository browser