Beispiel #1
0
Datei: env.c Projekt: zspeer/zash
void set_zenv(char *n, char *s, int overwrite)
{
  int exists;
  char *tmp;

  tmp = malloc(sizeof(char) * strlen(n) + strlen(s) + 2);
  strcpy(tmp, n);
  strcat(tmp, "=");
  strcat(tmp, s);
  
  exists = var_exists(n);
  if(exists)
    {
      if(!overwrite)
	{
	  if(tmp)
	    free(tmp);
	  return;
	}
      if(env[exists])
	free(env[exists]);
	
      env[exists] = strdup(tmp);
      if(tmp)
	free(tmp);

      return;
    }
  else
    {
      char **tmp_env;
      int i;

      ++env_len;
      tmp_env = malloc(sizeof(char *) * env_len + 1);
      for(i = 0; i < env_len; ++i)
	{
	  tmp_env[i] = strdup(env[i]);
	  free(env[i]);
	}
      tmp_env[i] = tmp;
      free(tmp);
      if(env)
	free(env);
      env = malloc(sizeof(char *) * env_len + 1);
      for(i = 0; i < env_len; ++i)
	{
	  env[i] = strdup(tmp_env[i]);
	  free(tmp_env[i]);
	}
      env[i] = NULL;
      free(tmp_env);
      return;
    }  
}
Beispiel #2
0
Datei: env.c Projekt: zspeer/zash
void unset_zenv(char *n)
{
  int exists;

  --env_len;
  exists = var_exists(n);
  if(!exists)
    return;

  while(exists < env_len)
    {
      if(env[exists])
	free(env[exists]);
      env[exists] = strdup(env[exists + 1]);
      ++exists;
    }
}
Beispiel #3
0
int main(int argc, char *argv[])
{

    char cdf_name[250];

    /***************** declared local pointers to store returned attribute value pointers ************/

    char *char_attribute_valuePtr;
    int *int_attribute_valuePtr;
    float *float_attribute_valuePtr;

    if (argc != 2)
    {
        printf("ERROR:this example program requires 1 argument - full path and file name of data file\n");
        exit( EXIT_FAILURE);
    }

    strcpy(cdf_name, argv[1]);

    open_cdf(cdf_name, 0);

    printf("Open %s complete...\n", cdf_name);

    /*** get the README GLOBAL ATTRIBUTE value ***/

    if (attribute_exists("README") )
        char_attribute_valuePtr = gattribute_get("README");
    if (attribute_exists("README") )
    {
        printf("\n\nREADME attribute = %s\n", char_attribute_valuePtr);
    }

    /*** get the grid_system_count global attribute value ***/

    if (attribute_exists("grid_system_count") )
        int_attribute_valuePtr = gattribute_get("grid_system_count");
    if (attribute_exists("grid_system_count") )
    {
        printf("\ngrid_system_count attribute = %d\n\n",
                *int_attribute_valuePtr);
    }

    /*** test for the existence of gattributes ***/

    printf("attribute_exists( \"model_name\" ) = %d\n",
            attribute_exists("model_name") );
    printf("attribute_exists( \"model_type\" ) = %d\n",
            attribute_exists("model_type") );
    printf("attribute_exists( \"elapsed_time_in_seconds\") = %d\n",
            attribute_exists("elapsed_time_in_seconds") );
    printf("attribute_exists( \"horse_power\") = %d\n",
            attribute_exists("horse_power") );
    printf("attribute_exists( \"units\") = %d\n", attribute_exists("units") );

    /*** get the elapsed_time_in_seconds global attribute value ***/

    if (attribute_exists("elapsed_time_in_seconds") )
        float_attribute_valuePtr = gattribute_get("elapsed_time_in_seconds");
    if (attribute_exists("elapsed_time_in_seconds") )
    {
        printf("\nelapsed_time_in_seconds attribute = %f\n",
                *float_attribute_valuePtr);
    }

    /*** get the description variable ATTRIBUTE value for x ***/

    if (var_exists("x") && attribute_exists("description") )
        char_attribute_valuePtr = vattribute_get("x", "description");
    if (var_exists("x") && attribute_exists("description") )
    {
        printf("\ndescription attribute = %s\n", char_attribute_valuePtr);
    }

    /*** get the is_vector_component variable attribute value for bx ***/

    if (var_exists("bx") && attribute_exists("is_vector_component") )
        int_attribute_valuePtr = vattribute_get("bx", "is_vector_component");
    if (var_exists("bx") && attribute_exists("is_vector_component") )
    {
        printf("\nis_vector_component attribute = %d\n",
                *int_attribute_valuePtr);
    }

    /*** get the valid_min variable attribute value for rho***/

    if (var_exists("rho") && attribute_exists("valid_min") )
        float_attribute_valuePtr = vattribute_get("rho", "valid_min");
    if (var_exists("rho") && attribute_exists("valid_min") )
    {
        printf("\nvalid_min attribute = %f\n", *float_attribute_valuePtr);
    }

    /*** get the units variable attribute value for rho***/

    if (var_exists("rho") && attribute_exists("units") )
        char_attribute_valuePtr = vattribute_get("rho", "units");
    if (var_exists("rho") && attribute_exists("units") )
    {
        printf("\nunits attribute = %s\n", char_attribute_valuePtr);
    }

    printf("DEBUG\tlegacy stuff\n\n\n\n");

    /********** legacy routines... before generic *attribute_get routines were available ***/

    /** char * get_units( "variable_name" ); **/

    if (var_exists("x") && attribute_exists("units") )
        printf(
                "\nAN example of using the get_units() routine:  units for X = %s\n",
                get_units("x") );

    /*** float gattribute_float_get( "global_attribute_name" ); **/

    if (attribute_exists("elapsed_time_in_seconds") )
        printf(
                "\nAN example of using the gattribute_float_get() routine:  elapsed_time_in_seconds %f\n",
                gattribute_float_get("elapsed_time_in_seconds") );

    /*** char * gattribute_char_get( "global_attribute_name" ); **/

    if (attribute_exists("model_name") )
        printf(
                "\nAN example of using the gattribute_char_get() routine, model_name = %s\n",
                gattribute_char_get("model_name") );

    return 1;
}
Beispiel #4
0
static int F() { // function declaration
    Tdata_type ftype;

    switch (token.type) {
    case TT_INT:
    case TT_DOUBLE:
    case TT_STRING: // rule: type idf ( AL ) FD
        ftype = get_type();
        tmp_ftype = ftype;
        next();

        check(TT_ID);
        try(add_func(ftype, token.p_string));
        next();

        term(TT_L_BRACKET);
        call(AL);
        term(TT_R_BRACKET);
        return(FD);

    default:
        return ERR_PARSER;
    }
}

