source: mainline/uspace/app/bdsh/cmds/mod_cmds.c@ 582a0b8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 582a0b8 was 582a0b8, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove unistd.h

  • Rename usleep() and sleep() to thread_usleep() and thread_sleep() and move to thread.[hc].
  • Include stddef.h in order to provide NULL.
  • Move getpagesize() to libposix.
  • Sync uspace/dist/src/c/demos with originals.
  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2008 Tim Post
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/* NOTES:
30 * module_* functions are pretty much identical to builtin_* functions at this
31 * point. On the surface, it would appear that making each function dual purpose
32 * would be economical.
33 *
34 * These are kept separate because the structures (module_t and builtin_t) may
35 * grow apart and become rather different, even though they're identical at this
36 * point.
37 *
38 * To keep things easy to hack, everything is separated. In reality this only adds
39 * 6 - 8 extra functions, but keeps each function very easy to read and modify. */
40
41/* TODO:
42 * Many of these could be unsigned, provided the modules and builtins themselves
43 * can follow suit. Long term goal. */
44
45#include <stdio.h>
46#include <stdlib.h>
47#include <stddef.h>
48#include <str.h>
49#include "errors.h"
50#include "cmds.h"
51#include "module_aliases.h"
52
53extern volatile unsigned int cli_interactive;
54
55/* Checks if an entry function matching command exists in modules[], if so
56 * its position in the array is returned */
57int is_module(const char *command)
58{
59 module_t *mod;
60 unsigned int i = 0;
61
62 if (NULL == command)
63 return -2;
64
65 for (mod = modules; mod->name != NULL; mod++, i++) {
66 if (!str_cmp(mod->name, command))
67 return i;
68 }
69
70 return -1;
71}
72
73/* Checks if a module is an alias (sharing an entry point with another
74 * module). Returns 1 if so */
75int is_module_alias(const char *command)
76{
77 unsigned int i = 0;
78
79 if (NULL == command)
80 return -1;
81
82 for(i=0; mod_aliases[i] != NULL; i+=2) {
83 if (!str_cmp(mod_aliases[i], command))
84 return 1;
85 }
86
87 return 0;
88}
89
90/* Returns the name of the module that an alias points to */
91char *alias_for_module(const char *command)
92{
93 unsigned int i = 0;
94
95 if (NULL == command)
96 return (char *)NULL;
97
98 for(i=0; mod_aliases[i] != NULL; i++) {
99 if (!str_cmp(mod_aliases[i], command))
100 return (char *)mod_aliases[++i];
101 i++;
102 }
103
104 return (char *)NULL;
105}
106
107
108/* Invokes the 'help' entry function for the module at position (int) module,
109 * which wants an unsigned int to determine brief or extended display. */
110int help_module(int module, unsigned int extended)
111{
112 module_t *mod = modules;
113
114 mod += module;
115
116 if (NULL != mod->help) {
117 mod->help(extended);
118 return CL_EOK;
119 } else {
120 return CL_ENOENT;
121 }
122}
123
124/* Invokes the module entry point modules[module], passing argv[] as an argument
125 * stack. */
126int run_module(int module, char *argv[], iostate_t *new_iostate)
127{
128 int rc;
129 module_t *mod = modules;
130
131 mod += module;
132
133 iostate_t *old_iostate = get_iostate();
134 set_iostate(new_iostate);
135
136 if (NULL != mod->entry) {
137 rc = ((int)mod->entry(argv));
138 } else {
139 rc = CL_ENOENT;
140 }
141
142 set_iostate(old_iostate);
143
144 return rc;
145}
Note: See TracBrowser for help on using the repository browser.