-
Notifications
You must be signed in to change notification settings - Fork 30
/
cower.c
2475 lines (2109 loc) · 67.8 KB
/
cower.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2010-2011 Dave Reisner
*
* cower.c
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/* glibc */
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <locale.h>
#include <pthread.h>
#include <regex.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <wchar.h>
#include <wordexp.h>
/* external libs */
#include <alpm.h>
#include <archive.h>
#include <curl/curl.h>
#include <openssl/crypto.h>
#include <yajl/yajl_parse.h>
/* macros {{{ */
#define ALLOC_FAIL(s) do { cwr_fprintf(stderr, LOG_ERROR, "could not allocate %zd bytes\n", s); } while(0)
#define MALLOC(p, s, action) do { p = calloc(1, s); if(!p) { ALLOC_FAIL(s); action; } } while(0)
#define CALLOC(p, l, s, action) do { p = calloc(l, s); if(!p) { ALLOC_FAIL(s); action; } } while(0)
#define FREE(x) do { if(x) free((void*)x); x = NULL; } while(0)
#define STREQ(x,y) (strcmp((x),(y)) == 0)
#define STR_STARTS_WITH(x,y) (strncmp((x),(y), strlen(y)) == 0)
#define NCFLAG(val, flag) (!cfg.color && (val)) ? (flag) : ""
#define COWER_USERAGENT "cower/3.x"
#define AUR_PKGBUILD_PATH "%s://aur.archlinux.org/packages/%s/PKGBUILD"
#define AUR_PKG_URL "%s://aur.archlinux.org/packages/%s/%s.tar.gz"
#define AUR_PKG_URL_FORMAT "%s://aur.archlinux.org/packages.php?ID="
#define AUR_RPC_URL "%s://aur.archlinux.org/rpc.php?type=%s&arg=%s"
#define THREAD_DEFAULT 10
#define TIMEOUT_DEFAULT 10L
#define UNSET -1
#define AUR_QUERY_TYPE "type"
#define AUR_QUERY_TYPE_INFO "info"
#define AUR_QUERY_TYPE_SEARCH "search"
#define AUR_QUERY_TYPE_MSRCH "msearch"
#define AUR_QUERY_ERROR "error"
#define NAME "Name"
#define VERSION "Version"
#define URL "URL"
#define AUR_ID "ID"
#define AUR_CAT "CategoryID"
#define AUR_DESC "Description"
#define AUR_LICENSE "License"
#define AUR_VOTES "NumVotes"
#define AUR_OOD "OutOfDate"
#define PKGBUILD_DEPENDS "depends=("
#define PKGBUILD_MAKEDEPENDS "makedepends=("
#define PKGBUILD_OPTDEPENDS "optdepends=("
#define PKGBUILD_PROVIDES "provides=("
#define PKGBUILD_CONFLICTS "conflicts=("
#define PKGBUILD_REPLACES "replaces=("
#define PKG_REPO "Repository"
#define PKG_AURPAGE "AUR Page"
#define PKG_PROVIDES "Provides"
#define PKG_DEPENDS "Depends On"
#define PKG_MAKEDEPENDS "Makedepends"
#define PKG_OPTDEPENDS "Optional Deps"
#define PKG_CONFLICTS "Conflicts With"
#define PKG_REPLACES "Replaces"
#define PKG_CAT "Category"
#define PKG_NUMVOTES "Votes"
#define PKG_LICENSE "License"
#define PKG_OOD "Out of Date"
#define PKG_DESC "Description"
#define COMMENT_BEG_DELIM "class=\"comment-header\">"
#define COMMENT_END_DELIM "</blockquote>"
#define INFO_INDENT 17
#define SRCH_INDENT 4
#define LIST_DELIM " "
#define NC "\033[0m"
#define BOLD "\033[1m"
#define BLACK "\033[0;30m"
#define RED "\033[0;31m"
#define GREEN "\033[0;32m"
#define YELLOW "\033[0;33m"
#define BLUE "\033[0;34m"
#define MAGENTA "\033[0;35m"
#define CYAN "\033[0;36m"
#define WHITE "\033[0;37m"
#define BOLDBLACK "\033[1;30m"
#define BOLDRED "\033[1;31m"
#define BOLDGREEN "\033[1;32m"
#define BOLDYELLOW "\033[1;33m"
#define BOLDBLUE "\033[1;34m"
#define BOLDMAGENTA "\033[1;35m"
#define BOLDCYAN "\033[1;36m"
#define BOLDWHITE "\033[1;37m"
#define REGEX_OPTS REG_ICASE|REG_EXTENDED|REG_NOSUB|REG_NEWLINE
#define REGEX_CHARS "^.+*?$[](){}|\\"
#define BRIEF_ERR "E"
#define BRIEF_WARN "W"
#define BRIEF_OK "S"
/* }}} */
/* typedefs and objects {{{ */
typedef enum __loglevel_t {
LOG_INFO = 1,
LOG_ERROR = (1 << 1),
LOG_WARN = (1 << 2),
LOG_DEBUG = (1 << 3),
LOG_VERBOSE = (1 << 4),
LOG_BRIEF = (1 << 5)
} loglevel_t;
typedef enum __operation_t {
OP_SEARCH = 1,
OP_INFO = (1 << 1),
OP_DOWNLOAD = (1 << 2),
OP_UPDATE = (1 << 3),
OP_MSEARCH = (1 << 4)
} operation_t;
typedef enum __html_strip_state_t {
READING_TAG,
NOT_READING_TAG
} html_strip_state_t;
enum {
OP_DEBUG = 1000,
OP_FORMAT,
OP_IGNOREPKG,
OP_IGNOREREPO,
OP_LISTDELIM,
OP_NOSSL,
OP_THREADS,
OP_TIMEOUT,
OP_VERSION
};
typedef enum __pkgdetail_t {
PKGDETAIL_DEPENDS = 0,
PKGDETAIL_MAKEDEPENDS,
PKGDETAIL_OPTDEPENDS,
PKGDETAIL_PROVIDES,
PKGDETAIL_CONFLICTS,
PKGDETAIL_REPLACES,
PKGDETAIL_MAX
} pkgdetail_t;
struct strings_t {
const char *error;
const char *warn;
const char *info;
const char *pkg;
const char *repo;
const char *url;
const char *ood;
const char *utd;
const char *nc;
const char *comment;
const char *commentheader;
};
struct aurpkg_t {
const char *id;
const char *name;
const char *ver;
const char *desc;
const char *url;
const char *lic;
const char *votes;
int cat;
int ood;
alpm_list_t *comments;
alpm_list_t *depends;
alpm_list_t *makedepends;
alpm_list_t *optdepends;
alpm_list_t *provides;
alpm_list_t *conflicts;
alpm_list_t *replaces;
};
struct yajl_parser_t {
alpm_list_t *pkglist;
struct aurpkg_t *aurpkg;
char curkey[32];
int json_depth;
};
struct response_t {
char *data;
size_t size;
};
struct task_t {
void *(*threadfn)(CURL*, void*);
void (*printfn)(struct aurpkg_t*);
};
struct openssl_mutex_t {
pthread_mutex_t *lock;
long *lock_count;
};
struct html_character_codes_t {
char *name_code;
char *glyph;
};
/* }}} */
/* function prototypes {{{ */
static alpm_list_t *alpm_find_foreign_pkgs(void);
static int alpm_init(void);
static int alpm_pkg_is_foreign(pmpkg_t*);
static const char *alpm_provides_pkg(const char*);
static int archive_extract_file(const struct response_t*);
static int aurpkg_cmp(const void*, const void*);
static void aurpkg_free(void*);
static struct aurpkg_t *aurpkg_new(void);
static CURL *curl_init_easy_handle(CURL*);
static char *curl_get_url_as_buffer(CURL*, const char*);
static size_t curl_write_response(void*, size_t, size_t, void*);
static int cwr_asprintf(char**, const char*, ...) __attribute__((format(printf,2,3)));
static int cwr_fprintf(FILE*, loglevel_t, const char*, ...) __attribute__((format(printf,3,4)));
static int cwr_printf(loglevel_t, const char*, ...) __attribute__((format(printf,2,3)));
static int cwr_vfprintf(FILE*, loglevel_t, const char*, va_list) __attribute__((format(printf,3,0)));
static alpm_list_t *filter_results(alpm_list_t*);
static alpm_list_t *get_aur_comments(char*);
static char *get_file_as_buffer(const char*);
static int getcols(void);
static void indentprint(const char*, int);
static int json_end_map(void*);
static int json_map_key(void*, const unsigned char*, size_t);
static int json_start_map(void*);
static int json_string(void*, const unsigned char*, size_t);
static void openssl_crypto_cleanup(void);
static void openssl_crypto_init(void);
static unsigned long openssl_thread_id(void);
static void openssl_thread_cb(int, int, const char*, int);
static alpm_list_t *parse_bash_array(alpm_list_t*, char*, pkgdetail_t);
static int parse_configfile(void);
static int parse_options(int, char*[]);
static void pkgbuild_get_extinfo(char*, alpm_list_t**[]);
static int print_escaped(const char*);
static void print_extinfo_list(alpm_list_t*, const char*, const char*, int);
static void print_pkg_formatted(struct aurpkg_t*);
static void print_pkg_info(struct aurpkg_t*);
static void print_pkg_search(struct aurpkg_t*);
static void print_results(alpm_list_t*, void (*)(struct aurpkg_t*));
static int resolve_dependencies(CURL*, const char*);
static int set_working_dir(void);
static int strings_init(void);
static char *strip_and_sanitize_html(char*);
static char *strreplace(const char *, const char *, const char *);
static char *strtrim(char*);
static void *task_download(CURL*, void*);
static void *task_query(CURL*, void*);
static void *task_update(CURL*, void*);
static void *thread_pool(void*);
static void usage(void);
static void version(void);
static size_t yajl_parse_stream(void*, size_t, size_t, void*);
/* }}} */
/* runtime configuration {{{ */
struct {
char *dlpath;
const char *delim;
const char *format;
const char *proto;
operation_t opmask;
loglevel_t logmask;
int color;
int extinfo;
int force;
int getdeps;
int maxthreads;
int quiet;
int skiprepos;
int printcomments;
long timeout;
alpm_list_t *targets;
struct {
alpm_list_t *pkgs;
alpm_list_t *repos;
} ignore;
} cfg; /* }}} */
/* globals {{{ */
struct strings_t *colstr;
pmdb_t *db_local;
alpm_list_t *workq;
struct openssl_mutex_t openssl_lock;
static yajl_callbacks callbacks = {
NULL, /* null */
NULL, /* boolean */
NULL, /* integer */
NULL, /* double */
NULL, /* number */
json_string, /* string */
json_start_map, /* start_map */
json_map_key, /* map_key */
json_end_map, /* end_map */
NULL, /* start_array */
NULL /* end_array */
};
static char const *digits = "0123456789";
static char const *printf_flags = "'-+ #0I";
static const char *aur_cat[] = { NULL, "None", "daemons", "devel", "editors",
"emulators", "games", "gnome", "i18n", "kde", "lib",
"modules", "multimedia", "network", "office",
"science", "system", "x11", "xfce", "kernels" };
static const struct html_character_codes_t html_character_codes[] = {
{""", "\""},
{"<", "<"},
{">", ">"},
{"–", "-"},
{"—", "--"},
{"‘", "\'"},
{"’", "\'"},
{"“", "\""},
{"”", "\""},
{"&", "&"}, /* do him last just to be safe */
{NULL, NULL}
};
/* }}} */
int alpm_init() { /* {{{ */
int ret = 0;
FILE *fp;
char line[PATH_MAX];
char *ptr, *section = NULL;
cwr_printf(LOG_DEBUG, "initializing alpm\n");
ret = alpm_initialize();
if (ret != 0) {
return ret;
}
ret = alpm_option_set_dbpath("/var/lib/pacman");
if (ret != 0) {
return ret;
}
cwr_printf(LOG_DEBUG, "setting alpm DBPath to: %s\n", alpm_option_get_dbpath());
db_local = alpm_option_get_localdb();
if (!db_local) {
return 1;
}
fp = fopen("/etc/pacman.conf", "r");
if (!fp) {
return 1;
}
/* only light error checking happens here. this is pacman's job, not mine */
while (fgets(line, PATH_MAX, fp)) {
strtrim(line);
if (strlen(line) == 0 || line[0] == '#') {
continue;
}
if ((ptr = strchr(line, '#'))) {
*ptr = '\0';
}
if (line[0] == '[' && line[strlen(line) - 1] == ']') {
ptr = &line[1];
free(section);
section = strdup(ptr);
section[strlen(section) - 1] = '\0';
if (!STREQ(section, "options") && !cfg.skiprepos &&
!alpm_list_find_str(cfg.ignore.repos, section)) {
alpm_db_register_sync(section);
cwr_printf(LOG_DEBUG, "registering alpm db: %s\n", section);
}
} else {
char *key, *token;
key = ptr = line;
strsep(&ptr, "=");
strtrim(key);
strtrim(ptr);
if (STREQ(key, "DBPath")) {
cwr_printf(LOG_DEBUG, "setting alpm DBPath to: %s\n", ptr);
alpm_option_set_dbpath(ptr);
} else if (STREQ(key, "IgnorePkg")) {
for (token = strtok(ptr, " "); token; token = strtok(NULL, ",")) {
if (!alpm_list_find_str(cfg.ignore.pkgs, token)) {
cwr_printf(LOG_DEBUG, "ignoring package: %s\n", token);
cfg.ignore.pkgs = alpm_list_add(cfg.ignore.pkgs, strdup(token));
}
}
}
}
}
free(section);
fclose(fp);
return ret;
} /* }}} */
alpm_list_t *alpm_find_foreign_pkgs() { /* {{{ */
const alpm_list_t *i;
alpm_list_t *ret = NULL;
for (i = alpm_db_get_pkgcache(db_local); i; i = alpm_list_next(i)) {
pmpkg_t *pkg = alpm_list_getdata(i);
if (alpm_pkg_is_foreign(pkg)) {
ret = alpm_list_add(ret, strdup(alpm_pkg_get_name(pkg)));
}
}
return ret;
} /* }}} */
int alpm_pkg_is_foreign(pmpkg_t *pkg) { /* {{{ */
const alpm_list_t *i;
const char *pkgname;
pkgname = alpm_pkg_get_name(pkg);
for (i = alpm_option_get_syncdbs(); i; i = alpm_list_next(i)) {
if (alpm_db_get_pkg(alpm_list_getdata(i), pkgname)) {
return 0;
}
}
return 1;
} /* }}} */
const char *alpm_provides_pkg(const char *pkgname) { /* {{{ */
const alpm_list_t *i;
pmdb_t *db;
for (i = alpm_option_get_syncdbs(); i; i = alpm_list_next(i)) {
db = alpm_list_getdata(i);
if (alpm_find_satisfier(alpm_db_get_pkgcache(db), pkgname)) {
return alpm_db_get_name(db);
}
}
return NULL;
} /* }}} */
int archive_extract_file(const struct response_t *file) { /* {{{ */
struct archive *archive;
struct archive_entry *entry;
const int archive_flags = ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_TIME;
int ok, ret = ARCHIVE_OK;
archive = archive_read_new();
archive_read_support_compression_all(archive);
archive_read_support_format_all(archive);
ret = archive_read_open_memory(archive, file->data, file->size);
if (ret == ARCHIVE_OK) {
while (archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
ok = archive_read_extract(archive, entry, archive_flags);
/* NOOP ON ARCHIVE_{OK,WARN,RETRY} */
if (ok == ARCHIVE_FATAL || ok == ARCHIVE_EOF) {
ret = ok;
break;
}
}
archive_read_close(archive);
}
archive_read_finish(archive);
return ret;
} /* }}} */
int aurpkg_cmp(const void *p1, const void *p2) { /* {{{ */
struct aurpkg_t *pkg1 = (struct aurpkg_t*)p1;
struct aurpkg_t *pkg2 = (struct aurpkg_t*)p2;
return strcmp((const char*)pkg1->name, (const char*)pkg2->name);
} /* }}} */
void aurpkg_free(void *pkg) { /* {{{ */
struct aurpkg_t *it;
if (!pkg) {
return;
}
it = (struct aurpkg_t*)pkg;
FREE(it->id);
FREE(it->name);
FREE(it->ver);
FREE(it->desc);
FREE(it->url);
FREE(it->lic);
FREE(it->votes);
FREELIST(it->comments);
FREELIST(it->depends);
FREELIST(it->makedepends);
FREELIST(it->optdepends);
FREELIST(it->provides);
FREELIST(it->conflicts);
FREELIST(it->replaces);
FREE(it);
} /* }}} */
struct aurpkg_t *aurpkg_new() { /* {{{ */
struct aurpkg_t *pkg;
CALLOC(pkg, 1, sizeof *pkg, return NULL);
return pkg;
} /* }}} */
int cwr_asprintf(char **string, const char *format, ...) { /* {{{ */
int ret = 0;
va_list args;
va_start(args, format);
ret = vasprintf(string, format, args);
va_end(args);
if (ret == -1) {
cwr_fprintf(stderr, LOG_ERROR, "failed to allocate string\n");
}
return ret;
} /* }}} */
int cwr_fprintf(FILE *stream, loglevel_t level, const char *format, ...) { /* {{{ */
int ret;
va_list args;
va_start(args, format);
ret = cwr_vfprintf(stream, level, format, args);
va_end(args);
return ret;
} /* }}} */
int cwr_printf(loglevel_t level, const char *format, ...) { /* {{{ */
int ret;
va_list args;
va_start(args, format);
ret = cwr_vfprintf(stdout, level, format, args);
va_end(args);
return ret;
} /* }}} */
int cwr_vfprintf(FILE *stream, loglevel_t level, const char *format, va_list args) { /* {{{ */
const char *prefix;
char bufout[128];
if (!(cfg.logmask & level)) {
return 0;
}
switch (level) {
case LOG_VERBOSE:
case LOG_INFO:
prefix = colstr->info;
break;
case LOG_ERROR:
prefix = colstr->error;
break;
case LOG_WARN:
prefix = colstr->warn;
break;
case LOG_DEBUG:
prefix = "debug:";
break;
default:
prefix = "";
break;
}
/* f.l.w.: 128 should be big enough... */
snprintf(bufout, 128, "%s %s", prefix, format);
return vfprintf(stream, bufout, args);
} /* }}} */
CURL *curl_init_easy_handle(CURL *handle) { /* {{{ */
if (!handle) {
return NULL;
}
curl_easy_reset(handle);
curl_easy_setopt(handle, CURLOPT_USERAGENT, COWER_USERAGENT);
curl_easy_setopt(handle, CURLOPT_ENCODING, "deflate, gzip");
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, cfg.timeout);
/* This is required of multi-threaded apps using timeouts. See
* curl_easy_setopt(3) */
if (cfg.timeout > 0L) {
curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
}
return handle;
} /* }}} */
char *curl_get_url_as_buffer(CURL *curl, const char *url) { /* {{{ */
long httpcode;
struct response_t response;
CURLcode curlstat;
curl = curl_init_easy_handle(curl);
response.data = NULL;
response.size = 0;
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curlstat = curl_easy_perform(curl);
if (curlstat != CURLE_OK) {
cwr_fprintf(stderr, LOG_ERROR, "%s: %s\n", url, curl_easy_strerror(curlstat));
goto finish;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpcode);
if (!(httpcode == 200 || httpcode == 404)) {
cwr_fprintf(stderr, LOG_ERROR, "%s: server responded with http%ld\n",
url, httpcode);
}
finish:
return response.data;
} /* }}} */
size_t curl_write_response(void *ptr, size_t size, size_t nmemb, void *stream) { /* {{{ */
size_t realsize = size * nmemb;
struct response_t *mem = (struct response_t*)stream;
mem->data = realloc(mem->data, mem->size + realsize + 1);
if (mem->data) {
memcpy(&(mem->data[mem->size]), ptr, realsize);
mem->size += realsize;
mem->data[mem->size] = '\0';
}
return realsize;
} /* }}} */
alpm_list_t *filter_results(alpm_list_t *list) { /* {{{ */
const alpm_list_t *i, *j;
alpm_list_t *filterlist = NULL;
if (!(cfg.opmask & OP_SEARCH)) {
return list;
}
for (i = cfg.targets; i; i = alpm_list_next(i)) {
regex_t regex;
const char *targ = (const char*)alpm_list_getdata(i);
filterlist = NULL;
if (regcomp(®ex, targ, REGEX_OPTS) == 0) {
for (j = list; j; j = alpm_list_next(j)) {
struct aurpkg_t *pkg = alpm_list_getdata(j);
const char *name = pkg->name;
const char *desc = pkg->desc;
if (regexec(®ex, name, 0, 0, 0) != REG_NOMATCH ||
regexec(®ex, desc, 0, 0, 0) != REG_NOMATCH) {
filterlist = alpm_list_add(filterlist, pkg);
} else {
aurpkg_free(pkg);
}
}
regfree(®ex);
}
/* switcheroo */
alpm_list_free(list);
list = filterlist;
}
return alpm_list_msort(filterlist, alpm_list_count(filterlist), aurpkg_cmp);
} /* }}} */
alpm_list_t *get_aur_comments(char *buf) { /* {{{ */
char *this_comment, *comment_beg, *comment_end;
alpm_list_t *list = NULL;
for (comment_beg = strstr(buf, COMMENT_BEG_DELIM);
comment_beg;
comment_beg = strstr(comment_end, COMMENT_BEG_DELIM)) {
comment_end = strstr(comment_beg, COMMENT_END_DELIM);
/* we have the beginning and ending of the comment. now strip out
* all the html tags and save the comment: */
comment_beg += strlen(COMMENT_BEG_DELIM);
CALLOC(this_comment, comment_end - comment_beg + 1, sizeof(char), return NULL);
strncpy(this_comment, comment_beg, comment_end - comment_beg);
this_comment = strip_and_sanitize_html(this_comment);
list = alpm_list_add(list, this_comment);
}
return list;
} /* }}} */
int getcols() { /* {{{ */
int termwidth = -1;
const int default_tty = 80;
const int default_notty = 0;
if(!isatty(fileno(stdout))) {
return default_notty;
}
#ifdef TIOCGSIZE
struct ttysize win;
if(ioctl(1, TIOCGSIZE, &win) == 0) {
termwidth = win.ts_cols;
}
#elif defined(TIOCGWINSZ)
struct winsize win;
if(ioctl(1, TIOCGWINSZ, &win) == 0) {
termwidth = win.ws_col;
}
#endif
return termwidth <= 0 ? default_tty : termwidth;
} /* }}} */
char *get_file_as_buffer(const char *path) { /* {{{ */
FILE *fp;
char *buf;
long fsize, nread;
fp = fopen(path, "r");
if (!fp) {
cwr_fprintf(stderr, LOG_ERROR, "fopen: %s\n", strerror(errno));
return NULL;
}
fseek(fp, 0L, SEEK_END);
fsize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
CALLOC(buf, 1, (ssize_t)fsize + 1, return NULL);
nread = fread(buf, 1, fsize, fp);
fclose(fp);
if (nread < fsize) {
cwr_fprintf(stderr, LOG_ERROR, "Failed to read full PKGBUILD\n");
return NULL;
}
return buf;
} /* }}} */
void indentprint(const char *str, int indent) { /* {{{ */
wchar_t *wcstr;
const wchar_t *p;
int len, cidx, cols;
if (!str) {
return;
}
cols = getcols();
/* if we're not a tty, print without indenting */
if (cols == 0) {
printf("%s", str);
return;
}
len = strlen(str) + 1;
CALLOC(wcstr, len, sizeof *wcstr, return);
len = mbstowcs(wcstr, str, len);
p = wcstr;
cidx = indent;
if (!p || !len) {
return;
}
while (*p) {
if (*p == L' ') {
const wchar_t *q, *next;
p++;
if (!p || *p == L' ') {
continue;
}
next = wcschr(p, L' ');
if (!next) {
next = p + wcslen(p);
}
/* len captures # cols */
len = 0;
q = p;
while (q < next) {
len += wcwidth(*q++);
}
if (len > (cols - cidx - 1)) {
/* wrap to a newline and reindent */
printf("\n%-*s", indent, "");
cidx = indent;
} else {
printf(" ");
cidx++;
}
continue;
}
printf("%lc", (wint_t)*p);
cidx += wcwidth(*p);
p++;
}
free(wcstr);
} /* }}} */
int json_end_map(void *ctx) { /* {{{ */
struct yajl_parser_t *parse_struct = (struct yajl_parser_t*)ctx;
if (--parse_struct->json_depth > 0) {
parse_struct->pkglist = alpm_list_add_sorted(parse_struct->pkglist,
parse_struct->aurpkg, aurpkg_cmp);
}
return 1;
} /* }}} */
int json_map_key(void *ctx, const unsigned char *data, size_t size) { /* {{{ */
struct yajl_parser_t *parse_struct = (struct yajl_parser_t*)ctx;
strncpy(parse_struct->curkey, (const char*)data, size);
parse_struct->curkey[size] = '\0';
return 1;
} /* }}} */
int json_start_map(void *ctx) { /* {{{ */
struct yajl_parser_t *parse_struct = (struct yajl_parser_t*)ctx;
if (parse_struct->json_depth++ >= 1) {
parse_struct->aurpkg = aurpkg_new();
}
return 1;
} /* }}} */
int json_string(void *ctx, const unsigned char *data, size_t size) { /* {{{ */
struct yajl_parser_t *parse_struct = (struct yajl_parser_t*)ctx;
const char *val = (const char*)data;
if (STREQ(parse_struct->curkey, AUR_QUERY_TYPE) &&
STR_STARTS_WITH(val, AUR_QUERY_ERROR)) {
return 1;
}
if (STREQ(parse_struct->curkey, AUR_ID)) {
parse_struct->aurpkg->id = strndup(val, size);
} else if (STREQ(parse_struct->curkey, NAME)) {
parse_struct->aurpkg->name = strndup(val, size);
} else if (STREQ(parse_struct->curkey, VERSION)) {
parse_struct->aurpkg->ver = strndup(val, size);
} else if (STREQ(parse_struct->curkey, AUR_CAT)) {
parse_struct->aurpkg->cat = atoi(val);
} else if (STREQ(parse_struct->curkey, AUR_DESC)) {
parse_struct->aurpkg->desc = strndup(val, size);
} else if (STREQ(parse_struct->curkey, URL)) {
parse_struct->aurpkg->url = strndup(val, size);
} else if (STREQ(parse_struct->curkey, AUR_LICENSE)) {
parse_struct->aurpkg->lic = strndup(val, size);
} else if (STREQ(parse_struct->curkey, AUR_VOTES)) {
parse_struct->aurpkg->votes = strndup(val, size);
} else if (STREQ(parse_struct->curkey, AUR_OOD)) {
parse_struct->aurpkg->ood = strncmp(val, "1", 1) == 0 ? 1 : 0;
}
return 1;
} /* }}} */
void openssl_crypto_cleanup() { /* {{{ */
int i;
if (!STREQ(cfg.proto, "https")) {
return;
}
CRYPTO_set_locking_callback(NULL);
for (i = 0; i < CRYPTO_num_locks(); i++) {
pthread_mutex_destroy(&(openssl_lock.lock[i]));
}
OPENSSL_free(openssl_lock.lock);
OPENSSL_free(openssl_lock.lock_count);
} /* }}} */
void openssl_crypto_init() { /* {{{ */
int i;
openssl_lock.lock = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
openssl_lock.lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
for (i = 0; i < CRYPTO_num_locks(); i++) {
openssl_lock.lock_count[i] = 0;
pthread_mutex_init(&(openssl_lock.lock[i]), NULL);
}
CRYPTO_set_id_callback(openssl_thread_id);
CRYPTO_set_locking_callback(openssl_thread_cb);
} /* }}} */
void openssl_thread_cb(int mode, int type, const char *file, int line) { /* {{{ */
(void)type; (void)file; (void)line;
if (mode & CRYPTO_LOCK) {
pthread_mutex_lock(&(openssl_lock.lock[type]));
openssl_lock.lock_count[type]++;
} else {
pthread_mutex_unlock(&(openssl_lock.lock[type]));
}
} /* }}} */
unsigned long openssl_thread_id(void) { /* {{{ */
unsigned long ret;
ret = (unsigned long)pthread_self();
return(ret);
} /* }}} */
alpm_list_t *parse_bash_array(alpm_list_t *deplist, char *array, pkgdetail_t type) { /* {{{ */
char *ptr, *token;
if (!array) {
return NULL;
}
if (type == PKGDETAIL_OPTDEPENDS) {
const char *arrayend = rawmemchr(array, '\0');
for (token = array; token <= arrayend; token++) {
if (*token == '\'' || *token == '\"') {
token++;
ptr = strchr(token, *(token - 1));
*ptr = '\0';
} else if (isalpha(*token)) {
ptr = token;
while (!isspace(*++ptr));
*(ptr - 1) = '\0';