static char *arith_string (SLtype type, VOID_STAR v) { char buf [1024]; char *s; s = buf; switch (type) { default: s = (char *) SLclass_get_datatype_name (type); break; case SLANG_CHAR_TYPE: sprintf (s, "%d", *(char *) v); break; case SLANG_UCHAR_TYPE: sprintf (s, "%u", *(unsigned char *) v); break; case SLANG_SHORT_TYPE: sprintf (s, "%d", *(short *) v); break; case SLANG_USHORT_TYPE: sprintf (s, "%u", *(unsigned short *) v); break; case SLANG_INT_TYPE: sprintf (s, "%d", *(int *) v); break; case SLANG_UINT_TYPE: sprintf (s, "%u", *(unsigned int *) v); break; case SLANG_LONG_TYPE: sprintf (s, "%ld", *(long *) v); break; case SLANG_ULONG_TYPE: sprintf (s, "%lu", *(unsigned long *) v); break; #ifdef HAVE_LONG_LONG case SLANG_LLONG_TYPE: sprintf (s, "%lld", *(long long *) v); break; case SLANG_ULLONG_TYPE: sprintf (s, "%llu", *(unsigned long long *) v); break; #endif #if SLANG_HAS_FLOAT case SLANG_FLOAT_TYPE: if (EOF == SLsnprintf (buf, sizeof (buf), Double_Format, *(float *) v)) sprintf (s, "%e", *(float *) v); break; case SLANG_DOUBLE_TYPE: if (EOF == SLsnprintf (buf, sizeof (buf), Double_Format, *(double *) v)) sprintf (s, "%e", *(double *) v); break; #endif } return SLmake_string (s); }
SLrline_Type *SLrline_open2 (SLFUTURE_CONST char *name, unsigned int width, unsigned int flags) { SLrline_Type *rli; SLrline_Type *arli; char hookname [1024]; if (NULL == (rli = SLrline_open (width, flags))) return NULL; if (NULL != rli->name) SLang_free_slstring (rli->name); if (NULL == (rli->name = SLang_create_slstring (name))) { SLrline_close (rli); return NULL; } arli = Active_Rline_Info; Active_Rline_Info = rli; SLsnprintf (hookname, sizeof(hookname), "%s_rline_open_hook", name); if (0 == SLang_run_hooks (hookname, 0)) (void) SLang_run_hooks ("rline_open_hook", 1, name); Active_Rline_Info = arli; return rli; }
void SLrline_close (SLrline_Type *rli) { if (rli == NULL) return; if (rli->name != NULL) { char hookname[1024]; SLrline_Type *arli = Active_Rline_Info; Active_Rline_Info = rli; SLsnprintf (hookname, sizeof(hookname), "%s_rline_close_hook", rli->name); if (0 == SLang_run_hooks (hookname, 0)) (void) SLang_run_hooks ("rline_close_hook", 1, rli->name); Active_Rline_Info = arli; SLang_free_slstring (rli->name); } free_history (rli->root); free_history_item (rli->saved_line); SLang_free_function (rli->list_completions_callback); SLang_free_function (rli->completion_callback); SLfree ((char *)rli->prompt); SLfree ((char *)rli->buf); SLfree ((char *)rli); }
static void default_format_float (float x, char *buf, unsigned int buflen) { if (EOF == SLsnprintf (buf, buflen, "%.8g", x)) { sprintf (buf, "%e", x); return; } if ((float) atof (buf) != x) { if (EOF == SLsnprintf (buf, buflen, "%.9g", x)) { sprintf (buf, "%e", x); return; } } check_decimal (buf, buflen, x); }
static void default_format_double (double x, char *buf, unsigned int buflen) { if (EOF == SLsnprintf (buf, buflen, "%.16g", x)) { sprintf (buf, "%e", x); return; } if (atof (buf) != x) { if (EOF == SLsnprintf (buf, buflen, "%.17g", x)) { sprintf (buf, "%e", x); return; } } check_decimal (buf, buflen, x); }
static VOID_STAR do_dlsym (VOID_STAR handle, SLFUTURE_CONST char *file, int check_error, SLFUTURE_CONST char *fmt, char *module) { char symbol[MAX_MODULE_NAME_SIZE + 32]; VOID_STAR s; SLsnprintf (symbol, sizeof(symbol), fmt, module); if (NULL != (s = (VOID_STAR) dlsym (handle, symbol))) return s; if (check_error) { SLCONST char *err; if (NULL == (err = (char *) dlerror ())) err = "UNKNOWN"; _pSLang_verror (SL_Import_Error, "Unable to get symbol %s from %s: %s", symbol, file, err); } return NULL; }
static Handle_Type *dynamic_link_module (SLFUTURE_CONST char *module) { Handle_Type *h; VOID_STAR handle; SLFUTURE_CONST char *err; char filebuf[1024]; char *save_file; char *save_err; int api_version; int *api_version_ptr; #define MAX_MODULE_NAME_SIZE 256 char module_so[MAX_MODULE_NAME_SIZE + 32]; char *module_name; char *file, *pathfile; if (strlen (module) >= MAX_MODULE_NAME_SIZE) { _pSLang_verror (SL_LimitExceeded_Error, "module name too long"); return NULL; } SLsnprintf (module_so, sizeof(module_so), "%s-module.%s", module, SO_SUFFIX); if (Module_Path != NULL) pathfile = SLpath_find_file_in_path (Module_Path, module_so); else pathfile = NULL; if ((pathfile == NULL) && (NULL != (pathfile = _pSLsecure_getenv (MODULE_PATH_ENV_NAME)))) pathfile = SLpath_find_file_in_path (pathfile, module_so); if (pathfile == NULL) pathfile = SLpath_find_file_in_path (MODULE_INSTALL_DIR, module_so); if (pathfile != NULL) file = pathfile; else file = module_so; save_err = NULL; save_file = file; while (1) { #ifndef RTLD_GLOBAL # define RTLD_GLOBAL 0 #endif #ifdef RTLD_NOW handle = (VOID_STAR) dlopen (file, RTLD_NOW | RTLD_GLOBAL); #else handle = (VOID_STAR) dlopen (file, RTLD_LAZY | RTLD_GLOBAL); #endif if (handle != NULL) { if (_pSLang_Load_File_Verbose & SLANG_LOAD_MODULE_VERBOSE) SLang_vmessage ("Importing %s", file); if (save_err != NULL) SLfree (save_err); break; } /* Purify reports that dlerror returns a pointer that generates UMR * errors. There is nothing that I can do about that.... */ if ((NULL == strchr (file, '/')) && (strlen(file) < sizeof(filebuf))) { err = (char *) dlerror (); if (err != NULL) save_err = SLmake_string (err); SLsnprintf (filebuf, sizeof (filebuf), "./%s", file); file = filebuf; continue; } if ((NULL == (err = save_err)) && (NULL == (err = (char *) dlerror ()))) err = "UNKNOWN"; _pSLang_verror (SL_Import_Error, "Error linking to %s: %s", save_file, err); if (save_err != NULL) SLfree (save_err); if (pathfile != NULL) SLfree (pathfile); return NULL; } /* Using SLpath_basename allows, e.g., import ("/path/to/module"); */ module_name = SLpath_basename (module); api_version_ptr = (int *) do_dlsym (handle, file, 0, "SLmodule_%s_api_version", module_name); if (api_version_ptr == NULL) api_version_ptr = (int *) do_dlsym (handle, file, 0, "_SLmodule_%s_api_version", module_name); if (api_version_ptr == NULL) api_version = 0; else api_version = *api_version_ptr; if ((-1 == check_api_version (file, api_version)) || (NULL == (h = allocate_handle_type (module, handle)))) { SLfree (pathfile); /* NULL ok */ dlclose (handle); return NULL; } if (NULL == (h->ns_init_fun = (int (*)(SLCONST char *)) do_dlsym (handle, file, 1, "init_%s_module_ns", module_name))) { SLfree (pathfile); free_handle_type (h); dlclose (handle); return NULL; } h->deinit_fun = (void (*)(void)) do_dlsym (handle, file, 0, "deinit_%s_module", module_name); SLfree (pathfile); /* NULL ok */ h->next = Handle_List; Handle_List = h; return h; }
static void finish_status(int col_flag) { char *v, ch; Line *l; int top, rows, narrows; char buf[256]; char *str; v = CBuf->status_line; if (*v == 0) v = Default_Status_Line; while (1) { char *v0 = v; while (1) { ch = *v; if (ch == 0) { SLsmg_write_nchars (v0, (unsigned int) (v-v0)); return; } if (ch == '%') { SLsmg_write_nchars (v0, (unsigned int) (v-v0)); break; } v++; } /* At this point *v == '%' */ v++; ch = *v++; switch (ch) { case 'F': SLsmg_write_string (CBuf->dir); str = CBuf->file; break; case 'S': /* stack depth */ sprintf(buf, "%03d", SLstack_depth()); str = buf; break; case 'a': if (CBuf->flags & ABBREV_MODE) str = " abbrev"; else str = NULL; break; case 'f': str = CBuf->file; break; case 'n': narrows = jed_count_narrows (); if (narrows) { sprintf (buf, " Narrow[%d]", narrows); str = buf; } else str = NULL; break; case 'o': if (CBuf->flags & OVERWRITE_MODE) str = " Ovwrt"; else str = NULL; break; case 'O': if (CBuf->flags & OVERWRITE_MODE) str = " ovr"; else str = " ins"; break; case 'b': str = CBuf->name; break; case 'p': str = buf; if (0 == User_Prefers_Line_Numbers) { top = JWindow->sy; rows = JWindow->rows - 1; l = JScreen[top + rows].line; if (l == CBuf->end) l = NULL; if (JScreen[top].line == CBuf->beg) { if (l == NULL) str = "All"; else str = "Top"; } else if (l == NULL) str = "Bot"; else { sprintf(buf, "%d%%", (int) ((LineNum * 100L) / (long) Max_LineNum)); } } else { if (User_Prefers_Line_Numbers == 1) sprintf(buf, "%u/%u", LineNum, Max_LineNum); else { if (col_flag) (void) calculate_column (); sprintf(buf, "%u/%u,%d", LineNum, Max_LineNum, Absolute_Column); } } break; case 'l': sprintf(buf, "%u", LineNum); str=buf; break; case 'L': sprintf(buf, "%u", Max_LineNum); str=buf; break; case 'v': SLsmg_write_string (Jed_Version_String); if (Jed_UTF8_Mode) str = "U"; else str = NULL; break; case 'm': str = CBuf->mode_string; break; case 't': str = status_get_time(); break; case 'c': if (col_flag) (void) calculate_column (); sprintf(buf, "%d", Absolute_Column); str = buf; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { char fmt[16]; int num = ch - '0'; while (isdigit(*v)) { num = num * 10 + (*v - '0'); v++; } if ((num < 0) || (-1 == SLsnprintf (fmt, sizeof(fmt), "%s%du", ((ch == '0') ? "%0" : "%"), num))) strcpy (fmt, "%u"); str = "*"; switch (*v++) { default: v--; break; case 'c': if (col_flag) (void) calculate_column (); if (-1 != SLsnprintf (buf, sizeof (buf), fmt, (unsigned int)Absolute_Column)) str = buf; break; case 'l': if (-1 != SLsnprintf (buf, sizeof (buf), fmt, LineNum)) str = buf; break; case 'L': if (-1 != SLsnprintf (buf, sizeof (buf), fmt, Max_LineNum)) str = buf; break; } } break; case '%': str = "%"; break; case 0: return; default: str = NULL; } if (str != NULL) SLsmg_write_string (str); } }
static void check_decimal (char *buf, unsigned int buflen, double x) { char *b, *bstart = buf, *bufmax = buf + buflen; char ch; unsigned int count = 0, expon; int has_point = 0; unsigned int expon_threshold = Double_Format_Expon_Threshold; if (*bstart == '-') bstart++; b = bstart; while (1) { ch = *b; if (isdigit (ch)) { count++; b++; continue; } if (ch == 0) break; /* all digits */ if (ch != '.') return; /* something else */ /* We are at a decimal point. If expon > 1, then buf does not contain * an exponential formatted quantity. */ if (count <= expon_threshold) return; /* We have something like: 1234567.123, which we want to * write as 1.234567123e+6 */ b += strlen(b); has_point = 1; break; /* handle below */ } /* We get here only when *b==0. */ if ((has_point == 0) && (count <= 6)) { if (b + 3 >= bufmax) { sprintf (buf, "%e", x); return; } *b++ = '.'; *b++ = '0'; *b = 0; return; } expon = count-1; /* Now add on the exponent. First move the decimal point but drop trailing 0s */ while ((count > 1) && (*(b-1) == '0')) { b--; count--; } if (count > 1) { while (count > 1) { bstart[count] = bstart[count-1]; count--; } bstart[count] = '.'; if (has_point == 0) b++; } if (EOF == SLsnprintf (b, bufmax-b, "e+%02d", expon)) sprintf (buf, "%e", x); }