Subversion Repositories SvarDOS

Rev

Rev 533 | Rev 536 | Go to most recent revision | 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
 *
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
 
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], "/?"))) {
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");
535 mateuszvis 57
    outputnl("EXIST filename    condition: filename exists (wildcards accepted)");
532 mateuszvis 58
    outputnl("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
    }
79
    JMP_NEXT_ARG(s);
80
    if (*s == 0) goto SYNTAX_ERR;
81
    /* is errorlevel matching? */
82
    if (i <= *rmod_exitcode) negflag ^= 1;
535 mateuszvis 83
    if (negflag) goto EXEC_S_CMD;
533 mateuszvis 84
    return(CMD_OK);
532 mateuszvis 85
  }
86
 
535 mateuszvis 87
  /* IF EXIST fname (or wildcard)
88
   * TODO: checking for a file on an empty diskette drive should NOT lead bother
89
   *       the user with the stupid 'retry, abort, fail' query! */
90
  if (imatchlim(s, "EXIST ", 6)) {
91
    struct DTA *dta = (void *)(0x80); /* default dta location */
92
    JMP_NEXT_ARG(s);
93
    /* copy filename to buffer */
94
    for (i = 0; (s[i] != ' ') && (s[i] != 0); i++) p->BUFFER[i] = s[i];
95
    p->BUFFER[i] = 0;
96
    /* move s to exec command */
97
    JMP_NEXT_ARG(s);
98
    if (*s == 0) goto SYNTAX_ERR;
99
    /* does file exist? */
100
    if (findfirst(dta, p->BUFFER, 0) == 0) negflag ^= 1;
101
    if (negflag) goto EXEC_S_CMD;
102
    return(CMD_OK);
103
  }
104
 
532 mateuszvis 105
  /* TODO IF str1==str2 */
106
 
107
  SYNTAX_ERR:
108
 
109
  /* invalid syntax */
110
  outputnl("Syntax error");
111
 
533 mateuszvis 112
  return(CMD_FAIL);
535 mateuszvis 113
 
114
  /* let's exec command (write it to start of cmdline and parse again) */
115
  EXEC_S_CMD:
116
  memmove((void *)(p->cmdline), s, strlen(s) + 1);  /* cmdline and s share the same memory! */
117
  return(CMD_CHANGED);
532 mateuszvis 118
}