예제 #1
0
void dij_write( long int word )
{
   if( write_buffer == 0 ) { open_buffer(); }
   write_buffer->last->data[write_buffer->last->size] = word;
   write_buffer->last->size++;
   if( write_buffer->last->size == 1024 )
   {
      write_buffer->last->next = malloc(sizeof(struct _buffer_chunk));
      write_buffer->last = write_buffer->last->next;
      write_buffer->last->size = 0;
      write_buffer->last->next = 0;
   }
}
예제 #2
0
void dij_push_code()
   {
   struct _context_stack *f;
   printf("enter push_code\n");
   open_buffer();
   f = (struct _context_stack *)malloc(sizeof(struct _context_stack));
   f->num_parameters = 0;
   f->num_locals = 0;
   f->num_returns = 0;
   f->parameters = 0;
   f->locals = 0;
   f->returns = 0;
   f->under = write_code;
   write_code = f;
   printf("exit push_code\n");
   }
예제 #3
0
파일: curlhelpers.c 프로젝트: andoma/libsvc
ntv_t *
libsvc_http_json_get(const char *url, const char *auth,
                     char *errbuf, size_t errlen)
{
  cache_entry_t *ce;
  static cache_entry_t *skel;
  time_t now = time(NULL);

  pthread_mutex_lock(&cache_mutex);

  if(skel == NULL)
    skel = calloc(1, sizeof(cache_entry_t));

  skel->ce_url = (char *)url;
  skel->ce_auth = (char *)auth;

  ce = RB_INSERT_SORTED(&cache_entries, skel, ce_link,
                        cache_entry_cmp);

  if(ce == NULL) {
    // Nothing found -> New item 'skel' was inserted
    ce = skel;
    skel = NULL;

    ce->ce_url  = strdup(url);
    ce->ce_auth = auth ? strdup(auth) : NULL;
  }

  while(ce->ce_status == -1)
    pthread_cond_wait(&cache_cond, &cache_mutex);

  if(ce->ce_expire > now) {

    if(ce->ce_status == 200) {
      ntv_t *m = ntv_json_deserialize(ce->ce_response, errbuf, errlen);
      pthread_mutex_unlock(&cache_mutex);
      return m;
    }

    if(ce->ce_status >= 400) {
      snprintf(errbuf, errlen, "HTTP Error %d", ce->ce_status);
      pthread_mutex_unlock(&cache_mutex);
      return NULL;
    }
  }

  ce->ce_status = -1;


  char *out;
  size_t outlen;
  FILE *f = open_buffer(&out, &outlen);

  struct curl_slist *slist = NULL;

  CURL *curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_URL, url);
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
  curl_easy_setopt(curl, CURLOPT_USERAGENT, "libsvc");
  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, hdrfunc);
  curl_easy_setopt(curl, CURLOPT_HEADERDATA, ce);

  slist = curl_slist_append(slist, "Accept: application/json");

  if(auth != NULL)
    slist = curl_slist_append(slist, tsprintf("Authorization: %s", auth));

  if(ce->ce_etag != NULL)
    slist = curl_slist_append(slist, tsprintf("If-None-Match: %s",
                                              ce->ce_etag));
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);

  ce->ce_expire = 0;
  free(ce->ce_etag);
  ce->ce_etag = NULL;

  pthread_mutex_unlock(&cache_mutex);
  CURLcode result = curl_easy_perform(curl);
  curl_slist_free_all(slist);

  pthread_mutex_lock(&cache_mutex);

  pthread_cond_broadcast(&cache_cond);

  fwrite("", 1, 1, f);
  fclose(f);

  if(result) {
    snprintf(errbuf, errlen, "%s", curl_easy_strerror(result));
    curl_easy_cleanup(curl);
    ce->ce_expire = 0;
    ce->ce_status = 0;

    free(ce->ce_response);
    ce->ce_response = NULL;

    pthread_mutex_unlock(&cache_mutex);
    free(out);
    return NULL;
  }

  long http_code = 0;
  curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
  ce->ce_status = http_code;

  if(http_code == 304) {

    ce->ce_status = 200;

  } else if(http_code == 200) {

    free(ce->ce_response);
    ce->ce_response = out;
    out = NULL;

  } else {

    snprintf(errbuf, errlen, "HTTP Error %lu", http_code);
    free(ce->ce_response);
    ce->ce_response = NULL;

  }

  free(out);
  curl_easy_cleanup(curl);

  ntv_t *m = NULL;
  if(ce->ce_response != NULL)
    m = ntv_json_deserialize(ce->ce_response, errbuf, errlen);

  pthread_mutex_unlock(&cache_mutex);
  return m;
}
예제 #4
0
파일: fileops.c 프로젝트: cbmeeks/sd2iec
/**
 * file_open - open a file on given secondary
 * @secondary: secondary address used in OPEN call
 *
 * This function opens the file named in command_buffer on the given
 * secondary address. All special names and prefixes/suffixed are handled
 * here, e.g. $/#/@/,S,W
 */
