Subversion Repositories SvarDOS

Rev

Rev 1023 | Rev 1070 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1023 Rev 1024
1
/* This file is part of the SvarCOM project and is published under the terms
1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
2
 * of the MIT license.
3
 *
3
 *
4
 * Copyright (C) 2021-2022 Mateusz Viste
4
 * Copyright (C) 2021-2022 Mateusz Viste
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
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
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:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice shall be included in
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
14
 * all copies or substantial portions of the Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
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
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
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
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
22
 * DEALINGS IN THE SOFTWARE.
23
 */
23
 */
24
 
24
 
25
/*
25
/*
26
 * FOR %variable IN (set) DO command [command-parameters]
26
 * FOR %variable IN (set) DO command [command-parameters]
27
 *
27
 *
28
 * %variable    a replaceable parameter name
28
 * %variable    a replaceable parameter name
29
 * (set)        a set of one of more files. Wildcards allowed.
29
 * (set)        a set of one of more files. Wildcards allowed.
30
 * command      the command to carry out for each matched file
30
 * command      the command to carry out for each matched file
31
 * command-parameters   parameters or switches for the specified command
31
 * command-parameters   parameters or switches for the specified command
32
 *
32
 *
33
 * To use FOR in a batch program, use %%variable instead of %variable
33
 * To use FOR in a batch program, use %%variable instead of %variable
34
 *
34
 *
35
 * the set of files must be surrounded by parenthesis, and may contain one
35
 * the set of files must be surrounded by parenthesis, and may contain one
36
 * or more space-delimited wildcards. Examples:
36
 * or more space-delimited wildcards. Examples:
37
 * (file.txt other.txt)
37
 * (file.txt other.txt)
38
 * (*.exe *.com *.bat)
38
 * (*.exe *.com *.bat)
39
 * (jan*.txt 199201??.txt)
39
 * (jan*.txt 199201??.txt)
40
 */
40
 */
41
 
41
 
42
/* Implementation notes:
42
/* Implementation notes:
43
 *
43
 *
44
 * For cannot be nested (no "FOR ... do FOR ..." allowed)
44
 * For cannot be nested (no "FOR ... do FOR ..." allowed)
45
 *
45
 *
46
 * When executed, FOR allocates a "FOR context" to RMOD's memory, this context
46
 * When executed, FOR allocates a "FOR context" to RMOD's memory, this context
47
 * holds the memory state of a FindNext iteration, as well as the command
47
 * holds the memory state of a FindNext iteration, as well as the command
48
 * to run on each matched file.
48
 * to run on each matched file.
49
 *
49
 *
50
 * This FOR context is looked at by command.c and used to provide a command
50
 * This FOR context is looked at by command.c and used to provide a command
51
 * instead of getting the command from interactive cli or BAT file. Repeats
51
 * instead of getting the command from interactive cli or BAT file. Repeats
52
 * until the FindNext buffer stops matching files.
52
 * until the FindNext buffer stops matching files.
53
 *
53
 *
54
 * This file only provides the help screen of FOR, as well as the FOR-context
54
 * This file only provides the help screen of FOR, as well as the FOR-context
55
 * initialization. Actual execution happens within command.c.
55
 * initialization. Actual execution happens within command.c.
56
 */
56
 */
57
 
57
 