static int AL() { // first function argument
    switch (token.type) {
    case TT_INT:
    case TT_DOUBLE:
    case TT_STRING: // rule: AL -> AD AL2
        call(AD);
        return(AL2);

    default: // rule: AL -> eps
        return OK;
    }
}

static int AL2() { // remaining function arguments
    switch (token.type) {
    case TT_COMMA: // rule: AL2 -> , AD AL2
        next();
        call(AD);
        return(AL2);

    default: // rule: AL2 -> eps
        return OK;
    }
}

static int AD() { // argument
    Tdata_type atype;

    switch (token.type) {
    case TT_INT:
    case TT_DOUBLE:
    case TT_STRING: // rule: AD -> type id
        atype = get_type();
        next();

        check(TT_ID);
        try(add_param(atype, token.p_string));
        next();

        return OK;

    default:
        return ERR_PARSER;
    }
}

static int FD() { // function definition
    unsigned i;

    switch (token.type) {
    case TT_SEMICOLON: // rule: FD -> ;
        try(func_declaration());

        next();
        return OK;

    case TT_OPEN_BRACKET: // rule: FD -> { STL }
        i = get_line();
        try(func_definition(i)); // instruction pointer

        next();

        call(STL);

        term(TT_CLOSE_BRACKET);

        remove_block();
        generate(I_ERR, NULL, NULL, NULL); // no return => runtime error

        return OK;

    default:
        return ERR_PARSER;
    }
}

static int STL() { // stat list
    switch (token.type) {
    case TT_CLOSE_BRACKET: // rule: STL -> eps
        return OK;

    default: // rule: STL -> S STL
        call(S);
        return(STL);
    }
}