void file_open(uint8_t secondary) {
  buffer_t *buf;
  uint8_t i = 0;
  uint8_t recordlen = 0;

  /* If the secondary is already in use, close the existing buffer */
  buf = find_buffer(secondary);
  if (buf != NULL) {
    /* FIXME: What should we do if an error occurs? */
    cleanup_and_free_buffer(buf);
  }

  /* Assume everything will go well unless proven otherwise */
  set_error(ERROR_OK);

  /* Strip 0x0d characters from end of name (C2BD-C2CA) */
  if (command_length > 1) {
    if (command_buffer[command_length-1] == 0x0d)
      command_length -= 1;
    else if (command_buffer[command_length-2] == 0x0d)
      command_length -= 2;
  }

  /* Clear the remainder of the command buffer, simplifies parsing */
  memset(command_buffer+command_length, 0, sizeof(command_buffer)-command_length);

  uart_trace(command_buffer,0,command_length);

  /* Direct access? */
  if (command_buffer[0] == '#') {
    open_buffer(secondary);
    return;
  }

  /* Parse type+mode suffixes */
  uint8_t *ptr = command_buffer;
  enum open_modes mode = OPEN_READ;
  uint8_t filetype = TYPE_DEL;

  while(i++ < 2 && *ptr && (ptr = ustrchr(ptr, ','))) {
    *ptr = 0;
    ptr++;
    switch (*ptr) {
    case 0:
      break;

    case 'R': /* Read */
      mode = OPEN_READ;
      break;

    case 'W': /* Write */
      mode = OPEN_WRITE;
      break;

    case 'A': /* Append */
      mode = OPEN_APPEND;
      break;

    case 'M': /* Modify */
      mode = OPEN_MODIFY;
      break;

    case 'D': /* DEL */
      filetype = TYPE_DEL;
      break;

    case 'S': /* SEQ */
      filetype = TYPE_SEQ;
      break;

    case 'P': /* PRG */
      filetype = TYPE_PRG;
      break;

    case 'U': /* USR */
      filetype = TYPE_USR;
      break;

    case 'L': /* REL */
      filetype = TYPE_REL;
      mode = OPEN_WRITE;
      if((ptr = ustrchr(ptr, ',')))
        recordlen = *(++ptr);
      i = 2;  // stop the scan
      break;
    }
  }

  /* Load directory? */
  if (command_buffer[0] == '$') {
    load_directory(secondary);
    return;
  }

  /* Parse path+partition numbers */
  uint8_t *fname;
  int8_t res;
  cbmdirent_t dent;
  path_t path;

  /* Parse path and file name */
  if (parse_path(command_buffer, &path, &fname, 0))
      return;

#ifdef CONFIG_M2I
  /* For M2I only: Remove trailing spaces from name */
  if (partition[path.part].fop == &m2iops) {
    res = ustrlen(fname);
    while (--res && fname[res] == ' ')
      fname[res] = 0;
  }
#endif

  /* Filename matching */
  if (opendir(&matchdh, &path))
    return;

  do {
    res = next_match(&matchdh, fname, NULL, NULL, FLAG_HIDDEN, &dent);
    if (res > 0)
      /* Error, abort */
      return;

    /* Don't match on DEL or DIR */
    if ((dent.typeflags & TYPE_MASK) != TYPE_DEL &&
        (dent.typeflags & TYPE_MASK) != TYPE_DIR)
      break;

    /* But do match if it's for writing */
    if (mode == OPEN_WRITE || secondary == 1)
      break;
  } while (res == 0);

  if(res && filetype == TYPE_REL && !recordlen) {
    set_error(ERROR_SYNTAX_UNABLE);
    return;
  }

  /* If match found is a REL... */
  if(!res && (dent.typeflags & TYPE_MASK) == TYPE_REL) {
    /* requested type must be REL or DEL */
    if(filetype != TYPE_REL && filetype != TYPE_DEL) {
      set_error(ERROR_FILE_TYPE_MISMATCH);
      return;
    }
    filetype = TYPE_REL;
    mode = OPEN_MODIFY;
  }

  /* Force mode+type for secondaries 0/1 */
  switch (secondary) {
  case 0:
    mode = OPEN_READ;
    if (filetype == TYPE_DEL)
      filetype = TYPE_PRG;
    break;

  case 1:
    mode = OPEN_WRITE;
    if (filetype == TYPE_DEL)
      filetype = TYPE_PRG;
    break;

  default:
    if (filetype == TYPE_DEL)
      filetype = TYPE_SEQ;
  }

  if (mode == OPEN_WRITE) {
    if (res == 0) {
      /* Match found */
      if (command_buffer[0] == '@') {
        /* Make sure there is a free buffer to open the new file later */
        if (!check_free_buffers()) {
          set_error(ERROR_NO_CHANNEL);
          return;
        }

        /* Copy dent because file_delete may change it */
        cbmdirent_t dentcopy = dent;

        /* Rewrite existing file: Delete the old one */
        if (file_delete(&path, &dentcopy) == 255)
          return;

        /* Force fatops to create a new name based on the (long) CBM- */
        /* name instead of creating one with the old SFN and no LFN.  */
        if (dent.opstype == OPSTYPE_FAT || dent.opstype == OPSTYPE_FAT_X00)
          dent.pvt.fat.realname[0] = 0;
      } else {
        /* Write existing file without replacement: Raise error */
        set_error(ERROR_FILE_EXISTS);
        return;
      }
    } else {
      /* Normal write or non-existing rewrite */
      /* Doesn't exist: Copy name to dent */
      memset(&dent, 0, sizeof(dent));
      ustrncpy(dent.name, fname, CBM_NAME_LENGTH);
      set_error(ERROR_OK); // because first_match has set FNF
    }
  } else if (res != 0) {
    /* File not found */
    set_error(ERROR_FILE_NOT_FOUND);
    return;
  }

  /* Grab a buffer */
  buf = alloc_buffer();
  if (!buf)
    return;

  buf->secondary = secondary;

  if(filetype == TYPE_REL) {
    display_filename_write(path.part,CBM_NAME_LENGTH,dent.name);
    open_rel(&path, &dent, buf, recordlen, (mode == OPEN_MODIFY));
    return;
  }

  switch (mode) {
  case OPEN_MODIFY:
  case OPEN_READ:
    /* Modify is the same as read, but allows reading *ed files.        */
    /* FAT doesn't have anything equivalent, so both are mapped to READ */
    display_filename_read(path.part,CBM_NAME_LENGTH,dent.name);
    open_read(&path, &dent, buf);
    break;

  case OPEN_WRITE:
  case OPEN_APPEND:
    display_filename_write(path.part,CBM_NAME_LENGTH,dent.name);
    open_write(&path, &dent, filetype, buf, (mode == OPEN_APPEND));
    break;
  }
}
예제 #5
0
파일: parse.c 프로젝트: ASpade/mulk
mulk_type_return_t init_url(CURLM *cm)
{
	int i;
	url_list_t *url;
	UriUriA *uri;
#ifdef ENABLE_METALINK
	int header;
	chunk_t *chunk;
	metalink_resource_list_t *resource;
#endif
	char *str_url = NULL;
	CURL *eh;
	
	/* we need at least a free buffer */
	if (get_buffer(NULL) < 0)
		return MULK_RET_ERR;

#ifdef ENABLE_METALINK
	if ((url = search_next_url(&uri, &chunk, &resource, &header)) == NULL)
#else
	if ((url = search_next_url(&uri)) == NULL)
#endif
		return MULK_RET_ERR;

	if ((eh = curl_easy_init()) == NULL)
		return MULK_RET_ERR;

#ifdef ENABLE_METALINK
	i = open_buffer(eh, url, uri, chunk, resource, header);
#else
	i = open_buffer(eh, url, uri);
#endif

	str_url = uri2string(uri);

#ifdef ENABLE_METALINK
	if (header) {
		curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, NULL);
		curl_easy_setopt(eh, CURLOPT_WRITEDATA, NULL);
		curl_easy_setopt(eh, CURLOPT_NOBODY, 1L);
	}
	else
