summaryrefslogtreecommitdiff
path: root/src/bin/cuturl.c
blob: 7451a2732c17afefc31c02390c60a1cab0d6abb7 (plain) (blame)
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
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*
 schemes are case sensitive but cononicals are lower case.
 domain is case insensitive. return it lowercased?
 port is optional and in decimal
 path
 scheme://username:password@domain:port/path?query_string#fragment_id
 mailto:username@domain

 optional stuff:
 scheme, username, password, port, path, query_string, fragment_id
*/

#define AorB(a,b) ((a)?(a):(b))

#define F_SCHEME 1<<0
#define F_USERNAME 1<<1
#define F_PASSWORD 1<<2
#define F_DOMAIN 1<<3
#define F_PORT 1<<4
#define F_PATH 1<<5
#define F_QUERY_STRING 1<<6
#define F_FRAGMENT_ID 1<<7

char *long_opts[]={"scheme","username","password","domain","port","path","query_string","fragment_id",0};
char *short_opts[]={"s","u","k","d","P","p","q","f"};

int main(int argc,char *argv[]) {
 char *line=0;
 char *scheme=0;
 char *username=0;
 char *password=0;
 char *domain=0;
 char *port=0;
 char *path=0;
 char *query_string=0;
 char *fragment_id=0;
 char sport[10];
 char args[256];
 struct servent *serv;
 int i,j,c=0;
 int size=1024;
 char fixme=0;
 char using_stdin=1;
 char malloced=0;
 if(argc > 1) {
  if(!strcmp(argv[1],"--help") || !strcmp(argv[1],"-h")) {
   printf("usage: echo urls | cuturl [options]\n");
   printf("usage: cuturl [options] url [options] [url]\n\n");
   printf("options: \n");
   for(i=0;long_opts[i];i++) {
    printf("        -%s|--%s\n",short_opts[i],long_opts[i]);
   }
   printf("To set default values use environment variables like: CUTURL_[OPTION]\n");
   return 2;
  }
 }
 argv++;
 argc--;
 while(1) {
  scheme=0;
  username=0;
  password=0;
  domain=0;
  port=0;
  path=0;
  query_string=0;
  fragment_id=0;
  if(!using_stdin) c=0;
  if(argc >= 1) {
   for(;argc>0;argc--,argv++) {
    for(i=0;long_opts[i];i++) {
     if(!strncmp(*argv,"--",2)) {
      if(!strcmp(*argv+2,long_opts[i])) {
       args[c]=1<<i;
       c++;
       break;
      }
     }
    }
    fixme=0;
    if(**argv=='-' && argv[0][1] != '-') {
     for(j=1;argv[0][j];j++) {
      for(i=0;short_opts[i];i++) {
       if(argv[0][j]==*short_opts[i]) {
        args[c]=1<<i;
        c++;
        fixme=1;
       }
      }
     }
    }
    if(fixme) continue;
    if(long_opts[i]) continue;
    //if we get here we are at data instead of flags. work on it.
    line=*argv;
    using_stdin=0;
    argc--;
    argv++;
    break;
   }
  }
  if(!argc && !line) {//if we are out of arguments and it didn't include data
   using_stdin=1;
  }
  if(using_stdin) {
   line=malloc(size+1);
   malloced=1;
   if(!fgets(line,size,stdin)) {
    return 0;
   }
  }
  if(!line) return 0;
  for(i=0;line[i] && line[i] != '\n' && line[i] != '\r';i++);
  line[i]=0;

/// end of interface logic
/// beginning of business logic

  //split at first single / into line and path
  for(i=0;line[i];i++) {
   if(line[i] == '/' && line[i+1] == '/') {
    i++;
    continue;
   }
   if(line[i] == '/') {
    line[i]=0;
    path=line+i+1;
    break;
   }
  }
  if(path) {
   if(strchr(path,'?')) {
    query_string=strchr(path,'?');
    *query_string=0;
    query_string++;
   }
  }

  if(query_string) {
   if(strchr(query_string,'#')) {
    fragment_id=strchr(query_string,'#');
    *fragment_id=0;
    fragment_id++;
   }
  }

  if(strstr(line,"://")) {
   scheme=line;
   domain=strstr(line,"://");
   *domain=0;
   domain+=3;
  } else {
   domain=line;
  }

  if(domain) {
   if(strchr(domain,'@')) {
    username=domain;
    domain=strchr(domain,'@');
    *domain=0;
    domain++;
   }
  }

  if(username) {
   if(strchr(username,':')) {
    password=strchr(username,':');
    *password=0;
    password++;
   }
  }

  if(domain) {
   if(strchr(domain,']')) {//the end of an IPv6 address
    if(strchr(strchr(domain,']'),':')) {
     port=strchr(strchr(domain,']'),':');
     if(port[1] == '?') {//for magnet links
      port=0;
     } else {
      *port=0;
      port++;
     }
    }
   } else {
    if(strchr(domain,':')) {
     port=strchr(domain,':');
     if(port[1] == '?') {//for magnet links
      port=0;
     } else {
      *port=0;
      port++;
     }
    }
   }
  }

  if(domain) {//for magnet links.
   if(strchr(domain,'?')) {
    query_string=strchr(domain,'?');
    *query_string=0;
    query_string++;
   }
  }

  if(strchr(domain,':') && !strchr(domain,']')) {//for scheme:?query_string
   scheme=domain;
   *strchr(scheme,':')=0;
   domain=0;
  }

  if(!scheme && username) {//for mailto:
   scheme=username;
   username=password;
   password=0;
  }

/// end of business logic

  // printf("scheme://username:password@domain:port/path?query_string#fragment_id\n\n");
  //let's set them to what'll get printed now...

  scheme=AorB(scheme,AorB(getenv("CUTURL_SCHEME"),"DEFAULT"));
  username=AorB(username,AorB(getenv("CUTURL_USERNAME"),"DEFAULT"));
  password=AorB(password,AorB(getenv("CUTURL_PASSWORD"),"DEFAULT"));
  domain=AorB(domain,AorB(getenv("CURURL_DOMAIN"),"DEFAULT"));
  serv=getservbyname(scheme,strcmp(scheme,"udp")?"tcp":"udp");
  if(serv) snprintf(sport,sizeof(sport)-1,"%d",ntohs(serv->s_port));
  port=AorB(port,AorB(getenv("CUTURL_PORT"),(serv?sport:"DEFAULT")));
  path=AorB(path,AorB(getenv("CUTURL_PATH"),"DEFAULT"));
  query_string=AorB(query_string,AorB(getenv("CUTURL_QUERY_STRING"),"DEFAULT"));
  fragment_id=AorB(fragment_id,AorB(getenv("CUTURL_FRAGMENT_ID"),"DEFAULT"));

  if(c) {
   for(i=0;i<c;i++) {
    if(args[i]&F_SCHEME) printf("%s\n",scheme);
    if(args[i]&F_USERNAME) printf("%s\n",username);
    if(args[i]&F_PASSWORD) printf("%s\n",password);
    if(args[i]&F_DOMAIN) printf("%s\n",domain);
    if(args[i]&F_PORT) printf("%s\n",port);
    if(args[i]&F_PATH) printf("%s\n",path);
    if(args[i]&F_QUERY_STRING) printf("%s\n",query_string);
    if(args[i]&F_FRAGMENT_ID) printf("%s\n",fragment_id);
   }
  } else {
   printf("scheme: %s\n",scheme);
   printf("username: %s\n",username);
   printf("password: %s\n",password);
   printf("domain: %s\n",domain);
   printf("port: %s\n",port);
   printf("path: %s\n",path);
   printf("query_string: %s\n",query_string);
   printf("fragment_id: %s\n",fragment_id);
  }
  if(malloced) {
   free(line);
   malloced=0;
   line=0;
  } else {
   line=0;//???
  }
 }
 return 0;
}