Ejemplo n.º 1
0
WildcardMatcher *begin_wildcard_matching(char *name)
{
	HANDLE h;
	WIN32_FIND_DATA fdat;
	WildcardMatcher *ret;
	char *last;

	h = FindFirstFile(name, &fdat);
	if (h == INVALID_HANDLE_VALUE)
		return NULL;

	ret = snew(WildcardMatcher);
	ret->h = h;
	ret->srcpath = dupstr(name);
	last = stripslashes(ret->srcpath, 1);
	*last = '\0';
	if (fdat.cFileName[0] == '.' &&
		(fdat.cFileName[1] == '\0' ||
		(fdat.cFileName[1] == '.' && fdat.cFileName[2] == '\0')))
		ret->name = NULL;
	else
		ret->name = dupcat(ret->srcpath, fdat.cFileName, NULL);

	return ret;
}
Ejemplo n.º 2
0
CSGI::Response Dispatcher::operator()(CSGI::Env& env)
{
    CSGI::Application *y = handlers_[stripslashes(env["REQUEST_URI"])];
    if (y != NULL) {
        return (*y)(env);
    }
    return (*default_handler_)(env);
}
Ejemplo n.º 3
0
void tag(struct hash data) {
	char key, * tagstring;
	struct prompt setup = {
		.prompt = "Tags (comma separated): ",
		.line = NULL,
		.history = NULL,
		.callback = tagcomplete,
	};

	if(!data.content)
		return;

	fputs("Tag (a)rtist, a(l)bum, (t)rack or (c)ancel?\n", stderr);

	while(!strchr("altc", (key = fetchkey(2))));

	if(key == 'c')
		return;

	popular = merge(toptags(key, data), usertags(value(& rc, "username")), 0);

	setup.line = oldtags(key, data);

	assert((tagstring = strdup(readline(& setup))) != NULL);

	if(setup.line) {
		free(setup.line);
		setup.line = NULL;
	}

	purge(popular);
	popular = NULL;

	sendtag(key, tagstring, data);
	free(tagstring);
}


char * oldtags(char key, struct hash track) {
	unsigned length, x;
	char * tags = NULL, * url = calloc(512, sizeof(char)),
		 * user = NULL, * artist = NULL, * arg = NULL,
		 * file = NULL, ** resp;
	
	assert(url != NULL);
	
	switch(key) {
		case 'a':
			file = "artisttags.xml";
			break;
		case 'l':
			file = "albumtags.xml";
			break;
		case 't':
		default:
			file = "tracktags.xml";
	}

	encode(value(& track, "creator"), & artist);
	stripslashes(artist);

	encode(value(& rc, "username"), & user);

	length = snprintf(
			url, 512, "http://ws.audioscrobbler.com/1.0/user/%s/%s?artist=%s",
			user, file, artist);

	free(user);
	free(artist);

	if(key == 'l') {
		encode(value(& track, "album"), & arg);
		stripslashes(arg);
		length += snprintf(url + length, 512 - length, "&album=%s", arg);
	} else if(key == 't') {
		encode(value(& track, "title"), & arg);
		stripslashes(arg);
		length += snprintf(url + length, 512 - length, "&track=%s", arg);
	}

	if(arg)
		free(arg);

	resp = fetch(url, NULL, NULL, NULL);
	free(url);

	if(!resp)
		return NULL;

	for(x = 0, length = 0; resp[x]; ++x) {
		char * pbeg = strstr(resp[x], "<name>"), * pend;
		if(pbeg) {
			pbeg += 6;
			pend = strstr(pbeg, "</name>");
			if(pend) {
				char * thistag = strndup(pbeg, pend - pbeg);
				unsigned nlength = strlen(thistag) + length;

				assert(thistag != NULL);

				if(length)
					++nlength;

				tags = realloc(tags, nlength + 1);

				assert(tags != NULL);

				sprintf(tags + length, "%s%s", length ? "," : "", thistag);

				free(thistag);
				length = nlength;
			}
		}
	}

	purge(resp);

	return tags;
}
Ejemplo n.º 4
0
Archivo: psftp.c Proyecto: rdebath/sgt
/*
 * Send a file and store it at the remote end. We have two very
 * similar commands here: `put' and `reput', which differ in that
 * `reput' checks for the existence of the destination file and
 * starts from where a previous aborted transfer left off.
 */
