Subversion Repositories SvarDOS

Rev

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

Rev 1074 Rev 1075
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
 
58
 
59
/* normalizing for-loop pattern-list separators.
59
/* normalizing for-loop pattern-list separators.
60
 * returns a space char if c is a for-pattern separator, c otherwise */
60
 * returns a space char if c is a for-pattern separator, c otherwise */
61
static char cmd_for_sepnorm(char c) {
61
static char cmd_for_sepnorm(char c) {
62
  /* the list of valid delimiters has been researched and kindly shared by Robert
62
  /* the list of valid delimiters has been researched and kindly shared by Robert
63
   * Riebisch via the ticket at https://osdn.net/projects/svardos/ticket/44058 */
63
   * Riebisch via the ticket at https://osdn.net/projects/svardos/ticket/44058 */
64
  switch (c) {
64
  switch (c) {
65
    case ' ':
65
    case ' ':
66
    case '\t':
66
    case '\t':
67
    case ';':
67
    case ';':
68
    case ',':
68
    case ',':
69
    case '/':
69
    case '/':
70
    case '=':
70
    case '=':
71
      return(' '); /* normalize FOR-loop separators to a space */
71
      return(' '); /* normalize FOR-loop separators to a space */
72
    default: /* anything else is kept as-is */
72
    default: /* anything else is kept as-is */
73
      return(c);
73
      return(c);
74
  }
74
  }
75
}
75
}
76
 
76
 
77
 
77
 
