예제 #1
0
static const response* add(const char* l)
{
  struct rule* r;
  
  if (*l != 'k' && *l != 'd' && *l != 'z' && *l != 'p' && *l != 'n') return 0;
  r = alloc_rule();
  r->code = *l++;

  if ((l = parse_pattern(l, ':', &r->sender)) != 0 && *l == ':')
    if ((l = parse_pattern(l+1, ':', &r->recipient)) != 0 && *l == ':')
      if ((l = parse_str(l+1, ':', &r->response)) != 0 && *l == ':')
	if ((l = parse_uint(l+1, ':', &r->databytes)) != 0)
	  if (*l == ':'
	      && (l = parse_str(l+1, ':', &r->relayclient)) != 0
	      && *l == ':')
	    parse_env(l+1, &r->environment);

  if (l == 0) return &resp_syntax;
  append_rule(r);

  /* Pre-load text files and pre-open CDB files */
  if (!try_load(&r->sender)) return &resp_erropenref;
  if (!try_load(&r->recipient)) return &resp_erropenref;
  return 0;
}
static void
print_playing_song_default (DBusGProxy *shell_proxy, DBusGProxy *player_proxy)
{
	gboolean errored;
	GHashTable *properties;
	char *string;
	GError *error = NULL;

	properties = get_playing_song_info (shell_proxy, player_proxy, &error);
	if (annoy (&error)) {
		return;
	}
	if (properties == NULL) {
		g_print ("%s\n", _("Not playing"));
		return;
	}

	if (g_hash_table_lookup (properties, "rb:stream-song-title") != NULL) {
		string = parse_pattern ("%st (%tt)", properties, 0);
	} else {
		string = parse_pattern ("%ta - %tt", properties, 0);
	}

	g_print ("%s\n", string);
	g_hash_table_destroy (properties);
	g_free (string);
}
예제 #3
0
static void
print_playing_song (GDBusProxy *mpris, const char *format)
{
	GHashTable *properties;
	GVariant *v;
	gint64 elapsed = 0;
	char *string;

	properties = get_playing_song_info (mpris);
	if (properties == NULL) {
		g_print ("%s\n", _("Not playing"));
		return;
	}

	v = g_dbus_proxy_get_cached_property (mpris, "Position");
	if (v != NULL) {
		elapsed = g_variant_get_int64 (v);
		g_variant_unref (v);
	}

	string = parse_pattern (format, properties, elapsed);
	g_print ("%s\n", string);
	g_hash_table_destroy (properties);
	g_free (string);
}
static void
print_playing_song (DBusGProxy *shell_proxy, DBusGProxy *player_proxy, const char *format)
{
	gboolean errored;
	GHashTable *properties;
	guint elapsed = 0;
	GError *error = NULL;
	char *string;

	properties = get_playing_song_info (shell_proxy, player_proxy, &error);
	if (annoy (&error)) {
		return;
	}
	if (properties == NULL) {
		g_print ("%s\n", _("Not playing"));
		return;
	}
	
	org_gnome_Rhythmbox_Player_get_elapsed (player_proxy, &elapsed, &error);
	annoy (&error);

	string = parse_pattern (format, properties, elapsed);
	g_print ("%s\n", string);
	g_hash_table_destroy (properties);
	g_free (string);
}
예제 #5
0
파일: qemu-io.c 프로젝트: AjayMashi/x-tier
static int aio_write_f(int argc, char **argv)
{
    int nr_iov, c;
    int pattern = 0xcd;
    struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);

    while ((c = getopt(argc, argv, "CqP:")) != EOF) {
        switch (c) {
        case 'C':
            ctx->Cflag = 1;
            break;
        case 'q':
            ctx->qflag = 1;
            break;
        case 'P':
            pattern = parse_pattern(optarg);
            if (pattern < 0) {
                g_free(ctx);
                return 0;
            }
            break;
        default:
            g_free(ctx);
            return command_usage(&aio_write_cmd);
        }
    }

    if (optind > argc - 2) {
        g_free(ctx);
        return command_usage(&aio_write_cmd);
    }

    ctx->offset = cvtnum(argv[optind]);
    if (ctx->offset < 0) {
        printf("non-numeric length argument -- %s\n", argv[optind]);
        g_free(ctx);
        return 0;
    }
    optind++;

    if (ctx->offset & 0x1ff) {
        printf("offset %" PRId64 " is not sector aligned\n",
               ctx->offset);
        g_free(ctx);
        return 0;
    }

    nr_iov = argc - optind;
    ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
    if (ctx->buf == NULL) {
        g_free(ctx);
        return 0;
    }

    gettimeofday(&ctx->t1, NULL);
    bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
                    ctx->qiov.size >> 9, aio_write_done, ctx);
    return 0;
}
예제 #6
0
파일: unsed.c 프로젝트: NinerLP/OS-2012
int main(int argc, char *argv[]){
	if (argc < 2){
		printf("No pattern\n");
		return -1;
	}
	struct RL* rl = rl_open(0,MAX_LEN);
	int i;
	char** patterns = malloc((argc-1)*sizeof(char*));
	char** repls = malloc((argc-1)*sizeof(char*));
	int* greeds = malloc((argc-1)*sizeof(int));
	char* str;
	for (i = 1; i < argc; i++){
		//get pattern
		strtok(argv[i], "/"); 
		char* pattern_tmp = strtok(NULL, "/");//skip s/
		patterns[i-1] = malloc(strlen(pattern_tmp)+1);
		strcpy(patterns[i-1], pattern_tmp);
		pattern_tmp = strtok(NULL, "/");
		repls[i-1] = malloc(strlen(pattern_tmp)+1);
		strcpy(repls[i-1], pattern_tmp);
		greeds[i-1] = (strtok(NULL, "/") != NULL);
		//printf("%s\n%s\n%d\n", patterns[i-1], repls[i-1], greeds[i-1]);
	}
	//test
	int res = rl_readline(rl, rl->buf, rl->buf_size);
	while (res!=0){
		if (res > 0){
			//do shit and write
			//printf("%s",rl->buf);
			str = malloc((res+1)*sizeof(char));
			strcpy(str,rl->buf);
			//printf("%s+%s\n", str,repls[i]);
			for (i = 0; i < argc-1; i++){
				//if (parse_pattern(str,patterns[i],repls[i],greeds[i]) == NULL){
				//	printf("NO F**K YOU");
				//}
				str = parse_pattern(str,patterns[i],repls[i],greeds[i]);
			}
			write(1,str,strlen(str));
			write(1,"\n",1);
			free(str);
		} else {
			break;
		}
		res = rl_readline(rl, rl->buf,rl->buf_size);
	} 
	
	for (i = 0; i < argc -1; i++){
		free(patterns[i]);
		free(repls[i]);
	}
	free(patterns);
	free(repls);
	free(greeds);
	return 0;
}
예제 #7
0
파일: parse.c 프로젝트: tylerberry/newts
int
parse_nf (char *string, List *list)
{
  newts_nfref *ref = nfref_alloc ();
  char *copy;

  if (string == NULL || list == NULL)
    return -1;

  copy = newts_strdup (string);

  if (!pattern_flag)
    sense = PARSE_ADD;

  if (*copy == ':')
    {
      return parse_file (copy + 1, list);
    }

  if (*copy == '!')
    {
      char *change = newts_strdup (copy + 1);
      newts_free (copy);
      copy = change;
      sense = PARSE_DELETE;
    }

  if (!pattern_flag &&
      (strchr (copy, '?') || strchr (copy, '[') || strchr (copy, '*') ||
       strchr (copy, ' ')))
    {
      /* Pattern matching. */

      int result;

      pattern_flag = TRUE;
      result = parse_pattern (copy, list);
      pattern_flag = FALSE;
      return result;
    }

  parse_single_nf (copy, ref);

  if (sense == PARSE_ADD)
    list_insert_next (list, list_tail (list), (void *) ref);
  else
    {
      list_remove_match (list, (void *) ref);
      nfref_free (ref);
    }

  newts_free (copy);

  return 0;
}
예제 #8
0
static inline struct simple_pattern *parse_pattern(char *str, SIMPLE_PREFIX_MODE default_mode) {
    // fprintf(stderr, "PARSING PATTERN: '%s'\n", str);

    SIMPLE_PREFIX_MODE mode;
    struct simple_pattern *child = NULL;

    char *s = str, *c = str;

    // skip asterisks in front
    while(*c == '*') c++;

    // find the next asterisk
    while(*c && *c != '*') c++;

    // do we have an asterisk in the middle?
    if(*c == '*' && c[1] != '\0') {
        // yes, we have
        child = parse_pattern(c, default_mode);
        c[1] = '\0';
    }

    // check what this one matches

    size_t len = strlen(s);
    if(len >= 2 && *s == '*' && s[len - 1] == '*') {
        s[len - 1] = '\0';
        s++;
        mode = SIMPLE_PATTERN_SUBSTRING;
    }
    else if(len >= 1 && *s == '*') {
        s++;
        mode = SIMPLE_PATTERN_SUFFIX;
    }
    else if(len >= 1 && s[len - 1] == '*') {
        s[len - 1] = '\0';
        mode = SIMPLE_PATTERN_PREFIX;
    }
    else
        mode = default_mode;

    // allocate the structure
    struct simple_pattern *m = callocz(1, sizeof(struct simple_pattern));
    if(*s) {
        m->match = strdupz(s);
        m->len = strlen(m->match);
        m->mode = mode;
    }
    else {
        m->mode = SIMPLE_PATTERN_SUBSTRING;
    }

    m->child = child;

    return m;
}
예제 #9
0
static void
print_playing_song_default (GDBusProxy *mpris)
{
	GHashTable *properties;
	char *string;

	properties = get_playing_song_info (mpris);
	if (properties == NULL) {
		g_print ("%s\n", _("Not playing"));
		return;
	}

	if (g_hash_table_lookup (properties, "rhythmbox:streamTitle") != NULL) {
		string = parse_pattern ("%st (%tt)", properties, 0);
	} else {
		string = parse_pattern ("%ta - %tt", properties, 0);
	}

	g_print ("%s\n", string);
	g_hash_table_destroy (properties);
	g_free (string);
}
예제 #10
0
bregonig *recompile_onig(bregonig *rxold, pattern_type type,
                         const TCHAR *ptn, TCHAR *msg)
{
    const TCHAR *patternp;
    const TCHAR *patternendp;
    const TCHAR *prerepp;
    const TCHAR *prerependp;
    const TCHAR *optionp;
    const TCHAR *optionendp;

    TRACE1(_T("recompile_onig(): %s\n"), ptn);
    type = parse_pattern(ptn, type, &patternp, &patternendp,
                         &prerepp, &prerependp, &optionp, &optionendp, msg);
    if (type == PTN_ERROR) {
        return NULL;
    }
    return recompile_onig_ex(rxold, type, ptn, patternp, patternendp,
                             prerepp, prerependp, optionp, optionendp, msg);
}
예제 #11
0
파일: qemu-io.c 프로젝트: AjayMashi/x-tier
static int read_f(int argc, char **argv)
{
    struct timeval t1, t2;
    int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
    int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
    int c, cnt;
    char *buf;
    int64_t offset;
    int count;
    /* Some compilers get confused and warn if this is not initialized.  */
    int total = 0;
    int pattern = 0, pattern_offset = 0, pattern_count = 0;

    while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
        switch (c) {
        case 'b':
            bflag = 1;
            break;
        case 'C':
            Cflag = 1;
            break;
        case 'l':
            lflag = 1;
            pattern_count = cvtnum(optarg);
            if (pattern_count < 0) {
                printf("non-numeric length argument -- %s\n", optarg);
                return 0;
            }
            break;
        case 'p':
            pflag = 1;
            break;
        case 'P':
            Pflag = 1;
            pattern = parse_pattern(optarg);
            if (pattern < 0) {
                return 0;
            }
            break;
        case 'q':
            qflag = 1;
            break;
        case 's':
            sflag = 1;
            pattern_offset = cvtnum(optarg);
            if (pattern_offset < 0) {
                printf("non-numeric length argument -- %s\n", optarg);
                return 0;
            }
            break;
        case 'v':
            vflag = 1;
            break;
        default:
            return command_usage(&read_cmd);
        }
    }

    if (optind != argc - 2) {
        return command_usage(&read_cmd);
    }

    if (bflag && pflag) {
        printf("-b and -p cannot be specified at the same time\n");
        return 0;
    }

    offset = cvtnum(argv[optind]);
    if (offset < 0) {
        printf("non-numeric length argument -- %s\n", argv[optind]);
        return 0;
    }

    optind++;
    count = cvtnum(argv[optind]);
    if (count < 0) {
        printf("non-numeric length argument -- %s\n", argv[optind]);
        return 0;
    }

    if (!Pflag && (lflag || sflag)) {
        return command_usage(&read_cmd);
    }

    if (!lflag) {
        pattern_count = count - pattern_offset;
    }

    if ((pattern_count < 0) || (pattern_count + pattern_offset > count))  {
        printf("pattern verification range exceeds end of read data\n");
        return 0;
    }

    if (!pflag) {
        if (offset & 0x1ff) {
            printf("offset %" PRId64 " is not sector aligned\n",
                   offset);
            return 0;
        }
        if (count & 0x1ff) {
            printf("count %d is not sector aligned\n",
                   count);
            return 0;
        }
    }

    buf = qemu_io_alloc(count, 0xab);

    gettimeofday(&t1, NULL);
    if (pflag) {
        cnt = do_pread(buf, offset, count, &total);
    } else if (bflag) {
        cnt = do_load_vmstate(buf, offset, count, &total);
    } else {
        cnt = do_read(buf, offset, count, &total);
    }
    gettimeofday(&t2, NULL);

    if (cnt < 0) {
        printf("read failed: %s\n", strerror(-cnt));
        goto out;
    }

    if (Pflag) {
        void *cmp_buf = g_malloc(pattern_count);
        memset(cmp_buf, pattern, pattern_count);
        if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
            printf("Pattern verification failed at offset %"
                   PRId64 ", %d bytes\n",
                   offset + pattern_offset, pattern_count);
        }
        g_free(cmp_buf);
    }

    if (qflag) {
        goto out;
    }

    if (vflag) {
        dump_buffer(buf, offset, count);
    }

    /* Finally, report back -- -C gives a parsable format */
    t2 = tsub(t2, t1);
    print_report("read", &t2, offset, count, total, cnt, Cflag);

