Exemple #1
0
// phrase -> '"' phrase_word_list '"'
static syntree *phrase (token ** lookahead, err_context context)
{
	syntree *tree = NULL;
	token *phrase_head = NULL, *temp = NULL;
	RETURN_NULL_IF_OVER_DEPTH_LIMIT(context);

	// Tokenize the phrase lexeme into words.
	phrase_head = ft_phrase_lex ((*lookahead)->lexeme->contents);

	ASSERT (phrase_head);

	advance (lookahead);
	temp = phrase_head->next;

	if (ft_test (&temp, TOK_FT_WORD))
	{	
		tree = phrase_word_list (&temp, context);
		ASSERT (tree);
		
		free_tokens (phrase_head);
		return (tree);
	}
	else
	{
		gen_cond_err_msg ("Error: Malformed phrase inside the CONTAINS condition string. A phrase should"
						  " contain one or more words separated by whitespace"
						  " enclosed inside double quotes.", context.count);
		goto error;
	}

error:
	free_tokens (phrase_head);
	return (NULL);
}
/*
 * process_file(): The main function that processes one log file
 *
 *	input:
 * 		ptr: Pointer to log file name.
 *
 *	output:
 *		none
 *	return
 *		none
 */
static void process_file(void *ptr)
{
	bool flag;
	char *filename = (char *) ptr;
	char **field = (char **) malloc(sizeof(char *) * MAX_NUM_FIELDS);
	linebuffer = (char *) malloc(sizeof(char) * MAX_LINE_SIZE);
	char *end_date =  (char *) malloc(sizeof(char) * MAX_LINE_SIZE);

	FILE *fin = fopen(filename,"r");
	if (fin == NULL)
	{
		fprintf(stderr,"Cannot open file %s\n", filename);
		exit(1);
	}
	if (start_from_checkpoint())
	{
		char *s = fgets(linebuffer, MAX_LINE_SIZE, fin);
		if (s == NULL) return;
		int num = parse_line(linebuffer, " []\"", field);
		printf("Starting date: %s\n",field[3]);
		free_tokens(num, field);
		fseek(fin, filepos, SEEK_SET);
	} 
	else
	{
		fprintf(stderr,"%s: processing log file %s\n", program_name, filename);
		char *s = fgets(linebuffer, MAX_LINE_SIZE, fin);
		if (s == NULL) return;
		filepos += strlen(linebuffer);
		int num = parse_line(linebuffer, " []\"", field);
		update_webstats(num, field);
		printf("Starting date: %s\n",field[3]);
		free_tokens(num, field);
	}


	flag = TRUE;
	alarm(CHECKPOINT_INTERVAL);
	while (flag) 
	{
		/* block alarm signal */
		manipulate_alarm_signal("block");	
		char * status = fgets(linebuffer, MAX_LINE_SIZE, fin); 
		if (status == NULL) {
				flag = FALSE;
				break;
		} 
		filepos += strlen(linebuffer);
		int num = parse_line(linebuffer, " []\"", field);
		update_webstats(num, field);
		strcpy(end_date, field[3]);
		free_tokens(num, field);
		/* unblock and receive alarm signal now after updating */
		manipulate_alarm_signal("unblock");	

		strcpy(linebuffer,"");
	}
	printf("Ending date: %s\n",end_date);
	free(end_date);
}
Exemple #3
0
int srvmgr_module_run(char *base_path, char *data, int authorized)
{
	char config_file[BUFSIZE];
	tTokenizer t;
	int ret;

	if (strncmp(data, MODULE_KEYWORD, strlen(MODULE_KEYWORD)) != 0)
		return -ENOTSUP;

	DPRINTF("[%s] Input data: '%s' ( user %s authorized )\n", MODULE_IDENTIFICATION,
		data, authorized ? "is" : "NOT");

	t = tokenize(data);
	if (t.numTokens == 0)
		return -EINVAL;

	snprintf(config_file, sizeof(config_file), "%s/manager.conf", base_path);
	if (access(config_file, R_OK) != 0) {
		DPRINTF("%s: Cannot read configuration file '%s'\n", __FUNCTION__,
			config_file);
		free_tokens(t);
		return -EINVAL;
	}

	ret = process_commands(config_file, authorized, t);
	free_tokens(t);

	return ret;
}
Exemple #4
0
int main(int argc, char **argv) {

    display_prompt(); 

    FILE *datafile = NULL;
    datafile = fopen("shell-config", "r");
    Node *head = NULL;
    if (datafile != NULL) {
        char line[1024];
        while (fgets(line, 1024, datafile) != NULL) {
            int slen = strlen(line);
            line[slen-1] = '\0';
            list_append(line, "", &head);
        }
        fclose(datafile);
    }
    char curr_mode = 's';
    char buffer[1024];
    Node *paused_list = NULL;
    Node *cpidlist = NULL;
    while (fgets(buffer, 1024, stdin) != NULL) {
        char *newbuffer = replace_pound(buffer);
        newbuffer[strlen(newbuffer)-1] = '\0'; 
        char **pointers_to_commands = tokenify(newbuffer, ";");
        free(newbuffer);
        if (pointers_to_commands[0] == NULL) {
            display_prompt();
            continue;
        }
        if (curr_mode == 's') {
            curr_mode = sequential_mode(pointers_to_commands, head); 
        }
        else if (curr_mode == 'p') {  
            curr_mode = parallel_mode(pointers_to_commands, head, &paused_list, &cpidlist);
        } 
        if (curr_mode == 'e') {
            free_tokens(pointers_to_commands);
            list_clear(paused_list);
            list_clear(cpidlist);
            list_clear(head);
            exit(0);
        }
        display_prompt();
        free_tokens(pointers_to_commands);
    }
    list_clear(head);
    list_clear(paused_list);
    list_clear(cpidlist);
    printf("\n");
    return 0;
}
Exemple #5
0
static int
read_final_states_helper(int **states)
{
    char *line;
    char **tokens;
    int nstates;
    int i;

    printf("Final states: ");

    fflush(stdout);

    line = read_line();
    tokens = tokenize(line);

    if (tokens == NULL)
        panic("Invalid input");

    nstates = tokens_size(tokens);

    *states = calloc(nstates, sizeof (int));
    bzero(*states, nstates * sizeof (int));

    for (i = 0; i < nstates; ++i)
        (*states)[i] = strtol(tokens[i], NULL, 10);

    free_tokens(tokens);

    return nstates;
}
Exemple #6
0
void *cvar_alloc_handle(const char *cvar_parameters,
		void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr))
{
	cvar_token_t *list_head;;
	cvar_token_t *t;
	handle_t handle;
	handle_t *state = NULL;
	int ret = 0;

	cvar_trace("entry");

	/* Tokenize parameters supplied by filebench. */
	list_head = NULL;
	ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER,
			DEFAULT_KEY_VALUE_DELIMITER, &list_head);
	if (ret)
		goto out;

	/* Get the value of mean and sigma. */
	t = find_token(list_head, RN_MEAN);
	if (t && t->value) {
		t->used = 1;
		handle.mean = atof(t->value);
	} else
		handle.mean = RN_MEAN_DEFAULT;

	t = find_token(list_head, RN_SIGMA);
	if (t && t->value) {
		t->used = 1;
		handle.sigma = atof(t->value);
	} else
		handle.sigma = RN_SIGMA_DEFAULT;

	cvar_trace("mean = %lf, sigma = %lf", handle.mean, handle.sigma);

	t = unused_tokens(list_head);
	if (t) {
		cvar_log_error("Unsupported parameter %s", t->key);
		goto out;
	}

	/* Seed the state. */
	mts_goodseed(&handle.state);

	/* All set. Now allocate space for the handle in the shared segment and
	 * copy the state over. */
	state = (handle_t *) cvar_malloc(sizeof(handle_t));
	if (!state) {
		cvar_log_error("Out of memory");
		goto out;
	}

	*state = handle;

out:
	free_tokens(list_head);

	cvar_trace("exit");
	return state;
}
result_t handle_system_event(const char *request, size_t req_len) {

    // tokenize request message
    size_t num_tokens = 0;
    char **tokens = str_split(request, ": ", &num_tokens);

    if(num_tokens < 2) {
        fprintf(stderr, "Could not find correct number of tokens in rpc system request.");
        return FAILURE;
    }

    if(strncmp(tokens[1], ACTION_VOLUME_MUTE, sizeof(ACTION_VOLUME_MUTE)-1) == 0) {
        audio_output_mute(!audio_input_is_muted());
    } else if(strncmp(tokens[1], ACTION_VOLUME_LEVEL, sizeof(ACTION_VOLUME_LEVEL)-1) == 0) {
        if(num_tokens < 3) {
            fprintf(stderr, "Could not find correct number of tokens for rpc system request's action volume level");
            return FAILURE;
        }
            float volume_level = atoi(tokens[2])/100.0;
            volume_level = (volume_level < 0.0f) ? 0.0f : (volume_level > 1.0f) ? 1.0f : volume_level;
            audio_output_set_volume(volume_level);
    } else if(strncmp(tokens[1], ACTION_SHUT_DOWN, sizeof(ACTION_SHUT_DOWN)-1) == 0) {
        system_shutdown();
    } else if(strncmp(tokens[1], ACTION_RESTART, sizeof(ACTION_RESTART)-1) == 0) {
        system_restart();
    } else if(strncmp(tokens[1], ACTION_SLEEP, sizeof(ACTION_SLEEP)-1) == 0) {
        system_sleep();
    } else if(strncmp(tokens[1], ACTION_LOG_OUT, sizeof(ACTION_LOG_OUT)-1) == 0) {
        system_logout();
    }

    // free tokens
    free_tokens(tokens, num_tokens);
}
Exemple #8
0
HIDDEN int
add_operator(struct ged *gedp, struct bu_list *hp, char ch, short int *last_tok)
{
    char illegal[2];

    BU_CK_LIST_HEAD(hp);

    switch (ch) {
	case 'u':
	    append_union(hp);
	    *last_tok = TOK_UNION;
	    break;
	case '+':
	    append_inter(hp);
	    *last_tok = TOK_INTER;
	    break;
	case '-':
	    append_subtr(hp);
	    *last_tok = TOK_SUBTR;
	    break;
	default:
	    illegal[0] = ch;
	    illegal[1] = '\0';
	    bu_vls_printf(gedp->ged_result_str, "Illegal operator: %s, aborting\n", illegal);
	    free_tokens(hp);
	    return GED_ERROR;
    }
    return GED_OK;
}
Exemple #9
0
/* 
 * Runs commands in sequential mode.
 */