#endif /* ENABLE_METALINK */
	{
		curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, write_data_cb);
		curl_easy_setopt(eh, CURLOPT_WRITEDATA, &(buffer_array[i]));
		curl_easy_setopt(eh, CURLOPT_NOBODY, 0L);

#ifdef ENABLE_METALINK
		if (chunk) {
		    char *range = NULL;

			string_printf(&range, "%" PRIdMAX "-%" PRIdMAX, (intmax_t) chunk->start, 
				(intmax_t) (chunk->start + chunk->length - 1));

			curl_easy_setopt(eh, CURLOPT_RANGE, range);

			string_free(range);
		}
#endif /* ENABLE_METALINK */
	}

	curl_easy_setopt(eh, CURLOPT_HEADER, 0L);
	curl_easy_setopt(eh, CURLOPT_USERAGENT, option_values.user_agent ? option_values.user_agent : "Mulk/" VERSION);
	curl_easy_setopt(eh, CURLOPT_URL, str_url);
	curl_easy_setopt(eh, CURLOPT_PRIVATE, str_url);
	curl_easy_setopt(eh, CURLOPT_ENCODING, "");
	curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L);
	curl_easy_setopt(eh, CURLOPT_NOPROGRESS, 1L);
	curl_easy_setopt(eh, CURLOPT_CONNECTTIMEOUT, 30L);
	curl_easy_setopt(eh, CURLOPT_LOW_SPEED_LIMIT, 100L);
	curl_easy_setopt(eh, CURLOPT_LOW_SPEED_TIME, 30L);
	curl_easy_setopt(eh, CURLOPT_SSL_VERIFYPEER, 0L);
	curl_easy_setopt(eh, CURLOPT_SSL_VERIFYHOST, 0L);

	if (option_values.user || option_values.password) {
		char *usr_pwd = NULL;

		string_printf(&usr_pwd, "%s:%s", option_values.user ? option_values.user : "",
		   	option_values.password ? option_values.password : "");
		curl_easy_setopt(eh, CURLOPT_USERPWD, usr_pwd);

		string_free(usr_pwd);
	}

	if (option_values.proxy)
		curl_easy_setopt(eh, CURLOPT_PROXY, option_values.proxy);

	if (option_values.cookie)
		curl_easy_setopt(eh, CURLOPT_COOKIE, option_values.cookie);
	if (option_values.load_cookies)
		curl_easy_setopt(eh, CURLOPT_COOKIEFILE, option_values.load_cookies);
	if (option_values.save_cookies)
		curl_easy_setopt(eh, CURLOPT_COOKIEJAR, option_values.save_cookies);

	if (curl_multi_add_handle(cm, eh) != CURLM_OK)
		return MULK_RET_ERR;

	return MULK_RET_OK;
}
예제 #6
0
void dij_push_data()
{
   open_buffer();
}