int test_main(void) { PJ_USE_EXCEPTION; PJ_TRY { return test_inner(); } PJ_CATCH_ANY { int id = PJ_GET_EXCEPTION(); PJ_LOG(3,("test", "FATAL: unhandled exception id %d (%s)", id, pj_exception_id_name(id))); } PJ_END; return -1; }
static int throw_in_handler(void) { PJ_USE_EXCEPTION; int rc = 0; PJ_TRY { PJ_THROW(ID_1); } PJ_CATCH_ANY { if (PJ_GET_EXCEPTION() != ID_1) rc = -300; else PJ_THROW(ID_2); } PJ_END; return rc; }
static int test_exception() { PJ_USE_EXCEPTION; PJ_TRY { void *data = my_malloc(200); free(data); randomly_throw_exception(); } PJ_CATCH_ANY { pj_exception_id_t x_id; x_id = PJ_GET_EXCEPTION(); printf("Caught exception %d (%s)\n", x_id, pj_exception_id_name(x_id)); } PJ_END return 1; }
static int test(void) { int rc = 0; PJ_USE_EXCEPTION; /* * No exception situation. */ PJ_TRY { PJ_UNUSED_ARG(rc); } PJ_CATCH_ANY { rc = -3; } PJ_END; if (rc != 0) return rc; /* * Basic TRY/CATCH */ PJ_TRY { rc = throw_id_1(); // should not reach here. rc = -10; } PJ_CATCH_ANY { int id = PJ_GET_EXCEPTION(); if (id != ID_1) { PJ_LOG(3,("", "...error: got unexpected exception %d (%s)", id, pj_exception_id_name(id))); if (!rc) rc = -20; } } PJ_END; if (rc != 0) return rc; /* * Multiple exceptions handlers */ PJ_TRY { rc = throw_id_2(); // should not reach here. rc = -25; } PJ_CATCH_ANY { switch (PJ_GET_EXCEPTION()) { case ID_1: if (!rc) rc = -30; break; case ID_2: if (!rc) rc = 0; break; default: if (!rc) rc = -40; break; } } PJ_END; if (rc != 0) return rc; /* * Test default handler. */ PJ_TRY { rc = throw_id_1(); // should not reach here rc = -50; } PJ_CATCH_ANY { switch (PJ_GET_EXCEPTION()) { case ID_1: if (!rc) rc = 0; break; default: if (!rc) rc = -60; break; } } PJ_END; if (rc != 0) return rc; /* * Nested handlers */ PJ_TRY { rc = try_catch_test(); } PJ_CATCH_ANY { rc = -70; } PJ_END; if (rc != 0) return rc; /* * Throwing exception inside handler */ rc = -80; PJ_TRY { int rc2; rc2 = throw_in_handler(); if (rc2) rc = rc2; } PJ_CATCH_ANY { if (PJ_GET_EXCEPTION() == ID_2) { rc = 0; } else { rc = -90; } } PJ_END; if (rc != 0) return rc; /* * Return from handler. Returning from the function inside a handler * should be okay (though returning from the function inside the * PJ_TRY block IS NOT OKAY!!). We want to test to see if handler * is cleaned up properly, but not sure how to do this. */ PJ_TRY { int rc2; rc2 = return_in_handler(); if (rc2) rc = rc2; } PJ_CATCH_ANY { rc = -100; } PJ_END; return 0; }
/** * This method is to parse and add the command attribute to command structure. **/ static pj_status_t add_cmd_node(pj_cli_t *cli, pj_cli_cmd_spec *group, pj_xml_node *xml_node, pj_cli_cmd_handler handler, pj_cli_cmd_spec **p_cmd, pj_cli_get_dyn_choice get_choice) { pj_xml_node *root = xml_node; pj_xml_attr *attr; pj_xml_node *sub_node; pj_cli_cmd_spec *cmd; pj_cli_arg_spec args[PJ_CLI_MAX_ARGS]; pj_str_t sc[PJ_CLI_MAX_SHORTCUTS]; pj_status_t status = PJ_SUCCESS; if (pj_stricmp2(&root->name, "CMD")) return PJ_EINVAL; /* Initialize the command spec */ cmd = PJ_POOL_ZALLOC_T(cli->pool, struct pj_cli_cmd_spec); /* Get the command attributes */ attr = root->attr_head.next; while (attr != &root->attr_head) { if (!pj_stricmp2(&attr->name, "name")) { pj_strltrim(&attr->value); if (!attr->value.slen || cmd_name_exists(cli, group, &attr->value)) { return PJ_CLI_EBADNAME; } pj_strdup(cli->pool, &cmd->name, &attr->value); } else if (!pj_stricmp2(&attr->name, "id")) { pj_bool_t is_valid = PJ_FALSE; if (attr->value.slen) { pj_cli_cmd_id cmd_id = pj_strtol(&attr->value); if (!pj_hash_get(cli->cmd_id_hash, &cmd_id, sizeof(pj_cli_cmd_id), NULL)) is_valid = PJ_TRUE; } if (!is_valid) return PJ_CLI_EBADID; cmd->id = (pj_cli_cmd_id)pj_strtol(&attr->value); } else if (!pj_stricmp2(&attr->name, "sc")) { pj_scanner scanner; pj_str_t str; PJ_USE_EXCEPTION; pj_scan_init(&scanner, attr->value.ptr, attr->value.slen, PJ_SCAN_AUTOSKIP_WS, &on_syntax_error); PJ_TRY { while (!pj_scan_is_eof(&scanner)) { pj_scan_get_until_ch(&scanner, ',', &str); pj_strrtrim(&str); if (!pj_scan_is_eof(&scanner)) pj_scan_advance_n(&scanner, 1, PJ_TRUE); if (!str.slen) continue; if (cmd->sc_cnt >= PJ_CLI_MAX_SHORTCUTS) { PJ_THROW(PJ_CLI_ETOOMANYARGS); } /* Check whether the shortcuts are already used */ if (cmd_name_exists(cli, group, &str)) { PJ_THROW(PJ_CLI_EBADNAME); } pj_strassign(&sc[cmd->sc_cnt++], &str); } } PJ_CATCH_ANY { pj_scan_fini(&scanner); return (PJ_GET_EXCEPTION()); } PJ_END; } else if (!pj_stricmp2(&attr->name, "desc")) {
static int test(void) { int rc = 0; PJ_USE_EXCEPTION; /* * No exception situation. */ PJ_TRY { rc = rc; } PJ_CATCH_ANY { rc = -3; } PJ_END; if (rc != 0) return rc; /* * Basic TRY/CATCH */ PJ_TRY { rc = throw_id_1(); // should not reach here. rc = -10; } PJ_CATCH_ANY { int id = PJ_GET_EXCEPTION(); if (id != ID_1) { PJ_LOG(3,("", "...error: got unexpected exception %d (%s)", id, pj_exception_id_name(id))); if (!rc) rc = -20; } } PJ_END; if (rc != 0) return rc; /* * Multiple exceptions handlers */ PJ_TRY { rc = throw_id_2(); // should not reach here. rc = -25; } PJ_CATCH_ANY { switch (PJ_GET_EXCEPTION()) { case ID_1: if (!rc) rc = -30; break; case ID_2: if (!rc) rc = 0; break; default: if (!rc) rc = -40; break; } } PJ_END; if (rc != 0) return rc; /* * Test default handler. */ PJ_TRY { rc = throw_id_1(); // should not reach here rc = -50; } PJ_CATCH_ANY { switch (PJ_GET_EXCEPTION()) { case ID_1: if (!rc) rc = 0; break; default: if (!rc) rc = -60; break; } } PJ_END; if (rc != 0) return rc; return 0; }
/* * Create UAC subscription. */ PJ_DEF(pjsip_event_sub*) pjsip_event_sub_create( pjsip_endpoint *endpt, const pj_str_t *from, const pj_str_t *to, const pj_str_t *event, int expires, int accept_cnt, const pj_str_t accept[], void *user_data, const pjsip_event_sub_cb *cb) { pjsip_tx_data *tdata; pj_pool_t *pool; const pjsip_hdr *hdr; pjsip_event_sub *sub; PJ_USE_EXCEPTION; PJ_LOG(5,(THIS_FILE, "Creating event subscription %.*s to %.*s", event->slen, event->ptr, to->slen, to->ptr)); /* Create pool for the event subscription. */ pool = pjsip_endpt_create_pool(endpt, "esub", SUB_POOL_SIZE, SUB_POOL_INC); if (!pool) { return NULL; } /* Init subscription. */ sub = pj_pool_calloc(pool, 1, sizeof(*sub)); sub->pool = pool; sub->endpt = endpt; sub->role = PJSIP_ROLE_UAC; sub->state = PJSIP_EVENT_SUB_STATE_PENDING; sub->state_str = state[sub->state]; sub->user_data = user_data; sub->timer.id = 0; sub->default_interval = expires; pj_memcpy(&sub->cb, cb, sizeof(*cb)); pj_list_init(&sub->auth_sess); pj_list_init(&sub->route_set); sub->mutex = pj_mutex_create(pool, "esub", PJ_MUTEX_RECURSE); if (!sub->mutex) { pjsip_endpt_destroy_pool(endpt, pool); return NULL; } /* The easiest way to parse the parameters is to create a dummy request! */ tdata = pjsip_endpt_create_request( endpt, &SUBSCRIBE, to, from, to, from, NULL, -1, NULL); if (!tdata) { pj_mutex_destroy(sub->mutex); pjsip_endpt_destroy_pool(endpt, pool); return NULL; } /* * Duplicate headers in the request to our structure. */ PJ_TRY { int i; /* From */ hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, NULL); pj_assert(hdr != NULL); sub->from = pjsip_hdr_clone(pool, hdr); /* To */ hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_TO, NULL); pj_assert(hdr != NULL); sub->to = pjsip_hdr_clone(pool, hdr); /* Contact. */ sub->contact = pjsip_contact_hdr_create(pool); sub->contact->uri = sub->from->uri; /* Call-ID */ hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CALL_ID, NULL); pj_assert(hdr != NULL); sub->call_id = pjsip_hdr_clone(pool, hdr); /* CSeq */ sub->cseq = pj_rand() % 0xFFFF; /* Event. */ sub->event = pjsip_event_hdr_create(sub->pool); pj_strdup(pool, &sub->event->event_type, event); /* Expires. */ sub->uac_expires = pjsip_expires_hdr_create(pool); sub->uac_expires->ivalue = expires; /* Accept. */ sub->local_accept = pjsip_accept_hdr_create(pool); for (i=0; i<accept_cnt && i < PJSIP_MAX_ACCEPT_COUNT; ++i) { sub->local_accept->count++; pj_strdup(sub->pool, &sub->local_accept->values[i], &accept[i]); } /* Register to hash table. */ create_subscriber_key( &sub->key, pool, PJSIP_ROLE_UAC, &sub->call_id->id, &sub->from->tag); pj_mutex_lock( mgr.mutex ); pj_hash_set( pool, mgr.ht, sub->key.ptr, sub->key.slen, sub); pj_mutex_unlock( mgr.mutex ); } PJ_DEFAULT { PJ_LOG(4,(THIS_FILE, "event_sub%p (%s): caught exception %d during init", sub, state[sub->state].ptr, PJ_GET_EXCEPTION())); pjsip_tx_data_dec_ref(tdata); pj_mutex_destroy(sub->mutex); pjsip_endpt_destroy_pool(endpt, sub->pool); return NULL; } PJ_END; /* All set, delete temporary transmit data as we don't need it. */ pjsip_tx_data_dec_ref(tdata); PJ_LOG(4,(THIS_FILE, "event_sub%p (%s): client created, target=%.*s, event=%.*s", sub, state[sub->state].ptr, to->slen, to->ptr, event->slen, event->ptr)); return sub; }