char sequential_mode(char **pointers_to_commands, Node *head) {
    char mode = 's';
    pid_t cpid, w;
    char **command;
    char return_char = 's';
    int i = 0;
    while (pointers_to_commands[i] != NULL) {
        command = tokenify(pointers_to_commands[i], " \n\t");
        if (command[0] == NULL) {
            i++;
            free_tokens(command);
            continue;
        } 
        if (handle_exit(command) != 'n') {
            free_tokens(command);
            return 'e';
        }
        if (return_char != 'e' && is_mode(command)) {
            return_char = handle_mode(command, mode, return_char);
            i++;
            free_tokens(command);
            continue;
        }
        handle_parallel_builtins(command, NULL, NULL, mode);
        cpid = fork();
        if (cpid == 0) {
            char *tempcommand;
            tempcommand = prepend_path(command, head);
            command[0] = tempcommand;   
            if (execv(command[0], command) < 0) {
                fprintf(stderr, "execv failed: %s\n", strerror(errno));
                printf("That's not a valid command! \n");
                free_tokens(command);
                exit(EXIT_FAILURE);
            }
            free(tempcommand);
            }
        else {
            int status = 0;
            w = wait(&status);
            i++;
        }
        free_tokens(command);

    }
    return return_char;
}
Exemple #10
0
static void
sm_free(struct state_machine *sm)
{
    sm_free_transitions(sm);
    free_tokens(sm->tokens);
    free(sm->fstates);
    free(sm);
}
result_t handle_shortcut_event(const char *request, size_t req_len) {

    // tokenize request message
    size_t num_tokens = 0;
    char **tokens = str_split(request, ": ", &num_tokens);

    if(num_tokens < 2) {
        fprintf(stderr, "Could not find correct number of tokens in rpc shortcut request.");
        return FAILURE;
    }

    if(strncmp(tokens[1], ACTION_SELECT_ALL, sizeof(ACTION_SELECT_ALL)-1) == 0) {
        system_hot_keys_event(kSelectAllHotKeys);
    } else if(strncmp(tokens[1], ACTION_CUT, sizeof(ACTION_CUT)-1) == 0) {
        system_hot_keys_event(kCutHotKeys);
    } else if(strncmp(tokens[1], ACTION_COPY, sizeof(ACTION_COPY)-1) == 0) {
        system_hot_keys_event(kCopyHotKeys);
    } else if(strncmp(tokens[1], ACTION_PASTE, sizeof(ACTION_PASTE)-1) == 0) {
        system_hot_keys_event(kPasteHotKeys);
    } else if(strncmp(tokens[1], ACTION_OPEN_FILE, sizeof(ACTION_OPEN_FILE)-1) == 0) {
        system_hot_keys_event(kOpenFileHotKeys);
    } else if(strncmp(tokens[1], ACTION_SAVE, sizeof(ACTION_SAVE)-1) == 0) {
        system_hot_keys_event(kSaveHotKeys);
    } else if(strncmp(tokens[1], ACTION_FIND, sizeof(ACTION_FIND)-1) == 0) {
        system_hot_keys_event(kFindHotKeys);
    } else if(strncmp(tokens[1], ACTION_PRINT, sizeof(ACTION_PRINT)-1) == 0) {
        system_hot_keys_event(kPrintHotKeys);
    } else if(strncmp(tokens[1], ACTION_NEW_WINDOW, sizeof(ACTION_NEW_WINDOW)-1) == 0) {
        system_hot_keys_event(kOpenNewWindowHotKeys);
    } else if(strncmp(tokens[1], ACTION_MINIMIZE_WINDOW, sizeof(ACTION_MINIMIZE_WINDOW)-1) == 0) {
        system_hot_keys_event(kMinimizeFrontWindowHotKeys);
    } else if(strncmp(tokens[1], ACTION_CLOSE_WINDOW, sizeof(ACTION_CLOSE_WINDOW)-1) == 0) {
        system_hot_keys_event(kCloseFrontWindowHotKeys);
    } else if(strncmp(tokens[1], ACTION_SWITCH_APPS, sizeof(ACTION_SWITCH_APPS)-1) == 0) {
        system_hot_keys_event(kSwitchAppsHotKeys);
    } else if(strncmp(tokens[1], ACTION_UNDO, sizeof(ACTION_UNDO)-1) == 0) {
        system_hot_keys_event(kUndoHotKeys);
    } else if(strncmp(tokens[1], ACTION_REDO, sizeof(ACTION_REDO)-1) == 0) {
        system_hot_keys_event(kRedoHotKeys);
    } else if(strncmp(tokens[1], ACTION_SYSTEM_SEARCH, sizeof(ACTION_SYSTEM_SEARCH)-1) == 0) {
        system_hot_keys_event(kSpotlightHotKeys);
    } else if(strncmp(tokens[1], ACTION_FORCE_QUIT, sizeof(ACTION_FORCE_QUIT)-1) == 0) {
        system_hot_keys_event(kForceQuitHotKeys);
    } else if(strncmp(tokens[1], ACTION_SHOW_DESKTOP, sizeof(ACTION_SHOW_DESKTOP)-1) == 0) {
        system_hot_keys_event(kShowDesktopHotKeys);
    } else if(strncmp(tokens[1], ACTION_LEFT_DESKTOP, sizeof(ACTION_LEFT_DESKTOP)-1) == 0) {
        system_hot_keys_event(kLeftDesktopHotKeys);
    } else if(strncmp(tokens[1], ACTION_RIGHT_DESKTOP, sizeof(ACTION_RIGHT_DESKTOP)-1) == 0) {
        system_hot_keys_event(kRightDesktopHotKeys);
    }

    // free tokens
    free_tokens(tokens, num_tokens);
}
Exemple #12
0
/* The function ft_lex calls ft_lex_main to do the actual job of converting the charater string to
 * a list of tokens. It then reorganizes the list (if required) before presenting it to the parser.
 * Reorganization may be required to insert appropriate operators in place, e.g. an accrue operator
 * is represented not only by a ',' but also by white space between words/phrases.
 * Returns NULL if any of the tokens are bad.
 */
token *ft_lex (TCHAR *sentence)
{
	int status = TMAN_OK;
	token *head = ft_lex_main (sentence, &status);
	if (status == TMAN_ERROR)
	{
		free_tokens(head);
		return NULL;
	}
	add_implied_ands (head);
	return (head);
}
Exemple #13
0
void test_tokenize(CuTest *tc) {
	char *file1 = "float bmi = weight/height;";
	char *file2 = "float pi = 3.14159265;";
	char *file3 = "print \"Hello World!\";\nfoo();\n";
	char *file4 = "print \"\\\"Hello World!\\\"\";\nint i = 0;\n\nbar(i++);\\";

	char *file1_expected[7] = {"float", "bmi", "=", "weight", "/", "height", ";"};
	char *file2_expected[5] = {"float", "pi", "=", "3.14159265", ";"};
	char *file3_expected[7] = {"print", "\"Hello World!\"", ";", "foo", "(", ")", ";"};
	char *file4_expected[15] = {"print", "\"\\\"Hello World!\\\"\"", ";", "int", "i", "=",\
					 "0", ";", "bar", "(", "i", "++", ")", ";", "\\" };

	token_t *head = NULL;

	CuAssertIntEquals(tc, 0, tokenize(file1, strlen(file1), &head));
	
	check_tokens(tc, head,  file1_expected, 7);
	free_tokens(head);
	head = NULL;

	CuAssertIntEquals(tc, 0, tokenize(file2, strlen(file2), &head));

	check_tokens(tc, head,  file2_expected, 5);
	free_tokens(head);
	head = NULL;

	CuAssertIntEquals(tc, 0, tokenize(file3, strlen(file3), &head));

	check_tokens(tc, head, file3_expected, 7);
	free_tokens(head);
	head = NULL;

	CuAssertIntEquals(tc, 0, tokenize(file4, strlen(file4), &head));

	check_tokens(tc, head, file4_expected, 15);
	free_tokens(head);
	head = NULL;
}
Exemple #14
0
/*-----------------------------------------------------------------------------
*   Parse one statement, if possible
*----------------------------------------------------------------------------*/
Bool parse_statement(ParseCtx *ctx)
{
	Bool parse_ok;

	save_scan_state();
	{
		parse_ok = _parse_statement(ctx);
		free_tokens(ctx);
	}
	if (parse_ok)
		drop_scan_state();
	else
		restore_scan_state();

	return parse_ok;
}
Exemple #15
0
/*******************************************************************************
 * Name:    gen_validate_path
 * Purpose: validate full path
 * Input:
 *   pszPath: path name
 * Output:
 * Return:
 *   success: 0
 *   failed : -1
 * Note:
 ******************************************************************************/
