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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e6edc8d1 was 0262f180, checked in by Vojtech Horky <vojtechhorky@…>, 12 years ago

Extend batch' command with -c'

Allow to execute all commands despite errors in some of them.

  • 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 <errno.h>
33#include "config.h"
34#include "util.h"
35#include "errors.h"
36#include "entry.h"
37#include "batch.h"
38#include "cmds.h"
39#include "input.h"
40
41static const char *cmdname = "batch";
42
43/* Dispays help for batch in various levels */
44void help_cmd_batch(unsigned int level)
45{
46 if (level == HELP_SHORT) {
47 printf(
48 "\n batch [filename] [-c]\n"
49 " Issues commands stored in the file.\n"
50 " Each command must correspond to the single line in the file.\n\n");
51 } else {
52 printf(
53 "\n `batch' - issues a batch of commands\n"
54 " Issues commands stored in the file. Each command must correspond\n"
55 " to the single line in the file. Empty lines can be used to visually\n"
56 " separate groups of commands. There is no support for comments,\n"
57 " variables, recursion or other programming constructs - the `batch'\n"
58 " command is indeed very trivial.\n"
59 " If the filename is followed by -c, execution continues even if some\n"
60 " of the commands failed.\n\n");
61 }
62
63 return;
64}
65
66/* Main entry point for batch, accepts an array of arguments and a
67 * pointer to the cliuser_t structure */
68int cmd_batch(char **argv, cliuser_t *usr)
69{
70 unsigned int argc;
71 bool continue_despite_errors = false;
72
73 /* Count the arguments */
74 for (argc = 0; argv[argc] != NULL; argc ++);
75
76 if (argc < 2) {
77 printf("%s - no input file provided.\n", cmdname);
78 return CMD_FAILURE;
79 }
80
81 if (argc > 2) {
82 if (str_cmp(argv[2], "-c") == 0)
83 continue_despite_errors = true;
84 }
85
86 int rc = 0;
87 FILE *batch = fopen(argv[1], "r");
88 if (batch == NULL) {
89 printf("%s - Cannot open file %s\n", cmdname, argv[1]);
90 return CMD_FAILURE;
91 } else {
92 cliuser_t fusr;
93 fusr.name = usr->name;
94 fusr.cwd = usr->cwd;
95 fusr.prompt = usr->prompt;
96 fusr.line = malloc(INPUT_MAX + 1);
97 char *cur = fusr.line;
98 char *end = fusr.line + INPUT_MAX;
99 int c = fgetc(batch);
100 while (fusr.line != NULL) {
101 if (c == '\n' || c == EOF || cur == end) {
102 *cur = '\0';
103 if (cur == fusr.line) {
104 /* skip empty line */
105 rc = 0;
106 free(cur);
107 } else {
108 printf(">%s\n", fusr.line);
109 rc = process_input(&fusr);
110 /* fusr->line was freed by process_input() */
111 if ((rc != EOK) && continue_despite_errors) {
112 /* Mute the error. */
113 rc = EOK;
114 }
115 }
116 if (rc == 0 && c != EOF) {
117 fusr.line = malloc(INPUT_MAX + 1);
118 cur = fusr.line;
119 end = fusr.line + INPUT_MAX;
120 } else {
121 break;
122 }
123 } else {
124 *cur++ = c;
125 }
126 c = fgetc(batch);
127 }
128 fclose(batch);
129 }
130
131 return CMD_SUCCESS;
132}
133
Note: See TracBrowser for help on using the repository browser.