static int S() { // stat
    unsigned i;
    Tlocal_sym_tab_node *conv;

    switch (token.type) {
    case TT_AUTO:
    case TT_INT:
    case TT_DOUBLE:
    case TT_STRING: // rule: S -> D ;
        call(D);
        term(TT_SEMICOLON);
        return OK;

    case TT_OPEN_BRACKET: // rule: S -> { STL }
        next();

        try(add_block());

        call(STL);

        term(TT_CLOSE_BRACKET);

        remove_block();

        return OK;

    case TT_IF: // rule: S -> if ( E ) S else S
        next();
        term(TT_L_BRACKET);

        call(E);
        try(type_compare(tmp_psitem.type, TYPE_INT));

        if (tmp_psitem.type != TYPE_INT) {
            try(tmp_var(TYPE_INT, &conv));

            generate(I_CONVERT, tmp_psitem.value.ptr, NULL, conv);
        } else {
            conv = tmp_psitem.value.ptr;
        }

        Tlocal_sym_tab_node *label;
        try(tmp_var(TYPE_INT, &label));
        generate(I_JUMPIFN, conv, label, NULL);

        term(TT_R_BRACKET);
        call(S);

        Tlocal_sym_tab_node *endif;
        try(tmp_var(TYPE_INT, &endif));
        generate(I_JUMP, NULL, endif, NULL);

        i = get_line();
        set_var_value_int(label->name, i);

        call(ELSE);

        i = get_line();
        set_var_value_int(endif->name, i);

        return OK;

    case TT_FOR: // rule: S -> for ( D ; E ; E ) S
        try(add_block());

        next();
        term(TT_L_BRACKET);
        call(D);
        term(TT_SEMICOLON);

        Tlocal_sym_tab_node *cond;
        try(tmp_var(TYPE_INT, &cond));
        i = get_line();
        set_var_value_int(cond->name, i);

        call(E);
        try(type_compare(tmp_psitem.type, TYPE_INT));

        if (tmp_psitem.type != TYPE_INT) {
            try(tmp_var(TYPE_INT, &conv));

            generate(I_CONVERT, tmp_psitem.value.ptr, NULL, conv);
        } else {
            conv = tmp_psitem.value.ptr;
        }

        Tlocal_sym_tab_node *endfor;
        try(tmp_var(TYPE_INT, &endfor));
        generate(I_JUMPIFN, conv, endfor, NULL);

        Tlocal_sym_tab_node *statlist;
        try(tmp_var(TYPE_INT, &statlist));
        generate(I_JUMP, NULL, statlist, NULL);

        Tlocal_sym_tab_node *assign;
        try(tmp_var(TYPE_INT, &assign));
        i = get_line();
        set_var_value_int(assign->name, i);

        term(TT_SEMICOLON);
        call(E);

        generate(I_JUMP, NULL, cond, NULL);

        i = get_line();
        set_var_value_int(statlist->name, i);

        term(TT_R_BRACKET);
        call(S);

        generate(I_JUMP, NULL, assign, NULL);

        i = get_line();
        set_var_value_int(endfor->name, i);

        remove_block();

        return OK;

    case TT_WHILE: // rule: S -> while ( E ) S
        next();

        term(TT_L_BRACKET);

        Tlocal_sym_tab_node *wcond;
        try(tmp_var(TYPE_INT, &wcond));
        i = get_line();
        set_var_value_int(wcond->name, i);

        call(E);
        try(type_compare(tmp_psitem.type, TYPE_INT));

        if (tmp_psitem.type != TYPE_INT) {
            try(tmp_var(TYPE_INT, &conv));

            generate(I_CONVERT, tmp_psitem.value.ptr, NULL, conv);
        } else {
            conv = tmp_psitem.value.ptr;
        }

        Tlocal_sym_tab_node *endwhile;
        try(tmp_var(TYPE_INT, &endwhile));
        generate(I_JUMPIFN, conv, endwhile, NULL);

        term (TT_R_BRACKET);

        call(S);

        generate(I_JUMP, NULL, wcond, NULL);

        i = get_line();
        set_var_value_int(endwhile->name, i);

        return OK;

    case TT_DO: // rule: S -> do S while ( E ) ;
        next();

        Tlocal_sym_tab_node *dwbody;
        try(tmp_var(TYPE_INT, &dwbody));
        i = get_line();
        set_var_value_int(dwbody->name, i);

        call(S);

        term(TT_WHILE);

        term(TT_L_BRACKET);

        call(E);
        try(type_compare(tmp_psitem.type, TYPE_INT));

        if (tmp_psitem.type != TYPE_INT) {
            try(tmp_var(TYPE_INT, &conv));

            generate(I_CONVERT, tmp_psitem.value.ptr, NULL, conv);
        } else {
            conv = tmp_psitem.value.ptr;
        }

        generate(I_JUMPIF, conv, dwbody, NULL);

        term(TT_R_BRACKET);

        term(TT_SEMICOLON);

        return OK;

    case TT_RETURN: // rule: S -> return E ;
        next();

        call(E);
        try(type_compare(tmp_ftype, tmp_psitem.type));

        if (tmp_psitem.type != tmp_ftype) {
            try(tmp_var(tmp_ftype, &conv));

            generate(I_CONVERT, tmp_psitem.value.ptr, NULL, conv);
        } else {
            conv = tmp_psitem.value.ptr;
        }

        generate(I_RETURN, conv, NULL, NULL);

        term(TT_SEMICOLON);
        return OK;

    case TT_CIN: // rule: S -> cin >> id IN ;
        next();
        term(TT_IN);

        check(TT_ID);
        if (!var_exists(token.p_string))
            return ERR_UNDEF;
        Tlocal_sym_tab_node *var = get_var(token.p_string);

        generate(I_CIN, NULL, NULL, var);

        next();

        call(IN);
        term(TT_SEMICOLON);
        return OK;

    case TT_COUT: // rule: S -> cout << E OUT ;
        next();
        term(TT_OUT);

        call(E);

        generate(I_COUT, tmp_psitem.value.ptr, NULL, NULL);

        call(OUT);
        term(TT_SEMICOLON);
        return OK;

    default: // rule: S -> E ;
        call(E);
        term(TT_SEMICOLON);
        return OK;
    }
}