int
gen_validate_path(char *pszPath)
{
    char **ppNameArr = NULL;
    unsigned long nCnt = 0;
    int i = 0;
    int nRet = 0;

    if (NULL == pszPath)
    {
        return -1;
    }

    if (sal_strlen(pszPath) > M_FULLPATH_MAX_LEN)
    {
        return -1;
    }

    if (NULL != sal_strstr(pszPath, "//"))
    {
        return -1;
    }

    if (split_tokens(pszPath, sal_strlen(pszPath), M_FULLPATH_DEPTH_MAX,
              "/", &nCnt, &ppNameArr) != 0)
    {
        return -1;
    }

    for (i = 0; i < nCnt; i++)
    {
        if (NULL == ppNameArr[i])
        {
            continue;
        }
        if (check_filename(ppNameArr[i]) != 0)
        {
            nRet = -1;
            break;
        }
    }
    free_tokens(&ppNameArr);
    return nRet;
}
result_t handle_player_event(const char *request, size_t req_len) {

    // tokenize request message
    size_t num_tokens = 0;
    char **tokens = str_split(request, ": ", &num_tokens);

    if(num_tokens < 2) {
        fprintf(stderr, "Could not find correct number of tokens in rpc player event request.");
        return FAILURE;
    }

    if( strncmp(tokens[1], ACTION_VOLUME_MUTE, sizeof(ACTION_VOLUME_MUTE)-1) == 0 ){
        player_mute_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_VOLUME_DOWN, sizeof(ACTION_VOLUME_DOWN)-1) == 0) {
        player_volume_down_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_VOLUME_UP, sizeof(ACTION_VOLUME_UP)-1) == 0) {
        player_volume_up_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_PLAY_PAUSE, sizeof(ACTION_PLAY_PAUSE)-1) == 0) {
        player_play_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_STOP, sizeof(ACTION_STOP)-1) == 0) {
        player_stop_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_STEP_FORWARD, sizeof(ACTION_STEP_FORWARD)-1) == 0) {
        player_step_forward_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_STEP_BACKWARD, sizeof(ACTION_STEP_BACKWARD)-1) == 0) {
        player_step_backward_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_SKIP_NEXT, sizeof(ACTION_SKIP_NEXT)-1) == 0) {
        player_next_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_SKIP_PREVIOUS, sizeof(ACTION_SKIP_PREVIOUS)-1) == 0) {
        player_previous_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_LOOP, sizeof(ACTION_LOOP)-1) == 0) {
        player_loop_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_SHUFFLE, sizeof(ACTION_SHUFFLE)-1) == 0) {
        player_shuffle_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_FULLSCREEN, sizeof(ACTION_FULLSCREEN)-1) == 0) {
        player_full_screen_hot_keys_event();
    } else if( strncmp(tokens[1], ACTION_SUBTITLES, sizeof(ACTION_SUBTITLES)-1) == 0) {
        player_subtitles_hot_keys_event();
    }

    // free tokens
    free_tokens(tokens, num_tokens);
}
Exemple #17
0
int main(void) {
	const char* str = "me {\n"
	                  "  you.z = \"foo\";\n"
						   "  you {\n"
							"    _x = 3;\n"
							"    z = \"qq\";\n"
							"  }\n"
							"  y_bleh = 4;\n"
							"}\n"
							"me.you.z = \"bleh\";\n";
	struct Token* t;
	struct Token* c;
	struct m3config* conf;
	size_t ret = tokenize(str, strlen(str), &t);
	if (ret != 0) {
		size_t i;
		ret--;
		printf("Error while tokenizing at offset %d.\n%s\n", ret, str);
		for (i = 0; i < ret; i++) {
			putchar(' ');
		}
		puts("^");
		return 1;
	} 
	c = t;
	while (c) {
		printf("%8s -> [%s]", TOK_NAMES[c->type], c->value);
		printf("\n");
		c = c->next;
	}
	conf = parse(t);
	free_tokens(t);

	printf("me.you.z = %s\n", m3conf_get_str(conf, "me.you.z", "NULL"));
	printf("me.you._x = %d\n", m3conf_get_int(conf, "me.you._x", 0));
	printf("me.y_bleh = %d\n", m3conf_get_int(conf, "me.y_bleh", 0));

	m3conf_free(conf);
	return 0;
}
Exemple #18
0
int			main(int ac, char **av, char **env)
{
  t_token		*root;
  t_parse_tree		*tree;
  t_shell		*sh;

  (void)av;
  (void)ac;
  if (!(sh = init_sh(env)))
    return (FAILURE);
  parse_config_file(sh);
  while (!sh->exe->exit && get_line_caps(sh->line) != FAILURE)
    {
      pre_parsing(sh);
      if (!(root = get_tokens(sh->line->line)))
	return (FAILURE);
      if ((tree = start_parsing(root, sh)) && (sh->exe->return_value = 2))
	exec_cmd(tree, sh->exe);
      free_tokens(root);
      XFREE(sh->line->line);
    }
  add_in_history_file(sh->line);
  return (clean_all(sh));
}
Exemple #19
0
void free_commands(command *commands) {
	command *c_ptr = commands;

	// Don't free a null pointer.
	if (c_ptr == NULL) {
		return;
	}

	// Iterate while the arguments array "argv" isn't NULL.
	while (c_ptr->argv != NULL) {

		// Otherwise free everything we can.
		free_tokens(c_ptr->argv);
		if (c_ptr->ifile != NULL) {
			free(c_ptr->ifile);
		}
		if (c_ptr->ofile != NULL) {
			free(c_ptr->ofile);
		}

		c_ptr++;
	}
	free(commands);
}
Exemple #20
0
int is_assignment(char *buf)
{
	int ret;
	char *tmp = NULL;
	tTokenizer t;

	t = tokenize(buf, "=");
	if (t.numTokens == 1) {
		ret = 0;
		goto end;
	}

	tmp = trim(t.tokens[0]);

	if (strchr(tmp, '(') != NULL) {
		ret = 0;
		goto end;
	}

	ret = 1;
end:
	free_tokens(t);
	return ret;
}
Exemple #21
0
// This test excercises the phrase matching in particular, and contains in general.
void test_contains_phrase_match ()
{
	int i, iter;
	unsigned int word_len, j;
	TCHAR doc[1000];
	TCHAR *doc1, *doc2;
	TCHAR free_text_str[35] = {'\0'};
	TCHAR *free_text;
	ft_doc_index *index;
	token *tokens;
	syntree *ft_pred;
	for (iter = 0; iter < 5; iter++)
	{
		doc1 = doc;
		i = 0;
		while (i < 999)
		{
			word_len = (unsigned int)(tm_random()*7.0 + 1.0);
			for (j=0; j < word_len && i < 999; j++, i++)
			{
				doc[i] = get_random_alphabet();
			}
			if (i < 999)
			{
				doc[i++] = _TEXT(' ');
			}
		}
		doc[i] = _TEXT('\0');
	
		index = ft_doc_index_new (doc);

		while (get_next_word_from_doc (&doc1, &word_len) != DONE)
		{
			// Don't put first word as stop word.
			if (is_stop_word_with_len (doc1, word_len))
			{
				doc1 += word_len;
				continue;
			}

			free_text = free_text_str;
			_stprintf (free_text, "\"");
			_sntprintf (free_text + 1, word_len+1, "%s", doc1); 
			doc1 += word_len;
			free_text += word_len+1;
			doc2 = doc1;
			if (get_next_word_from_doc (&doc2, &word_len) != DONE)
			{
				if (!is_stop_word_with_len (doc2, word_len))
				{
					_sntprintf (free_text,word_len+2, " %s", doc2);
					free_text += word_len+1;
				}
				doc2 += word_len;
			}
			if (get_next_word_from_doc (&doc2, &word_len) != DONE)
			{
				if (!is_stop_word_with_len (doc2, word_len))
				{
					_sntprintf (free_text,word_len+2, " %s", doc2);
					free_text += word_len+1;
				}
			}
			_stprintf (free_text, "\"");

			tokens = ft_lex (free_text_str);
			if (tokens != NULL)
			{
				ft_pred = ft_parse (tokens);
				if (ft_pred)
				{
					ASSERT (evaluate_contains (index, ft_pred) > 0.0);
				}
				free_tokens (tokens);
				cond_del (ft_pred);
			}
		}
		tman_delete (index);
	}
}
Exemple #22
0
int _script_builtin_function(char *var, char *fn, char *args)
{
	int ret = 0;
	struct timespec ts;
	struct timespec tse;

	if (_perf_measure)
		ts = utils_get_time( TIME_CURRENT );

	if (strcmp(fn, "set_all_variables_overwritable") == 0) {
		if (args != NULL) {
			int var = get_boolean(args);

			if ((var == 0) || (var == 1))
				variable_allow_overwrite(NULL, var);
			else
				desc_printf(gIO, gFd, "Invalid value for %s(): %s\n",
					fn, args);
		}
	}
	else
	if (strcmp(fn, "get_all_variables_overwritable") == 0) {
		if (var != NULL) {
			char tmp[4] = { 0 };
			snprintf(tmp, sizeof(tmp), "%d", variable_get_overwrite(NULL));
			variable_add(var, tmp, TYPE_QSCRIPT, -1, TYPE_INT);
		}
		else
			desc_printf(gIO, gFd, "All variables overwritable: %s\n",
				(variable_get_overwrite(NULL) == 1) ? "true" : "false");
	}
	else
	if (strcmp(fn, "set_variable_overwritable") == 0) {
		tTokenizer t;

		t = tokenize(args, ",");
		if (t.numTokens == 2)
			variable_allow_overwrite(trim(t.tokens[0]), get_boolean(t.tokens[1]));
		else
			desc_printf(gIO, gFd, "Syntax: set_variable_overwritable(variable, true|false)\n");

		free_tokens(t);
	}
	else
	if (strcmp(fn, "get_variable_overwritable") == 0) {
		char tmp[4] = { 0 };

		if ((args != NULL) && (strlen(args) > 0))
		{
			snprintf(tmp, sizeof(tmp), "%d", variable_get_overwrite(args));

			if (var != NULL)
				variable_add(var, tmp, TYPE_QSCRIPT, -1, TYPE_INT);
			else
				desc_printf(gIO, gFd, "Variable %s overwritable: %s\n",
					args, (strcmp(tmp, "1") == 0) ? "true" :
					((strcmp(tmp, "0") == 0) ? "false" : "not found"));
		}
		else
			desc_printf(gIO, gFd, "Variable name is missing\n");
	}
	else
	if (strcmp(fn, "enable_perf") == 0) {
		int enable = get_boolean(args);

		if ((enable == 0) || (enable == 1)) {
			DPRINTF("%sabling performance measuring\n", enable ? "En" : "Dis");

			_perf_measure = enable;
		}
		else
			DPRINTF("Incorrect setting for performace measuring: %d\n", enable);

		if (_perf_measure)
			ts = utils_get_time( TIME_CURRENT );
	}
	else
	if (strcmp(fn, "del") == 0) {
		variable_set_deleted(args, 1);
	}
	else
	if (strcmp(fn, "get") == 0) {
		char *val = variable_get_element_as_string(args, "get");

		if (val != NULL) {
			DPRINTF("%s: Processing internal 'get' function for arguments: %s\n", __FUNCTION__, args);
			DPRINTF("%s: Variable %s processed to %s\n", __FUNCTION__, var, val);

			if (var != NULL)
				variable_add(var, val, TYPE_QSCRIPT, -1, gettype(val));
		}
	}
	else
	if (strcmp(fn, "dumpvars") == NULL) {
		desc_variable_dump(gIO, gFd, args);
	}
	else
	if (strcmp(fn, "post") == 0) {
		char *val = variable_get_element_as_string(args, "post");

		if (val != NULL) {
			DPRINTF("%s: Processing internal 'post' function for arguments: %s\n", __FUNCTION__, args);
			DPRINTF("%s: Variable %s processed to %s\n", __FUNCTION__, var, val);

			if (var != NULL)
				variable_add(var, val, TYPE_QSCRIPT, -1, gettype(val));
		}
	}
	else
	if (strcmp(fn, "cookie") == 0) {
		char *val = variable_get_element_as_string(args, "cookie");

		if (val != NULL) {
			DPRINTF("%s: Processing internal 'cookie' function for arguments: %s\n", __FUNCTION__, args);
			DPRINTF("%s: Variable %s processed to %s\n", __FUNCTION__, var, val);

			if (var != NULL)
				variable_add(var, val, TYPE_QSCRIPT, -1, gettype(val));
		}
	}
	else
	if (strcmp(fn, "sleep") == 0) {
		int num = atoi(args);

		DPRINTF("%s: Sleeping for %d seconds...\n", __FUNCTION__, num);
		sleep(num);
	}
	else
	if (strcmp(fn, "dumptype") == 0) {
		char *str = variable_get_type_string(args, "any");

		desc_printf(gIO, gFd, "%s\n", str ? str : "<null>");
		str = utils_free("scripting.dumptype.str", str);
	}
	else
	if (strcmp(fn, "print") == 0) {
		if (args != NULL) {
			if ((args[0] == '"') || (args[0] == '\'')) {
				*args++;
				args[strlen(args) - 1] = 0;

				args = replace(args, "\\n", "\n");
				desc_printf(gIO, gFd, "%s", args);
			}
			else {
				char *var = variable_get_element_as_string(args, NULL);
				desc_printf(gIO, gFd, "%s", var ? var : "");
				var = utils_free("scripting.print.var", var);
			}
		}
	}
	else
	if (strcmp(fn, "printf") == 0) {
		if ((args != NULL) && (strlen(args) > 0)) {
			int i;
			tTokenizer t;

			*args++;

			t = tokenize(args, "\"");
			if (t.numTokens == 1) {
				char *instr = NULL;

				instr = strdup(t.tokens[0]);
				while (strstr(instr, "\\n") != NULL)
					instr = replace(instr, "\\n", "\n");

				desc_printf(gIO, gFd, "%s", instr);
				instr = utils_free("scripting.printf.instr", instr);
			}
			else
			if (t.numTokens == 2) {
				tTokenizer t2;
				char *instr = NULL;
				char *vars = NULL;

				instr = strdup( t.tokens[0] );
				vars = strdup( t.tokens[1] + 1 );

				t2 = tokenize(vars, ",");
				for (i = 0; i < t2.numTokens; i++) {
					DPRINTF("%s: Replacing variable %s\n", __FUNCTION__, trim(t2.tokens[i]));
					char *tmp = variable_get_element_as_string(trim(t2.tokens[i]), NULL);

					if (tmp != NULL)
						instr = replace(instr, "%s", tmp);
					else {
						instr = replace(instr, "%s", "NULL");
						DPRINTF("%s: Variable \"%s\" not found\n", __FUNCTION__, trim(t2.tokens[i]));
					}
				}

				while (strstr(instr, "\\n") != NULL)
					instr = replace(instr, "\\n", "\n");

				desc_printf(gIO, gFd, "%s", instr);
				free_tokens(t2);
			}
			else {
				free_tokens(t);
				ret = -EINVAL;
				goto cleanup;
			}

			free_tokens(t);
		}
		else {
			desc_printf(gIO, gFd, "Invalid syntax for printf()\n");
			ret = -EINVAL;
			goto cleanup;
		}
	}
	else
	if (strcmp(fn, "idb_dump_query_set") == 0) {
		idb_results_show( gIO, gFd, idb_get_last_select_data() );
	}
	else
	if (strcmp(fn, "idb_query") == 0) {
		char *filename = NULL;
		char *query = NULL;
		tTokenizer t;
		int i;

		t = tokenize(args, "\"");
		if (t.numTokens > 1) {
			int num = 0;
			for (i = 0; i < t.numTokens; i++) {
				if (strcmp(trim(t.tokens[i]), ",") != 0) {
					if (num == 0)
						filename = strdup(t.tokens[i]);
					else
					if (num == 1)
						query = strdup(t.tokens[i]);
					num++;
				}
			}
		}
		free_tokens(t);

		if (((filename == NULL) || (query == NULL)) && (args[0] == '@')) {
			*args++;

			DPRINTF("Reading query file '%s'\n", args);
			if (access(args, R_OK) == 0) {
				FILE *fp = NULL;
				char buf[BUFSIZE];

				fp = fopen(args, "r");
				if (fp != NULL) {
					int num = 0;

					while (!feof(fp)) {
						memset(buf, 0, sizeof(buf));

						fgets(buf, sizeof(buf), fp);
						if ((strlen(buf) > 0)
							&& (buf[strlen(buf) - 1] == '\n'))
							buf[strlen(buf) - 1] = 0;

						if (strlen(buf) > 0) {
							num++;
							int ret = idb_query(buf);
							desc_printf(gIO, gFd, "Query '%s' returned with code %d\n",
								buf, ret);
						}
					}

					desc_printf(gIO, gFd, "%d queries processed\n", num);
					fclose(fp);
				}
				else
					desc_printf(gIO, gFd, "Error: Cannot open file %s for reading\n",
						args);
			}
			else
				desc_printf(gIO, gFd, "Error: Cannot access file %s\n", t.tokens[1]);
		}
		else
		if ((filename != NULL) && (query != NULL)) {
			char tmp[4096] = { 0 };
			snprintf(tmp, sizeof(tmp), "INIT %s", filename);

			ret = 0;
			if (idb_query(tmp) != 0) {
				DPRINTF("Error while trying to initialize file '%s'\n", filename);
				ret = -EIO;
			}
			if (idb_query(query) != 0) {
				DPRINTF("Error while running query '%s'\n", query);
				ret = -EIO;
			}
			else
			if ((strncmp(query, "SELECT", 6) == 0) || (strcmp(query, "SHOW TABLES") == 0))
				idb_results_show( gIO, gFd, idb_get_last_select_data() );

			idb_query("COMMIT");
			idb_query("CLOSE");
		}
	}
	else {
		ret = -EINVAL;
		goto cleanup;
	}

cleanup:
	if (_perf_measure) {
		tse = utils_get_time( TIME_CURRENT );
		desc_printf(gIO, gFd, "\nPERF: Function %s() was running for %.3f microseconds (%.3f ms)\n",
			fn, get_time_float_us( tse, ts ), get_time_float_us(tse, ts) / 1000.);
	}

	return ret;
}
Exemple #23
0
int script_process_line(char *buf)
{
	int ret = -ENOTSUP;
	struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
	struct timespec tse;

	if (_perf_measure)
		ts = utils_get_time( TIME_CURRENT );

	/* Skip if it's a one line comment (more to be implemented) */
	if (is_comment(buf) == 1) {
		DPRINTF("%s: Found comment, skipping\n", __FUNCTION__);
		ret = 0;
		goto cleanup;
	}
	else
	if (strcmp(buf, "else {") == 0) {
		/* Reverse the condition */

		if ((_script_in_condition_and_met == 1)
			|| (_script_in_condition_and_met == -10))
			_script_in_condition_and_met = 0;
		else
		if ((_script_in_condition_and_met == 0)
			|| (_script_in_condition_and_met == -20))
			_script_in_condition_and_met = 1;
		else
		if (_script_in_condition_and_met != -1) {
			DPRINTF("%s: Invalid state for else statement\n", __FUNCTION__);

			ret = -EINVAL;
			goto cleanup;
		}

		ret = 0;
		goto cleanup;
	}
	else
	if (strcmp(buf, "}") == 0) {
		_script_in_condition_and_met = (_script_in_condition_and_met == 1) ? -10 : -20;
		ret = 0;
		goto cleanup;
	}

	if (_script_in_condition_and_met < -1)
		_script_in_condition_and_met = 1;

	if (_script_in_condition_and_met == 0) {
		ret = 0;
		goto cleanup;
	}

	/* Comparison with no ternary operator support... yet */
	if (regex_match("if \\(([^(]*)([^)]*)\\)", buf)) {
		if (regex_match("if \\(([^(]*) == ([^)]*)\\) {", buf)) {
			char **matches = NULL;
			int i, num_matches;

			matches = (char **)utils_alloc( "scripting.script_process_line.matches", sizeof(char *) );
			_regex_match("if \\(([^(]*) == ([^)]*)\\) {", buf, matches, &num_matches);

			if (num_matches >= 2)
				_script_in_condition_and_met = (valcmp(matches[0], matches[1]) == 0) ? 1 : 0;

			for (i = 0; i < num_matches; i++)
				matches[i] = utils_free("scripting.condition.matches[]", matches[i]);
			matches = utils_free("scripting.condition.matches", matches);
		}
	}
	else
	/* Definition */
	if (strncmp(buf, "define ", 7) == 0) {
		tTokenizer t;

		t = tokenize(buf + 7, " ");
		if (t.numTokens != 2) {
			ret = -EINVAL;
			goto cleanup;
		}
		if (variable_create(trim(t.tokens[0]), trim(t.tokens[1])) != 1) {
			ret = -EIO;
			goto cleanup;
		}
	}
	else
	/* Operators */
	if ((strstr(buf, "+=") != NULL) || (strstr(buf, "-=") != NULL) ||
		(strstr(buf, "%=") != NULL) || (strstr(buf, "*=") != NULL) ||
		(strstr(buf, "/=") != NULL)) {
		tTokenizer t;
		char *var;
		char *val;
		int op, vtype;

		t = tokenize(buf, "=");
		if (t.numTokens != 2) {
			ret = -EINVAL;
			goto cleanup;
		}

		var = trim(strdup(t.tokens[0]));
		val = trim(strdup(t.tokens[1]));

		op = var[ strlen(var) - 1];
		var[ strlen(var) - 1] = 0;

		var = trim(var);
		if (val[strlen(val) - 1] == ';')
			val[strlen(val) - 1] = 0;
		val = trim(val);

		vtype = variable_get_type(var, NULL);
		if ((vtype == TYPE_INT) || (vtype == TYPE_LONG)) {
			char tmp[32] = { 0 };
			long value = atol(variable_get_element_as_string(var, NULL));

			if (op == '+')
				value += atol(val);
			else
			if (op == '-')
				value -= atol(val);
			else
			if (op == '*')
				value *= atol(val);
			else
			if (op == '%')
				value %= atol(val);
			else
			if (op == '/')
				value /= atol(val);

			snprintf(tmp, sizeof(tmp), "%ld", value);
			variable_set_deleted(var, 1);
			variable_add(var, tmp, TYPE_QSCRIPT, -1, vtype);
			ret = 0;
		}
		else
			ret = -EINVAL;

		var = utils_free("scripting.operators.var", var);
		val = utils_free("scripting.operators.val", val);
		free_tokens(t);
		return ret;
	}
	else
	/* Assignment */
	if (is_assignment(buf)) {
		tTokenizer t;

		t = tokenize(buf, "=");
		char *val = strdup( trim(t.tokens[1]) );
		if (val[strlen(val) - 1] == ';') {
			val[strlen(val) - 1] = 0;

			if (is_numeric(val) || is_string(val)) {
				if (is_string(val)) {
					*val++;
					val[strlen(val) - 1] = 0;
				}
				if (variable_add(trim(t.tokens[0]), val, TYPE_QSCRIPT, -1, gettype(val)) < 0) {
					desc_printf(gIO, gFd, "Cannot set new value to variable %s\n", trim(t.tokens[0]));
					ret = -EEXIST;
				}
				else
					ret = 0;
			}
			else
			if (regex_match("([^(]*)([^)]*)", val)) {
				tTokenizer t2;
				char *args = NULL;
				char *fn;

				t2 = tokenize(val, "(");
				if (t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] != ')') {
					ret = -EINVAL;
					goto cleanup;
				}

				t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] = 0;
				fn = strdup(t2.tokens[0]);

				/* We need to make sure parenthesis won't break script line */
				if (t2.numTokens > 1) {
					int i;
					char argstmp[8192] = { 0 };

					for (i = 1; i < t2.numTokens; i++) {
						strcat(argstmp, t2.tokens[i]);

						if (i < t2.numTokens - 1)
							strcat(argstmp, "(");
					}

					args = strdup(argstmp);
				}

				if (args != NULL) {
					char args2[1024] = { 0 };

					snprintf(args2, sizeof(args2), "%s", args + 1);
					args2[ strlen(args2) - 1] = 0;
					args = utils_free("scripting.function-call.args", args);

					args = strdup( args2 );
				}
				free_tokens(t2);

				if (_script_builtin_function(trim(t.tokens[0]), fn, args) != 0)
					DPRINTF("Function %s doesn't seem to be builtin, we should try user-defined function\n", fn);

				DPRINTF("%s: Should be a function with return value\n", __FUNCTION__);
				args = utils_free("scripting.function-call.args", args);
				fn = utils_free("scripting.function-call.fn", fn);

				ret = 0;
			}
			else
				ret = -EINVAL;
		}

		free_tokens(t);
	}
	else
	if (regex_match("([^(]*)([^)]*)", buf)) {
		tTokenizer t2;
		char *args = NULL;
		char *fn;

		t2 = tokenize(buf, "(");
		if (t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] != ';') {
			ret = -EINVAL;
			goto cleanup;
		}

		t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] = 0;
		fn = strdup(t2.tokens[0]);

		/* We need to make sure parenthesis won't break script line */
		if (t2.numTokens > 1) {
			int i;
			char argstmp[8192] = { 0 };

			for (i = 1; i < t2.numTokens; i++) {
				strcat(argstmp, t2.tokens[i]);

				if (i < t2.numTokens - 1)
					strcat(argstmp, "(");
			}

			args = strdup(argstmp);
		}

		if (args != NULL) {
			if (args[strlen(args) - 1] == ')')
				args[strlen(args) - 1] = 0;
		}
		free_tokens(t2);

		if (_script_builtin_function(NULL, fn, args) != 0)
			DPRINTF("Function %s doesn't seem to be builtin, we should try user-defined function\n", fn);

		args = utils_free("scripting.function-call.args", args);
		fn = utils_free("scripting.function-call.fn", fn);

		ret = 0;
	}
	else
		DPRINTF("%s: Not implemented yet\n", __FUNCTION__);