out:
    qemu_io_free(buf);

    return 0;
}
예제 #12
0
static int
aio_read_f(int argc, char **argv)
{
    int nr_iov, c;
    struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
    BlockDriverAIOCB *acb;

    while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
        switch (c) {
        case 'C':
            ctx->Cflag = 1;
            break;
        case 'P':
            ctx->Pflag = 1;
            ctx->pattern = parse_pattern(optarg);
            if (ctx->pattern < 0)
                return 0;
            break;
        case 'q':
            ctx->qflag = 1;
            break;
        case 'v':
            ctx->vflag = 1;
            break;
        default:
            free(ctx);
            return command_usage(&aio_read_cmd);
        }
    }

    if (optind > argc - 2) {
        free(ctx);
        return command_usage(&aio_read_cmd);
    }

    ctx->offset = cvtnum(argv[optind]);
    if (ctx->offset < 0) {
        printf("non-numeric length argument -- %s\n", argv[optind]);
        free(ctx);
        return 0;
    }
    optind++;

    if (ctx->offset & 0x1ff) {
        printf("offset %" PRId64 " is not sector aligned\n",
               ctx->offset);
        free(ctx);
        return 0;
    }

    nr_iov = argc - optind;
    ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);

    gettimeofday(&ctx->t1, NULL);
    acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
                         ctx->qiov.size >> 9, aio_read_done, ctx);
    if (!acb) {
        free(ctx->buf);
        free(ctx);
        return -EIO;
    }

    return 0;
}
예제 #13
0
파일: complete.c 프로젝트: Jaharmi/zsh
mod_export Cmatcher
parse_cmatcher(char *name, char *s)
{
    Cmatcher ret = NULL, r = NULL, n;
    Cpattern line, word, left, right;
    int fl, fl2, ll, wl, lal, ral, err, both;

    if (!*s)
	return NULL;

    while (*s) {
	lal = ral = both = fl2 = 0;
	left = right = NULL;

	while (*s && inblank(*s)) s++;

	if (!*s) break;

	switch (*s) {
	case 'b': fl2 = CMF_INTER;
	case 'l': fl = CMF_LEFT; break;
	case 'e': fl2 = CMF_INTER;
	case 'r': fl = CMF_RIGHT; break;
	case 'm': fl = 0; break;
	case 'B': fl2 = CMF_INTER;
	case 'L': fl = CMF_LEFT | CMF_LINE; break;
	case 'E': fl2 = CMF_INTER;
	case 'R': fl = CMF_RIGHT | CMF_LINE; break;
	case 'M': fl = CMF_LINE; break;
	default:
	    if (name)
		zwarnnam(name, "unknown match specification character `%c'",
			 *s);
	    return pcm_err;
	}
	if (s[1] != ':') {
	    if (name)
		zwarnnam(name, "missing `:'");
	    return pcm_err;
	}
	s += 2;
	if (!*s) {
	    if (name)
		zwarnnam(name, "missing patterns");
	    return pcm_err;
	}
	if ((fl & CMF_LEFT) && !fl2) {
	    left = parse_pattern(name, &s, &lal, '|', &err);
	    if (err)
		return pcm_err;

	    if ((both = (*s && s[1] == '|')))
		s++;

	    if (!*s || !*++s) {
		if (name)
		    zwarnnam(name, "missing line pattern");
		return pcm_err;
	    }
	} else
	    left = NULL;

	line = parse_pattern(name, &s, &ll,
			     (((fl & CMF_RIGHT) && !fl2) ? '|' : '='),
			     &err);
	if (err)
	    return pcm_err;
	if (both) {
	    right = line;
	    ral = ll;
	    line = NULL;
	    ll = 0;
	}
	if ((fl & CMF_RIGHT) && !fl2 && (!*s || !*++s)) {
	    if (name)
		zwarnnam(name, "missing right anchor");
	} else if (!(fl & CMF_RIGHT) || fl2) {
	    if (!*s) {
		if (name)
		    zwarnnam(name, "missing word pattern");
		return pcm_err;
	    }
	    s++;
	}
	if ((fl & CMF_RIGHT) && !fl2) {
	    if (*s == '|') {
		left = line;
		lal = ll;
		line = NULL;
		ll = 0;
		s++;
	    }
	    right = parse_pattern(name, &s, &ral, '=', &err);
	    if (err)
		return pcm_err;
	    if (!*s) {
		if (name)
		    zwarnnam(name, "missing word pattern");
		return pcm_err;
	    }
	    s++;
	} else
	    right = NULL;

	if (*s == '*') {
	    if (!(fl & (CMF_LEFT | CMF_RIGHT))) {
		if (name)
		    zwarnnam(name, "need anchor for `*'");
		return pcm_err;
	    }
	    word = NULL;
	    if (*++s == '*') {
		s++;
		wl = -2;
	    } else
		wl = -1;
	} else {
	    word = parse_pattern(name, &s, &wl, 0, &err);

	    if (!word && !line) {
		if (name)
		    zwarnnam(name, "need non-empty word or line pattern");
		return pcm_err;
	    }
	}
	if (err)
	    return pcm_err;

	n = (Cmatcher) hcalloc(sizeof(*ret));
	n->next = NULL;
	n->flags = fl | fl2;
	n->line = line;
	n->llen = ll;
	n->word = word;
	n->wlen = wl;
	n->left = left;
	n->lalen = lal;
	n->right = right;
	n->ralen = ral;

	if (ret)
	    r->next = n;
	else
	    ret = n;

	r = n;
    }
    return ret;
}
예제 #14
0
파일: qemu-io.c 프로젝트: AjayMashi/x-tier
static int writev_f(int argc, char **argv)
{
    struct timeval t1, t2;
    int Cflag = 0, qflag = 0;
    int c, cnt;
    char *buf;
    int64_t offset;
    /* Some compilers get confused and warn if this is not initialized.  */
    int total = 0;
    int nr_iov;
    int pattern = 0xcd;
    QEMUIOVector qiov;

    while ((c = getopt(argc, argv, "CqP:")) != EOF) {
        switch (c) {
        case 'C':
            Cflag = 1;
            break;
        case 'q':
            qflag = 1;
            break;
        case 'P':
            pattern = parse_pattern(optarg);
            if (pattern < 0) {
                return 0;
            }
            break;
        default:
            return command_usage(&writev_cmd);
        }
    }

    if (optind > argc - 2) {
        return command_usage(&writev_cmd);
    }

    offset = cvtnum(argv[optind]);
    if (offset < 0) {
        printf("non-numeric length argument -- %s\n", argv[optind]);
        return 0;
    }
    optind++;

    if (offset & 0x1ff) {
        printf("offset %" PRId64 " is not sector aligned\n",
               offset);
        return 0;
    }

    nr_iov = argc - optind;
    buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
    if (buf == NULL) {
        return 0;
    }

    gettimeofday(&t1, NULL);
    cnt = do_aio_writev(&qiov, offset, &total);
    gettimeofday(&t2, NULL);

    if (cnt < 0) {
        printf("writev failed: %s\n", strerror(-cnt));
        goto out;
    }

    if (qflag) {
        goto out;
    }

    /* Finally, report back -- -C gives a parsable format */
    t2 = tsub(t2, t1);
    print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
out:
    qemu_iovec_destroy(&qiov);
    qemu_io_free(buf);
    return 0;
}
예제 #15
0
static int
multiwrite_f(int argc, char **argv)
{
	struct timeval t1, t2;
	int Cflag = 0, qflag = 0;
	int c, cnt;
	char **buf;
	int64_t offset, first_offset = 0;
	
	int total = 0;
	int nr_iov;
	int nr_reqs;
	int pattern = 0xcd;
	QEMUIOVector *qiovs;
	int i;
	BlockRequest *reqs;

	while ((c = getopt(argc, argv, "CqP:")) != EOF) {
		switch (c) {
		case 'C':
			Cflag = 1;
			break;
		case 'q':
			qflag = 1;
			break;
		case 'P':
			pattern = parse_pattern(optarg);
			if (pattern < 0)
				return 0;
			break;
		default:
			return command_usage(&writev_cmd);
		}
	}

	if (optind > argc - 2)
		return command_usage(&writev_cmd);

	nr_reqs = 1;
	for (i = optind; i < argc; i++) {
		if (!strcmp(argv[i], ";")) {
			nr_reqs++;
		}
	}

	reqs = qemu_malloc(nr_reqs * sizeof(*reqs));
	buf = qemu_malloc(nr_reqs * sizeof(*buf));
	qiovs = qemu_malloc(nr_reqs * sizeof(*qiovs));

	for (i = 0; i < nr_reqs; i++) {
		int j;

		
		offset = cvtnum(argv[optind]);
		if (offset < 0) {
			printf("non-numeric offset argument -- %s\n", argv[optind]);
			return 0;
		}
		optind++;

		if (offset & 0x1ff) {
			printf("offset %lld is not sector aligned\n",
				(long long)offset);
			return 0;
		}

        if (i == 0) {
            first_offset = offset;
        }

		
		for (j = optind; j < argc; j++) {
			if (!strcmp(argv[j], ";")) {
				break;
			}
		}

		nr_iov = j - optind;

		
		reqs[i].qiov = &qiovs[i];
		buf[i] = create_iovec(reqs[i].qiov, &argv[optind], nr_iov, pattern);
		reqs[i].sector = offset >> 9;
		reqs[i].nb_sectors = reqs[i].qiov->size >> 9;

		optind = j + 1;

		offset += reqs[i].qiov->size;
		pattern++;
	}

	gettimeofday(&t1, NULL);
	cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
	gettimeofday(&t2, NULL);

	if (cnt < 0) {
		printf("aio_multiwrite failed: %s\n", strerror(-cnt));
		goto out;
	}

	if (qflag)
		goto out;

	
	t2 = tsub(t2, t1);
	print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
out:
	for (i = 0; i < nr_reqs; i++) {
		qemu_io_free(buf[i]);
		qemu_iovec_destroy(&qiovs[i]);
	}
	qemu_free(buf);
	qemu_free(reqs);
	qemu_free(qiovs);
	return 0;
}
예제 #16
0
static int
write_f(int argc, char **argv)
{
	struct timeval t1, t2;
	int Cflag = 0, pflag = 0, qflag = 0, bflag = 0;
	int c, cnt;
	char *buf;
	int64_t offset;
	int count;
        
        int total = 0;
	int pattern = 0xcd;

	while ((c = getopt(argc, argv, "bCpP:q")) != EOF) {
		switch (c) {
		case 'b':
			bflag = 1;
			break;
		case 'C':
			Cflag = 1;
			break;
		case 'p':
			pflag = 1;
			break;
		case 'P':
			pattern = parse_pattern(optarg);
			if (pattern < 0)
				return 0;
			break;
		case 'q':
			qflag = 1;
			break;
		default:
			return command_usage(&write_cmd);
		}
	}

	if (optind != argc - 2)
		return command_usage(&write_cmd);

	if (bflag && pflag) {
		printf("-b and -p cannot be specified at the same time\n");
		return 0;
	}

	offset = cvtnum(argv[optind]);
	if (offset < 0) {
		printf("non-numeric length argument -- %s\n", argv[optind]);
		return 0;
	}

	optind++;
	count = cvtnum(argv[optind]);
	if (count < 0) {
		printf("non-numeric length argument -- %s\n", argv[optind]);
		return 0;
	}

	if (!pflag) {
		if (offset & 0x1ff) {
                        printf("offset %" PRId64 " is not sector aligned\n",
                               offset);
			return 0;
		}

		if (count & 0x1ff) {
			printf("count %d is not sector aligned\n",
				count);
			return 0;
		}
	}

	buf = qemu_io_alloc(count, pattern);

	gettimeofday(&t1, NULL);
	if (pflag)
		cnt = do_pwrite(buf, offset, count, &total);
	else if (bflag)
		cnt = do_save_vmstate(buf, offset, count, &total);
	else
		cnt = do_write(buf, offset, count, &total);
	gettimeofday(&t2, NULL);

	if (cnt < 0) {
		printf("write failed: %s\n", strerror(-cnt));
		goto out;
	}

	if (qflag)
		goto out;

	
	t2 = tsub(t2, t1);
	print_report("wrote", &t2, offset, count, total, cnt, Cflag);

out:
	qemu_io_free(buf);

	return 0;
}
예제 #17
0
SIMPLE_PATTERN *simple_pattern_create(const char *list, const char *separators, SIMPLE_PREFIX_MODE default_mode) {
    struct simple_pattern *root = NULL, *last = NULL;

    if(unlikely(!list || !*list)) return root;

    int isseparator[256] = {
            [' '] = 1       // space
            , ['\t'] = 1    // tab
            , ['\r'] = 1    // carriage return
            , ['\n'] = 1    // new line
            , ['\f'] = 1    // form feed
            , ['\v'] = 1    // vertical tab
    };

    if (unlikely(separators && *separators)) {
        memset(&isseparator[0], 0, sizeof(isseparator));
        while(*separators) isseparator[(unsigned char)*separators++] = 1;
    }

    char *buf = mallocz(strlen(list) + 1);
    const char *s = list;

    while(s && *s) {
        buf[0] = '\0';
        char *c = buf;

        char negative = 0;

        // skip all spaces
        while(isseparator[(unsigned char)*s])
            s++;

        if(*s == '!') {
            negative = 1;
            s++;
        }

        // empty string
        if(unlikely(!*s))
            break;

        // find the next space
        char escape = 0;
        while(*s) {
            if(*s == '\\' && !escape) {
                escape = 1;
                s++;
            }
            else {
                if (isseparator[(unsigned char)*s] && !escape) {
                    s++;
                    break;
                }

                *c++ = *s++;
                escape = 0;
            }
        }

        // terminate our string
        *c = '\0';

        // if we matched the empty string, skip it
        if(unlikely(!*buf))
            continue;

        // fprintf(stderr, "FOUND PATTERN: '%s'\n", buf);
        struct simple_pattern *m = parse_pattern(buf, default_mode);
        m->negative = negative;

        // link it at the end
        if(unlikely(!root))
            root = last = m;
        else {
            last->next = m;
            last = m;
        }
    }

    freez(buf);
    return (SIMPLE_PATTERN *)root;
}
예제 #18
0
파일: qemu-io.c 프로젝트: AjayMashi/x-tier
static int readv_f(int argc, char **argv)
{
    struct timeval t1, t2;
    int Cflag = 0, qflag = 0, vflag = 0;
    int c, cnt;
    char *buf;
    int64_t offset;
    /* Some compilers get confused and warn if this is not initialized.  */
    int total = 0;
    int nr_iov;
    QEMUIOVector qiov;
    int pattern = 0;
    int Pflag = 0;

    while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
        switch (c) {
        case 'C':
            Cflag = 1;
            break;
        case 'P':
            Pflag = 1;
            pattern = parse_pattern(optarg);
            if (pattern < 0) {
                return 0;
            }
            break;
        case 'q':
            qflag = 1;
            break;
        case 'v':
            vflag = 1;
            break;
        default:
            return command_usage(&readv_cmd);
        }
    }

    if (optind > argc - 2) {
        return command_usage(&readv_cmd);
    }


    offset = cvtnum(argv[optind]);
    if (offset < 0) {
        printf("non-numeric length argument -- %s\n", argv[optind]);
        return 0;
    }
    optind++;

    if (offset & 0x1ff) {
        printf("offset %" PRId64 " is not sector aligned\n",
               offset);
        return 0;
    }

    nr_iov = argc - optind;
    buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
    if (buf == NULL) {
        return 0;
    }

    gettimeofday(&t1, NULL);
    cnt = do_aio_readv(&qiov, offset, &total);
    gettimeofday(&t2, NULL);

    if (cnt < 0) {
        printf("readv failed: %s\n", strerror(-cnt));
        goto out;
    }

    if (Pflag) {
        void *cmp_buf = g_malloc(qiov.size);
        memset(cmp_buf, pattern, qiov.size);
        if (memcmp(buf, cmp_buf, qiov.size)) {
            printf("Pattern verification failed at offset %"
                   PRId64 ", %zd bytes\n", offset, qiov.size);
        }
        g_free(cmp_buf);
    }

    if (qflag) {
        goto out;
    }

    if (vflag) {
        dump_buffer(buf, offset, qiov.size);
    }

    /* Finally, report back -- -C gives a parsable format */
    t2 = tsub(t2, t1);
    print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);

