blob: 38931c0756eb2e22762ad8275e97aada4320ec9c [file] [log] [blame]
The Android Open Source Project23580ca2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AMEND_COMMANDS_H_
18#define AMEND_COMMANDS_H_
19
20#include "permissions.h"
21
22/* Invoke or dry-run a command. If "permissions" is non-NULL,
23 * the hook should fill it out with the list of files and operations that
24 * it would need to complete its operation. If "permissions" is NULL,
25 * the hook should do the actual work specified by its arguments.
26 *
27 * When a command is called with non-NULL "permissions", some arguments
28 * may be NULL. A NULL argument indicates that the argument is actually
29 * the output of another function, so is not known at permissions time.
30 * The permissions of leaf-node functions (those that have only literal
31 * strings as arguments) will get appended to the permissions of the
32 * functions that call them. However, to be completely safe, functions
33 * that receive a NULL argument should request the broadest-possible
34 * permissions for the range of the input argument.
35 *
36 * When a boolean command is called, "argc" is the boolean value and
37 * "argv" is NULL.
38 */
39typedef int (*CommandHook)(const char *name, void *cookie,
40 int argc, const char *argv[],
41 PermissionRequestList *permissions);
42
43int commandInit(void);
44void commandCleanup(void);
45
46/*
47 * Command management
48 */
49
50struct Command;
51typedef struct Command Command;
52
53typedef enum {
54 CMD_ARGS_UNKNOWN = -1,
55 CMD_ARGS_BOOLEAN = 0,
56 CMD_ARGS_WORDS
57} CommandArgumentType;
58
59int registerCommand(const char *name,
60 CommandArgumentType argType, CommandHook hook, void *cookie);
61
62Command *findCommand(const char *name);
63
64CommandArgumentType getCommandArgumentType(Command *cmd);
65
66int callCommand(Command *cmd, int argc, const char *argv[]);
67int callBooleanCommand(Command *cmd, bool arg);
68
69int getCommandPermissions(Command *cmd, int argc, const char *argv[],
70 PermissionRequestList *permissions);
71int getBooleanCommandPermissions(Command *cmd, bool arg,
72 PermissionRequestList *permissions);
73
74/*
75 * Function management
76 */
77
78typedef int (*FunctionHook)(const char *name, void *cookie,
79 int argc, const char *argv[],
80 char **result, size_t *resultLen,
81 PermissionRequestList *permissions);
82
83struct Function;
84typedef struct Function Function;
85
86int registerFunction(const char *name, FunctionHook hook, void *cookie);
87
88Function *findFunction(const char *name);
89
90int callFunction(Function *fn, int argc, const char *argv[],
91 char **result, size_t *resultLen);
92
93int getFunctionPermissions(Function *fn, int argc, const char *argv[],
94 PermissionRequestList *permissions);
95
96#endif // AMEND_COMMANDS_H_