cleanup:
	if ((_perf_measure) && ((ts.tv_nsec > 0) && (ts.tv_sec > 0)) && (_script_in_condition_and_met > 0)) {
		tse = utils_get_time( TIME_CURRENT );
		desc_printf(gIO, gFd, "PERF: Line \"%s\" was being processed for %.3f microseconds (%.3f ms)\n\n",
			buf, get_time_float_us( tse, ts ), get_time_float_us(tse, ts) / 1000.);
	}

	return ret;
}

int run_script(char *filename)
{
	FILE *fp;
	int opened = 0;
	char buf[4096] = { 0 };
	struct timespec ts = utils_get_time( TIME_CURRENT );
	struct timespec tse;

	if (access(filename, R_OK) != 0)
		return -ENOENT;

	_script_in_condition_and_met = -1;

	fp = fopen(filename, "r");
	if (fp == NULL)
		return -EPERM;

	if (gHttpHandler)
		http_host_header(gIO, gFd, HTTP_CODE_OK, gHost, "text/html", NULL, myRealm, 0);

	while (!feof(fp)) {
		memset(buf, 0, sizeof(buf));
		fgets(buf, sizeof(buf), fp);

		if ((strlen(buf) > 1) && (buf[strlen(buf) - 1] == '\n'))
			buf[strlen(buf) - 1] = 0;

		if ((strlen(buf) > 1) && (buf[0] != '\n')) {
			if (strcmp(buf, "<$") == 0)
				opened = 1;
			if (strcmp(buf, "$>") == 0)
				opened = 0;

			if ((opened) && (strcmp(buf, "<$") != 0))
				script_process_line(trim(buf));
		}
	}

	fclose(fp);

	idb_free_last_select_data();

	if (_perf_measure) {
		tse = utils_get_time( TIME_CURRENT );
		desc_printf(gIO, gFd, "PERF: File \"%s\" has been processed in %.3f microseconds (%.3f ms)\n\n",
			basename(filename), get_time_float_us( tse, ts ), get_time_float_us(tse, ts) / 1000.);
	}

	variable_dump();
	variable_free_all();
	return 0;
}
Exemple #24
0
/**
 * Execute command line
 *
 * @v command		Command line
 * @ret rc		Return status code
 *
 * Execute the named command and arguments.
 */