out:
    qemu_iovec_destroy(&qiov);
    qemu_io_free(buf);
    return 0;
}
예제 #19
0
int main(int argc, char *argv[])
{
	int leadin = 0, leadout = 0;
	const char *avifile = NULL;
	pattern_data *pattern = NULL;
	int patcount = 0;
	int arg;
	int i;

	/* iterate over arguments */
	for (arg = 1; arg < argc; arg++)
	{
		/* assume anything without a - is a filename */
		if (argv[arg][0] != '-')
		{
			if (avifile != NULL || pattern != NULL)
				return usage();
			avifile = argv[arg];
		}

		/* look for options */
		else if (strcmp(argv[arg], "-leadin") == 0)
		{
			if (++arg >= argc)
				return usage();
			leadin = atoi(argv[arg]);
		}
		else if (strcmp(argv[arg], "-leadout") == 0)
		{
			if (++arg >= argc)
				return usage();
			leadout = atoi(argv[arg]);
		}
		else if (strcmp(argv[arg], "-pattern") == 0)
		{
			if (avifile != NULL || pattern != NULL)
				return usage();
			if (++arg >= argc)
				return usage();
			pattern = parse_pattern(argv[arg], &patcount);
			if (pattern == NULL)
				return usage();
		}
	}

	/* must have an AVI file or a pattern */
	if (avifile == NULL && pattern == NULL)
		return usage();

	/* output header and leadin */
	printf("chdmeta 12\n");
	for (i = 0; i < leadin; i++)
	{
		int flags = (i == 0) ? 0x02 : (i == leadin - 1) ? 0x01 : 0x03;
		printf("02%02X%02X00000088FFFF88FFFF\n", flags, (i % 2));
	}

	/* if we got a file, output it */
	if (avifile != NULL)
		generate_from_avi(avifile);
	else if (pattern != NULL)
		generate_from_pattern(pattern, patcount);

	/* output leadout */
	for (i = 0; i < leadout; i++)
	{
		int flags = (i == 0) ? 0x02 : (i == leadout - 1) ? 0x01 : 0x03;
		printf("02%02X%02X00000080EEEE80EEEE\n", flags, (i % 2));
	}

	return 0;
}
예제 #20
0
파일: qemu-io.c 프로젝트: AjayMashi/x-tier
static int write_f(int argc, char **argv)
{
    struct timeval t1, t2;
    int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
    int cflag = 0;
    int c, cnt;
    char *buf = NULL;
    int64_t offset;
    int count;
    /* Some compilers get confused and warn if this is not initialized.  */
    int total = 0;
    int pattern = 0xcd;

    while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
        switch (c) {
        case 'b':
            bflag = 1;
            break;
        case 'c':
            cflag = 1;
            break;
        case 'C':
            Cflag = 1;
            break;
        case 'p':
            pflag = 1;
            break;
        case 'P':
            Pflag = 1;
            pattern = parse_pattern(optarg);
            if (pattern < 0) {
                return 0;
            }
            break;
        case 'q':
            qflag = 1;
            break;
        case 'z':
            zflag = 1;
            break;
        default:
            return command_usage(&write_cmd);
        }
    }

    if (optind != argc - 2) {
        return command_usage(&write_cmd);
    }

    if (bflag + pflag + zflag > 1) {
        printf("-b, -p, or -z cannot be specified at the same time\n");
        return 0;
    }

    if (zflag && Pflag) {
        printf("-z and -P cannot be specified at the same time\n");
        return 0;
    }

    offset = cvtnum(argv[optind]);
    if (offset < 0) {
        printf("non-numeric length argument -- %s\n", argv[optind]);
        return 0;
    }

    optind++;
    count = cvtnum(argv[optind]);
    if (count < 0) {
        printf("non-numeric length argument -- %s\n", argv[optind]);
        return 0;
    }

    if (!pflag) {
        if (offset & 0x1ff) {
            printf("offset %" PRId64 " is not sector aligned\n",
                   offset);
            return 0;
        }

        if (count & 0x1ff) {
            printf("count %d is not sector aligned\n",
                   count);
            return 0;
        }
    }

    if (!zflag) {
        buf = qemu_io_alloc(count, pattern);
    }

    gettimeofday(&t1, NULL);
    if (pflag) {
        cnt = do_pwrite(buf, offset, count, &total);
    } else if (bflag) {
        cnt = do_save_vmstate(buf, offset, count, &total);
    } else if (zflag) {
        cnt = do_co_write_zeroes(offset, count, &total);
    } else if (cflag) {
        cnt = do_write_compressed(buf, offset, count, &total);
    } else {
        cnt = do_write(buf, offset, count, &total);
    }
    gettimeofday(&t2, NULL);

    if (cnt < 0) {
        printf("write failed: %s\n", strerror(-cnt));
        goto out;
    }

    if (qflag) {
        goto out;
    }

    /* Finally, report back -- -C gives a parsable format */
    t2 = tsub(t2, t1);
    print_report("wrote", &t2, offset, count, total, cnt, Cflag);