static int D() { // variable declaration
    Tdata_type vtype;

    switch (token.type) {
    case TT_AUTO:
    case TT_INT:
    case TT_DOUBLE:
    case TT_STRING: // rule: D -> type id I
        vtype = get_type();
        tmp_type = vtype;
        next();

        check(TT_ID);
        try(add_var(vtype, token.p_string));
        try(tmpstr(token.p_string));
        next();

        return(I);

    default:
        return ERR_PARSER;
    }
}

static int I() { // variable definition
    switch (token.type) {
    case TT_ASSIGN: // rule: I -> = E
        next();
        call(E);

        try(assign_type_compare(tmp_buffer.str, tmp_psitem.type));

        Tlocal_sym_tab_node *var = get_var(tmp_buffer.str);
        Tlocal_sym_tab_node *conv;

        if (tmp_psitem.type != var->data_type) {
            try(tmp_var(var->data_type, &conv));

            generate(I_CONVERT, tmp_psitem.value.ptr, NULL, conv);
        } else {
            conv = tmp_psitem.value.ptr;
        }

        generate(I_ASSIGN, conv, NULL, var);

        return OK;

    default: // rule: I -> eps
        if (tmp_type == TYPE_AUTO)
            return ERR_AUTO;
        return OK;
    }
}

static int ELSE() { // else
    switch (token.type) {
    case TT_ELSE: // rule: ELSE -> else S
        next();

        return(S);

    default: // rule: ELSE -> eps
        return OK;
    }
}

static int IN() { // input variables
    switch (token.type) {
    case TT_IN: // rule: IN -> >> id IN
        next();

        check(TT_ID);
        if (!var_exists(token.p_string))
            return ERR_UNDEF;
        Tlocal_sym_tab_node *var = get_var(token.p_string);

        generate(I_CIN, NULL, NULL, var);

        next();

        return(IN);

    default: // rule: IN -> eps
        return OK;
    }
}

static int OUT() { // output expressions
    switch (token.type) {
    case TT_OUT: // rule: OUT -> << E OUT
        next();
        call(E);

        generate(I_COUT, tmp_psitem.value.ptr, NULL, NULL);

        return(OUT);

    default: // rule: OUT -> eps
        return OK;
    }
}

static int E() { // expression
    Tpsa_stack *psa_stack = ps_init();

    ps_push(psa_stack, OP_END);

    if (get_prec(OP_END) == ' ') {
        fprintf(stderr, "Syntax error: 'expression' expected, but '%s' given.\n", token_[token.type]);

        return ERR_PARSER;
    }

    int err;

    do {
        Tps_item *ps_item = ps_first_term(psa_stack);
        switch (get_prec(ps_item->op)) {
        case '=':
            err = ps_push_token(psa_stack);
            if (err != OK) {
                ps_destroy(psa_stack);
                return err;
            }
            next();
            break;

        case '<':
            err = ps_ins_after(psa_stack, ps_item, OP_EXPR);
            if (err != OK) {
                ps_destroy(psa_stack);
                return err;
            }

            err = ps_push_token(psa_stack);
            if (err != OK) {
                ps_destroy(psa_stack);
                return err;
            }

            next();
            break;

        case '>':
            err = psa(psa_stack);
            if (err != OK) {
                ps_destroy(psa_stack);
                return err;
            }
            break;
        }
    } while (get_prec(ps_first_term(psa_stack)->op) != ' ');

    Tps_item *top = ps_top(psa_stack);

    if (top->op != OP_NTERM || ps_prev(psa_stack)->op != OP_END) {
        fprintf(stderr, "Syntax error: unexpected '%s'\n", token_[token.type]);
        return ERR_PARSER;
    }

    tmp_psitem.type = top->type;
    tmp_psitem.value = top->value;

    ps_destroy(psa_stack);

    return OK;
}

int parse() {
    next();

    strInit(&tmp_buffer);

    int err = P();

    strFree(&tmp_buffer);

    return err;
}