]> ToastFreeware Gitweb - gregoa/bti.git/blobdiff - bti.c
Add bash completion script
[gregoa/bti.git] / bti.c
diff --git a/bti.c b/bti.c
index cf1b164837a3a298d125a4d98a3ee78e6fb3ec5c..0ae103d893ababc70f0c26ba655d5729e23156c3 100644 (file)
--- a/bti.c
+++ b/bti.c
@@ -27,6 +27,7 @@
 #include <time.h>
 #include <sys/stat.h>
 #include <curl/curl.h>
+#include <readline/readline.h>
 #include "bti_version.h"
 
 
@@ -53,6 +54,7 @@ struct session {
        char *proxy;
        char *time;
        char *homedir;
+       char *logfile;
        int bash;
        enum host host;
 };
@@ -73,6 +75,7 @@ static void display_help(void)
        fprintf(stdout, "  --password password\n");
        fprintf(stdout, "  --proxy PROXY:PORT\n");
        fprintf(stdout, "  --host HOST\n");
+       fprintf(stdout, "  --logfile logfile\n");
        fprintf(stdout, "  --bash\n");
        fprintf(stdout, "  --debug\n");
        fprintf(stdout, "  --version\n");
@@ -86,17 +89,14 @@ static void display_version(void)
 
 static char *get_string_from_stdin(void)
 {
-       char *temp;
-       char *string;
+       static char *string = (char *)NULL;
+       if (string) {
+               free(string);
+               string = (char *)NULL;
+       }
 
-       string = zalloc(1000);
-       if (!string)
-               return NULL;
+       string = readline("tweet: ");
 
-       if (!fgets(string, 999, stdin))
-               return NULL;
-       temp = strchr(string, '\n');
-       *temp = '\0';
        return string;
 }
 
@@ -224,6 +224,11 @@ static int send_tweet(struct session *session)
                     CURLFORM_COPYCONTENTS, session->tweet,
                     CURLFORM_END);
 
+       curl_formadd(&formpost, &lastptr,
+                    CURLFORM_COPYNAME, "source",
+                    CURLFORM_COPYCONTENTS, "bti",
+                    CURLFORM_END);
+
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
 
        switch (session->host) {
@@ -275,6 +280,7 @@ static void parse_configfile(struct session *session)
        char *password = NULL;
        char *host = NULL;
        char *proxy = NULL;
+       char *logfile = NULL;
        char *file;
 
        /* config file is ~/.bti  */
@@ -326,6 +332,11 @@ static void parse_configfile(struct session *session)
                        c += 6;
                        if (c[0] != '\0')
                                proxy = strdup(c);
+               } else if (!strncasecmp(c, "logfile", 7) &&
+                          (c[7] == '=')) {
+                       c += 8;
+                       if (c[0] != '\0')
+                               logfile = strdup(c);
                }
        } while (!feof(config_file));
 
@@ -345,6 +356,8 @@ static void parse_configfile(struct session *session)
                        free(session->proxy);
                session->proxy = proxy;
        }
+       if (logfile)
+               session->logfile = logfile;
 
        /* Free buffer and close file.  */
        free(line);
@@ -357,10 +370,14 @@ static void log_session(struct session *session, int retval)
        char *filename;
        char *host;
 
-       /* logfile is ~/.bti.log  */
-       filename = alloca(strlen(session->homedir) + 10);
+       /* Only log something if we have a log file set */
+       if (!session->logfile)
+               return;
+
+       filename = alloca(strlen(session->homedir) +
+                         strlen(session->logfile) + 3);
 
-       sprintf(filename, "%s/.bti.log", session->homedir);
+       sprintf(filename, "%s/%s", session->homedir, session->logfile);
 
        log_file = fopen(filename, "a+");
        if (log_file == NULL)
@@ -393,6 +410,7 @@ int main(int argc, char *argv[], char *envp[])
                { "password", 1, NULL, 'p' },
                { "host", 1, NULL, 'H' },
                { "proxy", 1, NULL, 'P' },
+               { "logfile", 1, NULL, 'L' },
                { "help", 0, NULL, 'h' },
                { "bash", 0, NULL, 'b' },
                { "version", 0, NULL, 'v' },
@@ -410,6 +428,7 @@ int main(int argc, char *argv[], char *envp[])
        char *dir;
 #endif
 
+       rl_bind_key('\t', rl_insert);
        session = session_alloc();
        if (!session) {
                fprintf(stderr, "no more memory...\n");
@@ -464,6 +483,12 @@ int main(int argc, char *argv[], char *envp[])
                        session->proxy = strdup(optarg);
                        dbg("proxy = %s\n", session->proxy);
                        break;
+               case 'L':
+                       if (session->logfile)
+                               free(session->logfile);
+                       session->logfile = strdup(optarg);
+                       dbg("logfile = %s\n", session->logfile);
+                       break;
                case 'H':
                        if (strcasecmp(optarg, "twitter") == 0)
                                session->host = HOST_TWITTER;