78
static enum cmd_result cmd_for(struct cmd_funcparam *p) {
78
static enum cmd_result cmd_for(struct cmd_funcparam *p) {
79
  struct forctx *f = (void *)(p->BUFFER);
79
  struct forctx *f = (void *)(p->BUFFER);
80
  unsigned short i;
80
  unsigned short i;
81
 
81
 
82
  /* forbid nested FORs */
82
  /* forbid nested FORs */
83
  if (p->rmod->forloop) {
83
  if (p->rmod->forloop) {
84
    nls_outputnl(18,7); /* "FOR cannot be nested" */
84
    nls_outputnl(18,7); /* "FOR cannot be nested" */
85
    return(CMD_FAIL);
85
    return(CMD_FAIL);
86
  }
86
  }
87
 
87
 
88
  /* help screen ONLY if /? is the only argument */
88
  /* help screen ONLY if /? is the only argument */
89
  if ((p->argc == 1) && (imatch(p->argv[0], "/?"))) {
89
  if ((p->argc == 1) && (imatch(p->argv[0], "/?"))) {
90
    nls_outputnl(18,0); /* "Runs a specified command for each file in a set of files" */
90
    nls_outputnl(18,0); /* "Runs a specified command for each file in a set of files" */
91
    outputnl("");
91
    outputnl("");
92
    nls_outputnl(18,1); /* "FOR %variable IN (set) DO command [parameters]" */
92
    nls_outputnl(18,1); /* "FOR %variable IN (set) DO command [parameters]" */
93
    outputnl("");
93
    outputnl("");
94
    nls_outputnl(18,2); /* "%variable   A replaceable parameter name. (single letter)" */
94
    nls_outputnl(18,2); /* "%variable   A replaceable parameter name. (single letter)" */
95
    nls_outputnl(18,3); /* "((set)       One of more space-separated strings or filename wildcards." */
95
    nls_outputnl(18,3); /* "(set)       One of more space-separated strings or filename wildcards." */
96
    nls_outputnl(18,4); /* "command     The command to carry out for each matched file." */
96
    nls_outputnl(18,4); /* "command     The command to carry out for each matched file." */
97
    nls_outputnl(18,5); /* "parameters  Parameters or switches for the specified command." */
97
    nls_outputnl(18,5); /* "parameters  Parameters or switches for the specified command." */
98
    outputnl("");
98
    outputnl("");
99
    nls_outputnl(18,6); /* "To use FOR in a batch program, use %%variable instead of %variable" */
99
    nls_outputnl(18,6); /* "To use FOR in a batch program, use %%variable instead of %variable" */
100
    return(CMD_OK);
100
    return(CMD_OK);
101
  }
101
  }
102
 
102
 
103
  /* clear out struct and copy command line to it */
103
  /* clear out struct and copy command line to it */
104
  bzero(f, sizeof(*f));
104
  bzero(f, sizeof(*f));
105
  strcpy(f->cmd, p->cmdline);
105
  strcpy(f->cmd, p->cmdline);
106
 
106
 
107
  /* locate the %varname (single char) */
107
  /* locate the %varname (single char) */
108
  i = p->argoffset;
108
  i = p->argoffset;
109
  while (f->cmd[i] == ' ') i++;
109
  while (f->cmd[i] == ' ') i++;
110
  if ((f->cmd[i] != '%') || (f->cmd[i+1] == ' ') || (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
110
  if ((f->cmd[i] != '%') || (f->cmd[i+1] == ' ') || (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
111
  f->varname = f->cmd[i+1];
111
  f->varname = f->cmd[i+1];
112
  i += 3;
112
  i += 3;
113
 
113
 
114
  /* look (and skip) the "IN" part */
114
  /* look (and skip) the "IN" part */
115
  while (f->cmd[i] == ' ') i++;
115
  while (f->cmd[i] == ' ') i++;
116
  if (((f->cmd[i] & 0xDF) != 'I') && ((f->cmd[i+1] & 0xDF) != 'N') && (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
116
  if (((f->cmd[i] & 0xDF) != 'I') && ((f->cmd[i+1] & 0xDF) != 'N') && (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
117
  i += 3;
117
  i += 3;
118
 
118
 
119
  /* look for patterns start */
119
  /* look for patterns start */
120
  while (f->cmd[i] == ' ') i++;
120
  while (f->cmd[i] == ' ') i++;
121
  if (f->cmd[i] != '(') goto INVALID_SYNTAX;
121
  if (f->cmd[i] != '(') goto INVALID_SYNTAX;
122
  i++;
122
  i++;
123
  while (cmd_for_sepnorm(f->cmd[i]) == ' ') i++;
123
  while (cmd_for_sepnorm(f->cmd[i]) == ' ') i++;
124
  f->nextpat = i;
124
  f->nextpat = i;
125
  /* look for patterns end and normalize all separators to a space */
125
  /* look for patterns end and normalize all separators to a space */
126
  while ((f->cmd[i] != ')') && (f->cmd[i] != 0)) {
126
  while ((f->cmd[i] != ')') && (f->cmd[i] != 0)) {
127
    f->cmd[i] = cmd_for_sepnorm(f->cmd[i]);
127
    f->cmd[i] = cmd_for_sepnorm(f->cmd[i]);
128
    i++;
128
    i++;
129
  }
129
  }
130
  if (f->cmd[i] != ')') goto INVALID_SYNTAX;
130
  if (f->cmd[i] != ')') goto INVALID_SYNTAX;
131
  f->cmd[i++] = 0; /* terminate patterns and move to next field */
131
  f->cmd[i++] = 0; /* terminate patterns and move to next field */
132
 
132
 
133
  /* look (and skip) the "DO" part */
133
  /* look (and skip) the "DO" part */
134
  while (f->cmd[i] == ' ') i++;
134
  while (f->cmd[i] == ' ') i++;
135
  if (((f->cmd[i] & 0xDF) != 'D') && ((f->cmd[i+1] & 0xDF) != 'O') && (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
135
  if (((f->cmd[i] & 0xDF) != 'D') && ((f->cmd[i+1] & 0xDF) != 'O') && (f->cmd[i+2] != ' ')) goto INVALID_SYNTAX;
136
  i += 3;
136
  i += 3;
137
  while (f->cmd[i] == ' ') i++;
137
  while (f->cmd[i] == ' ') i++;
138
 
138
 
139
  /* rest is the exec string */
139
  /* rest is the exec string */
140
  f->exec = i;
140
  f->exec = i;
141
 
141
 
142
  /* alloc memory for the forctx context and copy f to it */
142
  /* alloc memory for the forctx context and copy f to it */
143
  p->rmod->forloop = rmod_fcalloc(sizeof(*f), p->rmod->rmodseg, "SVFORCTX");
143
  p->rmod->forloop = rmod_fcalloc(sizeof(*f), p->rmod->rmodseg, "SVFORCTX");
144
  if (p->rmod->forloop == NULL) {
144
  if (p->rmod->forloop == NULL) {
145
    nls_outputnl_doserr(8);
145
    nls_outputnl_doserr(8);
146
    return(CMD_FAIL);
146
    return(CMD_FAIL);
147
  }
147
  }
148
  _fmemcpy(p->rmod->forloop, f, sizeof(*f));
148
  _fmemcpy(p->rmod->forloop, f, sizeof(*f));
149
 
149
 
150
  return(CMD_OK);
150
  return(CMD_OK);
151
 
151
 
152
  INVALID_SYNTAX:
152
  INVALID_SYNTAX:
153
  nls_outputnl(0,1); /* "Invalid syntax" */
153
  nls_outputnl(0,1); /* "Invalid syntax" */
154
  return(CMD_FAIL);
154
  return(CMD_FAIL);
155
}
155
}
156
 
156