int system ( const char *command ) {
	int count = split_command ( ( char * ) command, NULL );
	char *all_tokens[ count + 1 ];
	int ( * process_next ) ( int rc );
	char *command_copy;
	char **tokens;
	int argc;
	int process;
	int rc = 0;

	/* Create modifiable copy of command */
	command_copy = strdup ( command );
	if ( ! command_copy )
		return -ENOMEM;

	/* Split command into tokens */
	split_command ( command_copy, all_tokens );
	all_tokens[count] = NULL;

	/* Process individual commands */
	process = 1;
	for ( tokens = all_tokens ; ; tokens += ( argc + 1 ) ) {

		/* Find command terminator */
		argc = command_terminator ( tokens, &process_next );

		/* Expand tokens and execute command */
		if ( process ) {
			char *argv[ argc + 1 ];

			/* Expand tokens */
			if ( ( rc = expand_tokens ( argc, tokens, argv ) ) != 0)
				break;
			argv[argc] = NULL;

			/* Execute command */
			rc = execv ( argv[0], argv );

			/* Free tokens */
			free_tokens ( argv );
		}

		/* Stop processing, if applicable */
		if ( shell_stopped ( SHELL_STOP_COMMAND ) )
			break;

		/* Stop processing if we have reached the end of the
		 * command.
		 */
		if ( ! process_next )
			break;

		/* Determine whether or not to process next command */
		process = process_next ( rc );
	}

	/* Free modified copy of command */
	free ( command_copy );

	return rc;
}
Exemple #25
0
int tokenize(const char *parameters, const char parameter_delimiter,
		const char key_value_delimiter, cvar_token_t **list_head)
{
	char *param;
	char *param_start, *param_end;
	char *key_start, *key_end;
	cvar_token_t *lhead, *prev, *curr;
	int more_params;
	int no_value;
	int ret = 0;

	if (!parameters)
		goto out;

	ret = -1;

	lhead = prev = NULL;

	param = strdup(parameters);
	if (!param) {
		cvar_log_error("Out of memory");
		goto cleanup;
	}

	param_start = param;
	more_params = 1;

	while (more_params) {
		param_end = strchr(param_start, parameter_delimiter);
		if (param_end)
			*param_end = '\0';
		else {
			param_end = param_start + strlen(param_start);
			more_params = 0;
		}

		if (param_start != param_end) {
			key_start = param_start;
			key_end = strchr(param_start, key_value_delimiter);
			if (key_end) {
				*key_end = '\0';
				no_value = 0;
			} else {
				key_end = param_end;
				no_value = 1;
			}

			if (key_start == key_end) {
				cvar_log_error("Empty key at position %lu in parameter string "
						"\"%s\"", ((unsigned long)(key_start - param))/sizeof(char) + 1,
						parameters);
				goto cleanup;
			}


			curr = (cvar_token_t *) malloc(sizeof(cvar_token_t));
			if (!curr) {
				cvar_log_error("Out of memory");
				goto cleanup;
			}

			memset(curr, 0x00, sizeof(cvar_token_t));

			curr->key = strdup(key_start);
			if (!curr->key) {
				cvar_log_error("Out of memory");
				goto cleanup;
			}

			if (!no_value) {
				curr->value = strdup(key_end+1);
				if (!curr->value) {
					cvar_log_error("Out of memory");
					goto cleanup;
				}
			}

			if (!prev)
				lhead = prev = curr;
			else {
				prev->next = curr;
				prev = curr;
			}
		}

		if (more_params)
			param_start = param_end + 1;
	}

	*list_head = lhead;
	ret = 0;

out:
	return ret;

cleanup:
	free_tokens(lhead);
	lhead = NULL;
	goto out;
}
void* process(void *file_chunk)
{
	char* chunk = (char*)file_chunk;
	char* string = (char*)malloc(sizeof(char)*strlen("tester1.txt")+1);
	
//	get_stats();
//	sprintf(string, "tester%f.txt", (i)webstats.local_bytes++);
//	return_stats();
//	
	FILE* file = tmpfile();
	if (file == NULL)
	{
		err_sys("Unable to create temp file\n");
		exit(EXIT_FAILURE);
	}
	fseek(file, 0L, SEEK_SET);
	fwrite(chunk, sizeof(char), strlen(chunk), file);
	rewind(file);
	
//	get_stats();
//	FILE* validation_file = fopen("validation.txt","a");
//	fwrite(chunk, sizeof(char), strlen(chunk), validation_file);
//	fclose(validation_file);
//	return_stats();
	
//	sleep(1);
//	printf("Thread id: %u\n", pthread_self());
//	printf("%s", chunk);

	
	char *linebuffer = (char *) malloc(sizeof(char) * MAX_LINE_SIZE);
	char **field = (char **) malloc(sizeof(char *) * MAX_NUM_FIELDS);
	char *end_date = (char *) malloc(sizeof(char) * MAX_LINE_SIZE);

//	char *s;
//	while((s= fgets(linebuffer, MAX_LINE_SIZE, file))!=NULL)
//		printf("%s", linebuffer);
	char *s = fgets(linebuffer, MAX_LINE_SIZE, file);
	if (s != NULL)
	{
//		printf("linebuffer\n");
//		printf("%s", linebuffer);
		int num = parse_line(linebuffer, " []\"", field);
//		printf("passed line parsing in thread %u\n", pthread_self());
		update_webstats(field);
		printf("Starting date: %s\n",field[3]);
		free_tokens(num, field);
		
		while (fgets(linebuffer, MAX_LINE_SIZE, file) != NULL)
		{
			int num = parse_line(linebuffer, " []\"", field);
//			print_field(field);
			strcpy(end_date, field[3]);
			update_webstats(field);
			free_tokens(num, field);
			strcpy(linebuffer,"");
		}
		printf("Ending date: %s\n", end_date);
//		free(end_date);
//		free(linebuffer);
//		free(field);
		fclose(file);
	}
	else
	{
		fprintf(stderr,"Unable to read chunk. Aborting thread\n");
		exit(EXIT_FAILURE);
	}
	printf("DONE!\n");
	return NULL;
}
Exemple #27
0
void process_data(tokens fields) {

	tokens double_stops;
	char *character;
	int i, j;

	/* A data record may not follow a record with a new path indicator */

	if (new_path) {
		print_syntax_error(ERROR1);	
		new_path = FALSE;
	}

	/* Keep statistics for the -v option */

	if (verbose) {
		number_of_data_records += 1;
	}

	/* A data record may not appear before the first interpretation */
	/* record and must have the correct number of spines			*/

	if (!first_interpretation_yet) {
		print_syntax_error(ERROR12);
		current_no_of_spines = fields.number;
	} else if (current_no_of_spines != fields.number) {
		print_syntax_error(ERROR13);
	}

	/* Loop through each spine in the data record */

	for (j = 0; j < fields.number; j += 1) {

		/* Check for multiple-stop space errors */

		if (strncmp(fields.token[j],SPACE,1) == 0) {
			print_syntax_error(ERROR23);
		}
		character = strrchr(fields.token[j],SPACE_CHAR);
		if (character != NULL) {
			character += 1;
			if (*character == TERMINATE) {
				print_syntax_error(ERROR24);
			}
		} 
		if (strstr(fields.token[j],CONSECUTIVE_SPACE) != NULL) {
				print_syntax_error(ERROR25);
		} 

		/* Give a warning if any spine after the first spine may be   */
		/* misinterpreted as a different type of record in the future */

		if (j > 0) {
			if (strncmp(fields.token[j],"!!",2) == 0) {
				print_syntax_error(WARNING2);
			} else if (strncmp(fields.token[j],"!",1) == 0) {
				print_syntax_error(WARNING3);
			} else if (strncmp(fields.token[j],"**",2) == 0) {
				print_syntax_error(WARNING4);
			} else if (strncmp(fields.token[j],"*",1) == 0) {
				print_syntax_error(WARNING5);
			}
		}

		/* Keep statistics for the -v option */

		if (verbose) {
			if (strcmp(fields.token[j],NULL_TOKEN) == 0) {
				null_tokens += 1;
			}
			character = fields.token[j];
			while (*character != TERMINATE) {
				if (signifiers[*character] == 0) {
					signifiers[*character] = *character;
				}
				character += 1;
			}
		}

		/* Check that no null tokens are allocated in double stops */

		split_string(fields.token[j],SPACE,&double_stops);
		for (i = 0; i < double_stops.number; i += 1) {
			if (strcmp(double_stops.token[i],NULL_TOKEN) ==  0
												&& double_stops.number > 1) {
				print_syntax_error(ERROR26);
				break;
			}
		}
		free_tokens(double_stops);
	}
}
Exemple #28
0
command *get_commands(char *input) {

	//--------------------------- Local variables -----------------------------

	// Reference to beginning of tokens array; pointer into tokens array.
	char **tokens, **t_ptr;

	// Temporary string pointer.
	char *str;

	// Number of tokens, used to see if we're at the last token.
	int num_toks = 0;

	// All the commands.
	command *commands;

	// Index of token we're looking at, index of command.
	int ti, ci;

	//------------------------------- Real work -------------------------------

	// Get the tokens, make sure the list isn't empty.
	tokens = get_tokens(input);
	if (tokens == NULL) {
		// Don't need to free b/c can't free a NULL pointer...
		return NULL;
	}

	// Get the number of tokens, return NULL if no tokens.
	t_ptr = tokens;
	while(*(t_ptr++) != NULL) {
		num_toks++;
	}
	if (num_toks == 0) {
		// The "tokens" pointer itself wasn't NULL; we had a NULL-terminated
		// empty list instead, so a malloc was performed. Free it.
		free_tokens(tokens);
		return NULL;
	}

	// If a pipe is the first or last token throw an error message and return
	// NULL because the command is unparseable.
	if(strcmp(tokens[0], "|") == 0 || strcmp(tokens[num_toks - 1], "|") == 0) {
		fprintf(stderr, "'|' can't be first or last token in command.\n");
		free_tokens(tokens);
		return NULL;
	}

	// Allocate the commands array.
	commands = (command *) malloc (sizeof(command) * MAX_TOKS);
	if (commands == NULL) {
		fprintf(stderr, "Error when allocating \"commands\" array.\n");
		// Don't need to free tokens before leaving because we're exiting the
		// program...
		exit(1);
	}

	// Initialize everything in the commands array.
	for (ci = 0; ci < MAX_TOKS; ci++) {
		commands[ci].argv = NULL;
		commands[ci].argc = 0;
		commands[ci].ifile = NULL;
		commands[ci].ofile = NULL;
		commands[ci].append = false;
	}

	// Loop over the array of tokens, filling in command struct members
	// between pipes.
	ci = 0; // command index
	for(ti = 0; ti < num_toks; ti++) { // Iterate over the tokens.

		// Process pipe tokens by incrementing the command index. It's safe to
		// assume a pipe isn't the first token or last token because of an
		// earlier check, but we do need to make sure that the bare minimum
		// struct members have been initialized.
		if (strcmp(tokens[ti], "|") == 0) {

			// Make sure that the current command has a non-null "argv" and
			// argc >= 1.
			if (commands[ci].argc <= 0 || commands[ci].argv == NULL) {
				fprintf(stderr, "Parser didn't set at least program name.\n");
				free_tokens(tokens);
				return NULL;
			}

			// If we get here the current command has the minimum fields set,
			// so move onto the next command.
			commands[ci].argv[commands[ci].argc] = NULL;
			ci++;
		}
		// Process output redirection operator. The next token must be a file
		// path, whether relative or absolute. Corollary: this token can't be
		// the final one and this token can't be right before a pipe.
		else if (strcmp(tokens[ti], ">") == 0) {

			// Make sure next token exists and is legitimate.
			if (ti == num_toks - 1 ||
					strcmp(tokens[ti + 1], "|") == 0 ||
					strcmp(tokens[ti + 1], ">") == 0 ||
					strcmp(tokens[ti + 1], ">>") == 0 ||
					strcmp(tokens[ti + 1], "<") == 0) {
				fprintf(stderr, "There must be a valid file after \">\".\n");
				free_tokens(tokens);
				return NULL;
			}

			// If we get here the next token is legitimate. Use it and skip it.
			str = (char *) malloc (sizeof(char) *
					(strlen(tokens[ti + 1]) + 1));
			strcpy(str, tokens[ti + 1]);
			commands[ci].ofile = str;
			ti++; // Skip the next token.
		}
		// Process output redirection append operator. The next token must be a
		// file path, whether relative or absolute. Corollary: this token can't
		// be the final one.
		else if (strcmp(tokens[ti], ">>") == 0) {

			// Make sure next token exists and is legitimate.
			if (ti == num_toks - 1 ||
					strcmp(tokens[ti + 1], "|") == 0 ||
					strcmp(tokens[ti + 1], ">") == 0 ||
					strcmp(tokens[ti + 1], ">>") == 0 ||
					strcmp(tokens[ti + 1], "<") == 0) {
				fprintf(stderr, "There must be a valid file after \">>\".\n");
				free_tokens(tokens);
				return NULL;
			}

			// If we get here the next token is legitimate. Use it and skip it.
			str = (char *) malloc (sizeof(char) *
					(strlen(tokens[ti + 1]) + 1));
			strcpy(str, tokens[ti + 1]);
			commands[ci].ofile = str;
			ti++; // Skip the next token.
			commands[ci].append = true;
		}
		// Process output redirection operator. The next token must be a file
		// path, whether relative or absolute. Corollary: this token can't be
		// the final one.
		else if (strcmp(tokens[ti], "<") == 0) {

			// Make sure next token exists and is legitimate.
			if (ti == num_toks - 1 ||
					strcmp(tokens[ti + 1], "|") == 0 ||
					strcmp(tokens[ti + 1], ">") == 0 ||
					strcmp(tokens[ti + 1], ">>") == 0 ||
					strcmp(tokens[ti + 1], "<") == 0) {
				fprintf(stderr, "There must be a valid file after \"<\".\n");
				free_tokens(tokens);
				return NULL;
			}

			// If we get here the next token is legitimate. Use it and skip it.
			str = (char *) malloc (sizeof(char) *
					(strlen(tokens[ti + 1]) + 1));
			strcpy(str, tokens[ti + 1]);
			commands[ci].ifile = str;
			ti++; // Skip the next token.
		}
		// Process arguments or program names. Redirection filename tokens are
		// already handled by the >, <, and >> conditionals. If the "argv"
		// array is still null then initialize it. Once it's initialized (or
		// if it was already initialized) add tokens by copying them into
		// a new chunk of memory then logging the pointer in "argv".
		else {

			// Initialize if necessary. There can be only as many arguments
			// as tokens. We need to allocate space for a null terminator, too.
			if (commands[ci].argv == NULL) {
				commands[ci].argv = (char **)
						malloc (sizeof(char *) * (num_toks + 1));
				if (commands[ci].argv == NULL) {
					fprintf(stderr, "Problem allocating argv.\n");
					exit(1);
				}
			}

			// By here "argv" should have been allocated. Copy the current
			// token and append it to "argv".
			str = (char *) malloc (sizeof(char) * (strlen(tokens[ti]) + 1));
			strcpy(str, tokens[ti]);
			commands[ci].argv[commands[ci].argc] = str;
			commands[ci].argc++;
		}
	}
	// Make sure that the current command has a non-null "argv" and argc >= 1.
	if (commands[ci].argc <= 0 || commands[ci].argv == NULL) {
		fprintf(stderr, "Parser didn't set at least program name.\n");
		free_tokens(tokens);
		return NULL;
	}
	commands[ci].argv[commands[ci].argc] = NULL; // NULL-terminate last "argv".

	free_tokens(tokens); // It's OK to free tokens here b/c they were copied.

	return commands;
}
Exemple #29
0
/*
 * Scan the named file, createing a grid_info struct for each grid.  Store
 * the grid_info structs in the grid data base.
 * Input:  name - name of grads file.
 *         db - the grid data base
 * Return:  number of grids found.
 */
