home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / p / p024 / 12.img / ADS3.LIB / GPALSYM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-12  |  10.8 KB  |  345 lines

  1. /* Next available MSG number is  33 */
  2.  
  3. /***************************************************************************
  4.    Module Name:  gpalsym.c
  5.  
  6.    Description:  ADS sample program which exercises the ads_getsym and
  7.                  ads_putsym functions.
  8.  
  9.    Author     :
  10.                  Autodesk, Inc.
  11.                  2320 Marinship Way
  12.                  Sausalito, CA. 94965
  13.                  (415)332-2344
  14.  
  15.       ¬⌐┼v (C) 1992  Autodesk ñ╜Ñq
  16.  
  17.       Ñ╗│n┼ΘºK╢O¿╤▒z╢iªµÑ⌠ª≤Ñ╬│~╗▌¿D¬║½■¿⌐íB¡╫º∩ñ╬╡oªµ, ª²¼O░╚╜╨┐φ┤`ñU¡z
  18.       ¡∞½h :
  19.  
  20.       1)  ñWªC¬║¬⌐┼v│qºi░╚╗▌ÑX▓{ªb¿Cñ@Ñ≈½■¿⌐∙╪íC
  21.       2)  ¼█├÷¬║╗í⌐·ñσÑ≤ñ]Ñ▓╢╖⌐·╕ⁿ¬⌐┼v│qºiñ╬Ñ╗╢╡│\Ñi│qºiíC
  22.  
  23.       Ñ╗│n┼Θ╢╚┤ú¿╤º@¼░└│Ñ╬ñW¬║░╤ª╥, ª╙Ñ╝┴n⌐·⌐╬┴⌠ºtÑ⌠ª≤½O├╥; ╣∩⌐≤Ñ⌠ª≤»S«φ
  24.       Ñ╬│~ñº╛A║┘⌐╩, ÑHñ╬░╙╖~╛P░Γ⌐╥┴⌠ºtÑX¿π¬║½O├╥, ªbª╣ñ@╖ºñ⌐ÑHº_╗{íC
  25.  
  26.  
  27.     Function Entry Points:
  28.         void
  29.         main(argc, argv)
  30.         int    argc;              [Number of argument passed in cmd line]
  31.         char   *argv[];           [Array of pointers to arguments]
  32.  
  33.     Exported ADS Functions
  34.         TESTGPSYM                 [Test the ADS_XXXSYM functions]
  35.  
  36.     Modification History:
  37.         Feb 25 1992 - bcm - original creation
  38.  
  39.     Notes and restrictions on use:
  40.  
  41.  
  42. ***************************************************************************/
  43.  
  44. /**************************************************************************/
  45. /*  MODULE NAME  */
  46. /**************************************************************************/
  47. #define    GPALSYM
  48.  
  49. /****************************************************************************/
  50. /*  DEFINES  */
  51. /****************************************************************************/
  52. #define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
  53. #define SPOINT(p, x, y, z) p[X] = (x); p[Y] = (y); p[Z] = (z)
  54. #define CPOINT(dst, src) dst[X] = src[X]; dst[Y] = src[Y]; dst[Z] = src[Z]
  55.  
  56. /**************************************************************************/
  57. /*  TYPEDEFS  */
  58. /**************************************************************************/
  59. /* ADS Function Table */
  60. typedef struct {
  61.     char    *name;
  62.     int     (*fptr)();
  63. } ftblent;
  64.  
  65. typedef struct resbuf rbtype;
  66.  
  67. /**************************************************************************/
  68. /*  INCLUDES  */
  69. /**************************************************************************/
  70.  
  71. #include <stdio.h>
  72. #include "adslib.h"
  73. #include "ol_errno.h"
  74. /****************************************************************************/
  75. /*  LOCAL FUNCTION FORWARD DECLARATIONS  */
  76. /****************************************************************************/
  77. int     getsym();
  78. int     putsym();
  79.  
  80. /**************************************************************************/
  81. /*  GLOBAL VARIABLES  */
  82. /**************************************************************************/
  83. /* Table of ADS functions */
  84. ftblent exfun[] = {
  85.             {/*MSG0*/"GETSYM", getsym},
  86.             {/*MSG0*/"PUTSYM", putsym},
  87.         };
  88.  
  89. /**************************************************************************/
  90. /*  EXTERNAL FUNCTION DECLARATIONS  */
  91. /**************************************************************************/
  92.  
  93. /**************************************************************************/
  94. /*  EXTERNAL VARIABLE DECLARATIONS  */
  95. /**************************************************************************/
  96.  
  97. /****************************************************************************/
  98. /*  LOCAL FUNCTION DECLARATIONS  */
  99. /****************************************************************************/
  100.  
  101. /******************************************************************************/
  102. /*.doc geterrno(internal) */
  103. /*+
  104.     This function is called to obtain the value of the AutoCAD system
  105.     variable ERRNO and return it as a result.
  106. -*/
  107. /******************************************************************************/
  108. int
  109. /*FCN*/geterrno()
  110. {
  111.     rbtype errval;
  112.  
  113.     ads_getvar(/*MSG0*/"ERRNO", &errval);
  114.  
  115.     return errval.resval.rint;
  116. }
  117.  
  118. /******************************************************************************/
  119. /*.doc funcload(internal) */
  120. /*+
  121.     This function is called to define all function names in the ADS
  122.     function table.  Each named function will be callable from lisp or
  123.     invokable from another ADS application.
  124. -*/
  125. /******************************************************************************/
  126. int
  127. /*FCN*/funcload()
  128. {
  129.     int i;
  130.     for (i = 0; i < ELEMENTS(exfun); i++) {
  131.         if (ads_defun(exfun[i].name, i) != RTNORM)
  132.             return RTERROR;
  133.     }
  134.  
  135.     return RTNORM;
  136. }
  137.  
  138. /******************************************************************************/
  139. /*.doc funclunoad(internal) */
  140. /*+
  141.     This function is called to undefine all function names in the ADS
  142.     function table.  Each named function will be removed from the
  143.     AutoLISP hash table.
  144. -*/
  145. /******************************************************************************/
  146. int
  147. /*FCN*/funcunload()
  148. {
  149.     int i;
  150.  
  151.     /* Undefine each function we defined */
  152.  
  153.     for (i = 0; i < ELEMENTS(exfun); i++) {
  154.         ads_undef(exfun[i].name,i);
  155.     }
  156.  
  157.     return RTNORM;
  158. }
  159. /******************************************************************************/
  160. /*.doc dofun(internal) */
  161. /*+
  162.     This function is called to invoke the function which has the
  163.     registerd function code that is obtained from  ads_getfuncode.  The
  164.     function will return RTERROR if the function code is invalid, or
  165.     RSERR if the invoked function fails to return RTNORM.  The value
  166.     RSRSLT will be returned if the function code is valid and the
  167.     invoked subroutine returns RTNORM.
  168. -*/
  169. /******************************************************************************/
  170. int
  171. /*FCN*/dofun()
  172. {
  173.     int    val;
  174.     int    rc;
  175.  
  176.     ads_retvoid();
  177.  
  178.     if ((val = ads_getfuncode()) < 0 || val > ELEMENTS(exfun))
  179.         return RTERROR;
  180.  
  181.     rc = (*exfun[val].fptr)();
  182.  
  183.     return ((rc == RTNORM) ? RSRSLT:RSERR);
  184. }
  185.  
  186. /******************************************************************************/
  187. /*.doc main(internal) */
  188. /*+
  189.     This is the main entry point for the ADS application.  All ADS
  190.     requests will be dispatched from this function.  This is your
  191.     standard ADS dispatch loop.
  192. -*/
  193. /******************************************************************************/
  194. void
  195. /*FCN*/main(argc,argv)
  196. int argc;
  197. char *argv[];
  198. {
  199.     short scode = RSRSLT;          /* Normal result code (default) */
  200.     int   stat;
  201.  
  202.     ads_init(argc, argv);          /* Initiate communication with AutoLISP */
  203.  
  204.     for ( ;; ) {                   /* Request/Result loop */
  205.  
  206.         if ((stat = ads_link(scode)) < 0) {
  207.             printf(/*MSG1*/"GPALSYM: Ñ╤ ads_link() ╢╟ª^¬║ñú¿╬¬¼║A = %d\n", stat);
  208.             fflush(stdout);
  209.             exit(1);
  210.         }
  211.  
  212.         scode = RSRSLT;           /* Reset result code */
  213.  
  214.         switch (stat) {
  215.  
  216.         case RQXLOAD:             /* Load & define functions */
  217.             scode = funcload() ? -RSRSLT : -RSERR;
  218.             break;
  219.  
  220.         case RQXUNLD:             /* Unload functions */
  221.             scode = funcunload() ? -RSRSLT : -RSERR;
  222.             ads_printf(/*MSG2*/"─└⌐±íC\n");
  223.             break;
  224.  
  225.         case RQSUBR:             /* Handle request for external function */
  226.             dofun();
  227.             break;
  228.  
  229.         default:
  230.             break;
  231.         }
  232.     }
  233. }
  234.  
  235. /******************************************************************************/
  236. /*.doc getsym(internal) */
  237. /*+
  238.     This function is called from dofun function as a result of RQSUBR
  239.     request being sent to the main dispatch loop.  It requires one
  240.     string argument which specifies the name of an AutoLISP symbol
  241.     whos value should be returned.
  242.  
  243.     This function always returns RTNORM.  An error message will be
  244.     displayed in the event of an error, and nil will be returned.
  245.     Otherwise, the value of the AutoLISP symbol will be returned
  246.     to the AutoLISP caller.
  247. -*/
  248. /******************************************************************************/
  249. int
  250. /*FCN*/getsym()
  251. {
  252.     rbtype      *args;
  253.     int         rc;
  254.     rbtype      *varval = NULL;
  255.  
  256.     args = ads_getargs();
  257.     if (args == NULL || args->restype != RTSTR) {
  258.         ads_printf(/*MSG3*/"íu%sívññ╗▌¡nñ@╢╡íuAutoLISP ▓┼╕╣ªW║┘ívíC\n");
  259.         return RTNORM;
  260.     }
  261.  
  262.     rc = ads_getsym(args->resval.rstring, &varval);
  263.     if (rc != RTNORM) {
  264.         switch(geterrno()) {
  265.         case OL_ESYMNAM:
  266.             ads_printf(/*MSG4*/"íuAutoLISP ▓┼╕╣ªW║┘ív╡L«─íC\n");
  267.             break;
  268.         case OL_ENULLPTR:
  269.             ads_printf(/*MSG5*/"╢╟╗╝íuNULL ½ⁿ╝╨ívñ⌐ ads_getsym íC\n");
  270.             break;
  271.         case OL_ENEWRB:
  272.             ads_printf(/*MSG6*/"╡L¬k░t╕míu╡▓¬G╜w╜─░╧ívíC\n");
  273.             break;
  274.         case OL_ESYMVAL:
  275.             ads_printf(/*MSG7*/"íu▓┼╕╣¡╚ív╡L«─íC\n");
  276.             break;
  277.         }
  278.     } else {
  279.        if (varval != NULL) {
  280.            if (varval->rbnext == NULL)
  281.                ads_retval(varval);
  282.            else
  283.                ads_retlist(varval);
  284.        }
  285.     }
  286.     if (varval != NULL)
  287.         ads_relrb(varval);
  288.  
  289.     return RTNORM;
  290. }
  291.  
  292. /******************************************************************************/
  293. /*.doc putsym(internal) */
  294. /*+
  295.     This function is called from dofun function as a result of RQSUBR
  296.     request being sent to the main dispatch loop.  It requires two
  297.     arguments.  The first argument is a string containing the name of
  298.     an AutoLISP symbol to set.  The second argument can be any data
  299.     type or list containing any data type which can be represented
  300.     in an ADS resbuf structure.
  301.  
  302.     This function always returns RTNORM to the C caller.  This function
  303.     always return nil if the operation fails.  Otherwise, the value of
  304.     the second argument is returned.
  305.  
  306.     A message will be displayed if an error occurs, and the value
  307.     returned to the AutoLISP caller will be nil.
  308. -*/
  309. /******************************************************************************/
  310. int
  311. /*FCN*/putsym()
  312. {
  313.     rbtype      *args;
  314.     int         rc;
  315.  
  316.     args = ads_getargs();
  317.     ads_retnil();
  318.  
  319.     if (args == NULL || args->restype != RTSTR) {
  320.         ads_printf(/*MSG8*/"Requires name of AutoLISP symbol in \"s, and\n");
  321.         ads_printf(/*MSG9*/"value to be assigned to symbol.\n");
  322.     }
  323.  
  324.     rc = ads_putsym(args->resval.rstring, args->rbnext);
  325.     if (rc != RTNORM) {
  326.         switch(geterrno()) {
  327.         case OL_ESYMNAM:
  328.             ads_printf(/*MSG10*/"íuAutoLISP ▓┼╕╣ªW║┘ív╡L«─íC\n");
  329.             break;
  330.         case OL_ESYMVAL:
  331.             ads_printf(/*MSG11*/"íu▓┼╕╣¡╚ív╡L«─íC\n");
  332.             break;
  333.         }
  334.     } else {
  335.         if (args->rbnext != NULL) {
  336.             if (args->rbnext->rbnext == NULL)
  337.                 ads_retval(args->rbnext);
  338.             else
  339.                 ads_retlist(args->rbnext);
  340.         }
  341.     }
  342.     return RTNORM;
  343. }
  344.  
  345.