Subversion Repositories SvarDOS

Rev

Rev 1137 | 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
 *
989 mateusz.vi 4
 * Copyright (C) 2021-2022 Mateusz Viste
421 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
 
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
 
533 mateuszvis 33
static enum cmd_result 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)) {
989 mateusz.vi 38
    nls_outputnl(23,0); /* "Displays, sets, or removes DOS environment variables"); */
387 mateuszvis 39
    outputnl("");
989 mateusz.vi 40
    nls_outputnl(23,1); /* "SET [variable=[string]]" */
387 mateuszvis 41
    outputnl("");
989 mateusz.vi 42
    nls_outputnl(23,2); /* "variable  Specifies the environment-variable name" */
43
    nls_outputnl(23,3); /* "string    Specifies a series of characters to assign to the variable" */
387 mateuszvis 44
    outputnl("");
989 mateusz.vi 45
    nls_outputnl(23,4); /* "Type SET without parameters to display the current environment variables." */
533 mateuszvis 46
    return(CMD_OK);
387 mateuszvis 47
  }
48
 
357 mateuszvis 49
  /* no arguments - display content */
364 mateuszvis 50
  if (p->argc == 0) {
357 mateuszvis 51
    while (*env != 0) {
372 mateuszvis 52
      unsigned short i;
357 mateuszvis 53
      /* copy string to local buff for display */
54
      for (i = 0;; i++) {
55
        buff[i] = *env;
56
        env++;
57
        if (buff[i] == 0) break;
58
      }
372 mateuszvis 59
      outputnl(buff);
352 mateuszvis 60
    }
361 mateuszvis 61
  } else { /* set variable (do not rely on argv, SET has its own rules...) */
357 mateuszvis 62
    const char far *ptr;
1137 mateusz.vi 63
    unsigned short i;
1134 bttr 64
 
1138 mateusz.vi 65
    /* locate the first space (note that cmdline separators should be sanitized
66
     * to space only by now) */
67
    for (ptr = p->cmdline; *ptr != ' '; ptr++);
1134 bttr 68
 
1138 mateusz.vi 69
    /* now locate the first non-space: that's where the variable name begins */
70
    for (; *ptr == ' '; ptr++);
1134 bttr 71
 
72
    /* copy variable name to buff */
357 mateuszvis 73
    i = 0;
74
    for (; *ptr != '='; ptr++) {
451 mateuszvis 75
      if (*ptr == 0) goto syntax_err;
1134 bttr 76
      buff[i++] = *ptr;
357 mateuszvis 77
    }
78
 
1137 mateusz.vi 79
    /* make variable name all caps (country-dependend) */
80
    buff[i] = 0;
81
    nls_strtoup(buff);
1134 bttr 82
 
361 mateuszvis 83
    /* copy value now */
451 mateuszvis 84
    while (*ptr != 0) {
357 mateuszvis 85
      buff[i++] = *ptr;
86
      ptr++;
87
    }
88
 
89
    /* terminate buff */
90
    buff[i] = 0;
91
 
361 mateuszvis 92
    /* commit variable to environment */
364 mateuszvis 93
    i = env_setvar(p->env_seg, buff);
361 mateuszvis 94
    if (i == ENV_INVSYNT) goto syntax_err;
533 mateuszvis 95
    if (i == ENV_NOTENOM) {
989 mateusz.vi 96
      nls_outputnl(23,5); /* "Not enough available space within the environment block" */
533 mateuszvis 97
      return(CMD_FAIL);
98
    }
352 mateuszvis 99
  }
533 mateuszvis 100
  return(CMD_OK);
352 mateuszvis 101
 
357 mateuszvis 102
  syntax_err:
103
 
989 mateusz.vi 104
  nls_outputnl(0,1); /* "Invalid syntax" */
533 mateuszvis 105
  return(CMD_FAIL);
352 mateuszvis 106
}