Subversion Repositories SvarDOS

Rev

Rev 1729 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
532 mateuszvis 1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
3
 *
2214 mateusz.vi 4
 * Copyright (C) 2021-2024 Mateusz Viste
532 mateuszvis 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
 
533 mateuszvis 40
static enum cmd_result cmd_if(struct cmd_funcparam *p) {
532 mateuszvis 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], "/?"))) {
990 mateusz.vi 48
    nls_outputnl(35,0); /* "Performs conditional processing in batch programs." */
532 mateuszvis 49
    outputnl("");
990 mateusz.vi 50
    nls_outputnl(35,1); /* "IF [NOT] ERRORLEVEL num command" */
51
    nls_outputnl(35,2); /* "IF [NOT] string1==string2 command" */
52
    nls_outputnl(35,3); /* "IF [NOT] EXIST filename command" */
532 mateuszvis 53
    outputnl("");
990 mateusz.vi 54
    nls_outputnl(35,4); /* "NOT               command is executed only if condition is NOT met" */
55
    nls_outputnl(35,5); /* "ERRORLEVEL num    condition: last program returned an exit code >= num" */
56
    nls_outputnl(35,6); /* "string1==string2  condition: both strings must be equal" */
57
    nls_outputnl(35,7); /* "EXIST filename    condition: filename exists (wildcards accepted)" */
58
    nls_outputnl(35,8); /* "command           command to carry out if condition is met" */
533 mateuszvis 59
    return(CMD_OK);
532 mateuszvis 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
    }
536 mateuszvis 79
    /* move s to command */
532 mateuszvis 80
    JMP_NEXT_ARG(s);
81
    /* is errorlevel matching? */
82
    if (i <= *rmod_exitcode) negflag ^= 1;
536 mateuszvis 83
    goto EXEC_S_CMD_IF_NEGFLAG_SET;
532 mateuszvis 84
  }
85
 
535 mateuszvis 86
  /* IF EXIST fname (or wildcard)
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! */
89
  if (imatchlim(s, "EXIST ", 6)) {
90
    struct DTA *dta = (void *)(0x80); /* default dta location */
91
    JMP_NEXT_ARG(s);
92
    /* copy filename to buffer */
93
    for (i = 0; (s[i] != ' ') && (s[i] != 0); i++) p->BUFFER[i] = s[i];
94
    p->BUFFER[i] = 0;
536 mateuszvis 95
    /* move s to command */
535 mateuszvis 96
    JMP_NEXT_ARG(s);
536 mateuszvis 97
    if (*s == 0) goto SYNTAX_ERR; /* check now to avoid moving the diskette drive if syntax bad anyway */
535 mateuszvis 98
    /* does file exist? */
99
    if (findfirst(dta, p->BUFFER, 0) == 0) negflag ^= 1;
536 mateuszvis 100
    goto EXEC_S_CMD_IF_NEGFLAG_SET;
535 mateuszvis 101
  }
102
 
1729 mateusz.vi 103
  /* assume "IF str1==str2" and if that's not it then it's a syntax error */
104
  /* copy first argument to BUFF, until first '=' or space */
105
  for (i = 0; (s[i] != '=') && (s[i] != ' ') && (s[i] != 0); i++) p->BUFFER[i] = s[i];
106
  /* 1st arg cannot be empty */
107
  if (i == 0) goto SYNTAX_ERR;
108
  /* terminate buff string */
109
  p->BUFFER[i] = 0;
110
  s += i;
111
  while (*s == ' ') s++;
112
  /* should be two '=' now, otherwise it's a syntax error (equality sign is not
113
   * allowed in first string) */
114
  if ((s[0] != '=') || (s[1] != '=')) goto SYNTAX_ERR;
115
  /* skip all trailing equality chars (MSDOS accepts many of them, ie all
116
   * these are fine: "dupa==dupa", "dupa===dupa", "dupa====dupa", etc) */
117
  while (*s == '=') s++;
118
  while (*s == ' ') s++; /* skip any leading spaces */
119
  /* move along until space or NULL terminator, checking equality */
120
  for (i = 0; (p->BUFFER[i] != 0) && (p->BUFFER[i] == s[i]); i++);
121
  if ((p->BUFFER[i] == 0) && (s[i] == ' ')) negflag ^= 1;
122
  JMP_NEXT_ARG(s);
123
  goto EXEC_S_CMD_IF_NEGFLAG_SET;
532 mateuszvis 124
 
536 mateuszvis 125
  /* invalid syntax */
532 mateuszvis 126
  SYNTAX_ERR:
990 mateusz.vi 127
  nls_outputnl(0,1); /* "Invalid syntax" */
533 mateuszvis 128
  return(CMD_FAIL);
535 mateuszvis 129
 
130
  /* let's exec command (write it to start of cmdline and parse again) */
536 mateuszvis 131
  EXEC_S_CMD_IF_NEGFLAG_SET:
132
  if (*s == 0) goto SYNTAX_ERR;
133
  if (negflag == 0) return(CMD_OK);
2214 mateusz.vi 134
  memcpy_ltr((void *)(p->cmdline), s, sv_strlen(s) + 1);  /* cmdline and s share the same memory! */
535 mateuszvis 135
  return(CMD_CHANGED);
532 mateuszvis 136
}