58
static enum cmd_result cmd_for(struct cmd_funcparam *p) {
58
static enum cmd_result cmd_for(struct cmd_funcparam *p) {
59
  struct forctx *f = (void *)(p->BUFFER);
59
  struct forctx *f = (void *)(p->BUFFER);
60
  unsigned short i;
60
  unsigned short i;
61
 
61
 
62
  /* forbid nested FORs */
62
  /* forbid nested FORs */
63
  if (p->rmod->forloop) {
63
  if (p->rmod->forloop) {
64
    nls_outputnl(18,7); /* FOR cannot be nested */
64
    nls_outputnl(18,7); /* FOR cannot be nested */
65
    return(CMD_FAIL);
65
    return(CMD_FAIL);
66
  }
66
  }
67
 
67
 
68
  /* help screen ONLY if /? is the only argument */
68
  /* help screen ONLY if /? is the only argument */
69
  if ((p->argc == 1) && (imatch(p->argv[0], "/?"))) {
69
  if ((p->argc == 1) && (imatch(p->argv[0], "/?"))) {
70
    nls_outputnl(18,0); /* "Runs a specified command for each file in a set of files" */
70
    nls_outputnl(18,0); /* "Runs a specified command for each file in a set of files" */
71
    outputnl("");
71
    outputnl("");
72
    nls_outputnl(18,1); /* "FOR %variable IN (set) DO command [parameters]" */
72
    nls_outputnl(18,1); /* "FOR %variable IN (set) DO command [parameters]" */
73
    outputnl("");
73
    outputnl("");
74
    nls_outputnl(18,2); /* "%variable    a replaceable parameter name" */
74
    nls_outputnl(18,2); /* "%variable    a replaceable parameter name" */
75
    nls_outputnl(18,3); /* "(set)        a set of one of more files. Wildcards allowed." */
75
    nls_outputnl(18,3); /* "(set)        a set of one of more files. Wildcards allowed." */
76
    nls_outputnl(18,4); /* "command      the command to carry out for each matched file" */
76
    nls_outputnl(18,4); /* "command      the command to carry out for each matched file" */
77
    nls_outputnl(18,5); /* "parameters   parameters or switches for the specified command" */
77
    nls_outputnl(18,5); /* "parameters   parameters or switches for the specified command" */
78
    outputnl("");
78
    outputnl("");
79
    nls_outputnl(18,6); /* "To use FOR in a batch program, use %%variable instead of %variable" */
79
    nls_outputnl(18,6); /* "To use FOR in a batch program, use %%variable instead of %variable" */
80
    return(CMD_OK);
80
    return(CMD_OK);
81
  }
81
  }
82
 
82
 
83
  /* clear out struct and copy command line to it */
83
  /* clear out struct and copy command line to it */
84
  bzero(f, sizeof(*f));
84
  bzero(f, sizeof(*f));
85
  strcpy(f->cmd, p->cmdline);
85
  strcpy(f->cmd, p->cmdline);
86
 
86
 
87
  /* locate the %varname */
87
  /* locate the %varname (single char) */
88
  i = p->argoffset;
88
  i = p->argoffset;
89
  while (f->cmd[i] == ' ') i++;
89
  while (f->cmd[i] == ' ') i++;
90
  if (f->cmd[i] != '%') goto INVALID_SYNTAX;
90
  if ((f->cmd[i] != '%') || (f->cmd[i+1] == ' ') || (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
91
  f->varname = i;
91
  f->varname = f->cmd[i+1];
92
  /* find the end of varname (space) */
-
 
93
  while ((f->cmd[i] != ' ') && (f->cmd[i] != 0)) i++;
-
 
94
  if (f->cmd[i] != ' ') goto INVALID_SYNTAX;
92
  i += 3;
95
  f->cmd[i++] = 0; /* terminate varname and move to next field */
-
 
96
 
93
 
97
  /* look (and skip) the "IN" part */
94
  /* look (and skip) the "IN" part */
98
  while (f->cmd[i] == ' ') i++;
95
  while (f->cmd[i] == ' ') i++;
99
  if (((f->cmd[i] & 0xDF) != 'I') && ((f->cmd[i+1] & 0xDF) != 'N') && (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
96
  if (((f->cmd[i] & 0xDF) != 'I') && ((f->cmd[i+1] & 0xDF) != 'N') && (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
100
  i += 3;
97
  i += 3;
101
 
98
 
102
  /* look for patterns start */
99
  /* look for patterns start */
103
  while (f->cmd[i] == ' ') i++;
100
  while (f->cmd[i] == ' ') i++;
104
  if (f->cmd[i] != '(') goto INVALID_SYNTAX;
101
  if (f->cmd[i] != '(') goto INVALID_SYNTAX;
105
  i++;
102
  i++;
106
  while (f->cmd[i] == ' ') i++;
103
  while (f->cmd[i] == ' ') i++;
107
  f->curpat = i;
104
  f->nextpat = i;
108
  /* look for patterns end */
105
  /* look for patterns end */
109
  while ((f->cmd[i] != ')') && (f->cmd[i] != 0)) i++;
106
  while ((f->cmd[i] != ')') && (f->cmd[i] != 0)) i++;
110
  if (f->cmd[i] != ')') goto INVALID_SYNTAX;
107
  if (f->cmd[i] != ')') goto INVALID_SYNTAX;
111
  f->cmd[i++] = 0; /* terminate patterns and move to next field */
108
  f->cmd[i++] = 0; /* terminate patterns and move to next field */
112
 
109
 
113
  /* look (and skip) the "DO" part */
110
  /* look (and skip) the "DO" part */
114
  while (f->cmd[i] == ' ') i++;
111
  while (f->cmd[i] == ' ') i++;
115
  if (((f->cmd[i] & 0xDF) != 'D') && ((f->cmd[i+1] & 0xDF) != 'O') && (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
112
  if (((f->cmd[i] & 0xDF) != 'D') && ((f->cmd[i+1] & 0xDF) != 'O') && (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
116
  i += 3;
113
  i += 3;
117
  while (f->cmd[i] == ' ') i++;
114
  while (f->cmd[i] == ' ') i++;
118
 
115
 
119
  /* rest is the exec string */
116
  /* rest is the exec string */
120
  f->exec = i;
117
  f->exec = i;
121
 
118
 
122
  /* alloc memory for the forctx context and copy f to it */
119
  /* alloc memory for the forctx context and copy f to it */
123
  p->rmod->forloop = rmod_fcalloc(sizeof(*f), p->rmod->rmodseg, "SVFORCTX");
120
  p->rmod->forloop = rmod_fcalloc(sizeof(*f), p->rmod->rmodseg, "SVFORCTX");
124
  if (p->rmod->forloop == NULL) {
121
  if (p->rmod->forloop == NULL) {
125
    nls_outputnl_doserr(8);
122
    nls_outputnl_doserr(8);
126
    return(CMD_FAIL);
123
    return(CMD_FAIL);
127
  }
124
  }
128
  _fmemcpy(p->rmod->forloop, f, sizeof(*f));
125
  _fmemcpy(p->rmod->forloop, f, sizeof(*f));
129
 
126
 
130
  return(CMD_OK);
127
  return(CMD_OK);
131
 
128
 
132
  INVALID_SYNTAX:
129
  INVALID_SYNTAX:
133
  nls_outputnl(0,1); /* "Invalid syntax" */
130
  nls_outputnl(0,1); /* "Invalid syntax" */
134
  return(CMD_FAIL);
131
  return(CMD_FAIL);
135
}
132
}
136
 
133