Subversion Repositories SvarDOS

Rev

Rev 397 | Rev 435 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
421 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
 
352 mateuszvis 25
/*
357 mateuszvis 26
 * set [varname[=value]]
352 mateuszvis 27
 *
357 mateuszvis 28
 * value cannot contain any '=' character, but it can contain spaces
29
 * varname can also contain spaces
352 mateuszvis 30
 */
31
 
361 mateuszvis 32
 
372 mateuszvis 33
static int cmd_set(struct cmd_funcparam *p) {
364 mateuszvis 34
  char far *env = MK_FP(p->env_seg, 0);
372 mateuszvis 35
  char *buff = p->BUFFER;
387 mateuszvis 36
 
37
  if (cmd_ishlp(p)) {
38
    outputnl("Displays, sets, or removes DOS environment variables");
39
    outputnl("");
40
    outputnl("SET [variable=[string]]");
41
    outputnl("");
42
    outputnl("variable  Specifies the environment-variable name");
43
    outputnl("string    Specifies a series of characters to assign to the variable");
44
    outputnl("");
45
    outputnl("Type SET without parameters to display the current environment variables.");
46
  }
47
 
357 mateuszvis 48
  /* no arguments - display content */
364 mateuszvis 49
  if (p->argc == 0) {
357 mateuszvis 50
    while (*env != 0) {
372 mateuszvis 51
      unsigned short i;
357 mateuszvis 52
      /* copy string to local buff for display */
53
      for (i = 0;; i++) {
54
        buff[i] = *env;
55
        env++;
56
        if (buff[i] == 0) break;
57
      }
372 mateuszvis 58
      outputnl(buff);
352 mateuszvis 59
    }
361 mateuszvis 60
  } else { /* set variable (do not rely on argv, SET has its own rules...) */
357 mateuszvis 61
    const char far *ptr;
361 mateuszvis 62
    unsigned short i;
357 mateuszvis 63
    /* locate the first space */
364 mateuszvis 64
    for (ptr = p->cmdline; *ptr != ' '; ptr++);
357 mateuszvis 65
    /* now locate the first non-space: that's where the variable name begins */
66
    for (; *ptr == ' '; ptr++);
67
    /* copy variable to buff and switch it upercase */
68
    i = 0;
69
    for (; *ptr != '='; ptr++) {
70
      if (*ptr == '\r') goto syntax_err;
71
      buff[i] = *ptr;
72
      if ((buff[i] >= 'a') && (buff[i] <= 'z')) buff[i] -= ('a' - 'A');
73
      i++;
74
    }
75
 
361 mateuszvis 76
    /* copy value now */
357 mateuszvis 77
    while (*ptr != '\r') {
78
      buff[i++] = *ptr;
79
      ptr++;
80
    }
81
 
82
    /* terminate buff */
83
    buff[i] = 0;
84
 
361 mateuszvis 85
    /* commit variable to environment */
364 mateuszvis 86
    i = env_setvar(p->env_seg, buff);
361 mateuszvis 87
    if (i == ENV_INVSYNT) goto syntax_err;
369 mateuszvis 88
    if (i == ENV_NOTENOM) outputnl("Not enough available space within the environment block");
352 mateuszvis 89
  }
357 mateuszvis 90
  return(-1);
352 mateuszvis 91
 
357 mateuszvis 92
  syntax_err:
93
 
369 mateuszvis 94
  outputnl("Syntax error");
357 mateuszvis 95
  return(-1);
352 mateuszvis 96
}