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

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

Remove unnecessary includes from <stdio.h>.

  • 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 */
70int cmd_batch(char **argv, cliuser_t *usr)
71{
72 unsigned int argc;
73 bool continue_despite_errors = false;
74
75 /* Count the arguments */
76 for (argc = 0; argv[argc] != NULL; argc ++);
77
78 if (argc < 2) {
79 printf("%s - no input file provided.\n", cmdname);
80 return CMD_FAILURE;
81 }
82
83 if (argc > 2) {
84 if (str_cmp(argv[2], "-c") == 0)
85 continue_despite_errors = true;
86 }
87
88 errno_t rc = EOK;
89 FILE *batch = fopen(argv[1], "r");
90 if (batch == NULL) {
91 printf("%s - Cannot open file %s\n", cmdname, argv[1]);
92 return CMD_FAILURE;
93 } else {
94 cliuser_t fusr;
95 fusr.name = usr->name;
96 fusr.cwd = usr->cwd;
97 fusr.prompt = usr->prompt;
98 fusr.line = malloc(INPUT_MAX + 1);
99 char *cur = fusr.line;
100 char *end = fusr.line + INPUT_MAX;
101 int c = fgetc(batch);
102 while (fusr.line != NULL) {
103 if (c == '\n' || c == EOF || cur == end) {
104 *cur = '\0';
105 if (cur == fusr.line) {
106 /* skip empty line */
107 rc = EOK;
108 free(cur);
109 } else {
110 printf(">%s\n", fusr.line);
111 rc = process_input(&fusr);
112 /* fusr->line was freed by process_input() */
113 if ((rc != EOK) && continue_despite_errors) {
114 /* Mute the error. */
115 rc = EOK;
116 }
117 }
118 if (rc == 0 && c != EOF) {
119 fusr.line = malloc(INPUT_MAX + 1);
120 cur = fusr.line;
121 end = fusr.line + INPUT_MAX;
122 } else {
123 break;
124 }
125 } else {
126 *cur++ = c;
127 }
128 c = fgetc(batch);
129 }
130 fclose(batch);
131 }
132
133 return CMD_SUCCESS;
134}
135
Note: See TracBrowser for help on using the repository browser.