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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d5c1051 was d5c1051, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

"Obviously harmless" error handling tweaks.

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