//no way to call a dynamic function name with a variable, so we're cheating =) char *main_action_postage(PGconn *cnxn, int csock, char *str_uri_part, char *str_form_data, char *str_request, int int_request_len) { return // beware of prefixes! action_sequence will hit before action_sequences! // to fix, put the longer items first! E.g. action_sequences, then action_sequence //COPY csv action strncmp(str_uri_part, "action_copy", 12) == 0 ? action_copy(cnxn, str_form_data) : //PACKAGE strncmp(str_uri_part, "action_package_upload", 22) == 0 ? link_package_upload(str_request, int_request_len) : strncmp(str_uri_part, "action_package" , 15) == 0 ? action_package (str_form_data) : //FORK strncmp(str_uri_part, "action_upload", 14) == 0 ? link_postage_upload(str_request, int_request_len) : strncmp(str_uri_part, "action_file" , 11) == 0 ? action_file (str_form_data, str_uri_part, csock) : strncmp(str_uri_part, "action_fossil", 14) == 0 ? action_fossil (str_form_data, str_request) : //data service strncmp(str_uri_part, "action_order" , 13) == 0 ? action_order (cnxn, str_form_data) : strncmp(str_uri_part, "action_select", 14) == 0 ? action_select(cnxn, csock, str_form_data) : strncmp(str_uri_part, "action_update", 14) == 0 ? action_update(cnxn, str_form_data) : strncmp(str_uri_part, "action_insert", 14) == 0 ? action_insert(cnxn, str_form_data) : strncmp(str_uri_part, "action_delete", 14) == 0 ? action_delete(cnxn, str_form_data) : // didn't find a match: cat_cstr("HTTP/1.1 500 Internal Server Error\r\n", "Content-Type: application/json; charset=UTF-8\r\n\r\n", "{\"stat\": false, \"dat\": {\"error\": \"Action does not exist.\"}}"); }
uint8_t action_delete_list(action_ref * a_ptr) { if (!a_ptr) return FAILURE; //pointer is null action_ref a = *a_ptr; while(a) { //while an event still exists action_delete(&a); } return SUCCESS; }
sensor_ref create_temperature_sensor(uint8_t loc, time_ref period, uint16_t size) { sensor_ref * temperature_sensor; uint8_t (*delete_func)(); uint8_t (*enable_func)(); uint8_t (*disable_func)(); uint8_t channel; if(loc == 'I') { temperature_sensor = &internal_temperature_sensor; delete_func = &delete_internal_temperature_sensor; enable_func = &enable_internal_temperature_sensor; disable_func = &disable_internal_temperature_sensor; channel = TEMPERATURE_ADC; } else if (loc == 'A') { temperature_sensor = &external_temperature_sensor_A; delete_func = &delete_external_temperature_sensor_A; enable_func = &enable_external_temperature_sensor_A; disable_func = &disable_external_temperature_sensor_A; channel = PORT_A_ADC; } else if (loc == 'B') { temperature_sensor = &external_temperature_sensor_B; delete_func = &delete_external_temperature_sensor_B; enable_func = &enable_external_temperature_sensor_B; disable_func = &disable_external_temperature_sensor_B; channel = PORT_B_ADC; } else { return FAILURE; } if(*temperature_sensor) { // We already have one there if((time_cmp(period, sensor_get_period(*temperature_sensor)) == 0) && size == sensor_get_size(*temperature_sensor)) { // If they are the same...do nothing return *temperature_sensor; } else { // They are different, so delete the old one delete_func(); } } action_ref transmit_action = 0; action_ref lookup_temp_action = 0; node_ref temperature_node = 0; for(;;) { *temperature_sensor = new_sensor('T', channel, period, size); if(!*temperature_sensor) break; // Create the lookup_temp_action lookup_temp_action = new_action(); if(!lookup_temp_action) break; action_set_func(lookup_temp_action, &fix_temperature); temperature_node = new_node(*temperature_sensor, 0); if(!temperature_node) break; action_set_args(lookup_temp_action, temperature_node); sensor_add_action_on_data_full(*temperature_sensor, lookup_temp_action); // Create the transmit action transmit_action = new_transmit_action(*temperature_sensor); if(!transmit_action) break; sensor_add_action_on_data_full(*temperature_sensor, transmit_action); sensor_set_delete_func(*temperature_sensor, delete_func); sensor_set_enable_func(*temperature_sensor, enable_func); sensor_set_disable_func(*temperature_sensor, disable_func); return *temperature_sensor; } delete_func(); node_delete(&temperature_node); action_delete(&lookup_temp_action); action_delete(&transmit_action); return FAILURE; }
int main(void) { #if CGIC_LOCAL_TEST_0 /* Get input from web */ char *pinput = get_input(); //char *pinput = get_input_test(); if ( NULL == pinput ) { return 0; } /* Parse input from web */ struct data_from_web info; /* Parse input fail */ if ( 0 != parse_input(pinput, &info) ) { free(pinput); return 0; } /* Deal with info from web, deliver task according to "action" */ switch(info.action) { case ACTION_GETLIST: action_getlist(&info); break; case ACTION_BACKUP: action_backup(&info); break; case ACTION_RECOVER: action_recover(&info); break; case ACTION_DELETE: action_delete(&info); break; case ACTION_SETIP: action_setip(&info); break; case ACTION_GETIP: action_getip(&info); break; default: break; } /* Output json string */ free(pinput); return 0; #elif CGIC_LOCAL_TEST_1 /* Get input from web */ char *pinput = get_input_test(); /* Parse input from web */ struct data_from_web info; /* Parse input fail */ if ( 0 != parse_input(pinput, &info) ) { puts("parse input fail"); free(pinput); return 0; } /* Deal with info from web */ printf("module = %s\n", info.module); printf("action = %d\n", info.action); printf("dbid = %d\n", info.dbid); printf("remark = %s\n", info.remark); /* Output json string */ output_test(); puts("output_test finished"); free(pinput); return 0; #elif CGIC_LOCAL_TEST_2 /* Get input from web */ char *pinput = get_input_test(); if ( NULL == pinput ) { puts("No input string"); return 0; } /* Parse input from web */ struct data_from_web info; /* Parse input fail */ if ( 0 != parse_input(pinput, &info) ) { puts("Input fail"); free(pinput); return 0; } /* Deal with info from web */ puts("Parse result"); printf("module = %s\n", info.module); printf("action = %d\n", info.action); printf("dbid = %d\n", info.dbid); printf("remark = %s\n", info.remark); putchar('\n'); /* Deal with info from web, deliver task according to "action" */ puts("Dealing"); switch(info.action) { case ACTION_GETLIST: puts("Action: getlist"); action_getlist(&info); break; case ACTION_BACKUP: puts("Action: backup"); action_backup(&info); break; case ACTION_RECOVER: puts("Action: recover"); action_recover(&info); break; case ACTION_DELETE: puts("Action: delete"); action_delete(&info); break; case ACTION_SETIP: puts("Action: setip"); action_setip(&info); break; case ACTION_GETIP: puts("Action: getip"); action_getip(&info); break; default: puts("Unexpected action"); break; } /* Output json string */ free(pinput); return 0; #else /* The normal function */ /* Get input from web */ char *pinput = get_input(); if ( NULL == pinput ) { return 0; } /* Parse input from web */ struct data_from_web info; /* Parse input fail */ if ( 0 != parse_input(pinput, &info) ) { free(pinput); return 0; } /* Deal with info from web, deliver task according to "action" */ switch(info.action) { case ACTION_GETLIST: action_getlist(&info); break; case ACTION_BACKUP: action_backup(&info); break; case ACTION_RECOVER: action_recover(&info); break; case ACTION_DELETE: action_delete(&info); break; case ACTION_SETIP: action_setip(&info); break; case ACTION_GETIP: action_getip(&info); break; default: break; } /* Output json string */ free(pinput); return 0; #endif }
int main (int argc, char**argv) { //option variables bool do_help = false, do_version = false, do_test = false, has_opt = false, opt_armor = false, opt_yes = false, opt_fingerprint = false, opt_clearsign = false, opt_import_no_action = false; std::string recipient, user, input, output, name, filter, action_param, detach_sign, symmetric; char action = 0; int c, option_index; for (;;) { static struct option long_opts[] = { {"help", 0, 0, 'h' }, {"version", 0, 0, 'V' }, {"test", 0, 0, 'T' }, //global options {"armor", 0, 0, 'a' }, {"yes", 0, 0, 'y' }, {"recipient", 1, 0, 'r' }, {"user", 1, 0, 'u' }, //I/O redirection from default stdin/out {"in", 1, 0, 'R' }, {"out", 1, 0, 'o' }, //keyring management {"list", 0, 0, 'k' }, {"import", 0, 0, 'i' }, {"export", 0, 0, 'p' }, {"delete", 1, 0, 'x' }, {"rename", 1, 0, 'm' }, {"list-secret", 0, 0, 'K' }, {"import-secret", 0, 0, 'I' }, {"export-secret", 0, 0, 'P' }, {"delete-secret", 1, 0, 'X' }, {"rename-secret", 1, 0, 'M' }, {"gen-key", 1, 0, 'g' }, {"name", 1, 0, 'N' }, {"filter", 1, 0, 'F' }, {"fingerprint", 0, 0, 'f' }, {"no-action", 0, 0, 'n' }, //actions {"sign", 0, 0, 's' }, {"verify", 0, 0, 'v' }, {"encrypt", 0, 0, 'e' }, {"decrypt", 0, 0, 'd' }, //action options {"clearsign", 0, 0, 'C' }, {"detach-sign", 1, 0, 'b' }, {"symmetric", 1, 0, 'S' }, {0, 0, 0, 0 } }; option_index = -1; c = getopt_long (argc, argv, "hVTayr:u:R:o:kipx:m:KIPX:M:g:N:F:fnsvedCb:S:", long_opts, &option_index); if (c == -1) break; has_opt = true; switch (c) { case '?': case ':': case 'h': do_help = true; break; #define read_flag(ch,var) case ch: var=true; break; #define read_single_opt(ch,var,errmsg) \ case ch: if(var.length()) {progerr(errmsg); do_help=true;}\ else var=optarg; break; #define read_action(ch) read_action_comb(ch,0,0) #define read_action_comb(ch, hit, comb) \ case ch: \ if(hit && action==hit) { \ action=comb; \ if(optarg) action_param=optarg; \ } else if(action) { \ progerr("please specify a single action"); \ do_help=true; \ } else { \ action=ch; \ if(optarg) action_param=optarg; \ } break; read_flag ('V', do_version) read_flag ('T', do_test) read_flag ('a', opt_armor) read_flag ('y', opt_yes) read_single_opt ('r', recipient, "specify only one recipient") read_single_opt ('u', user, "specify only one local user") read_single_opt ('R', input, "cannot accept multiple inputs") read_single_opt ('o', output, "cannot accept multiple outputs") read_action ('k') read_action ('i') read_action ('p') read_action ('x') read_action ('m') read_action ('K') read_action ('I') read_action ('P') read_action ('X') read_action ('M') read_action ('g') read_single_opt ('N', name, "please specify single name") read_single_opt ('F', filter, "please specify single filter string") read_flag ('f', opt_fingerprint) read_flag ('n', opt_import_no_action) /* * combinations of s+e and d+v are possible. result is * 'E' = "big encrypt with sig" and 'D' "big decrypt * with verify". */ read_action_comb ('s', 'e', 'E') read_action_comb ('v', 'd', 'D') read_action_comb ('e', 's', 'E') read_action_comb ('d', 'v', 'D') read_flag ('C', opt_clearsign) read_single_opt ('b', detach_sign, "specify only one detach-sign file") read_single_opt ('S', symmetric, "specify only one symmetric parameter") #undef read_flag #undef read_single_opt #undef read_action default: //which doesn't just happen. do_help = true; break; } } if (optind != argc) { progerr ("unmatched non-option parameters"); do_help = true; } if ( (!has_opt) || do_help) { print_help (argv[0]); return 0; } if (do_version) { print_version(); return 0; } /* * something will be happening, therefore init everything */ keyring KR; algorithm_suite AS; //register all available algorithms fill_algorithm_suite (AS); /* * cin/cout redirection */ int exitval = 0; if (input.length() && !redirect_cin (input) ) { progerr ("could not open input file"); exitval = 1; goto exit; } if (output.length() && !redirect_cout (output) ) { progerr ("could not redirect to output file"); exitval = 1; goto exit; } /* * check the option flags and do whatever was requested */ if (do_test) { test(); goto exit; } if (symmetric.length() ) switch (action) { case 's': case 'v': break; default: progerr ("specified action doesn't support symmetric operation"); exitval = 1; goto exit; } switch (action) { case 'g': exitval = action_gen_key (action_param, name, KR, AS); break; case 'e': exitval = action_encrypt (recipient, opt_armor, KR, AS); break; case 'd': exitval = action_decrypt (opt_armor, KR, AS); break; case 's': exitval = action_sign (user, opt_armor, detach_sign, opt_clearsign, symmetric, KR, AS); break; case 'v': exitval = action_verify (opt_armor, detach_sign, opt_clearsign, opt_yes, symmetric, KR, AS); break; case 'E': exitval = action_sign_encrypt (user, recipient, opt_armor, KR, AS); break; case 'D': exitval = action_decrypt_verify (opt_armor, opt_yes, KR, AS); break; case 'k': exitval = action_list (opt_fingerprint, filter, KR); break; case 'i': exitval = action_import (opt_armor, opt_import_no_action, opt_yes, opt_fingerprint, filter, name, KR); break; case 'p': exitval = action_export (opt_armor, filter, name, KR); break; case 'x': exitval = action_delete (opt_yes, action_param, KR); break; case 'm': exitval = action_rename (opt_yes, action_param, name, KR); break; case 'K': exitval = action_list_sec (opt_fingerprint, filter, KR); break; case 'I': exitval = action_import_sec (opt_armor, opt_import_no_action, opt_yes, opt_fingerprint, filter, name, KR); break; case 'P': exitval = action_export_sec (opt_armor, opt_yes, filter, name, KR); break; case 'X': exitval = action_delete_sec (opt_yes, action_param, KR); break; case 'M': exitval = action_rename_sec (opt_yes, action_param, name, KR); break; default: progerr ("no action specified, use `--help'"); exitval = 1; break; } /* * all done. * keyring is _not_ automatically saved here to prevent frequent * rewriting and due the fact that everything that modifies it _must_ * also ensure and verify that it was written back correctly. */ exit: if (!KR.close() ) { progerr ("could not close keyring, " "something weird is going to happen."); } return exitval; }
sensor_ref create_light_sensor(uint8_t loc, time_ref period, uint16_t size) { sensor_ref * light_sensor; uint8_t (*delete_func)(); uint8_t (*enable_func)(); uint8_t (*disable_func)(); uint8_t channel; // Based on the location, change the functions if (loc == 'A') { light_sensor = &light_sensor_A; delete_func = &delete_light_sensor_A; enable_func = &enable_light_sensor_A; disable_func = &disable_light_sensor_A; channel = PORT_A_ADC; } else if (loc == 'B') { light_sensor = &light_sensor_B; delete_func = &delete_light_sensor_B; enable_func = &enable_light_sensor_B; disable_func = &disable_light_sensor_B; channel = PORT_B_ADC; } else { return FAILURE; } // Check to see if we already have a light sensor there if(*light_sensor) { // We already have one there if((time_cmp(period, sensor_get_period(*light_sensor)) == 0) && size == sensor_get_size(*light_sensor)) { // If they are the same...do nothing return *light_sensor; } else { // They are different, so delete the old one delete_func(); } } action_ref transmit_action = 0; // Create the sensor, the transmit action, and set the functions for(;;) { *light_sensor = new_sensor('L', channel, period, size); if(!*light_sensor) break; // Create the transmit action transmit_action = new_transmit_action(*light_sensor); if(!transmit_action) break; sensor_add_action_on_data_full(*light_sensor, transmit_action); sensor_set_delete_func(*light_sensor, delete_func); sensor_set_enable_func(*light_sensor, enable_func); sensor_set_disable_func(*light_sensor, disable_func); return *light_sensor; } delete_func(); action_delete(&transmit_action); return FAILURE; }
event_ref new_timer_event(uint8_t timer, uint16_t period, uint8_t repeat, uint16_t repeat_count) { event_ref e = 0; action_ref check_action = 0; node_ref period_arg = 0; node_ref trigger_arg = 0; time_ref time_trigger = 0; time_ref time_period = 0; for(;;) { // Create a new time period time_period = new_time(); if(!time_period) break; // Set up the period if(timer == CLOCK_TIME) { time_period->clock_time = period; } else if (timer == MILLISECONDS) { time_period->milliseconds = period; } else if (timer == SECONDS) { time_period->seconds = period; } else if (timer == MINUTES) { time_period->minutes = period; } else if (timer == HOURS) { time_period->hours = period; } else if (timer == DAYS) { time_period->days = period; } // Create a new time trigger time_trigger = new_time(); if(!time_trigger) break; // Set up the trigger = global + period add_time_to_time(time_trigger, global_time()); add_time_to_time(time_trigger, time_period); // Create a new event e = new_event(); if(!e) break; // Create a new action check_action = new_action(); if(!check_action) break; // Set its function action_set_func(check_action, &check_time_args); // Create a trigger arg trigger_arg = new_node(time_trigger, &time_delete); if(!trigger_arg) break; // Create the period arg period_arg = new_node(time_period, &time_delete); if(!period_arg) break; node_append(period_arg, trigger_arg); action_set_args(check_action, period_arg); event_set_check(e, check_action); event_set_repeat(e, repeat, repeat_count); return e; } event_delete(&e); node_delete(&period_arg); node_delete(&trigger_arg); action_delete(&check_action); time_delete(&time_period); time_delete(&time_trigger); return FAILURE; }