Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add config.c

This splits the config parsing logic out to a single file so we all
don't have to go blind by accidentally looking at the horror that it
imparts on our soul if we notice it.

Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information...
commit c8f56274f6d6f7cb8f7f8a2f002989c396a07042 1 parent 9185ab0
Greg Kroah-Hartman authored

Showing 4 changed files with 249 additions and 196 deletions. Show diff stats Hide diff stats

  1. +1 0  Makefile.am
  2. +7 196 bti.c
  3. +7 0 bti.h
  4. +234 0 config.c
1  Makefile.am
@@ -6,6 +6,7 @@ bin_SCRIPTS = \
6 6
7 7 bti_SOURCES = \
8 8 bti.h \
  9 + config.c \
9 10 bti.c
10 11
11 12 bti_CFLAGS = \
203 bti.c
... ... @@ -1,5 +1,5 @@
1 1 /*
2   - * Copyright (C) 2008-2010 Greg Kroah-Hartman <greg@kroah.com>
  2 + * Copyright (C) 2008-2011 Greg Kroah-Hartman <greg@kroah.com>
3 3 * Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
4 4 * Copyright (C) 2009-2010 Amir Mohammad Saied <amirsaied@gmail.com>
5 5 *
@@ -247,10 +247,10 @@ static void bti_curl_buffer_free(struct bti_curl_buffer *buffer)
247 247 free(buffer);
248 248 }
249 249
250   -static const char twitter_host[] = "http://api.twitter.com/1/statuses";
251   -static const char identica_host[] = "https://identi.ca/api/statuses";
252   -static const char twitter_name[] = "twitter";
253   -static const char identica_name[] = "identi.ca";
  250 +const char twitter_host[] = "http://api.twitter.com/1/statuses";
  251 +const char identica_host[] = "https://identi.ca/api/statuses";
  252 +const char twitter_name[] = "twitter";
  253 +const char identica_name[] = "identi.ca";
254 254
255 255 static const char twitter_request_token_uri[] = "http://twitter.com/oauth/request_token";
256 256 static const char twitter_access_token_uri[] = "http://twitter.com/oauth/access_token";
@@ -726,195 +726,6 @@ static int send_request(struct session *session)
726 726 return 0;
727 727 }
728 728
729   -static void parse_configfile(struct session *session)
730   -{
731   - FILE *config_file;
732   - char *line = NULL;
733   - size_t len = 0;
734   - char *account = NULL;
735   - char *password = NULL;
736   - char *consumer_key = NULL;
737   - char *consumer_secret = NULL;
738   - char *access_token_key = NULL;
739   - char *access_token_secret = NULL;
740   - char *host = NULL;
741   - char *proxy = NULL;
742   - char *logfile = NULL;
743   - char *action = NULL;
744   - char *user = NULL;
745   - char *replyto = NULL;
746   - char *retweet = NULL;
747   - int shrink_urls = 0;
748   -
749   - config_file = fopen(session->configfile, "r");
750   -
751   - /* No error if file does not exist or is unreadable. */
752   - if (config_file == NULL)
753   - return;
754   -
755   - do {
756   - ssize_t n = getline(&line, &len, config_file);
757   - if (n < 0)
758   - break;
759   - if (line[n - 1] == '\n')
760   - line[n - 1] = '\0';
761   - /* Parse file. Format is the usual value pairs:
762   - account=name
763   - passwort=value
764   - # is a comment character
765   - */
766   - *strchrnul(line, '#') = '\0';
767   - char *c = line;
768   - while (isspace(*c))
769   - c++;
770   - /* Ignore blank lines. */
771   - if (c[0] == '\0')
772   - continue;
773   -
774   - if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
775   - c += 8;
776   - if (c[0] != '\0')
777   - account = strdup(c);
778   - } else if (!strncasecmp(c, "password", 8) &&
779   - (c[8] == '=')) {
780   - c += 9;
781   - if (c[0] != '\0')
782   - password = strdup(c);
783   - } else if (!strncasecmp(c, "consumer_key", 12) &&
784   - (c[12] == '=')) {
785   - c += 13;
786   - if (c[0] != '\0')
787   - consumer_key = strdup(c);
788   - } else if (!strncasecmp(c, "consumer_secret", 15) &&
789   - (c[15] == '=')) {
790   - c += 16;
791   - if (c[0] != '\0')
792   - consumer_secret = strdup(c);
793   - } else if (!strncasecmp(c, "access_token_key", 16) &&
794   - (c[16] == '=')) {
795   - c += 17;
796   - if (c[0] != '\0')
797   - access_token_key = strdup(c);
798   - } else if (!strncasecmp(c, "access_token_secret", 19) &&
799   - (c[19] == '=')) {
800   - c += 20;
801   - if (c[0] != '\0')
802   - access_token_secret = strdup(c);
803   - } else if (!strncasecmp(c, "host", 4) &&
804   - (c[4] == '=')) {
805   - c += 5;
806   - if (c[0] != '\0')
807   - host = strdup(c);
808   - } else if (!strncasecmp(c, "proxy", 5) &&
809   - (c[5] == '=')) {
810   - c += 6;
811   - if (c[0] != '\0')
812   - proxy = strdup(c);
813   - } else if (!strncasecmp(c, "logfile", 7) &&
814   - (c[7] == '=')) {
815   - c += 8;
816   - if (c[0] != '\0')
817   - logfile = strdup(c);
818   - } else if (!strncasecmp(c, "replyto", 7) &&
819   - (c[7] == '=')) {
820   - c += 8;
821   - if (c[0] != '\0')
822   - replyto = strdup(c);
823   - } else if (!strncasecmp(c, "action", 6) &&
824   - (c[6] == '=')) {
825   - c += 7;
826   - if (c[0] != '\0')
827   - action = strdup(c);
828   - } else if (!strncasecmp(c, "user", 4) &&
829   - (c[4] == '=')) {
830   - c += 5;
831   - if (c[0] != '\0')
832   - user = strdup(c);
833   - } else if (!strncasecmp(c, "shrink-urls", 11) &&
834   - (c[11] == '=')) {
835   - c += 12;
836   - if (!strncasecmp(c, "true", 4) ||
837   - !strncasecmp(c, "yes", 3))
838   - shrink_urls = 1;
839   - } else if (!strncasecmp(c, "verbose", 7) &&
840   - (c[7] == '=')) {
841   - c += 8;
842   - if (!strncasecmp(c, "true", 4) ||
843   - !strncasecmp(c, "yes", 3))
844   - session->verbose = 1;
845   - } else if (!strncasecmp(c,"retweet", 7) &&
846   - (c[7] == '=')) {
847   - c += 8;
848   - if (c[0] != '\0')
849   - retweet = strdup(c);
850   - }
851   - } while (!feof(config_file));
852   -
853   - if (password)
854   - session->password = password;
855   - if (account)
856   - session->account = account;
857   - if (consumer_key)
858   - session->consumer_key = consumer_key;
859   - if (consumer_secret)
860   - session->consumer_secret = consumer_secret;
861   - if (access_token_key)
862   - session->access_token_key = access_token_key;
863   - if (access_token_secret)
864   - session->access_token_secret = access_token_secret;
865   - if (host) {
866   - if (strcasecmp(host, "twitter") == 0) {
867   - session->host = HOST_TWITTER;
868   - session->hosturl = strdup(twitter_host);
869   - session->hostname = strdup(twitter_name);
870   - } else if (strcasecmp(host, "identica") == 0) {
871   - session->host = HOST_IDENTICA;
872   - session->hosturl = strdup(identica_host);
873   - session->hostname = strdup(identica_name);
874   - } else {
875   - session->host = HOST_CUSTOM;
876   - session->hosturl = strdup(host);
877   - session->hostname = strdup(host);
878   - }
879   - free(host);
880   - }
881   - if (proxy) {
882   - if (session->proxy)
883   - free(session->proxy);
884   - session->proxy = proxy;
885   - }
886   - if (logfile)
887   - session->logfile = logfile;
888   - if (replyto)
889   - session->replyto = replyto;
890   - if (retweet)
891   - session->retweet = retweet;
892   - if (action) {
893   - if (strcasecmp(action, "update") == 0)
894   - session->action = ACTION_UPDATE;
895   - else if (strcasecmp(action, "friends") == 0)
896   - session->action = ACTION_FRIENDS;
897   - else if (strcasecmp(action, "user") == 0)
898   - session->action = ACTION_USER;
899   - else if (strcasecmp(action, "replies") == 0)
900   - session->action = ACTION_REPLIES;
901   - else if (strcasecmp(action, "public") == 0)
902   - session->action = ACTION_PUBLIC;
903   - else if (strcasecmp(action, "group") == 0)
904   - session->action = ACTION_GROUP;
905   - else
906   - session->action = ACTION_UNKNOWN;
907   - free(action);
908   - }
909   - if (user)
910   - session->user = user;
911   - session->shrink_urls = shrink_urls;
912   -
913   - /* Free buffer and close file. */
914   - free(line);
915   - fclose(config_file);
916   -}
917   -
918 729 static void log_session(struct session *session, int retval)
919 730 {
920 731 FILE *log_file;
@@ -1341,7 +1152,7 @@ int main(int argc, char *argv[], char *envp[])
1341 1152 dbg("http_proxy = %s\n", session->proxy);
1342 1153 }
1343 1154
1344   - parse_configfile(session);
  1155 + bti_parse_configfile(session);
