source: mainline/uspace/app/bdsh/cmds/builtins/batch/batch.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2011 Petr Koupy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <stdbool.h>
32#include <stddef.h>
33#include <errno.h>
34#include <str.h>
35#include "config.h"
36#include "util.h"
37#include "errors.h"
38#include "entry.h"
39#include "batch.h"
40#include "cmds.h"
41#include "input.h"
42
43static const char *cmdname = "batch";
44
45/* Dispays help for batch in various levels */
46void help_cmd_batch(unsigned int level)
47{
48 if (level == HELP_SHORT) {
49 printf(
50 "\n batch [filename] [-c]\n"
51 " Issues commands stored in the file.\n"
52 " Each command must correspond to the single line in the file.\n\n");
53 } else {
54 printf(
55 "\n `batch' - issues a batch of commands\n"
56 " Issues commands stored in the file. Each command must correspond\n"
57 " to the single line in the file. Empty lines can be used to visually\n"
58 " separate groups of commands. There is no support for comments,\n"
59 " variables, recursion or other programming constructs - the `batch'\n"
60 " command is indeed very trivial.\n"
61 " If the filename is followed by -c, execution continues even if some\n"
62 " of the commands failed.\n\n");
63 }
64
65 return;
66}
67
68/** Main entry point for batch, accepts an array of arguments and a
69 * pointer to the cliuser_t structure
70 */
71int cmd_batch(char **argv, cliuser_t *usr)
72{
73 unsigned int argc;
74 bool continue_despite_errors = false;
75
76 /* Count the arguments */
77 argc = 0;
78 while (argv[argc] != NULL)
79 argc++;
80
81 if (argc < 2) {
82 printf("%s - no input file provided.\n", cmdname);
83 return CMD_FAILURE;
84 }
85
86 if (argc > 2) {
87 if (str_cmp(argv[2], "-c") == 0)
88 continue_despite_errors = true;
89 }
90
91 errno_t rc = EOK;
92 FILE *batch = fopen(argv[1], "r");
93 if (batch == NULL) {
94 printf("%s - Cannot open file %s\n", cmdname, argv[1]);
95 return CMD_FAILURE;
96 } else {
97 cliuser_t fusr;
98 fusr.name = usr->name;
99 fusr.cwd = usr->cwd;
100 fusr.prompt = usr->prompt;
101 fusr.line = malloc(INPUT_MAX + 1);
102 char *cur = fusr.line;
103 char *end = fusr.line + INPUT_MAX;
104 int c = fgetc(batch);
105 while (fusr.line != NULL) {
106 if (c == '\n' || c == EOF || cur == end) {
107 *cur = '\0';
108 if (cur == fusr.line) {
109 /* skip empty line */
110 rc = EOK;
111 free(cur);
112 } else {
113 printf(">%s\n", fusr.line);
114 rc = process_input(&fusr);
115 /* fusr->line was freed by process_input() */
116 if ((rc != EOK) && continue_despite_errors) {
117 /* Mute the error. */
118 rc = EOK;
119 }
120 }
121 if (rc == 0 && c != EOF) {
122 fusr.line = malloc(INPUT_MAX + 1);
123 cur = fusr.line;
124 end = fusr.line + INPUT_MAX;
125 } else {
126 break;
127 }
128 } else {
129 *cur++ = c;
130 }
131 c = fgetc(batch);
132 }
133 fclose(batch);
134 }
135
136 return CMD_SUCCESS;
137}
Note: See TracBrowser for help on using the repository browser.