Subversion Repositories SvarDOS

Rev

Rev 532 | Rev 535 | 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");
57
    outputnl("EXIST filename    condition: filename exists");
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;
533 mateuszvis 83
    if (negflag) { /* let's exec command (write it to start of cmdline and exec again) */
84
      memmove((void *)(p->cmdline), s, strlen(s) + 1);  /* cmdline and s share the same memory! */
85
      return(CMD_CHANGED);
532 mateuszvis 86
    }
533 mateuszvis 87
    return(CMD_OK);
532 mateuszvis 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
 
533 mateuszvis 98
  return(CMD_FAIL);
532 mateuszvis 99
}