OpenDNSSEC-enforcer  2.0.2
help_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 NLNet Labs
3  * Copyright (c) 2014 OpenDNSSEC AB (svb)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include "config.h"
30 
31 #include "file.h"
32 #include "log.h"
33 #include "str.h"
34 #include "daemon/cmdhandler.h"
35 #include "daemon/engine.h"
36 #include "clientpipe.h"
37 
38 #include "daemon/help_cmd.h"
39 
40 static const char *module_str = "help_cmd";
41 
42 static void
43 usage(int sockfd)
44 {
45  client_printf(sockfd,
46  "help\n"
47  " [command]\n"
48  );
49 }
50 
51 static void
52 help(int sockfd)
53 {
54  client_printf(sockfd,
55  "Without arguments print an overview of available commands for the daemon\n"
56  "and a short description of usage. With [command] set, print usage information\n"
57  "of command and extended help if available.\n\n"
58  );
59 }
60 
61 static int
62 handles(const char *cmd, ssize_t n)
63 {
64  return ods_check_command(cmd, n, help_funcblock()->cmdname)?1:0;
65 }
66 
67 static int
68 run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
69  db_connection_t *dbconn)
70 {
71  struct cmd_func_block* fb;
72  (void) engine;
73  (void) dbconn;
74 
75  ods_log_debug("[%s] help command", module_str);
76 
77  if (n < 4) return -1;
78  if (strncmp(cmd, help_funcblock()->cmdname, 4) != 0) return -1;
79 
80  if (n < 6) {
81  /* Anouncement */
82  client_printf(sockfd, "\nCommands:\n");
83  cmdhandler_get_usage(sockfd);
84  } else {
85  if ((fb = get_funcblock(cmd+5, n-5))) {
86  client_printf(sockfd, "Usage:\n");
87  fb->usage(sockfd);
88  client_printf(sockfd, "\nHelp:\n");
89  if (fb->help) {
90  fb->help(sockfd);
91  } else {
92  client_printf(sockfd, "No help available for '%s'\n",
93  cmd+5);
94  return 1;
95  }
96  } else {
97  client_printf(sockfd, "Help: command '%s' unknown. Type "
98  "'help' without arguments to get a list of supported "
99  "commands.\n", cmd+5);
100  return 2;
101  }
102  }
103  return 0;
104 }
105 
106 static struct cmd_func_block funcblock = {
107  "help", &usage, &help, &handles, &run
108 };
109 
110 struct cmd_func_block*
112 {
113  return &funcblock;
114 }
void(* help)(int sockfd)
Definition: cmdhandler.h:64
void cmdhandler_get_usage(int sockfd)
Definition: cmdhandler.c:163
void ods_log_debug(const char *format,...)
Definition: log.c:41
int(* run)(int sockfd, struct engine_struct *engine, const char *cmd, ssize_t n, db_connection_t *dbconn)
Definition: cmdhandler.h:79
const char * cmdname
Definition: cmdhandler.h:59
void(* usage)(int sockfd)
Definition: cmdhandler.h:61
struct cmd_func_block * help_funcblock(void)
Definition: help_cmd.c:111
struct cmd_func_block * get_funcblock(const char *cmd, ssize_t n)
Definition: cmdhandler.c:175
int(* handles)(const char *cmd, ssize_t n)
Definition: cmdhandler.h:67