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

Last change on this file since 99013b84 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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