Subversion Repositories SvarDOS

Rev

Rev 536 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 536 Rev 990
1
/* This file is part of the SvarCOM project and is published under the terms
1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
2
 * of the MIT license.
3
 *
3
 *
4
 * Copyright (C) 2021 Mateusz Viste
4
 * Copyright (C) 2021-2022 Mateusz Viste
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
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
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:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice shall be included in
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
14
 * all copies or substantial portions of the Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
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
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
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
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
22
 * DEALINGS IN THE SOFTWARE.
23
 */
23
 */
24
 
24
 
25
/*
25
/*
26
 * if [not] exists
26
 * if [not] exists
27
 * if [not] errorlevel 1
27
 * if [not] errorlevel 1
28
 * if [not] string ==string (string==string and string == string works, too)
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
29
 * if [not] errorlevel == 1   <-- I do NOT support this one, even though
30
 *                                MSDOS 5 and 6 considers it equivalent to
30
 *                                MSDOS 5 and 6 considers it equivalent to
31
 *                                IF ERRORLEVEL 1. This is a misleading and
31
 *                                IF ERRORLEVEL 1. This is a misleading and
32
 *                                undocumented syntax (does not actually
32
 *                                undocumented syntax (does not actually
33
 *                                check for equality).
33
 *                                check for equality).
34
 */
34
 */
35
 
35
 
36
 
36
 
37
#define JMP_NEXT_ARG(s) while ((*s != ' ') && (*s != 0)) s++; while (*s == ' ') s++;
37
#define JMP_NEXT_ARG(s) while ((*s != ' ') && (*s != 0)) s++; while (*s == ' ') s++;
38
 
38
 
39
 
39
 
