-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportdbc.c
162 lines (142 loc) · 3.57 KB
/
portdbc.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
/*
* portdbc - CRUX's portdb command line client
*
* Copyright 2010-2024 Jose Beneyto
*
* Licensed under GPLv2, see file COPYING in this source tree.
*/
#include "defines.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
int main(int argc, char** argv)
{
xmlDoc *doc = NULL;
xmlNode *r_node = NULL;
char *url = NULL;
char *portdb_url = "https://crux.nu/portdb/";
char *tmpfile = "/tmp/.portdbc.xml";
char *configfile = "/tmp/.portdbc.conf";
FILE *file_handle = NULL;
// use HOME directory when available
char *home_dir = NULL;
home_dir = getenv("HOME");
if (home_dir != NULL)
{
asprintf(&tmpfile, "%s/.portdbc.xml", home_dir);
asprintf(&configfile, "%s/.portdbc.conf", home_dir);
// try to read config values
if ((file_handle = fopen(configfile, "r+")))
{
char line[256], option[256], value[256];
int linenum=0;
while(fgets(line, 256, file_handle) != NULL)
{
linenum++;
// ignore comments from config file
if(line[0] == '#') continue;
// try to read the pair 'option = value'
if(sscanf(line, "%s = %s", option, value) != 2)
{
//fprintf(stderr, "Invalid syntax, line %d\n", linenum);
continue;
}
//fprintf(stdout, "%-2d: %s = %s\n", linenum, option, value);
if(strcmp(option, "portdb_url") == 0)
{
asprintf(&portdb_url, "%s", value);
}
}
//fprintf(stdout, "portdb_url: %s\n", portdb_url);
}
}
int command = NOCMD;
if (argc > 1)
{
if (strcmp("repos", argv[1]) == 0)
{
asprintf(&url, "%s?f=xml", portdb_url);
command = CMD_REPOS;
}
else if (strcmp("dups", argv[1]) == 0)
{
asprintf(&url, "%s?f=xml&a=dups", portdb_url);
command = CMD_DUPS;
}
else if (strcmp("search", argv[1]) == 0 && argc > 2)
{
asprintf(&url, "%s?f=xml&a=search&q=%s", portdb_url, argv[2]);
command = CMD_SEARCH;
}
else if (strcmp("list", argv[1]) == 0 && argc > 2)
{
asprintf(&url, "%s?f=xml&a=repo&q=%s", portdb_url, argv[2]);
command = CMD_LIST;
}
else if (strcmp("getup", argv[1]) == 0 && argc > 2)
{
asprintf(&url, "%s?a=getup&q=%s", portdb_url, argv[2]);
command = CMD_GETUP;
// at this point we should print the output since it is not an xml
printHttpFile(url);
// and exit
return 0;
}
else if (strcmp("version", argv[1]) == 0)
{
printVersion();
return 0;
}
else
{
printUsage();
return 0;
}
}
else
{
printUsage();
return 0;
}
saveHttpFile(url, tmpfile);
free(url);
doc = xmlReadFile(tmpfile, NULL, 0);
if (doc == NULL)
{
fprintf(stderr, "Error, could not parse file %s\n", tmpfile);
return -1;
}
// get the root element node
r_node = xmlDocGetRootElement(doc);
switch (command)
{
case CMD_REPOS:
// validate as portdb xml compatible
if (!r_node || !r_node->name || xmlStrcmp(r_node->name, (const xmlChar *) "repos"))
{
xmlFreeDoc(doc);
fprintf(stderr, "Error, temporary file not compatible\n");
return -1;
}
// print all repositories
listRepos(r_node);
break;
case CMD_DUPS:
listDups(r_node);
break;
case CMD_SEARCH:
case CMD_LIST:
listPorts(r_node);
break;
}
// free
xmlFreeDoc(doc);
// free global variables allocated by the parser
xmlCleanupParser();
// remove temporary file
remove(tmpfile);
return 0;
}