Skip to content
Browse files

added configuration file getopt

  • Loading branch information...
1 parent d3b2b7e commit e291947ea7652efc23e654a0d7c5eda1f556db3d Giovanni Pellerano committed Dec 8, 2011
Showing with 23 additions and 21 deletions.
  1. +1 −1 src/janus.c
  2. +5 −7 src/janus.h
  3. +6 −0 src/main.c
  4. +11 −13 src/os_cmds.c
View
2 src/janus.c
@@ -342,7 +342,7 @@ void JANUS_Bootstrap(void)
{
uint8_t i, j;
- janus_commands_file_setup(OSSELECTED);
+ janus_commands_file_setup(conf.file);
/* now we had the commands stored and the infos detected */
/* execute "informative" commands (second section in the commands file) */
View
12 src/janus.h
@@ -36,12 +36,11 @@
#endif
/* --- */
-#define CONST_JANUS_VERSION "0.2"
+#define CONST_JANUS_VERSION "0.3"
#define CONST_JANUS_IFNAME "janus"
#define CONST_JANUS_WEBSITE "http://github.com/evilaliv3/janus"
-
+#define CONST_JANUS_CONF_FILE "/etc/janus/current-os"
#define CONST_JANUS_LISTEN_IP "127.0.0.1"
-
#define CONST_JANUS_LISTEN_PORT_IN 30201
#define CONST_JANUS_LISTEN_PORT_OUT 10203
#define CONST_JANUS_BUFSIZE 512
@@ -52,8 +51,6 @@
#define JANUS_BANNER " Janus-"CONST_JANUS_VERSION" "CONST_JANUS_WEBSITE" "
#define CONST_JANUS_BANNER_LENGTH sizeof(JANUS_BANNER)
-#define OSSELECTED "/etc/janus/current-os"
-
struct ethernet_header
{
uint8_t dst_ethernet[ETH_ALEN];
@@ -63,8 +60,9 @@ struct ethernet_header
struct janus_config
{
- char banner [CONST_JANUS_BANNER_LENGTH];
- char listen_ip [CONST_JANUS_BUFSIZE];
+ char banner[CONST_JANUS_BANNER_LENGTH];
+ char file[CONST_JANUS_BUFSIZE];
+ char listen_ip[CONST_JANUS_BUFSIZE];
uint16_t listen_port_in;
uint16_t listen_port_out;
uint16_t pqueue_len;
View
6 src/main.c
@@ -43,6 +43,7 @@ static void janus_help(const char *pname)
{
#define JANUS_HELP_FORMAT \
"Usage: Janus [OPTION]... :\n"\
+ " --conf\t\t\t<file>\tset the configuration file to use\n"\
" --listen-ip\t\t<ip>\tset the listen ip address\n"\
" --listen-port-in\t<port>\tset the listen port for incoming traffic\n"\
" --listen-port-out\t<port>\tset the listen port for outgoing traffic\n"\
@@ -139,6 +140,7 @@ int main(int argc, char **argv)
int pqueue;
struct option janus_options[] = {
+ { "conf", required_argument, NULL, 'c'},
{ "listen-ip", required_argument, NULL, 'l'},
{ "listen-port-in", required_argument, NULL, 'i'},
{ "listen-port-out", required_argument, NULL, 'o'},
@@ -151,6 +153,7 @@ int main(int argc, char **argv)
snprintf(conf.banner, CONST_JANUS_BANNER_LENGTH, "%s", JANUS_BANNER);
+ snprintf(conf.file, sizeof (conf.file), "%s", CONST_JANUS_CONF_FILE);
snprintf(conf.listen_ip, sizeof (conf.listen_ip), "%s", CONST_JANUS_LISTEN_IP);
conf.listen_port_in = CONST_JANUS_LISTEN_PORT_IN;
conf.listen_port_out = CONST_JANUS_LISTEN_PORT_OUT;
@@ -160,6 +163,9 @@ int main(int argc, char **argv)
{
switch (charopt)
{
+ case 'c':
+ snprintf(conf.file, sizeof (conf.file), "%s", optarg);
+ break;
case 'l':
if (validate(optarg, REGEXP_HOST))
snprintf(conf.listen_ip, sizeof (conf.listen_ip), "%s", optarg);
View
24 src/os_cmds.c
@@ -19,6 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
+#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -299,8 +300,7 @@ static char *expand_command(char *original_rawcmd)
do
{
/* delete the "~" and increment after */
- *p = 0x00;
- ++p;
+ *(p++) = 0x00;
memcpy(&buffer[j], last_p, strlen(last_p));
j += strlen(last_p);
@@ -311,8 +311,7 @@ static char *expand_command(char *original_rawcmd)
j += strlen(get_sysmap_str(*p));
/* delete the command code and increment after */
- *p = 0x00;
- ++p;
+ *(p++) = 0x00;
last_p = p;
/* index to the next "~" if present */
p = strchr(p, '~');
@@ -445,10 +444,11 @@ void janus_commands_file_setup(char *oscmds_filename)
while (!feof(oscmds))
{
- uint32_t parserRet;
char rdLine[MAXLINESIZE] = {0};
- fgets(rdLine, MAXLINESIZE, oscmds);
+ if(fgets(rdLine, MAXLINESIZE, oscmds) == NULL)
+ break;
+
++fndx;
rdLine[strlen(rdLine) - 1] = 0x00;
@@ -475,8 +475,7 @@ void janus_commands_file_setup(char *oscmds_filename)
runtime_exception("invalid compilation detected at %d. supported OS name and maintainer: required\n", fndx);
/* 1st SECTION: the parsing of os-cmds/ require three stage analysis */
- parserRet = handle_CheckCommand(rdLine);
- switch (parserRet)
+ switch (handle_CheckCommand(rdLine))
{
case NOT_MY_DATA:
break;
@@ -490,8 +489,7 @@ void janus_commands_file_setup(char *oscmds_filename)
runtime_exception("incorrect use of #..# at line %d\n", fndx);
/* 2nd SECTION: the data collection operation */
- parserRet = collect_second_section(rdLine, fndx);
- switch (parserRet)
+ switch (collect_second_section(rdLine, fndx))
{
case NOT_MY_DATA:
break;
@@ -502,8 +500,7 @@ void janus_commands_file_setup(char *oscmds_filename)
}
/* 3rd SECTION: the system interfacing */
- parserRet = collect_third_section(rdLine);
- switch (parserRet)
+ switch (collect_third_section(rdLine))
{
case NOT_MY_DATA:
break;
@@ -535,7 +532,8 @@ void sysmap_command(char req)
* TODO: could be useful use a different popen (different from do_popen) and check
* if someshit is written on strerr ?
*/
- system(mandatory_command[i].command);
+ if (system(mandatory_command[i].command) == -1)
+ printf("[exec_cmd] (error) %s [%s] %s\n", mandatory_command[i].info, mandatory_command[i].command, strerror(errno));
break;
}
}

0 comments on commit e291947

Please sign in to comment.
Something went wrong with that request. Please try again.