40
static enum cmd_result cmd_if(struct cmd_funcparam *p) {
40
static enum cmd_result cmd_if(struct cmd_funcparam *p) {
41
  unsigned char negflag = 0;
41
  unsigned char negflag = 0;
42
  unsigned short i;
42
  unsigned short i;
43
  const char *s = p->cmdline + p->argoffset;
43
  const char *s = p->cmdline + p->argoffset;
44
 
44
 
45
  /* help screen ONLY if /? is the only argument - I do not want to output
45
  /* help screen ONLY if /? is the only argument - I do not want to output
46
   * help for ex. for "if %1 == /? echo ..." */
46
   * help for ex. for "if %1 == /? echo ..." */
47
  if ((p->argc == 1) && (imatch(p->argv[0], "/?"))) {
47
  if ((p->argc == 1) && (imatch(p->argv[0], "/?"))) {
48
    outputnl("Performs conditional processing in batch programs.");
48
    nls_outputnl(35,0); /* "Performs conditional processing in batch programs." */
49
    outputnl("");
49
    outputnl("");
50
    outputnl("IF [NOT] ERRORLEVEL num command");
50
    nls_outputnl(35,1); /* "IF [NOT] ERRORLEVEL num command" */
51
    outputnl("IF [NOT] string1==string2 command");
51
    nls_outputnl(35,2); /* "IF [NOT] string1==string2 command" */
52
    outputnl("IF [NOT] EXIST filename command");
52
    nls_outputnl(35,3); /* "IF [NOT] EXIST filename command" */
53
    outputnl("");
53
    outputnl("");
54
    outputnl("NOT               command is executed only if condition is NOT met");
54
    nls_outputnl(35,4); /* "NOT               command is executed only if condition is NOT met" */
55
    outputnl("ERRORLEVEL num    condition: last program returned an exit code >= num");
55
    nls_outputnl(35,5); /* "ERRORLEVEL num    condition: last program returned an exit code >= num" */
56
    outputnl("string1==string2  condition: both strings must be equal");
56
    nls_outputnl(35,6); /* "string1==string2  condition: both strings must be equal" */
57
    outputnl("EXIST filename    condition: filename exists (wildcards accepted)");
57
    nls_outputnl(35,7); /* "EXIST filename    condition: filename exists (wildcards accepted)" */
58
    outputnl("command           command to carry out if condition is met.");
58
    nls_outputnl(35,8); /* "command           command to carry out if condition is met" */
59
    return(CMD_OK);
59
    return(CMD_OK);
60
  }
60
  }
61
 
61
 
62
  /* negation? */
62
  /* negation? */
63
  if (imatchlim(s, "NOT ", 4)) {
63
  if (imatchlim(s, "NOT ", 4)) {
64
    negflag = 1;
64
    negflag = 1;
65
    JMP_NEXT_ARG(s);
65
    JMP_NEXT_ARG(s);
66
  }
66
  }
67
 
67
 
68
  /* IF ERRORLEVEL x cmd */
68
  /* IF ERRORLEVEL x cmd */
69
  if (imatchlim(s, "ERRORLEVEL ", 11)) {
69
  if (imatchlim(s, "ERRORLEVEL ", 11)) {
70
    unsigned char far *rmod_exitcode = MK_FP(p->rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
70
    unsigned char far *rmod_exitcode = MK_FP(p->rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
71
    JMP_NEXT_ARG(s);
71
    JMP_NEXT_ARG(s);
72
    if (*s == 0) goto SYNTAX_ERR;
72
    if (*s == 0) goto SYNTAX_ERR;
73
    /* convert errorlevel to an uint */
73
    /* convert errorlevel to an uint */
74
    if ((*s < '0') || (*s > '9')) {
74
    if ((*s < '0') || (*s > '9')) {
75
      i = 0xffff;
75
      i = 0xffff;
76
    } else {
76
    } else {
77
      atous(&i, s);
77
      atous(&i, s);
78
    }
78
    }
79
    /* move s to command */
79
    /* move s to command */
80
    JMP_NEXT_ARG(s);
80
    JMP_NEXT_ARG(s);
81
    /* is errorlevel matching? */
81
    /* is errorlevel matching? */
82
    if (i <= *rmod_exitcode) negflag ^= 1;
82
    if (i <= *rmod_exitcode) negflag ^= 1;
83
    goto EXEC_S_CMD_IF_NEGFLAG_SET;
83
    goto EXEC_S_CMD_IF_NEGFLAG_SET;
84
  }
84
  }
85
 
85
 
86
  /* IF EXIST fname (or wildcard)
86
  /* IF EXIST fname (or wildcard)
87
   * TODO: checking for a file on an empty diskette drive should NOT lead bother
87
   * TODO: checking for a file on an empty diskette drive should NOT lead bother
88
   *       the user with the stupid 'retry, abort, fail' query! */
88
   *       the user with the stupid 'retry, abort, fail' query! */
89
  if (imatchlim(s, "EXIST ", 6)) {
89
  if (imatchlim(s, "EXIST ", 6)) {
90
    struct DTA *dta = (void *)(0x80); /* default dta location */
90
    struct DTA *dta = (void *)(0x80); /* default dta location */
91
    JMP_NEXT_ARG(s);
91
    JMP_NEXT_ARG(s);
92
    /* copy filename to buffer */
92
    /* copy filename to buffer */
93
    for (i = 0; (s[i] != ' ') && (s[i] != 0); i++) p->BUFFER[i] = s[i];
93
    for (i = 0; (s[i] != ' ') && (s[i] != 0); i++) p->BUFFER[i] = s[i];
94
    p->BUFFER[i] = 0;
94
    p->BUFFER[i] = 0;
95
    /* move s to command */
95
    /* move s to command */
96
    JMP_NEXT_ARG(s);
96
    JMP_NEXT_ARG(s);
97
    if (*s == 0) goto SYNTAX_ERR; /* check now to avoid moving the diskette drive if syntax bad anyway */
97
    if (*s == 0) goto SYNTAX_ERR; /* check now to avoid moving the diskette drive if syntax bad anyway */
98
    /* does file exist? */
98
    /* does file exist? */
99
    if (findfirst(dta, p->BUFFER, 0) == 0) negflag ^= 1;
99
    if (findfirst(dta, p->BUFFER, 0) == 0) negflag ^= 1;
100
    goto EXEC_S_CMD_IF_NEGFLAG_SET;
100
    goto EXEC_S_CMD_IF_NEGFLAG_SET;
101
  }
101
  }
102
 
102
 
103
  /* IF str1==str2 ? (and if that's not it, then it's a syntax error) */
103
  /* IF str1==str2 ? (and if that's not it, then it's a syntax error) */
104
  if (strstr(s, "==") != NULL) {
104
  if (strstr(s, "==") != NULL) {
105
    /* copy first argument to BUFF, until first '=' or space */
105
    /* copy first argument to BUFF, until first '=' or space */
106
    for (i = 0; (s[i] != '=') && (s[i] != ' '); i++) p->BUFFER[i] = s[i];
106
    for (i = 0; (s[i] != '=') && (s[i] != ' '); i++) p->BUFFER[i] = s[i];
107
    /* 1st arg cannot be empty */
107
    /* 1st arg cannot be empty */
108
    if (i == 0) goto SYNTAX_ERR;
108
    if (i == 0) goto SYNTAX_ERR;
109
    /* terminate buff string and move s forward to the equality char (or space) */
109
    /* terminate buff string and move s forward to the equality char (or space) */
110
    p->BUFFER[i++] = 0;
110
    p->BUFFER[i++] = 0;
111
    s += i;
111
    s += i;
112
    while (*s == ' ') s++;
112
    while (*s == ' ') s++;
113
    /* if second char is not a '=' then syntax error (equality sign is not
113
    /* if second char is not a '=' then syntax error (equality sign is not
114
     * allowed in first string) */
114
     * allowed in first string) */
115
    if (*s != '=') goto SYNTAX_ERR;
115
    if (*s != '=') goto SYNTAX_ERR;
116
    /* skip all trailing equality chars (MSDOS accepts many of them, ie all
116
    /* skip all trailing equality chars (MSDOS accepts many of them, ie all
117
     * these are fine: "dupa==dupa", "dupa===dupa", "dupa====dupa", etc) */
117
     * these are fine: "dupa==dupa", "dupa===dupa", "dupa====dupa", etc) */
118
    while (*s == '=') s++;
118
    while (*s == '=') s++;
119
    while (*s == ' ') s++; /* skip any leading spaces */
119
    while (*s == ' ') s++; /* skip any leading spaces */
120
    /* move along until space or NULL terminator, checking equality */
120
    /* move along until space or NULL terminator, checking equality */
121
    for (i = 0; (p->BUFFER[i] != 0) && (p->BUFFER[i] == s[i]); i++);
121
    for (i = 0; (p->BUFFER[i] != 0) && (p->BUFFER[i] == s[i]); i++);
122
    if ((p->BUFFER[i] == 0) && (s[i] == ' ')) negflag ^= 1;
122
    if ((p->BUFFER[i] == 0) && (s[i] == ' ')) negflag ^= 1;
123
    JMP_NEXT_ARG(s);
123
    JMP_NEXT_ARG(s);
124
    goto EXEC_S_CMD_IF_NEGFLAG_SET;
124
    goto EXEC_S_CMD_IF_NEGFLAG_SET;
125
  }
125
  }
126
 
126
 
127
  /* invalid syntax */
127
  /* invalid syntax */
128
  SYNTAX_ERR:
128
  SYNTAX_ERR:
129
  outputnl("Syntax error");
129
  nls_outputnl(0,1); /* "Invalid syntax" */
130
  return(CMD_FAIL);
130
  return(CMD_FAIL);
131
 
131
 
132
  /* let's exec command (write it to start of cmdline and parse again) */
132
  /* let's exec command (write it to start of cmdline and parse again) */
133
  EXEC_S_CMD_IF_NEGFLAG_SET:
133
  EXEC_S_CMD_IF_NEGFLAG_SET:
134
  if (*s == 0) goto SYNTAX_ERR;
134
  if (*s == 0) goto SYNTAX_ERR;
135
  if (negflag == 0) return(CMD_OK);
135
  if (negflag == 0) return(CMD_OK);
136
  memmove((void *)(p->cmdline), s, strlen(s) + 1);  /* cmdline and s share the same memory! */
136
  memmove((void *)(p->cmdline), s, strlen(s) + 1);  /* cmdline and s share the same memory! */
137
  return(CMD_CHANGED);
137
  return(CMD_CHANGED);
138
}
138
}
139
 
139