Annotation of freem/src/mlib.c, revision 1.1
1.1 ! snw 1: /*
! 2: * *
! 3: * * *
! 4: * * *
! 5: * ***************
! 6: * * * * *
! 7: * * MUMPS *
! 8: * * * * *
! 9: * ***************
! 10: * * *
! 11: * * *
! 12: * *
! 13: *
! 14: * mlib.h
! 15: * Function prototypes, structs, and macros for FreeM
! 16: * binding library
! 17: *
! 18: *
! 19: * Author: Serena Willis <jpw@coherent-logic.com>
! 20: * Copyright (C) 1998 MUG Deutschland
! 21: * Copyright (C) 2020 Coherent Logic Development LLC
! 22: *
! 23: *
! 24: * This file is part of FreeM.
! 25: *
! 26: * FreeM is free software: you can redistribute it and/or modify
! 27: * it under the terms of the GNU Affero Public License as published by
! 28: * the Free Software Foundation, either version 3 of the License, or
! 29: * (at your option) any later version.
! 30: *
! 31: * FreeM is distributed in the hope that it will be useful,
! 32: * but WITHOUT ANY WARRANTY; without even the implied warranty of
! 33: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 34: * GNU Affero Public License for more details.
! 35: *
! 36: * You should have received a copy of the GNU Affero Public License
! 37: * along with FreeM. If not, see <https://www.gnu.org/licenses/>.
! 38: *
! 39: **/
! 40:
! 41: #include <stdlib.h>
! 42:
! 43: #include "mpsdef.h"
! 44: #include "freem.h"
! 45: #include "init.h"
! 46: #include "transact.h"
! 47: #include "version.h"
! 48:
! 49: #include <stdio.h>
! 50: #include <ctype.h>
! 51: #include <string.h>
! 52: #include <errno.h>
! 53:
! 54: /* the following function prototypes are not in freem.h, as they are intended to be
! 55: * private and internal to mlib.c
! 56: */
! 57: void mref2str(freem_ref_t *ref, char *key);
! 58: void freem_release_io(void);
! 59: void freem_return_io(void);
! 60:
! 61: extern int xecline(int typ);
! 62:
! 63: void mref2str(freem_ref_t *ref, char *key)
! 64: {
! 65: register int i;
! 66:
! 67: for (i = 0; i < 256; i++) key[i] = NUL;
! 68:
! 69: switch (ref->reftype) {
! 70:
! 71: case MREF_RT_LOCAL: /* do nothing for locals, as they have no sigil */
! 72: break;
! 73:
! 74: case MREF_RT_GLOBAL:
! 75: strcat (key, "^");
! 76: break;
! 77:
! 78: case MREF_RT_SSVN:
! 79: strcat (key, "^$");
! 80: break;
! 81:
! 82: }
! 83:
! 84: if (ref->subscript_count > 0) {
! 85: strcat (key, ref->name);
! 86: strcat (key, "\202");
! 87:
! 88: for (i = 0; i < ref->subscript_count; i++) {
! 89: strcat (key, ref->subscripts[i]);
! 90:
! 91: if (i < ref->subscript_count - 1) strcat (key, "\202");
! 92: }
! 93: }
! 94: else {
! 95: strcat (key, ref->name);
! 96: }
! 97:
! 98: // ends with EOL
! 99: strncat (key, "\201", 2);
! 100:
! 101: }
! 102:
! 103: pid_t freem_init(char *environment_name, char *namespace_name)
! 104: {
! 105: int ierr_sav;
! 106: int errno_sav;
! 107:
! 108: libflag = TRUE;
! 109: noclear = TRUE;
! 110: frm_filter = TRUE;
! 111: direct_mode = FALSE;
! 112:
! 113: errno = 0;
! 114:
! 115: strcpy (shm_env, environment_name);
! 116: snprintf (config_file, 4096, "%s/freem/%s/freem.conf", SYSCONFDIR, shm_env);
! 117:
! 118: init (namespace_name);
! 119: ierr_sav = ierr;
! 120: errno_sav = errno;
! 121:
! 122: freem_release_io ();
! 123:
! 124:
! 125: if (ierr_sav > OK || errno_sav != 0) {
! 126: return -1;
! 127: }
! 128: else {
! 129: return pid;
! 130: }
! 131: }
! 132:
! 133: short freem_version(char *result)
! 134: {
! 135: strncpy (result, FREEM_VERSION_ID, 255);
! 136:
! 137: return OK;
! 138: }
! 139:
! 140: short freem_get(freem_ref_t *ref)
! 141: {
! 142: char key[256];
! 143: char result[256];
! 144:
! 145: freem_return_io ();
! 146:
! 147: // set up the EOL-delimited string
! 148: mref2str (ref, key);
! 149:
! 150: switch (ref->reftype) {
! 151:
! 152: case MREF_RT_LOCAL:
! 153: // call into the symbol table to get the local var
! 154: symtab (get_sym, key, result);
! 155: ref->status = merr ();
! 156: break;
! 157:
! 158: case MREF_RT_GLOBAL:
! 159: // call into the FreeM global handler; result in &result
! 160: global (get_sym, key, result);
! 161: ref->status = merr ();
! 162: break;
! 163:
! 164: case MREF_RT_SSVN:
! 165: // call into the SSVN code
! 166: ssvn (get_sym, key, result);
! 167: ref->status = merr ();
! 168: break;
! 169:
! 170: }
! 171:
! 172: stcnv_m2c (result);
! 173:
! 174: strncpy (ref->value, result, 255);
! 175:
! 176: freem_release_io ();
! 177:
! 178: return ref->status;
! 179: }
! 180:
! 181: short freem_set(freem_ref_t *ref)
! 182: {
! 183: char key[256];
! 184: char data[256];
! 185:
! 186: freem_return_io ();
! 187:
! 188: mref2str (ref, key);
! 189:
! 190: snprintf (data, 254, "%s\201", ref->value);
! 191:
! 192: switch (ref->reftype) {
! 193:
! 194: case MREF_RT_LOCAL:
! 195: // call into the symbol table to set the local var
! 196: symtab (set_sym, key, data);
! 197: ref->status = merr ();
! 198: break;
! 199:
! 200: case MREF_RT_GLOBAL:
! 201: // call into the FreeM global handler; data in &data
! 202: global (set_sym, key, data);
! 203: ref->status = merr ();
! 204: break;
! 205:
! 206: case MREF_RT_SSVN:
! 207: // call into the SSVN code
! 208: ssvn (set_sym, key, data);
! 209: ref->status = merr ();
! 210: break;
! 211:
! 212: }
! 213:
! 214: freem_release_io ();
! 215:
! 216:
! 217: return ref->status;
! 218: }
! 219:
! 220: short freem_kill(freem_ref_t *ref)
! 221: {
! 222: char key[256];
! 223: char result[256];
! 224:
! 225: mref2str (ref, key);
! 226:
! 227: freem_return_io ();
! 228:
! 229: switch (ref->reftype) {
! 230:
! 231: case MREF_RT_LOCAL:
! 232: // call into the symbol table to kill the local var
! 233: symtab (kill_sym, key, result);
! 234: ref->status = merr ();
! 235: break;
! 236:
! 237: case MREF_RT_GLOBAL:
! 238: // call into the FreeM global handler; result in &result
! 239: global (kill_sym, key, result);
! 240: ref->status = merr ();
! 241: break;
! 242:
! 243: case MREF_RT_SSVN:
! 244: // call into the SSVN code
! 245: ssvn (kill_sym, key, result);
! 246: ref->status = merr ();
! 247: break;
! 248:
! 249: }
! 250:
! 251: stcnv_m2c (result);
! 252:
! 253: strncpy (ref->value, result, 255);
! 254:
! 255: freem_release_io ();
! 256:
! 257:
! 258: return ref->status;
! 259: }
! 260:
! 261: short freem_data(freem_ref_t *ref)
! 262: {
! 263: char key[256];
! 264: char result[256];
! 265:
! 266: mref2str (ref, key);
! 267:
! 268: freem_return_io ();
! 269:
! 270: switch (ref->reftype) {
! 271:
! 272: case MREF_RT_LOCAL:
! 273: // call into the symbol table
! 274: symtab (dat, key, result);
! 275: ref->status = merr ();
! 276: break;
! 277:
! 278: case MREF_RT_GLOBAL:
! 279: // call into the FreeM global handler; result in &result
! 280: global (dat, key, result);
! 281: ref->status = merr ();
! 282: break;
! 283:
! 284: case MREF_RT_SSVN:
! 285: // call into the SSVN code
! 286: ssvn (dat, key, result);
! 287: ref->status = merr ();
! 288: break;
! 289:
! 290: }
! 291:
! 292: stcnv_m2c (result);
! 293:
! 294: strncpy (ref->value, result, 255);
! 295:
! 296: freem_release_io ();
! 297:
! 298: return ref->status;
! 299: }
! 300:
! 301: short freem_order(freem_ref_t *ref)
! 302: {
! 303: char key[256];
! 304: char result[256];
! 305:
! 306: mref2str (ref, key);
! 307:
! 308: freem_return_io ();
! 309:
! 310: switch (ref->reftype) {
! 311:
! 312: case MREF_RT_LOCAL:
! 313: // call into the symbol table for $ORDER
! 314: symtab (fra_order, key, result);
! 315: ref->status = merr ();
! 316: break;
! 317:
! 318: case MREF_RT_GLOBAL:
! 319: // call into the FreeM global handler; result in &result
! 320: global (fra_order, key, result);
! 321: ref->status = merr ();
! 322: break;
! 323:
! 324: case MREF_RT_SSVN:
! 325: // call into the SSVN code
! 326: ssvn (fra_order, key, result);
! 327: ref->status = merr ();
! 328: break;
! 329:
! 330: }
! 331:
! 332: stcnv_m2c (result);
! 333:
! 334: strncpy (ref->value, result, 255);
! 335: freem_release_io ();
! 336:
! 337: return ref->status;
! 338: }
! 339:
! 340: short freem_query(freem_ref_t *ref)
! 341: {
! 342: char key[256];
! 343: char result[256];
! 344:
! 345: mref2str (ref, key);
! 346:
! 347: freem_return_io ();
! 348:
! 349: switch (ref->reftype) {
! 350:
! 351: case MREF_RT_LOCAL:
! 352: // call into the symbol table
! 353: symtab (fra_query, key, result);
! 354: ref->status = merr ();
! 355: break;
! 356:
! 357: case MREF_RT_GLOBAL:
! 358: // call into the FreeM global handler; result in &result
! 359: global (fra_query, key, result);
! 360: ref->status = merr ();
! 361: break;
! 362:
! 363: case MREF_RT_SSVN:
! 364: // call into the SSVN code
! 365: ssvn (fra_query, key, result);
! 366: ref->status = merr ();
! 367: break;
! 368:
! 369: }
! 370:
! 371: stcnv_m2c (result);
! 372:
! 373: strncpy (ref->value, result, 255);
! 374:
! 375: freem_release_io ();
! 376:
! 377: return ref->status;
! 378: }
! 379:
! 380: short freem_lock(freem_ref_t *ref, long lck_timeout)
! 381: {
! 382: char key[256];
! 383: char buf[256];
! 384:
! 385: mref2str (ref, key);
! 386: snprintf (buf, 255, "+%s\201", key);
! 387:
! 388: freem_return_io ();
! 389:
! 390: lock (buf, lck_timeout, 'l');
! 391: ref->status = merr ();
! 392:
! 393: freem_release_io ();
! 394:
! 395: return ref->status;
! 396: }
! 397:
! 398: short freem_unlock(freem_ref_t *ref, long lck_timeout)
! 399: {
! 400: char key[256];
! 401: char buf[256];
! 402:
! 403: mref2str (ref, key);
! 404: snprintf (buf, 255, "-%s\201", key);
! 405:
! 406: freem_return_io ();
! 407:
! 408: lock (buf, lck_timeout, 'l');
! 409: ref->status = merr ();
! 410:
! 411: freem_release_io ();
! 412:
! 413: return ref->status;
! 414: }
! 415:
! 416: short freem_tstart(char *tp_id, short serial, short restartable, char **sym_save)
! 417: {
! 418: return tp_tstart (tp_id, serial, restartable, sym_save);
! 419: }
! 420:
! 421: short freem_trestart(void)
! 422: {
! 423: return tp_trestart ();
! 424: }
! 425:
! 426: short freem_trollback(int tp_levels)
! 427: {
! 428: return tp_trollback (tp_levels);
! 429: }
! 430:
! 431: short freem_tcommit(void)
! 432: {
! 433: return tp_tcommit ();
! 434: }
! 435:
! 436: int freem_tlevel(void)
! 437: {
! 438: return tp_level;
! 439: }
! 440:
! 441: short freem_function(freem_ent_t *ent)
! 442: {
! 443: register int i;
! 444: char buf[STRLEN] = {0};
! 445: char tmp[STRLEN] = {0};
! 446: char tmp1[STRLEN] = {0};
! 447:
! 448: freem_return_io ();
! 449:
! 450: if (ent->argument_count > 0) {
! 451: snprintf (buf, STRLEN - 1, "S %%ZFRMRTN=$$%s(", ent->name);
! 452:
! 453: for (i = 0; i < ent->argument_count; i++) {
! 454:
! 455: strncpy (tmp1, ent->arguments[i], STRLEN - 1);
! 456: stcnv_c2m (tmp1);
! 457:
! 458: if (znamenumeric (tmp1) == TRUE) {
! 459: /* arguments that are canonical MUMPS numbers don't get quoted */
! 460: snprintf (tmp, STRLEN - 1, "%s", ent->arguments[i]);
! 461: }
! 462: else {
! 463: /* string arguments do get quoted */
! 464: snprintf (tmp, STRLEN - 1, "\"%s\"", ent->arguments[i]);
! 465: }
! 466:
! 467: strcat (buf, tmp);
! 468:
! 469: if(i < ent->argument_count - 1) {
! 470: strcat (buf, ",");
! 471: }
! 472: }
! 473:
! 474: strcat (buf, ")");
! 475: }
! 476: else {
! 477: snprintf (buf, STRLEN - 1, "S %%ZFRMRTN=$$%s", ent->name);
! 478: }
! 479:
! 480: stcnv_c2m (buf);
! 481: sprintf (tmp, "%%ZFRMXEC\201");
! 482: symtab (set_sym, tmp, buf);
! 483: snprintf ((char *) ztrap, 12, "^%%ZFRMXEC\201");
! 484:
! 485: xecline (3);
! 486: ent->status = merr ();
! 487:
! 488: tmp[0] = NUL;
! 489: ent->value[0] = NUL;
! 490:
! 491: snprintf (tmp, 255, "%%ZFRMRTN\201");
! 492: symtab (get_sym, tmp, ent->value);
! 493: stcnv_m2c (ent->value);
! 494:
! 495: freem_release_io ();
! 496:
! 497: return ent->status;
! 498: }
! 499:
! 500: short freem_procedure(freem_ent_t *ent)
! 501: {
! 502: register int i;
! 503: char buf[STRLEN] = {0};
! 504: char tmp[STRLEN] = {0};
! 505: char tmp1[STRLEN] = {0};
! 506:
! 507: freem_return_io ();
! 508:
! 509: if (ent->argument_count > 0) {
! 510: snprintf (buf, STRLEN - 1, "DO %s(", ent->name);
! 511:
! 512: for (i = 0; i < ent->argument_count; i++) {
! 513:
! 514: strncpy (tmp1, ent->arguments[i], STRLEN - 1);
! 515: stcnv_c2m (tmp1);
! 516:
! 517: if (znamenumeric (tmp1) == TRUE) {
! 518: /* arguments that are canonical MUMPS numbers don't get quoted */
! 519: snprintf (tmp, STRLEN - 1, "%s", ent->arguments[i]);
! 520: }
! 521: else {
! 522: /* string arguments do get quoted */
! 523: snprintf (tmp, STRLEN - 1, "\"%s\"", ent->arguments[i]);
! 524: }
! 525:
! 526: strcat (buf, tmp);
! 527:
! 528: if(i < ent->argument_count - 1) {
! 529: strcat (buf, ",");
! 530: }
! 531: }
! 532:
! 533: strcat (buf, ")");
! 534: }
! 535: else {
! 536: snprintf (buf, STRLEN - 1, "DO %s", ent->name);
! 537: }
! 538:
! 539: stcnv_c2m (buf);
! 540: sprintf (tmp, "%%ZFRMXEC\201");
! 541: symtab (set_sym, tmp, buf);
! 542:
! 543:
! 544: snprintf ((char *) ztrap, 12, "^%%ZFRMXEC\201");
! 545:
! 546:
! 547: xecline (3);
! 548: ent->status = merr ();
! 549:
! 550: tmp[0] = NUL;
! 551: ent->value[0] = NUL;
! 552:
! 553: freem_release_io ();
! 554:
! 555: return ent->status;
! 556: }
! 557:
! 558: short freem_errmsg(int code, char *msg)
! 559: {
! 560: char emtmp[256];
! 561: char cvtmp[256];
! 562:
! 563: if (code > MAXERR || code < 0) {
! 564: return FALSE;
! 565: }
! 566:
! 567: stcpy (cvtmp, errmes[code]);
! 568: stcnv_m2c (cvtmp);
! 569:
! 570: merr_num_to_code (code, emtmp);
! 571:
! 572: sprintf (msg, "%s: %s", emtmp, cvtmp);
! 573:
! 574: return TRUE;
! 575: }
! 576:
! 577: void freem_release_io(void)
! 578: {
! 579: set_io (UNIX);
! 580: }
! 581:
! 582: void freem_return_io(void)
! 583: {
! 584: set_io (MUMPS);
! 585: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>