1345 1156
1346 1157 while (1) {
1347 1158 option = getopt_long_only(argc, argv,
@@ -1465,7 +1276,7 @@ int main(int argc, char *argv[], char *envp[])
1465 1276 * previously set options from the command line, but
1466 1277 * the user asked for it...
1467 1278 */
1468   - parse_configfile(session);
  1279 + bti_parse_configfile(session);
1469 1280 break;
1470 1281 case 'h':
1471 1282 display_help();
7 bti.h
@@ -78,4 +78,11 @@ struct bti_curl_buffer {
78 78 int length;
79 79 };
80 80
  81 +extern const char twitter_host[];
  82 +extern const char identica_host[];
  83 +extern const char twitter_name[];
  84 +extern const char identica_name[];
  85 +
  86 +extern void bti_parse_configfile(struct session *session);
  87 +
81 88 #endif
234 config.c
... ... @@ -0,0 +1,234 @@
  1 +/*
  2 + * Copyright (C) 2008-2011 Greg Kroah-Hartman <greg@kroah.com>
  3 + * Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
  4 + * Copyright (C) 2009-2010 Amir Mohammad Saied <amirsaied@gmail.com>
  5 + *
  6 + * This program is free software; you can redistribute it and/or modify it
  7 + * under the terms of the GNU General Public License as published by the
  8 + * Free Software Foundation version 2 of the License.
  9 + *
  10 + * This program is distributed in the hope that it will be useful, but
  11 + * WITHOUT ANY WARRANTY; without even the implied warranty of
  12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13 + * General Public License for more details.
  14 + *
  15 + * You should have received a copy of the GNU General Public License along
  16 + * with this program; if not, write to the Free Software Foundation, Inc.,
  17 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18 + */
  19 +
  20 +#define _GNU_SOURCE
  21 +
  22 +#include <stdio.h>
  23 +#include <stdlib.h>
  24 +#include <stddef.h>
  25 +#include <string.h>
  26 +#include <getopt.h>
  27 +#include <errno.h>
  28 +#include <ctype.h>
  29 +#include <fcntl.h>
  30 +#include <unistd.h>
  31 +#include <time.h>
  32 +#include <sys/stat.h>
  33 +#include <sys/types.h>
  34 +#include <sys/wait.h>
  35 +#include <curl/curl.h>
  36 +#include <libxml/xmlmemory.h>
  37 +#include <libxml/parser.h>
  38 +#include <libxml/tree.h>
  39 +#include <pcre.h>
  40 +#include <termios.h>
  41 +#include <dlfcn.h>
  42 +#include <oauth.h>
  43 +#include "bti.h"
  44 +
  45 +void bti_parse_configfile(struct session *session)
  46 +{
  47 + FILE *config_file;
  48 + char *line = NULL;
  49 + size_t len = 0;
  50 + char *account = NULL;
  51 + char *password = NULL;
  52 + char *consumer_key = NULL;
  53 + char *consumer_secret = NULL;
  54 + char *access_token_key = NULL;
  55 + char *access_token_secret = NULL;
  56 + char *host = NULL;
  57 + char *proxy = NULL;
  58 + char *logfile = NULL;
  59 + char *action = NULL;
  60 + char *user = NULL;
  61 + char *replyto = NULL;
  62 + char *retweet = NULL;
  63 + int shrink_urls = 0;
  64 +
  65 + config_file = fopen(session->configfile, "r");
  66 +
  67 + /* No error if file does not exist or is unreadable. */
  68 + if (config_file == NULL)
  69 + return;
  70 +
  71 + do {
  72 + ssize_t n = getline(&line, &len, config_file);
  73 + if (n < 0)
  74 + break;
  75 + if (line[n - 1] == '\n')
  76 + line[n - 1] = '\0';
  77 + /* Parse file. Format is the usual value pairs:
  78 + account=name
  79 + passwort=value
  80 + # is a comment character
  81 + */
  82 + *strchrnul(line, '#') = '\0';
  83 + char *c = line;
  84 + while (isspace(*c))
  85 + c++;
  86 + /* Ignore blank lines. */
  87 + if (c[0] == '\0')
  88 + continue;
  89 +
  90 + if (!strncasecmp(c, "account", 7) && (c[7] == '=')) {
  91 + c += 8;
  92 + if (c[0] != '\0')
  93 + account = strdup(c);
  94 + } else if (!strncasecmp(c, "password", 8) &&
  95 + (c[8] == '=')) {
  96 + c += 9;
  97 + if (c[0] != '\0')
  98 + password = strdup(c);
  99 + } else if (!strncasecmp(c, "consumer_key", 12) &&
  100 + (c[12] == '=')) {
  101 + c += 13;
  102 + if (c[0] != '\0')
  103 + consumer_key = strdup(c);
  104 + } else if (!strncasecmp(c, "consumer_secret", 15) &&
  105 + (c[15] == '=')) {
  106 + c += 16;
  107 + if (c[0] != '\0')
  108 + consumer_secret = strdup(c);
  109 + } else if (!strncasecmp(c, "access_token_key", 16) &&
  110 + (c[16] == '=')) {
  111 + c += 17;
  112 + if (c[0] != '\0')
  113 + access_token_key = strdup(c);
  114 + } else if (!strncasecmp(c, "access_token_secret", 19) &&
  115 + (c[19] == '=')) {
  116 + c += 20;
  117 + if (c[0] != '\0')
  118 + access_token_secret = strdup(c);
  119 + } else if (!strncasecmp(c, "host", 4) &&
  120 + (c[4] == '=')) {
  121 + c += 5;
  122 + if (c[0] != '\0')
  123 + host = strdup(c);
  124 + } else if (!strncasecmp(c, "proxy", 5) &&
  125 + (c[5] == '=')) {
  126 + c += 6;
  127 + if (c[0] != '\0')
  128 + proxy = strdup(c);
  129 + } else if (!strncasecmp(c, "logfile", 7) &&
  130 + (c[7] == '=')) {
  131 + c += 8;
  132 + if (c[0] != '\0')
  133 + logfile = strdup(c);
  134 + } else if (!strncasecmp(c, "replyto", 7) &&
  135 + (c[7] == '=')) {
  136 + c += 8;
  137 + if (c[0] != '\0')
  138 + replyto = strdup(c);
  139 + } else if (!strncasecmp(c, "action", 6) &&
  140 + (c[6] == '=')) {
  141 + c += 7;
  142 + if (c[0] != '\0')
  143 + action = strdup(c);
  144 + } else if (!strncasecmp(c, "user", 4) &&
  145 + (c[4] == '=')) {
  146 + c += 5;
  147 + if (c[0] != '\0')
  148 + user = strdup(c);
  149 + } else if (!strncasecmp(c, "shrink-urls", 11) &&
  150 + (c[11] == '=')) {
  151 + c += 12;
  152 + if (!strncasecmp(c, "true", 4) ||
  153 + !strncasecmp(c, "yes", 3))
  154 + shrink_urls = 1;
  155 + } else if (!strncasecmp(c, "verbose", 7) &&
  156 + (c[7] == '=')) {
  157 + c += 8;
  158 + if (!strncasecmp(c, "true", 4) ||
  159 + !strncasecmp(c, "yes", 3))
  160 + session->verbose = 1;
  161 + } else if (!strncasecmp(c,"retweet", 7) &&
  162 + (c[7] == '=')) {
  163 + c += 8;
  164 + if (c[0] != '\0')
  165 + retweet = strdup(c);
  166 + }
  167 + } while (!feof(config_file));
  168 +
  169 + if (password)
  170 + session->password = password;
  171 + if (account)
  172 + session->account = account;
  173 + if (consumer_key)
  174 + session->consumer_key = consumer_key;
  175 + if (consumer_secret)
  176 + session->consumer_secret = consumer_secret;
  177 + if (access_token_key)
  178 + session->access_token_key = access_token_key;
  179 + if (access_token_secret)
  180 + session->access_token_secret = access_token_secret;
  181 + if (host) {
  182 + if (strcasecmp(host, "twitter") == 0) {
  183 + session->host = HOST_TWITTER;
  184 + session->hosturl = strdup(twitter_host);
  185 + session->hostname = strdup(twitter_name);
  186 + } else if (strcasecmp(host, "identica") == 0) {
  187 + session->host = HOST_IDENTICA;
  188 + session->hosturl = strdup(identica_host);
  189 + session->hostname = strdup(identica_name);
  190 + } else {
  191 + session->host = HOST_CUSTOM;
  192 + session->hosturl = strdup(host);
  193 + session->hostname = strdup(host);
  194 + }
  195 + free(host);
  196 + }
  197 + if (proxy) {
  198 + if (session->proxy)
  199 + free(session->proxy);
  200 + session->proxy = proxy;
  201 + }
  202 + if (logfile)
  203 + session->logfile = logfile;
  204 + if (replyto)
  205 + session->replyto = replyto;
  206 + if (retweet)
  207 + session->retweet = retweet;
  208 + if (action) {
  209 + if (strcasecmp(action, "update") == 0)
  210 + session->action = ACTION_UPDATE;
  211 + else if (strcasecmp(action, "friends") == 0)
  212 + session->action = ACTION_FRIENDS;
  213 + else if (strcasecmp(action, "user") == 0)
  214 + session->action = ACTION_USER;
  215 + else if (strcasecmp(action, "replies") == 0)
  216 + session->action = ACTION_REPLIES;
  217 + else if (strcasecmp(action, "public") == 0)
  218 + session->action = ACTION_PUBLIC;
  219 + else if (strcasecmp(action, "group") == 0)
  220 + session->action = ACTION_GROUP;
  221 + else
  222 + session->action = ACTION_UNKNOWN;
  223 + free(action);
  224 + }
  225 + if (user)
  226 + session->user = user;
  227 + session->shrink_urls = shrink_urls;
  228 +
  229 + /* Free buffer and close file. */
  230 + free(line);
  231 + fclose(config_file);
  232 +}
  233 +
  234 +

0 comments on commit c8f5627

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