27
27
#include <string.h>
28
28
#include <time.h>
29
29
#include <url/url.h>
30
+ #include "clib-settings.h"
30
31
31
32
#define CLIB_SEARCH_CACHE_TIME 1 * 24 * 60 * 60
32
33
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__ )
35
35
#define setenv (k , v , _ ) _putenv_s(k, v)
36
36
#define realpath (a , b ) _fullpath(a, b, strlen(a))
37
37
#endif
@@ -48,50 +48,50 @@ static void setopt_nocache(command_t *self) { opt_cache = 0; }
48
48
49
49
static void setopt_json (command_t * self ) { opt_json = 1 ; }
50
50
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
+ } \
64
64
}
65
65
66
66
static 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 ;
95
95
}
96
96
97
97
/*
@@ -125,67 +125,67 @@ static char *wiki_html_cache() {
125
125
static void display_package (const registry_package_ptr_t pkg ,
126
126
cc_color_t fg_color_highlight ,
127
127
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" );
134
134
}
135
135
136
136
static void add_package_to_json (const registry_package_ptr_t pkg ,
137
137
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 );
140
140
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 ));
145
145
146
- json_array_append_value (json_list , json_pkg_root );
146
+ json_array_append_value (json_list , json_pkg_root );
147
147
}
148
148
149
149
int main (int argc , char * argv []) {
150
- opt_color = 1 ;
151
- opt_cache = 1 ;
150
+ opt_color = 1 ;
151
+ opt_cache = 1 ;
152
152
153
- debug_init (& debugger , "clib-search" );
153
+ debug_init (& debugger , "clib-search" );
154
154
155
- clib_cache_init (CLIB_SEARCH_CACHE_TIME );
155
+ clib_cache_init (CLIB_SEARCH_CACHE_TIME );
156
156
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 ...]" ;
160
160
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 );
163
163
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 );
166
166
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 );
169
169
170
- command_parse (& program , argc , argv );
170
+ command_parse (& program , argc , argv );
171
171
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 ]);
174
174
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 ;
178
178
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" );
182
182
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 );
186
186
187
- // TODO, implement caching for the new registries.
188
- /*
187
+ // TODO, implement caching for the new registries.
188
+ /*
189
189
char *html = wiki_html_cache();
190
190
if (NULL == html) {
191
191
command_free(&program);
@@ -197,45 +197,45 @@ int main(int argc, char *argv[]) {
197
197
debug(&debugger, "found %zu packages", pkgs->len);
198
198
*/
199
199
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 );
206
206
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 ;
209
209
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
+ }
214
214
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" );
228
216
217
+ while ((pkg = registry_package_iterator_next (it ))) {
218
+ if (matches (program .argc , program .argv , pkg )) {
229
219
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 );
235
223
}
224
+ } else {
225
+ debug (& debugger , "skipped package %s" , registry_package_get_id (pkg ));
226
+ }
227
+ }
236
228
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 );
238
235
}
239
- command_free (& program );
240
- return 0 ;
236
+
237
+ registry_package_iterator_destroy (it );
238
+ }
239
+ command_free (& program );
240
+ return 0 ;
241
241
}
0 commit comments