2727#include <string.h>
2828#include <time.h>
2929#include <url/url.h>
30+ #include "clib-settings.h"
3031
3132#define CLIB_SEARCH_CACHE_TIME 1 * 24 * 60 * 60
3233
33- #if defined(_WIN32 ) || defined(WIN32 ) || defined(__MINGW32__ ) || \
34- defined(__MINGW64__ ) || defined(__CYGWIN__ )
34+ #if defined(_WIN32 ) || defined(WIN32 ) || defined(__MINGW32__ ) || defined(__MINGW64__ ) || defined(__CYGWIN__ )
3535#define setenv (k , v , _ ) _putenv_s(k, v)
3636#define realpath (a , b ) _fullpath(a, b, strlen(a))
3737#endif
@@ -48,50 +48,50 @@ static void setopt_nocache(command_t *self) { opt_cache = 0; }
4848
4949static void setopt_json (command_t * self ) { opt_json = 1 ; }
5050
51- #define COMPARE (v ) \
52- { \
53- if (NULL == v) { \
54- rc = 0; \
55- goto cleanup; \
56- } \
57- case_lower(v); \
58- for (int i = 0; i < count; i++) { \
59- if (strstr(v, args[i])) { \
60- rc = 1; \
61- break; \
62- } \
63- } \
51+ #define COMPARE (v ) \
52+ { \
53+ if (NULL == v) { \
54+ rc = 0; \
55+ goto cleanup; \
56+ } \
57+ case_lower(v); \
58+ for (int i = 0; i < count; i++) { \
59+ if (strstr(v, args[i])) { \
60+ rc = 1; \
61+ break; \
62+ } \
63+ } \
6464 }
6565
6666static int matches (int count , char * args [], registry_package_ptr_t pkg ) {
67- // Display all packages if there's no query
68- if (0 == count )
69- return 1 ;
70-
71- char * description = NULL ;
72- char * name = NULL ;
73- char * repo = NULL ;
74- char * href = NULL ;
75- int rc = 0 ;
76-
77- name = clib_package_parse_name (registry_package_get_id (pkg ));
78- COMPARE (name );
79-
80- description = strdup (registry_package_get_description (pkg ));
81- COMPARE (description );
82-
83- repo = strdup (registry_package_get_id (pkg ));
84- COMPARE (repo );
85-
86- href = strdup (registry_package_get_href (pkg ));
87- COMPARE (href );
88-
89- cleanup :
90- free (description );
91- free (name );
92- free (repo );
93- free (href );
94- return rc ;
67+ // Display all packages if there's no query
68+ if (0 == count )
69+ return 1 ;
70+
71+ char * description = NULL ;
72+ char * name = NULL ;
73+ char * repo = NULL ;
74+ char * href = NULL ;
75+ int rc = 0 ;
76+
77+ name = clib_package_parse_name (registry_package_get_id (pkg ));
78+ COMPARE (name );
79+
80+ description = strdup (registry_package_get_description (pkg ));
81+ COMPARE (description );
82+
83+ repo = strdup (registry_package_get_id (pkg ));
84+ COMPARE (repo );
85+
86+ href = strdup (registry_package_get_href (pkg ));
87+ COMPARE (href );
88+
89+ cleanup :
90+ free (description );
91+ free (name );
92+ free (repo );
93+ free (href );
94+ return rc ;
9595}
9696
9797/*
@@ -125,67 +125,67 @@ static char *wiki_html_cache() {
125125static void display_package (const registry_package_ptr_t pkg ,
126126 cc_color_t fg_color_highlight ,
127127 cc_color_t fg_color_text ) {
128- cc_fprintf (fg_color_highlight , stdout , " %s\n" , registry_package_get_id (pkg ));
129- printf (" url: " );
130- cc_fprintf (fg_color_text , stdout , "%s\n" , registry_package_get_href (pkg ));
131- printf (" desc: " );
132- cc_fprintf (fg_color_text , stdout , "%s\n" , registry_package_get_description (pkg ));
133- printf ("\n" );
128+ cc_fprintf (fg_color_highlight , stdout , " %s\n" , registry_package_get_id (pkg ));
129+ printf (" url: " );
130+ cc_fprintf (fg_color_text , stdout , "%s\n" , registry_package_get_href (pkg ));
131+ printf (" desc: " );
132+ cc_fprintf (fg_color_text , stdout , "%s\n" , registry_package_get_description (pkg ));
133+ printf ("\n" );
134134}
135135
136136static void add_package_to_json (const registry_package_ptr_t pkg ,
137137 JSON_Array * json_list ) {
138- JSON_Value * json_pkg_root = json_value_init_object ();
139- JSON_Object * json_pkg = json_value_get_object (json_pkg_root );
138+ JSON_Value * json_pkg_root = json_value_init_object ();
139+ JSON_Object * json_pkg = json_value_get_object (json_pkg_root );
140140
141- json_object_set_string (json_pkg , "repo" , registry_package_get_id (pkg ));
142- json_object_set_string (json_pkg , "href" , registry_package_get_href (pkg ));
143- json_object_set_string (json_pkg , "description" , registry_package_get_description (pkg ));
144- json_object_set_string (json_pkg , "category" , registry_package_get_category (pkg ));
141+ json_object_set_string (json_pkg , "repo" , registry_package_get_id (pkg ));
142+ json_object_set_string (json_pkg , "href" , registry_package_get_href (pkg ));
143+ json_object_set_string (json_pkg , "description" , registry_package_get_description (pkg ));
144+ json_object_set_string (json_pkg , "category" , registry_package_get_category (pkg ));
145145
146- json_array_append_value (json_list , json_pkg_root );
146+ json_array_append_value (json_list , json_pkg_root );
147147}
148148
149149int main (int argc , char * argv []) {
150- opt_color = 1 ;
151- opt_cache = 1 ;
150+ opt_color = 1 ;
151+ opt_cache = 1 ;
152152
153- debug_init (& debugger , "clib-search" );
153+ debug_init (& debugger , "clib-search" );
154154
155- clib_cache_init (CLIB_SEARCH_CACHE_TIME );
155+ clib_cache_init (CLIB_SEARCH_CACHE_TIME );
156156
157- command_t program ;
158- command_init (& program , "clib-search" , CLIB_VERSION );
159- program .usage = "[options] [query ...]" ;
157+ command_t program ;
158+ command_init (& program , "clib-search" , CLIB_VERSION );
159+ program .usage = "[options] [query ...]" ;
160160
161- command_option (& program , "-n" , "--no-color" , "don't colorize output" ,
162- setopt_nocolor );
161+ command_option (& program , "-n" , "--no-color" , "don't colorize output" ,
162+ setopt_nocolor );
163163
164- command_option (& program , "-c" , "--skip-cache" , "skip the search cache" ,
165- setopt_nocache );
164+ command_option (& program , "-c" , "--skip-cache" , "skip the search cache" ,
165+ setopt_nocache );
166166
167- command_option (& program , "-j" , "--json" , "generate a serialized JSON output" ,
168- setopt_json );
167+ command_option (& program , "-j" , "--json" , "generate a serialized JSON output" ,
168+ setopt_json );
169169
170- command_parse (& program , argc , argv );
170+ command_parse (& program , argc , argv );
171171
172- for (int i = 0 ; i < program .argc ; i ++ )
173- case_lower (program .argv [i ]);
172+ for (int i = 0 ; i < program .argc ; i ++ )
173+ case_lower (program .argv [i ]);
174174
175- // set color theme
176- cc_color_t fg_color_highlight = opt_color ? CC_FG_DARK_CYAN : CC_FG_NONE ;
177- cc_color_t fg_color_text = opt_color ? CC_FG_DARK_GRAY : CC_FG_NONE ;
175+ // set color theme
176+ cc_color_t fg_color_highlight = opt_color ? CC_FG_DARK_CYAN : CC_FG_NONE ;
177+ cc_color_t fg_color_text = opt_color ? CC_FG_DARK_GRAY : CC_FG_NONE ;
178178
179- // We search the local manifest for extra registries.
180- // It is important to give the extra registries preference over the default registry.
181- clib_secrets_t secrets = clib_secrets_load_from_file ("clib_secrets.json" );
179+ // We search the local manifest for extra registries.
180+ // It is important to give the extra registries preference over the default registry.
181+ clib_secrets_t secrets = clib_secrets_load_from_file ("clib_secrets.json" );
182182
183- clib_package_t * package = clib_package_load_local_manifest (0 );
184- registries_t registries = registry_manager_init_registries (package -> registries , secrets );
185- registry_manager_fetch_registries (registries );
183+ clib_package_t * package = clib_package_load_local_manifest (0 );
184+ registries_t registries = registry_manager_init_registries (package -> registries , secrets );
185+ registry_manager_fetch_registries (registries );
186186
187- // TODO, implement caching for the new registries.
188- /*
187+ // TODO, implement caching for the new registries.
188+ /*
189189 char *html = wiki_html_cache();
190190 if (NULL == html) {
191191 command_free(&program);
@@ -197,45 +197,45 @@ int main(int argc, char *argv[]) {
197197 debug(&debugger, "found %zu packages", pkgs->len);
198198 */
199199
200- registry_iterator_t it = registry_iterator_new (registries );
201- registry_ptr_t registry = NULL ;
202- while ((registry = registry_iterator_next (it ))) {
203- printf ("SEARCH: packages from %s\n" , registry_get_url (registry ));
204- registry_package_ptr_t pkg ;
205- registry_package_iterator_t it = registry_package_iterator_new (registry );
200+ registry_iterator_t it = registry_iterator_new (registries );
201+ registry_ptr_t registry = NULL ;
202+ while ((registry = registry_iterator_next (it ))) {
203+ printf ("SEARCH: packages from %s\n" , registry_get_url (registry ));
204+ registry_package_ptr_t pkg ;
205+ registry_package_iterator_t it = registry_package_iterator_new (registry );
206206
207- JSON_Array * json_list = NULL ;
208- JSON_Value * json_list_root = NULL ;
207+ JSON_Array * json_list = NULL ;
208+ JSON_Value * json_list_root = NULL ;
209209
210- if (opt_json ) {
211- json_list_root = json_value_init_array ();
212- json_list = json_value_get_array (json_list_root );
213- }
210+ if (opt_json ) {
211+ json_list_root = json_value_init_array ();
212+ json_list = json_value_get_array (json_list_root );
213+ }
214214
215- printf ("\n" );
216-
217- while ((pkg = registry_package_iterator_next (it ))) {
218- if (matches (program .argc , program .argv , pkg )) {
219- if (opt_json ) {
220- add_package_to_json (pkg , json_list );
221- } else {
222- display_package (pkg , fg_color_highlight , fg_color_text );
223- }
224- } else {
225- debug (& debugger , "skipped package %s" , registry_package_get_id (pkg ));
226- }
227- }
215+ printf ("\n" );
228216
217+ while ((pkg = registry_package_iterator_next (it ))) {
218+ if (matches (program .argc , program .argv , pkg )) {
229219 if (opt_json ) {
230- char * serialized = json_serialize_to_string_pretty (json_list_root );
231- puts (serialized );
232-
233- json_free_serialized_string (serialized );
234- json_value_free (json_list_root );
220+ add_package_to_json (pkg , json_list );
221+ } else {
222+ display_package (pkg , fg_color_highlight , fg_color_text );
235223 }
224+ } else {
225+ debug (& debugger , "skipped package %s" , registry_package_get_id (pkg ));
226+ }
227+ }
236228
237- registry_package_iterator_destroy (it );
229+ if (opt_json ) {
230+ char * serialized = json_serialize_to_string_pretty (json_list_root );
231+ puts (serialized );
232+
233+ json_free_serialized_string (serialized );
234+ json_value_free (json_list_root );
238235 }
239- command_free (& program );
240- return 0 ;
236+
237+ registry_package_iterator_destroy (it );
238+ }
239+ command_free (& program );
240+ return 0 ;
241241}
0 commit comments