From 1369c7ca98eac845b1093881a0539c2637eadf4e Mon Sep 17 00:00:00 2001 From: Antoni Sawicki Date: Thu, 23 May 2019 02:43:46 -0700 Subject: use regexp to process url files --- fileio.c | 62 +++++++++++++++++++++----------------------------------------- 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/fileio.c b/fileio.c index 3ff3d31..ad4c3de 100644 --- a/fileio.c +++ b/fileio.c @@ -221,60 +221,40 @@ void edit_save(void) { // -// Go To URL called by action=goto_url -// -// TODO: Support for webloc and other formats +// Go To URL - Process .url / .desktop / .webloc / etc files +// Called by action=goto_url // void goto_url(void) { FILE *in; - const char urlstr[] = "[InternetShortcut]"; - const char dskstr[] = "[Desktop Entry]"; - const char xmlstr[] = "URL.*(.+)", REG_EXTENDED | REG_ICASE)!=0 || + regcomp(&r_wb, ".*bplist.*URL_.*([[:print:]]+).*", REG_EXTENDED | REG_ICASE)!=0) + error("Unable to compile regex."); + in=fopen(wp.phys_filename, "rb"); if(!in) error("Unable to open file.
%s", strerror(errno)); - //rd=fread(chkbuf, sizeof(chkbuf), 1, in); - - if(!fgets(chkbuf, sizeof(chkbuf), in)) - error("Unable to read or not a link file
%s
", wp.virt_filename); - - if( - strncmp(chkbuf, urlstr, strlen(urlstr))!=0 && - strncmp(chkbuf, dskstr, strlen(dskstr))!=0 && - strncmp(chkbuf, xmlstr, strlen(xmlstr))!=0 - ) - error("Not a URL file"); - - // process link by looking for url - buff=calloc(1024, 1); - - while(fgets(buff, 1024, in)) { - if(strncasecmp(buff, "URL=", 4)==0) { - buff+=4; - found=1; - break; - } - else if(strncasecmp(buff, "RL=", 3)==0) { - buff+=3; - found=1; - break; - } - } - + fread(buff, sizeof(buff), 1, in); fclose(in); - if(!found) - error("No URL spec found in file
", wp.virt_filename); + if(regexec(&r_is, buff, 2, m, 0) && + regexec(&r_de, buff, 2, m, 0) && + regexec(&r_wl, buff, 2, m, 0) && + regexec(&r_wb, buff, 2, m, 0) ) + error("No URL spec found in file
%s", wp.virt_filename); + + if(m[1].rm_eo-m[1].rm_so > sizeof(buff)-1) + error("Regex match too long"); - redirect("%s", buff); + redirect("%.*s", m[1].rm_eo-m[1].rm_so, buff+m[1].rm_so); } -- cgit v1.2.3