diff options
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/cuturl.c | 273 |
1 files changed, 192 insertions, 81 deletions
diff --git a/src/bin/cuturl.c b/src/bin/cuturl.c index 945d640..4a2f3f5 100644 --- a/src/bin/cuturl.c +++ b/src/bin/cuturl.c @@ -1,5 +1,7 @@ + #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. @@ -19,7 +21,7 @@ #define DEFAULT_PORT "80" #define DEFAULT_QUERY_STRING "" #define DEFAULT_FRAGMENT_ID "" -*/ + #define DEFAULT_SCHEME "DEFAULT" #define DEFAULT_USERNAME "DEFAULT" #define DEFAULT_PASSWORD "DEFAULT" @@ -27,9 +29,32 @@ #define DEFAULT_PATH "DEFAULT" #define DEFAULT_QUERY_STRING "DEFAULT" #define DEFAULT_FRAGMENT_ID "DEFAULT" +*/ + +#define AorB(a,b) ((a)?(a):(b)) + +#define DEFAULT_SCHEME AorB(getenv("CUTURL_SCHEME"),"DEFAULT") +#define DEFAULT_USERNAME AorB(getenv("CUTURL_USERNAME"),"DEFAULT") +#define DEFAULT_PASSWORD AorB(getenv("CUTURL_PASSWORD"),"DEFAULT") +#define DEFAULT_PORT AorB(getenv("CUTURL_PORT"),"DEFAULT") +#define DEFAULT_PATH AorB(getenv("CUTURL_PATH"),"DEFAULT") +#define DEFAULT_QUERY_STRING AorB(getenv("CUTURL_QUERY_STRING"),"DEFAULT") +#define DEFAULT_FRAGMENT_ID AorB(getenv("CUTURL_FRAGMENT_ID"),"DEFAULT") -int main() { - char line[1024]; +#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; char *scheme=0; char *username=0; char *password=0; @@ -38,104 +63,190 @@ int main() { char *path=0; char *query_string=0; char *fragment_id=0; + //exactly 8 parts! let's store that in a byte. + unsigned char flags=0; int i; - fgets(line,sizeof(line),stdin); - for(i=0;line[i] && line[i] != '\n' && line[i] != '\r';i++); - line[i]=0; - - //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; + int size=1024; + char using_stdin=1; + char malloced=0; + if(argc > 0) { + 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; } } + while(1) { + // + // todo: add argument parsing + // + if(!using_stdin) flags=0; + if(argc > 0) { + for(argc--,argv++;argc>0;argc--,argv++) { + for(i=0;long_opts[i];i++) { + if(!strncmp(*argv,"--",2)) { + if(!strcmp(*argv+2,long_opts[i])) { + flags|=(1<<i); + break; + } + } + else if(**argv=='-') { + if(*(*argv+1)==*short_opts[i]) { + flags|=(1<<i); + break; + } + } + } + if(long_opts[i]) continue; + line=*argv; + using_stdin=0; + argc--; + argv++; + break; + } + } + if(using_stdin) { + line=malloc(size); + 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; - if(path) { - if(strchr(path,'?')) { - query_string=strchr(path,'?'); - *query_string=0; - query_string++; + //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(query_string) { - if(strchr(query_string,'#')) { - fragment_id=strchr(query_string,'#'); - *fragment_id=0; - fragment_id++; + if(path) { + if(strchr(path,'?')) { + query_string=strchr(path,'?'); + *query_string=0; + query_string++; + } } - } - if(strstr(line,"://")) { - scheme=line; - domain=strstr(line,"://"); - *domain=0; - domain+=3; - } else { - domain=line; - } + if(query_string) { + if(strchr(query_string,'#')) { + fragment_id=strchr(query_string,'#'); + *fragment_id=0; + fragment_id++; + } + } - if(domain) { - if(strchr(domain,'@')) { - username=domain; - domain=strchr(domain,'@'); + if(strstr(line,"://")) { + scheme=line; + domain=strstr(line,"://"); *domain=0; - domain++; + domain+=3; + } else { + domain=line; } - } - if(username) { - if(strchr(username,':')) { - password=strchr(username,':'); - *password=0; - password++; + if(domain) { + if(strchr(domain,'@')) { + username=domain; + domain=strchr(domain,'@'); + *domain=0; + domain++; + } } - } - if(domain) { - if(strchr(domain,']')) { - if(strchr(strchr(domain,']'),':')) { - port=strchr(strchr(domain,']'),':'); - *port=0; - port++; + if(username) { + if(strchr(username,':')) { + password=strchr(username,':'); + *password=0; + password++; } - } else { - if(strchr(domain,':')) { - port=strchr(domain,':'); - *port=0; - port++; + } + + if(domain) { + if(strchr(domain,']')) { + if(strchr(strchr(domain,']'),':')) { + port=strchr(strchr(domain,']'),':'); + *port=0; + port++; + } + } else { + if(strchr(domain,':')) { + port=strchr(domain,':'); + *port=0; + port++; + } } } - } - if(domain) { - if(strchr(domain,'?')) { - query_string=strchr(domain,'?'); - *query_string=0; - query_string++; + if(domain) { + if(strchr(domain,'?')) { + query_string=strchr(domain,'?'); + *query_string=0; + query_string++; + } } - } - if(!scheme && username) { - scheme=username; - username=password; - password=0; - } + if(!scheme && username) { + scheme=username; + username=password; + password=0; + } + + if(!domain) { + fprintf(stderr,"how the fuck is this supposed to be a URL?\n"); + return 1; + } + // 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,DEFAULT_SCHEME); + username=AorB(username,DEFAULT_USERNAME); + password=AorB(password,DEFAULT_PASSWORD); + //domain=domain; doesn't change. heh. + port=AorB(port,DEFAULT_PORT); + path=AorB(path,DEFAULT_PATH); + query_string=AorB(query_string,DEFAULT_QUERY_STRING); + fragment_id=AorB(fragment_id,DEFAULT_FRAGMENT_ID); -// printf("scheme://username:password@domain:port/path?query_string#fragment_id\n\n"); - 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(flags) { + if(flags&F_SCHEME) printf("%s\n",scheme); + if(flags&F_USERNAME) printf("%s\n",username); + if(flags&F_PASSWORD) printf("%s\n",password); + if(flags&F_DOMAIN) printf("%s\n",domain); + if(flags&F_PORT) printf("%s\n",port); + if(flags&F_PATH) printf("%s\n",path); + if(flags&F_QUERY_STRING) printf("%s\n",query_string); + if(flags&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; + } else { + line=0; + } + } return 0; } |