int get_grads_info( char *name, struct grid_db *db )
{
   FILE *f;
   char **token;
   int ntokens;
   int time, var;
   int grids = 0;
   struct grid_info *info;
   int pos = 0;
   int fileheader_size = 0;
   float args[1000];
   struct projection *proj;
   struct vcs *vcs[MAXVARS];

   /* GrADS file info: */
   char dset_name[1000];
   float missing_value;
   int byteswapped = 0;
   int nr, nc, nl[MAXLEVELS], maxnl;
   int vertical;
   float westbound, northbound, bottombound;
   float rowinc, colinc, levinc;
   float height[MAXLEVELS];
   char varname[MAXVARS][100];
   int timestamp[MAXTIMES], datestamp[MAXTIMES];
   int numtimes, numvars;
   int projection_type = PROJ_LINEAR;
   int use_file_template = 0;
   char specific_file_name[TEMPLATE_OUTPUT_LEN];

   /* Initialize args */
   args[0] = args[1] = args[2] = args[3] = args[4] = 0.0f;

   f = fopen( name, "r" );
   if (!f) {
      printf("Error: couldn't open %s\n", name );
      return 0;
   }
   

   while (1) {
      char line[MAXLINE];
      /* read a line of the control file */
      if (!fgets( line, MAXLINE, f ))
         break;

      /* break line into tokens */
      token = tokenize( line, &ntokens );

      if (ntokens==0) continue;

      if (strcasecmp(token[0],"DSET")==0) {
         if (ntokens<2) {
            printf("Error: missing filename argument on DSET line\n");
         }
         else {
            if (token[1][0]=='^') {

               /* skip the ^ (current directory) character */
               /* This is not the current directory, rather it is the same 
                  directory in which the control file is located */
              int i;
              for(i=strlen(name)-1;i>=0;i--){
                if(name[i] == '/'){
                  strncpy(dset_name,name,i+1);
                  i++;
                  break;
                }
              } 
				  if(i<0) i=0; /* No path in ctl file name */
              strcpy( dset_name+i, token[1]+1 );
            }
            else {
               strcpy( dset_name, token[1] );
            }
         }
			if(Debug_i) printf("dset name >%s<\n",dset_name);
      }
      else if (strcasecmp(token[0],"TITLE")==0) {
         /* ignore the title line */
      }
      else if (strcasecmp(token[0],"UNDEF")==0) {
         missing_value = atof(token[1]);
      }
      else if (strcasecmp(token[0],"BYTESWAPPED")==0) {
         /* Is this valid??? Shouldn't it be "OPTIONS BYTESWAPPED" ? */
         byteswapped = 1;
      }
      else if (strcasecmp(token[0],"FORMAT")==0) {
         if (strcasecmp(token[1],"SEQUENTIAL")==0) {
               /* this is the only format currently supported; */
               /* also note: FORMAT keyword has been replaced by OPTIONS */
         }
         else {
            printf("Warning: FORMAT not fully supported\n");
            printf("         only SEQUENTIAL format is allowed.\n");
         }
      }
      else if (strcasecmp(token[0],"OPTIONS")==0) {
         if (strcasecmp(token[1],"SEQUENTIAL")==0) {
            /* Don't need to do anything, supported by default??? */
         }
         else if (strcasecmp(token[1],"BYTESWAPPED")==0) {
            byteswapped=1;
         }
         else if (strcasecmp(token[1],"TEMPLATE")==0) {
            use_file_template=1;
         }
#ifdef WORDS_BIGENDIAN
	      else if (strcasecmp(token[1],"LITTLE_ENDIAN")==0 ) {
           byteswapped=1;
         }
#else
	      else if (strcasecmp(token[1],"BIG_ENDIAN")==0  ) {
           byteswapped=1;
         }
#endif
         else {
            printf("Warning: OPTIONS %s not supported\n", token[1]);
         }
      }
      else if (strcasecmp(token[0],"FILEHEADER")==0) {
         if (ntokens<2) {
            printf("Error: missing position argument on FILEHEADER line\n");
         }
         fileheader_size = atoi( token[1] );
         pos = fileheader_size;
      }
      else if (strcasecmp(token[0],"XDEF")==0) {
         if (ntokens<4) {
            printf("Error: missing arguments to XDEF line\n");
         }
         else {
            nc = atoi( token[1] );
            if (strcasecmp(token[2],"LINEAR")==0) {
               westbound = -atof( token[3] );
               colinc = atof( token[4] );
            }
            else if (strcasecmp(token[2],"LEVELS")==0) {
               printf("Warning: XDEF LEVELS not fully supported\n");
               westbound = -atof( token[3] );
               colinc = fabs( atof(token[3]) - atof(token[4]) );
            }
            else {
               printf("Warning: XDEF %s not fully supported\n", token[2]);
            }
         }
      }
      else if (strcasecmp(token[0],"YDEF")==0) {
         if (ntokens<4) {
            printf("Error: missing arguments to YDEF line\n");
         }
         else {
            nr = atoi( token[1] );
            if (strcasecmp(token[2],"LINEAR")==0) {
               float southbound = atof( token[3] );
               rowinc = atof( token[4] );
               northbound = southbound + rowinc * (nr-1);
            }
            else if (strncmp(token[2],"GAUSR",5)==0
                     || strncmp(token[2],"gausr",5)==0) {
               printf("Warning: YDEF GAUSRnn not supported\n");
            }
            else {
               printf("Warning: YDEF %s not fully supported\n", token[2]);
            }
         }
      }
      else if (strcasecmp(token[0],"PDEF")==0) {
         if (ntokens<4) {
            printf("Error: missing arguments to PDEF line\n");
         }
         else {
            nc = atoi( token[1] );
            nr = atoi( token[2] );
            if (strcasecmp(token[3],"PSE")==0) {
               if (ntokens<11) {
                  printf("Error: missing arguments to PDEF line\n");
                  printf("'PDEF ... pse ...' must have 10 arguments\n");
               }
               else {
                  double pseLat       = atof( token[4] );    /* degrees */
                  double pseLon       = atof( token[5] );    /* degrees */
                  double pseCenterCol = atof( token[6] );    /* grid units */
                  double pseCenterRow = atof( token[7] );    /* grid units */
                  double pseDeltaX    = atof( token[8] );    /* in km */
                  /*double pseDeltaY    = atof( token[9] ); */   /* ignored */
                  int    pseNorthOrSouth = atoi( token[10] );/* +1 or -1 */

                  /* grads documentation describes this format as 
                     "high accuracy polar stereo (eccentric)" while vis5d
                     calls it "azimuthal stereographic". The conversions
                     performed here are somewhat cryptic, and were obtained
                     with some combination of reading the documentation and
                     trial and error. */
                  /* center latitude. NOT TESTED FOR SOUTH POLAR PROJECTION! */
                  args[0] = pseNorthOrSouth * 90.0;

                  /* center longitude. Why minus 270? Seems to work though. */
                  args[1] = pseLon - 270.0;
                  /* put longitude in the range -180 to +180 */
                  args[1] = fmod( args[1], 360.0 );
                  if ( args[1] > 180.0 ) args[1] -= 360.0;
                  if ( args[1] <-180.0 ) args[1] += 360.0;

                  /* center grid row. Why the minus sign and nr-1? */
                  args[2] = nr - ( pseCenterRow + 1 );

                  /* center grid column */
                  args[3] = pseCenterCol;

                  /* column inc. in km, GrADS gives column increment at this
                     lattitude, we need column increment at the pole. */
                  args[4] = pseDeltaX * ( 1.0 + sin( 90/57.29578 ) ) /
                                        ( 1.0 + sin( pseLat/57.29578 ) );

                  /* what to do with token[9] - row inc. in km??? */
                  /* what to do with token[10] - 1 is N pole, -1 is S pole???*/
                  projection_type = PROJ_STEREO;
               }
            }
            else {
               printf("Warning: \"PDEF ... %s ...\" not supported\n", token[3] );
            }
         }
      }
      else if (strcasecmp(token[0],"ZDEF")==0) {
         if (ntokens<3) {
            printf("Error: missing arguments to ZDEF line\n");
         }
         else {
            float pressure[MAXLEVELS];
            int i;
            maxnl = atoi( token[1] );
            if (strcasecmp(token[2],"LINEAR")==0) {
               vertical = VERT_EQUAL_KM;
               bottombound = atof( token[3] );
               levinc = atof( token[4] );
               for (i=0;i<maxnl;i++) {
                  pressure[i] = bottombound + i * levinc;
               }
            }
            else if (strcasecmp(token[2],"LEVELS")==0) {
               vertical = VERT_UNEQUAL_MB ; /*VERT_UNEQUAL_KM;*/
               for (i=0;i<maxnl && i+3<ntokens;i++) {
                  pressure[i] = atof( token[3+i] );
                  height[i] = pressure_to_height(pressure[i]);
               }
            }

         }

      }
      else if (strcasecmp(token[0],"TDEF")==0) {
         if (ntokens!=5) {
            printf("Error: missing arguments to TDEF line\n");
         }
         else {
            int date0, time0, days, seconds;
            int i, it, id, ii;

            numtimes = atoi( token[1] );
            /* token[2]=="LINEAR" */

            if (!parse_time( token[3], &date0, &time0 )) {
               printf("Error reading grads header, bad time: %s\n", token[3] );
            }
            if (!parse_time_inc( token[4], &days, &seconds )) {
               printf("Error reading grads header, bad time increment: %s\n",
                      token[4] );
            }

            id = v5dYYDDDtoDays(date0);
            it = v5dHHMMSStoSeconds(time0);
            for (i=0;i<numtimes&&i<MAXTIMES;i++) {
               timestamp[i] = v5dSecondsToHHMMSS(it);
               datestamp[i] = v5dDaysToYYDDD(id);
               it = it + seconds;
               ii = it / 86400;
               it = it - 86400 * ii;
               id = id + days + ii;
            }
         }
      }
      else if (strcasecmp(token[0],"VARS")==0) {
         /* TODO: variables */
         if (ntokens!=2) {
            printf("Error: wrong number of arguments to VARS line\n");
         }
         else {
            char **vartok;
            int i, ntok;

            numvars = atoi( token[1] );

            for (i=0;i<numvars;i++) {
               fgets( line, MAXLINE, f );
               vartok = tokenize( line, &ntok );
               strcpy( varname[i], vartok[0] );   /* var name */
               nl[i] = atoi( vartok[1] );         /* number of levels */

               /* JPE nl=0 has a special meaning and will create a 
						different vcs below 
					  if (nl[i]==0)  nl[i] = 1; */
					
               /* vartok[2] = units */
               /* vartok[3] = text description */
               free_tokens( vartok );
            }
         }
      }
      else if (strcasecmp(token[0],"ENDVARS")==0){
         /* ignore */
      }
      else {
         printf("Warning: unknown token: %s\n",token[0] );
      }

   } /*while*/


   /*
    * Generate grid_info structs
    */

   if ( projection_type == PROJ_LINEAR ) {

      args[0] = northbound;
      args[1] = westbound;
      args[2] = rowinc;
      args[3] = colinc;
      proj = new_projection( db, PROJ_LINEAR, nr, nc, args );
   }
   else if ( projection_type == PROJ_STEREO ) {
      /* args have already been set properly. */
      proj = new_projection( db, PROJ_STEREO, nr, nc, args );
   }

   /* Potentially different vcs for each grid because number of levels */
   /* can vary per variable. */
   for (var=0;var<numvars;var++) {
      if (vertical==VERT_EQUAL_KM) {
         args[0] = bottombound;
         args[1] = levinc;
      }
      else {
         int i;
         if(nl[var]==0){
			  nl[var]=1;
			  args[0] = 0.0;
			}else{
			  for (i=0;i<maxnl;i++) {
				 args[i] = height[i];
			  }
			}
      }
      vcs[var] = new_vcs( db, vertical, nl[var], 0, args );
   }

   if ( numtimes > MAXTIMES ) {
      printf("Warning: %d is too many time steps, %d is limit.\n",
             numtimes, MAXTIMES );
   }
   for (time=0;time<numtimes&&time<MAXTIMES;time++) {
      if ( use_file_template ) {
         int yr, mo, dy, hr, mn, jday;
         char prev_file_name[1000];

         /* Save the old file name so we can see if it has changed. */
         strcpy( prev_file_name, specific_file_name );

         /* Replace FileName, which may be a format string, with result
            of expanding the file format string. */
         yr = yy2yyyy( datestamp[time] / 1000 );
         jday = datestamp[time] - 1000*(datestamp[time] / 1000);
         julian2mmdd( yr, jday, &mo, &dy );
         hr = timestamp[time] / 10000;
         mn = timestamp[time] / 100 - hr*100;
	 expand_GrADS_file_template(dset_name, specific_file_name,
				    yr, mo, dy, hr, mn,
				    hr, yr, mo, dy, hr, mn);

         if ( strcmp( prev_file_name, specific_file_name ) != 0 ) {
            /* Changing files - reset pos to the beginning of the file */
            pos = fileheader_size;
         }
         printf("In get_grads_info, date:%05d, time:%06d, file:%s\n",
                 datestamp[time], timestamp[time], specific_file_name );
      } else {
         strcpy( specific_file_name, dset_name );
      }

      for (var=0;var<numvars;var++) {

         info = alloc_grid_info();


         info->FileName = strdup( specific_file_name );
         info->Format = FILE_GRADS;
         info->TimeStep = time;
         info->VarNum = var;
         info->Position = pos;         /* pos. of data in binary grid file */
         pos += nr * nc * nl[var] * 4;

         info->Nr = nr;
         info->Nc = nc;
         info->Nl = nl[var];

         info->DateStamp = datestamp[time];
         info->TimeStamp = timestamp[time];
         info->VarName = strdup( varname[var] );

         info->Proj = proj;
         info->Vcs = vcs[var];

         info->MissingValue = missing_value;
         info->byteswapped = byteswapped;

         append_grid( info, db );
         grids++;

      }

   }

   return grids;
}
Exemple #30
0
void *cvar_alloc_handle(const char *cvar_parameters,
		void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr))
{
	cvar_token_t *list_head;;
	cvar_token_t *t;
	handle_t handle;
	handle_t *state = NULL;
	int ret = 0;

	cvar_trace("entry");

	/* Tokenize parameters supplied by filebench. */
	list_head = NULL;
	ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER,
			DEFAULT_KEY_VALUE_DELIMITER, &list_head);
	if (ret)
		goto out;

	/* Get the value of lower, upper and mode. */
	t = find_token(list_head, RT_LOWER);
	if (t && t->value) {
		t->used = 1;
		handle.lower = atof(t->value);
	} else
		handle.lower = RT_LOWER_DEFAULT;

	t = find_token(list_head, RT_UPPER);
	if (t && t->value) {
		t->used = 1;
		handle.upper = atof(t->value);
	} else
		handle.upper = RT_UPPER_DEFAULT;

	t = find_token(list_head, RT_MODE);
	if (t && t->value) {
		t->used = 1;
		handle.mode = atof(t->value);
	} else
		handle.mode = RT_MODE_DEFAULT;

	cvar_trace("lower = %lf, upper = %lf, mode = %lf", handle.lower,
			handle.upper, handle.mode);

	/* Validate parameters. */
	if (handle.upper < handle.lower) {
		cvar_log_error("Invalid parameter values: lower = %lf and upper = %lf. "
				"upper must be greater than lower", handle.lower, handle.upper);
		goto out;
	}

	if ((handle.mode > handle.upper) || (handle.mode < handle.lower)) {
		cvar_log_error("Invalid parameter values: lower = %lf, mode = %lf and "
				"upper = %lf. mode must be between lower and upper",
				handle.lower, handle.mode, handle.upper);
		goto out;
	}

	/* Check if there are unused tokens. */
	t = unused_tokens(list_head);
	if (t) {
		cvar_log_error("Unsupported parameter %s", t->key);
		goto out;
	}

	/* Seed the state. */
	mts_goodseed(&handle.state);

	/* All set. Now allocate space for the handle in the shared segment and
	 * copy the state over. */
	state = (handle_t *) cvar_malloc(sizeof(handle_t));
	if (!state) {
		cvar_log_error("Out of memory");
		goto out;
	}

	*state = handle;

out:
	free_tokens(list_head);

	cvar_trace("exit");
	return state;
}