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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 81bc309 was 81bc309, checked in by Petr Koupy <petr.koupy@…>, 14 years ago

batch command rewritten as builtin command.

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