unsigned char * cob_external_addr (const char *exname, const int exlength) { static struct cob_external *basext = NULL; struct cob_external *eptr; for (eptr = basext; eptr; eptr = eptr->next) { if (!strcmp (exname, eptr->ename)) { if (exlength > eptr->esize) { cob_runtime_error ("EXTERNAL item '%s' has size > %d", exname, exlength); cob_stop_run (1); } cob_initial_external = 0; return (ucharptr)eptr->ext_alloc; } } eptr = cob_malloc (sizeof (struct cob_external)); eptr->next = basext; eptr->esize = exlength; eptr->ename = cob_malloc (strlen (exname) + 1); strcpy (eptr->ename, exname); eptr->ext_alloc = cob_malloc ((size_t)exlength); basext = eptr; cob_initial_external = 1; return (ucharptr)eptr->ext_alloc; }
void cob_display_env_value (cob_field *f) { char *p; char *env2; size_t len; if (!cob_local_env) { cob_set_exception (COB_EC_IMP_DISPLAY); return; } if (!*cob_local_env) { cob_set_exception (COB_EC_IMP_DISPLAY); return; } env2 = cob_malloc (f->size + 1); cob_field_to_string (f, env2); len = strlen (cob_local_env) + strlen (env2) + 3; p = cob_malloc (len); sprintf (p, "%s=%s", cob_local_env, env2); if (putenv (p) != 0) { cob_set_exception (COB_EC_IMP_DISPLAY); } free (env2); }
void cob_unstring_init (cob_field *src, cob_field *ptr, const size_t num_dlm) { static size_t udlmcount = 0; unstring_src_copy = *src; unstring_src = &unstring_src_copy; unstring_ptr = NULL; if (ptr) { unstring_ptr_copy = *ptr; unstring_ptr = &unstring_ptr_copy; } unstring_offset = 0; unstring_count = 0; unstring_ndlms = 0; cob_exception_code = 0; if (!dlm_list) { if (num_dlm <= DLM_DEFAULT_NUM) { dlm_list = cob_malloc (DLM_DEFAULT_NUM * sizeof(struct dlm_struct)); udlmcount = DLM_DEFAULT_NUM; } else { dlm_list = cob_malloc (num_dlm * sizeof(struct dlm_struct)); udlmcount = num_dlm; } } else { if (num_dlm > udlmcount) { free (dlm_list); dlm_list = cob_malloc (num_dlm * sizeof(struct dlm_struct)); udlmcount = num_dlm; } } if (unstring_ptr) { unstring_offset = cob_get_int (unstring_ptr) - 1; if (unstring_offset < 0 || unstring_offset >= (int)unstring_src->size) { cob_set_exception (COB_EC_OVERFLOW_UNSTRING); } } #ifdef I18N_UTF8 /* I18N_UTF8: No offset arrangement needed also in NATIONAL. */ #else /*!I18N_UTF8*/ if (COB_FIELD_TYPE (unstring_src) == COB_TYPE_NATIONAL || COB_FIELD_TYPE (unstring_src) == COB_TYPE_NATIONAL_EDITED) { unstring_offset *= 2; } #endif /*I18N_UTF8*/ }
void cob_accept_command_line (cob_field *f) { char *buff; size_t i; size_t size; size_t len; if (commlncnt) { cob_memcpy (f, commlnptr, (int)commlncnt); return; } buff = cob_malloc (COB_MEDIUM_BUFF); size = 0; for (i = 1; i < (size_t)cob_argc; ++i) { len = strlen (cob_argv[i]); if (size + len >= COB_MEDIUM_BUFF) { /* overflow */ break; } memcpy (buff + size, cob_argv[i], len); size += len; buff[size++] = ' '; } cob_memcpy (f, (ucharptr)buff, (int)size); free (buff); }
void cob_init_strings (void) { inspect_mark = cob_malloc ((size_t)COB_NORMAL_BUFF); dlm_list = cob_malloc (DLM_DEFAULT_NUM * sizeof(struct dlm_struct)); inspect_mark_size = COB_NORMAL_BUFF; dlm_list_size = DLM_DEFAULT_NUM; figurative_ptr = NULL; figurative_size = 0; alpha_fld.size = 0; alpha_fld.data = NULL; alpha_fld.attr = &const_alpha_attr; str_cob_low.size = 1; str_cob_low.data = (cob_u8_ptr)"\0"; str_cob_low.attr = &const_strall_attr; }
static void alloc_figurative (const cob_field *f1, const cob_field *f2) { unsigned char *s; size_t size1; size_t size2; size_t n; size2 = f2->size; if (size2 > figurative_size) { if (figurative_ptr) { free (figurative_ptr); } figurative_ptr = cob_malloc (size2); figurative_size = size2; } size1 = 0; s = figurative_ptr; for (n = 0; n < size2; ++n, ++s) { *s = f1->data[size1]; size1++; if (size1 >= f1->size) { size1 = 0; } } alpha_fld.size = size2; alpha_fld.data = figurative_ptr; }
void cob_unstring_init (cob_field *src, cob_field *ptr, const size_t num_dlm) { unstring_src_copy = *src; unstring_src = &unstring_src_copy; unstring_ptr = NULL; if (ptr) { unstring_ptr_copy = *ptr; unstring_ptr = &unstring_ptr_copy; } unstring_offset = 0; unstring_count = 0; unstring_ndlms = 0; cob_set_exception (0); if (num_dlm > dlm_list_size) { free (dlm_list); dlm_list = cob_malloc (num_dlm * sizeof(struct dlm_struct)); dlm_list_size = num_dlm; } if (unstring_ptr) { unstring_offset = cob_get_int (unstring_ptr) - 1; if (unstring_offset < 0 || unstring_offset >= (int)unstring_src->size) { cob_set_exception (COB_EC_OVERFLOW_UNSTRING); } } }
void cob_allocate (unsigned char **dataptr, cob_field *retptr, cob_field *sizefld) { void *mptr = NULL; struct cob_alloc_cache *cache_ptr; int fsize; cob_exception_code = 0; fsize = cob_get_int (sizefld); if (fsize > 0) { cache_ptr = cob_malloc (sizeof (struct cob_alloc_cache)); mptr = malloc ((size_t)fsize); if (!mptr) { cob_set_exception (COB_EC_STORAGE_NOT_AVAIL); free (cache_ptr); } else { memset (mptr, 0, (size_t)fsize); cache_ptr->cob_pointer = mptr; cache_ptr->size = (size_t)fsize; cache_ptr->next = cob_alloc_base; cob_alloc_base = cache_ptr; } } if (dataptr) { *dataptr = (unsigned char *)mptr; } if (retptr) { *(void **)(retptr->data) = mptr; } }
void cob_inspect_init (cob_field *var, const int replacing) { size_t i; size_t digcount; inspect_var_copy = *var; inspect_var = &inspect_var_copy; inspect_replacing = replacing; inspect_sign = cob_get_sign (var); inspect_size = COB_FIELD_SIZE (var); inspect_data = COB_FIELD_DATA (var); inspect_start = NULL; inspect_end = NULL; digcount = inspect_size * sizeof (int); if (digcount > lastsize) { free (inspect_mark); inspect_mark = cob_malloc (digcount); lastsize = digcount; } for (i = 0; i < inspect_size; i++) { inspect_mark[i] = -1; } cob_exception_code = 0; }
void cob_display_command_line (cob_field *f) { if (commlnptr) { free (commlnptr); } commlnptr = cob_malloc (f->size); commlncnt = f->size; memcpy (commlnptr, f->data, commlncnt); }
void cob_table_sort_init (const int nkeys, const unsigned char *collating_sequence) { sort_nkeys = 0; sort_keys = cob_malloc (nkeys * sizeof (struct cob_file_key)); if (collating_sequence) { sort_collate = collating_sequence; } else { sort_collate = cob_current_module->collating_sequence; } }
void cob_display_environment (cob_field *f) { if (!cob_local_env) { cob_local_env = cob_malloc (COB_SMALL_BUFF); } if (f->size > COB_SMALL_MAX) { cob_set_exception (COB_EC_IMP_DISPLAY); return; } cob_field_to_string (f, cob_local_env); }
void cob_init_strings (void) { inspect_mark = cob_malloc (COB_MEDIUM_BUFF); lastsize = COB_MEDIUM_BUFF; alpha_attr.type = COB_TYPE_ALPHANUMERIC; alpha_attr.digits = 0; alpha_attr.scale = 0; alpha_attr.flags = 0; alpha_attr.pic = NULL; alpha_fld.size = 0; alpha_fld.data = NULL; alpha_fld.attr = &alpha_attr; }
void cob_inspect_before (const cob_field *str) { unsigned char *p; unsigned char *data; size_t size; int sign; char *buf = NULL; unsigned char *p2; unsigned int n; int fig; switch (COB_FIELD_TYPE (str)) { case COB_TYPE_NUMERIC_DISPLAY: data = COB_FIELD_DATA (str); size = COB_FIELD_SIZE (str); sign = cob_get_sign ((cob_field *)str); n = 0; fig = 0; while (size > 1 && *data == '0') { size--; data++; } while (size--) { n = n * 10 + cob_d2i (*data++); fig++; } buf = cob_malloc (fig); sprintf (buf, "%d", n); p2 = (unsigned char *)buf; break; default: fig = str->size; p2 = str->data; break; } for (p = inspect_start; p < inspect_end - fig + 1; p++) { if (memcmp (p, p2, fig) == 0) { inspect_end = p; break; } } if (buf) { free (buf); } }
void cob_get_environment (cob_field *envname, cob_field *envval) { const char *p; char *buff; if (envname->size < COB_SMALL_BUFF) { buff = cob_malloc (COB_SMALL_BUFF); cob_field_to_string (envname, buff); p = getenv (buff); if (!p) { cob_set_exception (COB_EC_IMP_ACCEPT); p = " "; } cob_memcpy (envval, (ucharptr)p, (int) strlen (p)); free (buff); } else { cob_set_exception (COB_EC_IMP_ACCEPT); p = " "; cob_memcpy (envval, (ucharptr)p, (int) strlen (p)); } }
int CBL_ERROR_PROC (unsigned char *x, unsigned char *pptr) { struct handlerlist *hp = NULL; struct handlerlist *h = hdlrs; int (**p)(char *s) = NULL; COB_CHK_PARMS (CBL_ERROR_PROC, 2); memcpy (&p, &pptr, sizeof (void *)); if (!p || !*p) { return -1; } /* remove handler anyway */ while (h != NULL) { if (h->proc == *p) { if (hp != NULL) { hp->next = h->next; } else { hdlrs = h->next; } if (hp) { free (hp); } break; } hp = h; h = h->next; } if (*x != 0) { /* remove handler */ return 0; } h = cob_malloc (sizeof(struct handlerlist)); h->next = hdlrs; h->proc = *p; hdlrs = h; return 0; }
int SYSTEM (const unsigned char *cmd) { char *buff; int i; COB_CHK_PARMS (SYSTEM, 1); if (cob_current_module->cob_procedure_parameters[0]) { i = (int)cob_current_module->cob_procedure_parameters[0]->size; if (i > COB_MEDIUM_MAX) { cob_runtime_error ("Parameter to SYSTEM call is larger than 8192 characters"); cob_stop_run (1); } i--; for (; i >= 0; i--) { if (cmd[i] != ' ' && cmd[i] != 0) { break; } } if (i >= 0) { buff = cob_malloc ((size_t)(i + 2)); memcpy (buff, cmd, (size_t)(i + 1)); if (cob_screen_initialized) { cob_screen_set_mode (0); } i = system (buff); free (buff); if (cob_screen_initialized) { cob_screen_set_mode (1); } return i; } } return 1; }
void cob_check_numeric (cob_field *f, const char *name) { unsigned char *data; char *p; char *buff; size_t i; if (!cob_is_numeric (f)) { buff = cob_malloc (COB_SMALL_BUFF); p = buff; data = f->data; for (i = 0; i < f->size; ++i) { if (isprint (data[i])) { *p++ = data[i]; } else { p += sprintf (p, "\\%03o", data[i]); } } *p = '\0'; cob_runtime_error ("'%s' not numeric: '%s'", name, buff); cob_stop_run (1); } }
void cob_screen_accept (cob_screen *s, cob_field *line, cob_field *column) { struct cob_inp_struct *sptr; struct cob_inp_struct *sptr2; size_t idx; size_t n; size_t posu; size_t posd; size_t prevy; size_t firsty; int starty; if (!cob_screen_initialized) { cob_screen_init (); } if (!cob_base_inp) { cob_base_inp = cob_malloc (COB_INP_SIZE); } else { memset (cob_base_inp, 0, COB_INP_SIZE); } cob_exception_code = 0; cob_current_y = 0; cob_current_x = 0; totl_index = 0; move (0, 0); cob_prep_input (s); /* No input fields is an error */ if (!totl_index) { cob_check_pos_status (8000); return; } qsort (cob_base_inp, totl_index, sizeof(struct cob_inp_struct), compare_yx); sptr = cob_base_inp; starty = sptr->this_y; posu = 0; posd = 0; prevy = 0; firsty = 0; /* Set up array for Cursor UP/DOWN */ for (n = 0; n < totl_index; n++) { sptr = cob_base_inp + n; if (sptr->this_y > starty) { if (!firsty) { firsty = n; } starty = sptr->this_y; sptr2 = cob_base_inp + posd; for (idx = posd; idx < n; idx++, sptr2++) { sptr2->down_index = n; } posu = prevy; prevy = n; posd = n; } sptr->up_index = posu; } sptr = cob_base_inp; for (n = 0; n < firsty; n++, sptr++) { sptr->up_index = posd; } curr_index = 0; global_return = 0; cob_screen_get_all (); cob_check_pos_status (global_return); }
void cob_init (int argc, char **argv) { char *s; size_t i; char buff[32]; if (!cob_initialized) { cob_set_signal (); cob_argc = argc; cob_argv = argv; /* Get emergency buffer */ runtime_err_str = cob_malloc (COB_ERRBUF_SIZE); #ifdef HAVE_SETLOCALE setlocale (LC_ALL, ""); setlocale (LC_NUMERIC, "C"); s = setlocale (LC_ALL, NULL); if (s) { locale_save = strdup (s); } #endif #ifdef ENABLE_NLS bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); #endif /* Dirty hack until we implement something better */ #if defined(_WIN32) && !defined(_MSC_VER) _setmode (_fileno (stdin), _O_BINARY); _setmode (_fileno (stdout), _O_BINARY); _setmode (_fileno (stderr), _O_BINARY); #endif cob_init_numeric (); cob_init_strings (); cob_init_move (); cob_init_intrinsic (); cob_init_fileio (); cob_init_termio (); cob_init_call (); for (i = 0; i < 8; ++i) { memset (buff, 0, sizeof (buff)); snprintf (buff, 31, "COB_SWITCH_%d", (int)(i + 1)); s = getenv (buff); if (s && strcasecmp (s, "ON") == 0) { cob_switch[i] = 1; } else { cob_switch[i] = 0; } } s = getenv ("COB_LINE_TRACE"); if (s && (*s == 'Y' || *s == 'y')) { cob_line_trace = 1; } cob_initialized = 1; } }
static int ORBITS_ (const int entry) { /* Local variables */ #include "orbits.c.l.h" static int initialized = 0; static cob_field *cob_user_parameters[COB_MAX_FIELD_PARAMS]; static struct cob_module module = { NULL, NULL, NULL, NULL, cob_user_parameters, 0, '.', '$', ',', 1, 1, 1, 0 }; /* Start of function code */ /* CANCEL callback handling */ if (unlikely(entry < 0)) { if (!initialized) { return 0; } cob_close (h_ENTRADA, 0, NULL); cob_close (h_SORTIDA, 0, NULL); mpz_clear (d0.value); d0.scale = 0; mpz_clear (d1.value); d1.scale = 0; mpz_clear (d2.value); d2.scale = 0; mpz_clear (d3.value); d3.scale = 0; mpz_clear (d4.value); d4.scale = 0; initialized = 0; return 0; } /* Initialize frame stack */ frame_ptr = &frame_stack[0]; frame_ptr->perform_through = 0; /* Push module stack */ module.next = cob_current_module; cob_current_module = &module; /* Initialize program */ if (unlikely(initialized == 0)) { if (!cob_initialized) { cob_fatal_error (COB_FERROR_INITIALIZED); } cob_check_version (COB_SOURCE_FILE, COB_PACKAGE_VERSION, COB_PATCH_LEVEL); /* Initialize decimal numbers */ cob_decimal_init (&d0); cob_decimal_init (&d1); cob_decimal_init (&d2); cob_decimal_init (&d3); cob_decimal_init (&d4); (*(int *) (b_1)) = 0; if (!h_ENTRADA) { h_ENTRADA = cob_malloc (sizeof(cob_file)); } h_ENTRADA->select_name = (const char *)"ENTRADA"; h_ENTRADA->file_status = h_ENTRADA_status; memset (h_ENTRADA_status, '0', 2); h_ENTRADA->assign = &c_1; h_ENTRADA->record = &f_14; h_ENTRADA->record_size = NULL; h_ENTRADA->record_min = 0; h_ENTRADA->record_max = 33; h_ENTRADA->nkeys = 0; h_ENTRADA->keys = NULL; h_ENTRADA->file = NULL; h_ENTRADA->organization = 1; h_ENTRADA->access_mode = 1; h_ENTRADA->lock_mode = 0; h_ENTRADA->open_mode = 0; h_ENTRADA->flag_optional = 0; h_ENTRADA->last_open_mode = 0; h_ENTRADA->special = 0; h_ENTRADA->flag_nonexistent = 0; h_ENTRADA->flag_end_of_file = 0; h_ENTRADA->flag_begin_of_file = 0; h_ENTRADA->flag_first_read = 0; h_ENTRADA->flag_read_done = 0; h_ENTRADA->flag_select_features = 0; h_ENTRADA->flag_needs_nl = 0; h_ENTRADA->flag_needs_top = 0; h_ENTRADA->file_version = 0; if (!h_SORTIDA) { h_SORTIDA = cob_malloc (sizeof(cob_file)); } h_SORTIDA->select_name = (const char *)"SORTIDA"; h_SORTIDA->file_status = h_SORTIDA_status; memset (h_SORTIDA_status, '0', 2); h_SORTIDA->assign = &c_2; h_SORTIDA->record = &f_16; h_SORTIDA->record_size = NULL; h_SORTIDA->record_min = 0; h_SORTIDA->record_max = 80; h_SORTIDA->nkeys = 0; h_SORTIDA->keys = NULL; h_SORTIDA->file = NULL; h_SORTIDA->organization = 1; h_SORTIDA->access_mode = 1; h_SORTIDA->lock_mode = 0; h_SORTIDA->open_mode = 0; h_SORTIDA->flag_optional = 0; h_SORTIDA->last_open_mode = 0; h_SORTIDA->special = 0; h_SORTIDA->flag_nonexistent = 0; h_SORTIDA->flag_end_of_file = 0; h_SORTIDA->flag_begin_of_file = 0; h_SORTIDA->flag_first_read = 0; h_SORTIDA->flag_read_done = 0; h_SORTIDA->flag_select_features = 0; h_SORTIDA->flag_needs_nl = 0; h_SORTIDA->flag_needs_top = 0; h_SORTIDA->file_version = 0; initialized = 1; } /* Allocate LOCAL storage */ b_17 = cob_malloc (24); b_21 = cob_malloc (24); b_25 = cob_malloc (24); b_29 = cob_malloc (24); b_33 = cob_malloc (24); b_37 = cob_malloc (24); b_41 = cob_malloc (24); b_45 = cob_malloc (8); b_46 = cob_malloc (4); b_47 = cob_malloc (8); b_48 = cob_malloc (8); b_49 = cob_malloc (8); b_50 = cob_malloc (8); b_51 = cob_malloc (8); b_52 = cob_malloc (4); b_53 = cob_malloc (1); b_55 = cob_malloc (56); b_61 = cob_malloc (56); b_67 = cob_malloc (56); /* Initialialize LOCAL storage */ {double temp = 0.0; memcpy (b_17, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_17 + 8, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_17 + 16, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_21, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_21 + 8, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_21 + 16, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_25, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_25 + 8, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_25 + 16, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_29, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_29 + 8, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_29 + 16, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_33, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_33 + 8, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_33 + 16, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_37, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_37 + 8, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_37 + 16, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_41, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_41 + 8, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_41 + 16, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_45, (char *)&temp, sizeof(temp));} memset (b_46, 0, 4); {double temp = 0.0; memcpy (b_47, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_48, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_49, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_50, (char *)&temp, sizeof(temp));} {double temp = 0.0; memcpy (b_51, (char *)&temp, sizeof(temp));} memset (b_52, 0, 4); *(unsigned char *)(b_53) = 32; memcpy (b_55, "X", 1); memset (b_55 + 1, 32, 17); *(unsigned char *)(b_55 + 18) = 32; memcpy (b_55 + 19, "Y", 1); memset (b_55 + 19 + 1, 32, 17); *(unsigned char *)(b_55 + 37) = 32; memcpy (b_55 + 38, "Z", 1); memset (b_55 + 38 + 1, 32, 17); memset (b_61, 61, 18); *(unsigned char *)(b_61 + 18) = 32; memset (b_61 + 19, 61, 18); *(unsigned char *)(b_61 + 37) = 32; memset (b_61 + 38, 61, 18); memset (b_67, 48, 18); *(unsigned char *)(b_67 + 18) = 32; memset (b_67 + 19, 48, 18); *(unsigned char *)(b_67 + 37) = 32; memset (b_67 + 38, 48, 18); cob_save_call_params = cob_call_params; /* Entry dispatch */ goto l_2; /* PROCEDURE DIVISION */ /* Entry ORBITS */ l_2:; /* MAIN SECTION */ /* BEGIN */ /* orbits.cob:97: DISPLAY */ { cob_display (0, 1, 1, &c_3); } /* orbits.cob:98: MOVE */ { cob_move (&c_4, (f_45.data = b_45, &f_45)); } /* orbits.cob:99: OPEN */ { cob_exception_code = 0; { cob_open (h_ENTRADA, 1, 0, NULL); } if (unlikely(cob_exception_code != 0)) { /* PERFORM Default Error Handler */ frame_ptr++; frame_ptr->perform_through = 1; frame_ptr->return_address = &&l_14; goto l_1; l_14: frame_ptr--; } }