int sftp_general_put(struct sftp_command *cmd, int restart)
{
    struct fxp_handle *fh;
    char *fname, *origoutfname, *outfname;
    uint64 offset;
    FILE *fp;
    int ret;

    if (back == NULL) {
	printf("psftp: not connected to a host; use \"open host.name\"\n");
	return 0;
    }

    if (cmd->nwords < 2) {
	printf("put: expects a filename\n");
	return 0;
    }

    fname = cmd->words[1];
    origoutfname = (cmd->nwords == 2 ?
		    stripslashes(cmd->words[1], 1) : cmd->words[2]);
    outfname = canonify(origoutfname);
    if (!outfname) {
	printf("%s: %s\n", origoutfname, fxp_error());
	return 0;
    }

    fp = fopen(fname, "rb");
    if (!fp) {
	printf("local: unable to open %s\n", fname);
	sfree(outfname);
	return 0;
    }
    if (restart) {
	fh = fxp_open(outfname,
		      SSH_FXF_WRITE);
    } else {
	fh = fxp_open(outfname,
		      SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
    }
    if (!fh) {
	printf("%s: %s\n", outfname, fxp_error());
	sfree(outfname);
	return 0;
    }

    if (restart) {
	char decbuf[30];
	struct fxp_attrs attrs;
	if (!fxp_fstat(fh, &attrs)) {
	    printf("read size of %s: %s\n", outfname, fxp_error());
	    sfree(outfname);
	    return 0;
	}
	if (!(attrs.flags & SSH_FILEXFER_ATTR_SIZE)) {
	    printf("read size of %s: size was not given\n", outfname);
	    sfree(outfname);
	    return 0;
	}
	offset = attrs.size;
	uint64_decimal(offset, decbuf);
	printf("reput: restarting at file position %s\n", decbuf);
	if (uint64_compare(offset, uint64_make(0, LONG_MAX)) > 0) {
	    printf("reput: remote file is larger than we can deal with\n");
	    sfree(outfname);
	    return 0;
	}
	if (fseek(fp, offset.lo, SEEK_SET) != 0)
	    fseek(fp, 0, SEEK_END);    /* *shrug* */
    } else {
	offset = uint64_make(0, 0);
    }

    printf("local:%s => remote:%s\n", fname, outfname);

    /*
     * FIXME: we can use FXP_FSTAT here to get the file size, and
     * thus put up a progress bar.
     */
    ret = 1;
    while (1) {
	char buffer[4096];
	int len;

	len = fread(buffer, 1, sizeof(buffer), fp);
	if (len == -1) {
	    printf("error while reading local file\n");
	    ret = 0;
	    break;
	} else if (len == 0) {
	    break;
	}
	if (!fxp_write(fh, buffer, offset, len)) {
	    printf("error while writing: %s\n", fxp_error());
	    ret = 0;
	    break;
	}
	offset = uint64_add32(offset, len);
    }

    fxp_close(fh);
    fclose(fp);
    sfree(outfname);

    return ret;
}
Ejemplo n.º 5
0
Archivo: psftp.c Proyecto: rdebath/sgt
/*
 * Get a file and save it at the local end. We have two very
 * similar commands here: `get' and `reget', which differ in that
 * `reget' checks for the existence of the destination file and
 * starts from where a previous aborted transfer left off.
 */
int sftp_general_get(struct sftp_command *cmd, int restart)
{
    struct fxp_handle *fh;
    char *fname, *outfname;
    uint64 offset;
    FILE *fp;
    int ret;

    if (back == NULL) {
	printf("psftp: not connected to a host; use \"open host.name\"\n");
	return 0;
    }

    if (cmd->nwords < 2) {
	printf("get: expects a filename\n");
	return 0;
    }

    fname = canonify(cmd->words[1]);
    if (!fname) {
	printf("%s: %s\n", cmd->words[1], fxp_error());
	return 0;
    }
    outfname = (cmd->nwords == 2 ?
		stripslashes(cmd->words[1], 0) : cmd->words[2]);

    fh = fxp_open(fname, SSH_FXF_READ);
    if (!fh) {
	printf("%s: %s\n", fname, fxp_error());
	sfree(fname);
	return 0;
    }

    if (restart) {
	fp = fopen(outfname, "rb+");
    } else {
	fp = fopen(outfname, "wb");
    }

    if (!fp) {
	printf("local: unable to open %s\n", outfname);
	fxp_close(fh);
	sfree(fname);
	return 0;
    }

    if (restart) {
	long posn;
	fseek(fp, 0L, SEEK_END);
	posn = ftell(fp);
	printf("reget: restarting at file position %ld\n", posn);
	offset = uint64_make(0, posn);
    } else {
	offset = uint64_make(0, 0);
    }

    printf("remote:%s => local:%s\n", fname, outfname);

    /*
     * FIXME: we can use FXP_FSTAT here to get the file size, and
     * thus put up a progress bar.
     */
    ret = 1;
    while (1) {
	char buffer[4096];
	int len;
	int wpos, wlen;

	len = fxp_read(fh, buffer, offset, sizeof(buffer));
	if ((len == -1 && fxp_error_type() == SSH_FX_EOF) || len == 0)
	    break;
	if (len == -1) {
	    printf("error while reading: %s\n", fxp_error());
	    ret = 0;
	    break;
	}

	wpos = 0;
	while (wpos < len) {
	    wlen = fwrite(buffer, 1, len - wpos, fp);
	    if (wlen <= 0) {
		printf("error while writing local file\n");
		ret = 0;
		break;
	    }
	    wpos += wlen;
	}
	if (wpos < len) {	       /* we had an error */
	    ret = 0;
	    break;
	}
	offset = uint64_add32(offset, len);
    }

    fclose(fp);
    fxp_close(fh);
    sfree(fname);

    return ret;
}
Ejemplo n.º 6
0
int main(int argc, const char * argv[])
{
  const string_t s1 = "Created by Cator VeeVee on 10/22/13 (cator)";
  const string_t s2 = "cator";
  const string_t s3 = "Cator";
  const string_t s4 = "Wei";
  
  string_t str;
  
  printf("s1 = '%s' \n", s1);
  printf("s2 = '%s' \n", s2);
  printf("s3 = '%s' \n", s3);
  printf("s4 = '%s' \n", s4);
  
  
  __("strpos"); {
    printf("strpos(s1, s2): %ld \n", strpos(s1, s2));
    printf("stripos(s1, s2): %ld \n", stripos(s1, s2));
    printf("strrpos(s1, s3): %ld \n", strrpos(s1, s3));
    printf("strripos(s1, s3): %ld \n", strripos(s1, s3));
    
    printf("strpos(s1, s4): %ld \n", strpos(s1, s4));
    printf("stripos(s1, s4): %ld \n", stripos(s1, s4));
    printf("strrpos(s1, s4): %ld \n", strrpos(s1, s4));
    printf("strripos(s1, s4): %ld \n", strripos(s1, s4));
  }
  
  
  __("copy_substr"); {
    str = copy_substr(s1, 11, 5);
    printf("copy_substr(s1, 11, 5): '%s'\n", str);
    free(str);
    
    str = copy_substr(s1, -6, 5);
    printf("copy_substr(s1, -6, 5): '%s'\n", str);
    free(str);
    
    str = copy_substr(s1, -6, -1);
    printf("copy_substr(s1, -6, -1): '%s'\n", str);
    free(str);
    
    str = copy_substr(s1, 34, 10000);
    printf("copy_substr(s1, 34, 10000): '%s'\n", str);
    free(str);
    
    str = copy_substr(s1, 34, 0);
    printf("copy_substr(s1, 34, 0): '%s'\n", str);
    free(str);
  }
  
  
  __("substr_count"); {
    printf("substr_count(s1, \"Vee\"): %ld\n", substr_count(s1, "Vee"));
    printf("substr_count(s1, \"Cator\"): %ld\n", substr_count(s1, "Cator"));
  }
  
  
  __("strcase"); {
    str = copy_str(s1);
    
    printf("strtolowwer(s1): '%s'\n", strtolower(&str));
    printf("strtoupper(s1): '%s'\n", strtoupper(&str));
    printf("lcfirst(s1): '%s'\n", lcfirst(&str));
    strtolower(&str);
    printf("ucfirst(s1): '%s'\n", ucfirst(&str));
    strtolower(&str);
    printf("ucwords(s1): '%s'\n", ucwords(&str));
    
    free(str);
  }
  
  
  __("trim"); {
    str = copy_str(" \t\v cator \r\n ");
    printf("ltrim(\" \\t\\v cator \\r\\n \"): '%s'\n", ltrim(&str));
    free(str);
    
    str = copy_str(" \t\v cator \r\n ");
    printf("rtrim(\" \\t\\v cator \\r\\n \"): '%s'\n", rtrim(&str));
    free(str);
    
    str = copy_str(" \t\v cator \r\n ");
    printf("trim(\" \\t\\v cator \\r\\n \"): '%s'\n", trim(&str));
    free(str);
  }
  
  
  __("repeat"); {
    str = copy_repeat("-=", 10);
    printf("copy_repeat(\"-=\", 10): '%s'\n", str);
    free(str);
    
    str = copy_strpad(s2, 20, "-=", STR_PAD_LEFT);
    printf("copy_strpad(s2, 20, \"-=\", STR_PAD_LEFT): '%s'\n", str);
    free(str);
    
    str = copy_strpad(s2, 20, "-=", STR_PAD_RIGHT);
    printf("copy_strpad(s2, 20, \"-=\", STR_PAD_RIGHT): '%s'\n", str);
    free(str);
    
    str = copy_strpad(s2, 20, "-=", STR_PAD_BOTH);
    printf("copy_strpad(s2, 20, \"-=\", STR_PAD_BOTH): '%s'\n", str);
    free(str);
  }
  
  
  __("replace"); {
    str = copy_str(s1);
    printf("strtr(&str, \"ctr\", \"CTR\"): '%s'\n", strtr(&str, "ctr", "CTR"));
    free(str);
    
    str = copy_replace("cator", "*name*", s1);
    printf("copy_replace(\"cator\", \"*name*\", s1): '%s'\n", str);
    free(str);
      
    str = copy_ireplace("cator", "*name*", s1);
    printf("copy_ireplace(\"cator\", \"*name*\", s1): '%s'\n", str);
    free(str);
  }
    
  __("html"); {
    string_t html = "<p>Test 'paragraph'.</p><!-- Comment --> <a href=\"#fragment\">Other text</a>";
    printf("html: ‘%s’\n", html);
    
    str = copy_str(html);
    printf("striptags(html): '%s'\n", striptags(&str));
    free(str);
    
    str = copy_htmlencode(html, false);
    printf("copy_htmlencode(html, false): '%s'\n", str);
    free(str);
    
    str = copy_htmlencode(html, true);
    printf("copy_htmlencode(html, true): '%s'\n", str);
    free(str);
    
    str = copy_htmlencode(html, false);
    printf("htmldecode(encoded_html): '%s'\n", htmldecode(&str));
    free(str);
  }
    
  __("url"); {
    string_t url = "http://*****:*****@catorv.com/index.html?name=cator&age=100#vee~";
    printf("url: '%s'\n", url);
    
    str = copy_urlencode(url);
    printf("copy_urlencode(url): '%s'\n", str);
    printf("urldecode(copy_urlencode(url)): '%s'\n", urldecode(&str));
    free(str);
    
    urlcompoments_t *components = copy_urlcompoments(url);
    if (components) {
      printf("copy_urlcompoments - scheme: '%s'\n", components->scheme);
      printf("copy_urlcompoments - user: '******'\n", components->user);
      printf("copy_urlcompoments - password: '******'\n", components->password);
      printf("copy_urlcompoments - host: '%s'\n", components->host);
      printf("copy_urlcompoments - port: %d\n", components->port);
      printf("copy_urlcompoments - path: '%s'\n", components->path);
      printf("copy_urlcompoments - query: '%s'\n", components->query);
      printf("copy_urlcompoments - fragment: '%s'\n", components->fragment);
      free_urlcomponents(components);
    }
  }
  
  __("base64"); {
    str = copy_base64encode(s1);
    printf("copy_base64encode(s1): '%s'\n", str);
    printf("base64decode(encoded_str): '%s'\n", base64decode(&str));
    free(str);
  }
  
  __("md5"); {
    md5context_t md5;
    unsigned char decrypt[16];
    size_t len = strlen(s2);
    int i;
    
    md5init(&md5);
    md5update(&md5, s2, len);
    md5final(decrypt, &md5);
    
    printf("copy_md5(s2): '");
    for(i = 0; i < 16; i++) {
      printf("%02x", decrypt[i]);
    }
    printf("'\n");
    
    // or
    
    str = copy_md5(s3);
    printf("copy_md5(s3): '%s'\n", str);
    free(str);
  }
  
  __("slashes"); {
    str = copy_addslashes("ab'cd\"dd\\...");
    printf("copy_addslashes: '%s'\n", str);
    printf("stripslashes: '%s'\n", stripslashes(&str));
    free(str);
  }
  
  return 0;
}
Ejemplo n.º 7
0
Archivo: vmd.C Proyecto: quolc/VMDLeap
// read in the startup script, execute it, and then execute any other commands
// which might be necessary (i.e. to load any molecules at start)
// This searches for the startup file in the following
// places (and in this order), reading only the FIRST one found:
//		1. Current directory
//		2. Home directory
//		3. 'Default' directory (here, /usr/local/vmd)
// If a name was given in the -startup switch, that file is checked for ONLY.
void VMDreadStartup(VMDApp *app) {
  char namebuf[512], *envtxt;
  int found = FALSE;
  FILE * tfp;
  char *DataPath; // path of last resort to find a .vmdrc file

  // These options were set by environment variables or command line options
  app->display_set_screen_height(displayHeight);
  app->display_set_screen_distance(displayDist);
  app->set_eofexit(eofexit);
  if (showTitle == TITLE_ON && which_display != DISPLAY_TEXT) {
    app->display_titlescreen();
  }

  if((envtxt = getenv("VMDDIR")) != NULL)
    DataPath = stringdup(envtxt);
  else
    DataPath = stringdup(DEF_VMDENVVAR);
  stripslashes(DataPath); // strip out ending '/' chars.

  // check if the file is available
  if(startupFileStr) {	// name specified by -startup
    if((tfp = fopen(startupFileStr, "rb")) != NULL) {
      found = TRUE;
      fclose(tfp);
      strcpy(namebuf, startupFileStr);
    }
  } else {	// search in different directories, for default file
    const char *def_startup = VMD_STARTUP;
    // first, look in current dir
    strcpy(namebuf, def_startup);
    if((tfp = fopen(namebuf, "rb")) != NULL) {
      found = TRUE;
      fclose(tfp);
    } else {
      // not found in current dir; look in home dir
      if((envtxt = getenv("HOME")) != NULL)
        strcpy(namebuf,envtxt);
      else
        strcpy(namebuf,".");
      strcat(namebuf,"/");
      strcat(namebuf,def_startup);
      if((tfp = fopen(namebuf, "rb")) != NULL) {
        found = TRUE;
        fclose(tfp);
      } else {
        // not found in home dir; look in default dir
	strcpy(namebuf, DataPath);
	strcat(namebuf,"/");
	strcat(namebuf, def_startup);
        if((tfp = fopen(namebuf, "rb")) != NULL) {
          found = TRUE;
          fclose(tfp);
	}
      }
    }
  }
  delete [] DataPath; DataPath = NULL;

  //
  // execute any commands needed at start
  //
  
  // read in molecules requested via command-line switches
  FileSpec spec;
  spec.waitfor = -1; // wait for all files to load before proceeding
  int molid = -1;    // set sentinel value to determine if files were loaded

  if (startNewMoleculeFlags.num() > 0) {
    msgInfo << "File loading in progress, please wait." << sendmsg;
  }

  for (int i=0; i<startNewMoleculeFlags.num(); i++) {
    const char *filename = initFilenames[i];
    const char *filetype = initFiletypes[i];
    if (!filetype) {
      filetype = app->guess_filetype(filename);
      if (!filetype) {
        // assume pdb 
        msgErr << "Unable to determine file type for file '"
          << filename << "'.  Assuming pdb." << sendmsg;
        filetype = "pdb";
      }
    }
    if (startNewMoleculeFlags[i]) {
      molid = app->molecule_load(-1, filename, filetype, &spec);
    } else {
      molid = app->molecule_load(molid, filename, filetype, &spec);
    }
    if (molid < 0) {
      msgErr  << "Loading of startup molecule files aborted." << sendmsg;
      break;
    }
  }

  // if the startup file was found, read in the commands there
  if(found) {
    app->logfile_read(namebuf);
  }

  // Load the extension packages here, _after_ reading the .vmdrc file,
  // so that the search path for extensions can be customized.
  app->commandQueue->runcommand(
    new TclEvalEvent("vmd_load_extension_packages"));   
  
  // Switch to Python if requested, before reading beginCmdFile
  if (cmdFileUsesPython) {
    if (!app->textinterp_change("python")) {
      // bail out since Python scripts won't be readable by Tcl.
      msgErr << "Skipping startup script because Python could not be started." 
             << sendmsg;
      return;
    }
  }
  // after reading in startup file and loading any molecule, the file
  // specified by the -e option is set up to be executed.  
  if(beginCmdFile) {
    app->logfile_read(beginCmdFile);
  } 
}
Ejemplo n.º 8
0
void Dispatcher::add_handler(std::string s, CSGI::Application * a)
{
    handlers_[stripslashes(s)] = a;
}