out:
    if (!zflag) {
        qemu_io_free(buf);
    }

    return 0;
}
예제 #21
0
static struct Pattern *
parse_pattern (unsigned      pattern_index,
               struct Token *token_list,
               DskMemPool   *pool,
               DskError    **error)
{
  dsk_boolean last_was_alter;
  dsk_boolean accept_empty;

  /* Handle parens */
  struct Token *token;
  for (token = token_list; token; token = token->next)
    if (token->type == TOKEN_LPAREN)
      {
        /* find matching rparen (or error) */
        struct Token *rparen = token->next;
        int balance = 1;
        struct Pattern *subpattern;
        while (rparen)
          {
            if (rparen->type == TOKEN_LPAREN)
              balance++;
            else if (rparen->type == TOKEN_RPAREN)
              {
                balance--;
                if (balance == 0)
                  break;
              }
            rparen = rparen->next;
          }
        if (balance)
          {
            /* missing right-paren */
            dsk_set_error (error, "missing right-paren in regex");
            return NULL;
          }

        /* recurse */
        rparen->prev->next = NULL;
        subpattern = parse_pattern (pattern_index, token->next, pool, error);
        if (subpattern == NULL)
          return NULL;

        /* replace parenthesized expr with subpattern; slice out remainder of list */
        token->type = TOKEN_PATTERN;
        token->pattern = subpattern;
        token->next = rparen->next;
        if (rparen->next)
          token->next->prev = token;
      }
    else if (token->type == TOKEN_RPAREN)
      {
        dsk_set_error (error, "unexpected right-paren in regex");
        return NULL;
      }

  /* Handle star/plus/qm */
  for (token = token_list; token; token = token->next)
    if (token->type == TOKEN_QUESTION_MARK
     || token->type == TOKEN_STAR
     || token->type == TOKEN_PLUS)
      {
        struct Pattern *new_pattern;
        if (token->prev == NULL || token->prev->type != TOKEN_PATTERN)
          {
            dsk_set_error (error, "'%c' must be precede by pattern",
                           token->type == TOKEN_QUESTION_MARK ? '?' 
                           : token->type == TOKEN_STAR ? '*'
                           : '+');
            return NULL;
          }
        new_pattern = dsk_mem_pool_alloc (pool, sizeof (struct Pattern));
        switch (token->type)
          {
          case TOKEN_QUESTION_MARK:
            new_pattern->type = PATTERN_OPTIONAL;
            new_pattern->info.optional = token->prev->pattern;
            break;
          case TOKEN_STAR:
            new_pattern->type = PATTERN_STAR;
            new_pattern->info.star = token->prev->pattern;
            break;
          case TOKEN_PLUS:
            new_pattern->type = PATTERN_PLUS;
            new_pattern->info.plus = token->prev->pattern;
            break;
          default:
            dsk_assert_not_reached ();
          }
        token->prev->pattern = new_pattern;

        /* remove token */
        if (token->prev)
          token->prev->next = token->next;
        else
          token_list = token->next;
        if (token->next)
          token->next->prev = token->prev;
        /* token isn't in the list now!  but it doesn't matter b/c token->next is still correct */
      }

  /* Handle concatenation */
  for (token = token_list; token && token->next; )
    {
      if (token->type == TOKEN_PATTERN
       && token->next->type == TOKEN_PATTERN)
        {
          /* concat */
          struct Pattern *new_pattern = dsk_mem_pool_alloc (pool, sizeof (struct Pattern));
          struct Token *kill;
          new_pattern->type = PATTERN_CONCAT;
          new_pattern->info.concat.a = token->pattern;
          new_pattern->info.concat.b = token->next->pattern;
          token->pattern = new_pattern;

          /* remove token->next */
          kill = token->next;
          token->next = kill->next;
          if (kill->next)
            kill->next->prev = token;
        }
      else
        token = token->next;
    }

  /* At this point we consist of nothing but alternations and
     patterns.  Scan through, discarding TOKEN_ALTER,
     and keeping track of whether the empty pattern matches */
  last_was_alter = DSK_TRUE;     /* trick the empty pattern detector
                                    into triggering on initial '|' */
  accept_empty = DSK_FALSE;
  for (token = token_list; token; token = token->next)
    if (token->type == TOKEN_ALTER)
      {
        if (last_was_alter)
          accept_empty = DSK_TRUE;
        last_was_alter = DSK_TRUE;

        /* remove token from list */
        if (token->prev)
          token->prev->next = token->next;
        else
          token_list = token->next;
        if (token->next)
          token->next->prev = token->prev;
      }
    else
      {
        last_was_alter = DSK_FALSE;
      }
  if (last_was_alter)
    accept_empty = DSK_TRUE;

  /* if we accept an empty token, toss a PATTERN_EMPTY onto the list of patterns
     in the alternation. */
  if (accept_empty || token_list == NULL)
    {
      struct Token *t = dsk_mem_pool_alloc (pool, sizeof (struct Token));
      t->next = token_list;
      t->prev = NULL;
      if (t->next)
        t->next->prev = t;
      token_list = t;

      t->type = TOKEN_PATTERN;
      t->pattern = dsk_mem_pool_alloc (pool, sizeof (struct Pattern));
    }

  /* At this point, token_list!=NULL,
     and it consists entirely of patterns.
     Reduce it to a singleton with the alternation pattern. */
  while (token_list->next != NULL)
    {
      /* create alternation pattern */
      struct Pattern *new_pattern = dsk_mem_pool_alloc (pool, sizeof (struct Pattern));
      new_pattern->type = PATTERN_ALT;
      new_pattern->info.alternation.a = token_list->pattern;
      new_pattern->info.alternation.b = token_list->next->pattern;
      token_list->pattern = new_pattern;

      /* remove token->next */
      {
        struct Token *kill = token_list->next;
        token_list->next = kill->next;
        if (kill->next)
          kill->next->prev = token_list;
      }
    }

  /* Return value consists of merely a single token-list. */
  dsk_assert (token_list != NULL && token_list->next == NULL);
  return token_list->pattern;
}
예제 #22
0
파일: qemu-io.c 프로젝트: AjayMashi/x-tier
static int multiwrite_f(int argc, char **argv)
{
    struct timeval t1, t2;
    int Cflag = 0, qflag = 0;
    int c, cnt;
    char **buf;
    int64_t offset, first_offset = 0;
    /* Some compilers get confused and warn if this is not initialized.  */
    int total = 0;
    int nr_iov;
    int nr_reqs;
    int pattern = 0xcd;
    QEMUIOVector *qiovs;
    int i;
    BlockRequest *reqs;

    while ((c = getopt(argc, argv, "CqP:")) != EOF) {
        switch (c) {
        case 'C':
            Cflag = 1;
            break;
        case 'q':
            qflag = 1;
            break;
        case 'P':
            pattern = parse_pattern(optarg);
            if (pattern < 0) {
                return 0;
            }
            break;
        default:
            return command_usage(&writev_cmd);
        }
    }

    if (optind > argc - 2) {
        return command_usage(&writev_cmd);
    }

    nr_reqs = 1;
    for (i = optind; i < argc; i++) {
        if (!strcmp(argv[i], ";")) {
            nr_reqs++;
        }
    }

    reqs = g_malloc0(nr_reqs * sizeof(*reqs));
    buf = g_malloc0(nr_reqs * sizeof(*buf));
    qiovs = g_malloc(nr_reqs * sizeof(*qiovs));

    for (i = 0; i < nr_reqs && optind < argc; i++) {
        int j;

        /* Read the offset of the request */
        offset = cvtnum(argv[optind]);
        if (offset < 0) {
            printf("non-numeric offset argument -- %s\n", argv[optind]);
            goto out;
        }
        optind++;

        if (offset & 0x1ff) {
            printf("offset %lld is not sector aligned\n",
                   (long long)offset);
            goto out;
        }

        if (i == 0) {
            first_offset = offset;
        }

        /* Read lengths for qiov entries */
        for (j = optind; j < argc; j++) {
            if (!strcmp(argv[j], ";")) {
                break;
            }
        }

        nr_iov = j - optind;

        /* Build request */
        buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
        if (buf[i] == NULL) {
            goto out;
        }

        reqs[i].qiov = &qiovs[i];
        reqs[i].sector = offset >> 9;
        reqs[i].nb_sectors = reqs[i].qiov->size >> 9;

        optind = j + 1;

        pattern++;
    }

    /* If there were empty requests at the end, ignore them */
    nr_reqs = i;

    gettimeofday(&t1, NULL);
    cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
    gettimeofday(&t2, NULL);

    if (cnt < 0) {
        printf("aio_multiwrite failed: %s\n", strerror(-cnt));
        goto out;
    }

    if (qflag) {
        goto out;
    }

    /* Finally, report back -- -C gives a parsable format */
    t2 = tsub(t2, t1);
    print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
out:
    for (i = 0; i < nr_reqs; i++) {
        qemu_io_free(buf[i]);
        if (reqs[i].qiov != NULL) {
            qemu_iovec_destroy(&qiovs[i]);
        }
    }
    g_free(buf);
    g_free(reqs);
    g_free(qiovs);
    return 0;
}
예제 #23
0
파일: sk_rule.c 프로젝트: Safe3/silk
int parse_rule_content(char* rule)
{
	if(rule == NULL)
		return -1;

	char* index = rule;
	char** toks;
	int i;	// j;
	int num_toks;
//	int ruletype;
	int level;

	rule_node_t rule_node[10];	//每一行内容可能包含多个content
	int rule_num = 0;
	memset(&rule_node, 0, sizeof(rule_node_t)*10);

	while(*index == ' ' || *index =='\t')
		index++;
	for(i=0; i<strlen(index); i++)
	{
		if(*(index+i) == '\n')
		{
			*(index+i) = 0x00;
			break;
		}
	}

	toks = m_split(index, ";", PARSERULE_SIZE, &num_toks, 0);
	for(i=0; i<num_toks; i++)
	{
		char key[PARSERULE_SIZE]={0};
		char value[PARSERULE_SIZE]={0};
		char* idx;

		idx = toks[i];
		while(*idx == ' ' || *idx == '\t')
			idx++;
		if(*idx == '\n' || *idx == 0x00)	//是否是最后一个元素,即换行符
			break;

		if(parse_key_value_pair(idx, key, value) < 0)
		{
			save_err_log("profile %s, line:%d, this line is not in the expected format", file_name, file_line);
			exit(1);
		}

		if(!strcmp(key, "classtype"))
		{
			strcpy(rule_node[0].type, value);
		}
		else if(!strcmp(key, "level"))
		{
			level = rule_level(value);
			if(level < 0)
			{
				m_split_free(&toks, num_toks);
				return -1;
			}
			rule_node[0].level = level;
		}
		else if(!strcmp(key, "msg"))
		{
			strcpy(rule_node[0].msg, value);
		}
		else if(!strcmp(key, "content") || !strcmp(key, "uricontent"))
		{
			if(parse_pattern(value, rule_node[rule_num].content, &rule_node[rule_num].content_len)<0)
			{
				save_err_log("profile %s, line:%d, this line is not in the expected format", file_name, file_line);
				m_split_free(&toks, num_toks);
				return -1;
			}
			rule_num++;
		}
	}

	for(i=0; i<rule_num; i++)
	{
		rule_node[i].nocase = NOCASE;
		rule_node[i].id = rule_id++;
		strcpy(rule_node[i].file_name, file_name);
		rule_node[i].file_line = file_line;

		if(i != 0)
		{
			strcpy(rule_node[i].type, rule_node[0].type);
			rule_node[i].level = rule_node[0].level;
			strcpy(rule_node[i].msg, rule_node[0].msg);
		}
	}

	m_split_free(&toks, num_toks);

	if(rule_node[0].content_len > 0 && rule_num >= 1)
	{
		rule_node_t* cur_rule_node = &rule_node[0];
		if(rule_num == 1)
		{
			fpdetect_add_rule(cur_rule_node);
		}
		else
		{
			int j=1;
			for(j=1; j<rule_num; j++)
			{
				cur_rule_node = &rule_node[j];

				if(cur_rule_node->content_len > 0)
					fpdetect_add_rule(cur_rule_node);
			}
		}
	}
	return 0;
}