commit a30e322cd044564b7d39800749ca53ff28fd8a6d
parent 2bdf6384ca3104c891e6f386fd984daacb57708c
Author: Antoine Amarilli <ant.amarilli@free.fr>
Date: Sat, 15 Jan 2011 17:52:22 +0100
parser
Diffstat:
irctest.c | | | 128 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- |
1 file changed, 117 insertions(+), 11 deletions(-)
diff --git a/irctest.c b/irctest.c
@@ -30,6 +30,7 @@
#include <string.h>
#include <stdlib.h>
#include <errno.h>
+#include <argp.h>
#include "libircclient.h"
@@ -42,17 +43,103 @@
#define _GNU_SOURCE
+const char *argp_program_version = "irccat 0.1";
+const char *argp_program_bug_address = "<a3nm@a3nm.net>";
+
+/* Program documentation. */
+static char doc[] =
+ "Irccat -- an IRC toolkit";
+
+/* A description of the arguments we accept. */
+static char args_doc[] = "SERVER[:PORT] NICK [CHANNEL]...";
+
+/* The options we understand. */
+static struct argp_option options[] = {
+ {"verbose", 'v', 0, 0,
+ "Produce verbose output" },
+ {"interval", 'i', "secs", 0,
+ "Wait secs seconds between multiple lines (default: 1)" },
+ { 0 }
+};
+
/*
* We store data in IRC session context.
*/
-typedef struct
+struct arguments
{
char ** channels;
int n_channels;
+ int interval;
+ int verbose;
char * nick;
+ char * server;
char ready;
-} irc_ctx_t;
+};
+
+typedef struct arguments irc_ctx_t;
+
+/* Parse a single option. */
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+ /* Get the input argument from argp_parse, which we
+ know is a pointer to our arguments structure. */
+ struct arguments *arguments = state->input;
+
+ if (key != ARGP_KEY_ARG && key != ARGP_KEY_END && (! arguments->channels))
+ {
+ switch (key)
+ {
+ case 'v':
+ arguments->verbose = 1;
+ break;
+ case 'i':
+ // TODO use float, and be more robust
+ printf ("seen i, nchan is %d\n", arguments->n_channels);
+ arguments->interval = atoi(arg);
+ break;
+
+ case ARGP_KEY_NO_ARGS:
+ argp_usage (state);
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ } else {
+ switch(key)
+ {
+ /* TODO option exclusion doesn't work! */
+ case ARGP_KEY_ARG:
+ printf("saw an arg\n");
+ if (state->arg_num == 0)
+ arguments->server = arg;
+ else if (state->arg_num == 1)
+ arguments->nick = arg;
+ else {
+ if (arguments->channels == NULL)
+ {
+ printf("start with %s, at %d\n", arg, state->arg_num);
+ arguments->channels = state->argv + (state->next - 1);
+ }
+ arguments->n_channels++;
+ }
+ break;
+
+ case ARGP_KEY_END:
+ if (state->arg_num < 2)
+ /* Not enough arguments. */
+ argp_usage (state);
+ break;
+ }
+
+ }
+ return 0;
+}
+
+/* Our argp parser. */
+static struct argp argp = { options, parse_opt, args_doc, doc };
+
THREAD_FUNCTION(irc_listen)
@@ -118,7 +205,7 @@ void event_privmsg (irc_session_t * session, const char * event, const char * or
{
dump_event (session, event, origin, params, count);
- printf ("[ %s ] < %s > %s\n", params[0],
+ printf ("[%s] <%s> %s\n", params[0],
origin ? origin : "someone",
params[1] );
}
@@ -188,7 +275,7 @@ void event_channel (irc_session_t * session, const char * event, const char * or
if ( count != 2 )
return;
- printf ("[ %s ] < %s > %s\n", params[0],
+ printf ("[%s] <%s> %s\n", params[0],
origin ? origin : "someone", params[1] );
if ( !origin )
@@ -282,6 +369,8 @@ int main (int argc, char **argv)
return 1;
}
+
+
memset (&callbacks, 0, sizeof(callbacks));
callbacks.event_connect = event_connect;
@@ -302,29 +391,47 @@ int main (int argc, char **argv)
callbacks.event_unknown = dump_event;
callbacks.event_numeric = event_numeric;
- //callbacks.event_dcc_chat_req = irc_event_dcc_chat;
- //callbacks.event_dcc_send_req = irc_event_dcc_send;
+ //TODO callbacks.event_dcc_chat_req = irc_event_dcc_chat;
+ //TODO callbacks.event_dcc_send_req = irc_event_dcc_send;
s = irc_create_session (&callbacks);
if ( !s )
{
- sprintf (stderr, "Could not create session\n");
+ fprintf (stderr, "Could not create session\n");
return 1;
}
- ctx.channels = argv;
ctx.ready = 0;
ctx.n_channels = argc - 3;
ctx.nick = argv[2];
irc_set_ctx (s, &ctx);
+
+ struct arguments arguments;
+
+ /* Default values. */
+ arguments.n_channels = 0;
+ arguments.channels = (char**) NULL;
+ arguments.verbose = 0;
+ arguments.interval = 1;
+
+ /* Parse our arguments; every option seen by parse_opt will
+ be reflected in arguments. */
+ argp_parse (&argp, argc, argv, 0, 0, &arguments);
+
+ printf ("ARG1 = %s\nARG2 = %s\nINTERVAL = %d\nNCHAN = %d\n"
+ "VERBOSE = %s",
+ arguments.channels[0], arguments.channels[1],
+ arguments.interval, arguments.n_channels,
+ arguments.verbose ? "yes" : "no");
+
//irc_option_set (s, LIBIRC_OPTION_STRIPNICKS);
if ( irc_connect (s, argv[1], 6667, 0, argv[2], 0, 0) )
{
- sprintf (stderr, "Could not connect: %s\n", irc_strerror (irc_errno(s)));
+ fprintf (stderr, "Could not connect: %s\n", irc_strerror (irc_errno(s)));
return 1;
}
@@ -332,13 +439,12 @@ int main (int argc, char **argv)
thread_id_t tid;
if ( CREATE_THREAD (&tid, irc_listen, s) )
{
- sprintf (stderr, "CREATE_THREAD failed: %s\n", strerror(errno));
+ fprintf (stderr, "CREATE_THREAD failed: %s\n", strerror(errno));
return 42;
}
/*else
printf ("Listener thread was started successfully.\n");*/
- void **value_ptr;
char *line = NULL;
int res, size=100;