home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_03 / v7n3010a.txt < prev    next >
Text File  |  1989-01-16  |  14KB  |  529 lines

  1.   
  2. Listing 1:
  3.  
  4. int well_formed(), well_formed_2(), woof();    /* Declarations */
  5.  
  6. typedef struct element {
  7.    char *field;
  8.    int (*validate)();
  9.    } FORM_RULES;
  10.  
  11. FORM_RULES form[] ={
  12.    { "field1", well_formed},
  13.    { "field2", well_formed_2}
  14.    };
  15.  
  16. int well_formed()
  17.    {
  18.    return 0;
  19.    }
  20.  
  21. int well_formed_2()
  22.    {
  23.    return 0;
  24.    }
  25.  
  26. int woof()
  27.    {
  28.    return 0;
  29.    }
  30.  
  31.  
  32.  
  33.  
  34.  
  35. Listing 2:
  36.  
  37. main()
  38. /* Changes well_formed reference to woof */
  39.    {
  40.    int i;    
  41.    printf("\n Function addresses are %lx %lx %lx",
  42.        well_formed, well_formed_2, woof);
  43.  
  44.    printf("\n Member addresses are %lx %lx",
  45.        form[0].validate, form[1].validate);
  46.    
  47.    form[0].validate = woof;
  48.  
  49.    printf("\n Member addresses are %lx %lx",
  50.        form[0].validate, form[1].validate);
  51.  
  52.    }
  53.  
  54. Output:
  55.  
  56.  Function addresses are 3e220000 3e220009 3e220012
  57.  Member addresses are 3e220000 3e220009
  58.  Member addresses are 3e220012 3e220009
  59.  
  60.  
  61.  
  62. Listing 3:
  63.  
  64. struct element
  65.    {
  66.    char *field;
  67.    int validate_function_id;
  68.    };
  69.  
  70. #define F_WELL_FORMED 0
  71. #define F_WELL_FORMED_2 1
  72. #define F_WOOF 2
  73.  
  74. struct element form[] = {
  75.    { "field1", F_WELL_FORMED},
  76.    { "field2", F_WELL_FORMED_2},
  77.    };
  78.  
  79. int well_formed(), well_formed_2(), woof();    /* Declarations */
  80.  
  81. static int (*validate_function[])() =  
  82.                                 { well_formed, well_formed_2, woof};
  83. #define MAX_VALIDATE_FUNC  (sizeof(validate_function)/sizeof(int (*)()) )
  84.  
  85. main()
  86.    {
  87.    form[0].validate_function_id = F_WOOF;   
  88.    
  89.    /* Indirect call */
  90.    call_function(form[0].validate_function_id);
  91.  
  92.    /* Direct call -- if no checking was desired */    
  93.    (*validate_function[form[0].validate_function_id])();   
  94.    }
  95.  
  96. call_function(id)
  97. int id;
  98.     {
  99.    /* Check the function id and call it */
  100.    if (id >= 0 && id < MAX_VALIDATE_FUNC) 
  101.       (*validate_function[id])();
  102.    else
  103.        printf("\n Out of range validate function id", id);
  104.     }
  105.  
  106.  
  107.  
  108. Listing 4:
  109.  
  110. typedef int (* VALIDATER)();    /* Pointer to C function returning int */    
  111. #define VNOP ((VALIDATER) 0 )   /* Null function -- validate not required */
  112.  
  113. typedef struct field_element {
  114.    char **fptr;        /* Address of a field pointer */
  115.    VALIDATER fi;       /* Pointer to field function,
  116.                           if fi == NOP then no field is not touched */
  117.    } FORM_RULES;
  118.  
  119. /* Pointers to allocated fields */
  120.  
  121. char *Field_A, *Field_B;
  122.  
  123. /* Form definition */
  124.  
  125. FORM_RULES twx[] = {
  126.    { &Field_A, VNOP},
  127.    { &Field_B, VNOP},
  128.    };
  129.  
  130. main()
  131.    {
  132.    Field_A = "ABC";    /* Same as malloc, but with content */
  133.    Field_B = "DEF";    
  134.  
  135.    printf("\n Field A via pointer is %s double pointer %s",
  136.        Field_A, *(twx[0].fptr));
  137.    printf("\n Field B via pointer is %s double pointer %s",
  138.        Field_B, *(twx[1].fptr));
  139.    }
  140.  
  141.  
  142.  
  143.  
  144. Listing 5:
  145.  
  146. #include <stdio.h>
  147.  
  148. typedef int VALIDATE;
  149. typedef VALIDATE (* VALIDATER)();    /* Pointer to C function returning int */    
  150.  
  151. #define VNOP ((VALIDATER) 0 )   /* Null function -- validate not required */
  152.  
  153. typedef struct field_element {
  154.    char **fptr;        /* Address of a field pointer */
  155.    VALIDATER *fi;       /* Pointer to field function,
  156.                           if fi == NOP then no field is not touched */
  157.    } FORM_RULES;
  158.  
  159. /* Pointers to functions */
  160.  
  161. VALIDATER Function_A;
  162. VALIDATER Function_B;
  163.  
  164. /* Form definition */
  165.  
  166. FORM_RULES twx[] = {
  167.    { NULL, &Function_A},
  168.    { NULL, &Function_B},
  169.    };
  170.  
  171. VALIDATE function_one(), function_two();
  172.  
  173. main()
  174.    {
  175.    Function_A = function_one;
  176.    Function_B = function_two;
  177.    
  178.    /* Call the functions */
  179.    (**twx[0].fi)();
  180.    (**twx[1].fi)();   
  181.    }
  182.  
  183. VALIDATE function_one()
  184.     {
  185.     printf("\n Function one called");
  186.     return 0;
  187.     }
  188.  
  189. VALIDATE function_two()
  190.     {
  191.     printf("\n Function two called");
  192.     return 0;
  193.     }
  194.  
  195.  
  196. Listing 6:
  197.  
  198. typedef int VALIDATE;
  199. #define VNOP ((VALIDATE *) 0 )   /* Null function -- validate not required */
  200.  
  201. struct field_element {
  202.    char **fptr;        /* Address of a field pointer */
  203.    VALIDATE **fi;      /* Pointer to pointer to field function */
  204.    }; 
  205.  
  206. /* Pointers to functions */
  207.  
  208. VALIDATE *Function_A;
  209. VALIDATE *Function_B;
  210.  
  211. struct field_element twx[] = {
  212.    { NULL, &Function_A},
  213.    { NULL, &Function_B},
  214.    };
  215.    
  216.  
  217.  
  218.  
  219.     func_a()
  220.        {
  221.        printf("\n Func_a was called--need the real one for this \n");
  222.        return ;
  223.        }
  224.  
  225.  
  226.  
  227.  
  228.  
  229. SCREEN.C Listing 
  230.  
  231. /*
  232. *       SCREEN.C
  233. *  Modification of IVCLOCK using intervention code
  234. *
  235. *  SCREEN goes to the scheduler via timer every 37 clock tics
  236. *  The timer clock ticks every 55 msec. x 37 == once per 2 seconds
  237. *  OR every keyboard entry.
  238. *
  239. *  The command line format is as follows:
  240. *
  241. *       screen
  242. *
  243. *       screen -r
  244. *                  removes the system from resident status
  245. *
  246. *  A Change had to be made in the Interrupt Handler for INT 9
  247. *    to allow the system to respond on every key stroke
  248. *
  249. *  CTL-F1  puts the system to sleep (without removing it)
  250. *  CTL-F2  wakes it up again.
  251. *
  252. *                                       David Tal -8/XI/88
  253. */
  254.  
  255.  
  256. #include <dos.h>
  257. #include <stdio.h>
  258. #include <binterv.h>
  259. #include <bintrupt.h>
  260. #include <bkeys.h>
  261. #include <bscreens.h>
  262.  
  263.                 /* scr is the function which will be called        */
  264.                 /* approximately once per 2 sec. or at a keystroke */
  265. extern void scr (IV_EVENT *);
  266.  
  267. #define STKSIZE  2000
  268. #define OK       0
  269. #define FALL_OUT 101
  270.  
  271. #define NOT_FOUND         1
  272. #define ERROR_DISABLING   2
  273. #define ERROR_REMOVING    3
  274. #define ALREADY_INSTALLED 4
  275.  
  276.              /* Definitive signature for this version of sched.*/
  277. #define ol_sign "screen 08/11/88"
  278.  
  279.                 /* Allocation of stack space for the intervention   */
  280.                 /* scheduler and user function.                     */
  281. char schedstk [STKSIZE];
  282.  
  283.                 /* This is the data structure to pass to the        */
  284.                 /* scheduler so that it will give control every     */
  285.                 /* 37 clock ticks.                                     */
  286. IV_TIME timetab = {37, IV_TM_INTERVAL};
  287.  
  288. IV_KEY keytab[3] = {
  289.                       KB_C_C_F3,KB_S_C_F3,IV_KY_SERVICE,
  290.                       KB_C_C_F1,KB_S_C_F1,IV_KY_SLEEP,
  291.                       KB_C_C_F2,KB_S_C_F2,IV_KY_WAKE
  292.                    };
  293.                 /* Internal functions -- install & remove ISR.      */
  294. int install_iv (void);
  295. int remove_iv  (void);
  296.  
  297.  
  298.  /* here is the main program   */
  299.  
  300. int main (argc, argv)
  301. int   argc;
  302. char *argv [];
  303. {
  304.     if (argc == 1)
  305.         exit (install_iv ());
  306.  
  307.     if ((argc == 2)                                      &&
  308.         (((argv [1][0] == '-') || (argv [1][0] == '/')) &&
  309.          ((argv [1][1] == 'r') || (argv [1][1] == 'R'))))
  310.         exit (remove_iv ());
  311.     printf ("usage: screen [-r]\n");
  312.     exit (0);
  313. }
  314.  
  315. /**
  316. *
  317. * Name          INSTALL_IV -- Install interrupt vectors for IVREADAD and
  318. *                             go TSR(terminate and stay resident).
  319. *
  320. * Synopsis      ret = install_iv ();
  321. *
  322. *               int ret         Return code from IVINSTAL if there was
  323. *                               a problem with installation of the
  324. *                               intervention code routine.
  325. *
  326. * Description   This function installs screen if another copy
  327. *               is not already installed.
  328. *
  329. * Returns       ret     ALREADY_INSTALLED (4)
  330. *                           A copy of screen is already installed.
  331. *                       FALL_OUT (101)
  332. *                           ISRESEXT() failed.
  333. *                       IV_INSTALLED (5)
  334. *                           A copy of screen is already installed.
  335. *
  336. **/
  337.  
  338. #include <butil.h>
  339.  
  340. int install_iv ()
  341.  
  342. {
  343.     int          ercode;
  344.     IV_VECTORS   vecs;
  345.  
  346.                 /* Check to see if IVREADAD already installed.      */
  347.     ivvecs (IV_RETVEC, &vecs);
  348.     if (ivsense (&vecs, ol_sign) != FARNIL)
  349.     {
  350.         puts ("SCREEN already installed.");
  351.         return (ALREADY_INSTALLED);
  352.     }
  353.                 /* Install the interrupt service routine--i.e. tell */
  354.                 /* the scheduler about our sched routine.          */
  355.  
  356.  
  357. /* note: this routine ivinstam is an altered version of ivinstal, in which
  358.          all keyboard interrupts are accepted   */
  359.  
  360.  
  361.     if (0 !=( ercode =
  362.          ivinstam (scr, ol_sign, schedstk, STKSIZE,
  363.                    &keytab, 3 ,&timetab,
  364.                    sizeof(timetab) / sizeof(IV_TIME),
  365.                 /* screen uses DOS support.     */
  366.                    IV_DOS_NEED)))
  367.         {       /* Error!                                           */
  368.             printf ("Install error %d.\n", ercode);
  369.             return (ercode);
  370.         }
  371.                 /* Terminate and stay resident.                     */
  372.  
  373.    puts("      ******************************");
  374.    puts("      *                            *");
  375.    puts("      * EGA Screen Saver Installed *");
  376.    puts("      *                            *");
  377.    puts("      *  autoerase every 3 minutes *");
  378.    puts("      ******************************");
  379.  
  380.    isresext (OK);
  381.  
  382.                 /* Should never get here.                           */
  383.     return (FALL_OUT);
  384. }
  385.  
  386. /**
  387. *
  388. * Name          REMOVE_IV -- Remove a previously-installed copy of screen
  389. *
  390. * Synopsis      ret = remove_iv ();
  391. *
  392. *               int ret         Return code.
  393. *                                 NO_ERROR  (0)-
  394. *                                     No error encountered.
  395. *                                 NOT_FOUND (1)-
  396. *                                     pgm not found.
  397. *                                 ERROR_DISABLING (2)-
  398. *                                     pgm  could not be disabled.
  399. *                                     This error should *never* be
  400. *                                     seen.
  401. *                                 ERROR_REMOVING (3)--
  402. *                                     pgm could not be removed
  403. *                                     (most likely overwritten MALLOC
  404. *                                     pointers).
  405. *
  406. * Description   This function removes a currently-active copy of screen
  407. *               from memory, cleaning up interrupt vectors and freeing
  408. *               memory in the process.
  409. *
  410. * Returns       ret (nonzero if error--see above).
  411. *
  412. **/
  413.  
  414. int remove_iv ()
  415. {
  416.     IV_VECTORS   vecs;
  417.     IV_CTRL far *pivctrl;
  418.  
  419.     ivvecs (IV_RETVEC, &vecs);
  420.     if ((pivctrl = ivsense (&vecs, ol_sign)) == FARNIL)
  421.     {
  422.         puts ("SCREEN not found.");
  423.         return (NOT_FOUND);
  424.     }
  425.  
  426.     if (ivdisabl (pivctrl))
  427.     {
  428.         puts ("Error disabling SCREEN");
  429.         return (ERROR_DISABLING);
  430.     }
  431.  
  432.     if (isremove (pivctrl->psp))
  433.     {
  434.         puts ("Error removing SCREEN");
  435.         return (ERROR_REMOVING);
  436.     }
  437.     puts ("SCREEN successfully removed");
  438.     return (0);
  439. }
  440.  
  441.  
  442. /*                     Pointer to intervention event
  443. *                                       structure.  The structure
  444. *                                       contains the current time of
  445. *                                       day (in timer ticks since
  446. *                                       midnight) as well as other
  447. *                                       data.
  448. *
  449. * Description   This function accepts control from the scheduler
  450. *               every  2 sec
  451. *
  452. *
  453. * Returns       None.
  454. *
  455. **/
  456. #include <binterv.h>
  457. #include <time.h>
  458. #include <bscreens.h>
  459. #define TRUE 1
  460. #define FALSE 0
  461.  
  462. void scr (pevent)
  463.                    /* scr is the part of the program that does something */
  464. IV_EVENT *pevent;
  465.  
  466. {
  467.  
  468.      static long oldtime = {0};
  469.      long sec; /* the current time (in seconds since xx/yy/zz) */
  470.      static char scoff = {FALSE}; /*  flag indicating that the scr. is OFF*/
  471.      static int pmode,pc,actpag;
  472.      static int setpage;
  473.      int seconds_to_wait;
  474.  
  475.      seconds_to_wait = 180;   /* set the number of seconds before the
  476.                                  screen resets here */
  477.      if (pevent->key.action  == IV_KY_SLEEP)
  478.             printf("IV_KY_SLEEP\n");
  479.  
  480.      if (pevent->key.action  == IV_KY_WAKE)
  481.             printf("IV_KY_WAKE\n");
  482.  
  483.      if (pevent->key.action  == IV_KY_NONE)
  484.  
  485.         /* no key pressed  */
  486.         {
  487.         if(oldtime == 0) oldtime = time(&sec); /* initialization of oldtime */
  488.         if(( time(&sec) - oldtime > seconds_to_wait) && (scoff == FALSE))
  489.                  {
  490. /*  TIMED OUT! reset the screen */
  491.  
  492.                   scmode(&pmode,&pc,&actpag); /* get mode and active page */
  493.                   if (pmode ==3)setpage = 3-actpag;
  494.                        else setpage = 1-actpag;  /* Basically handles only
  495.                           text and graphics mode for EGA.  Assume one page
  496.                           is blank.  This works for me 99+ % of the time */
  497.  
  498.                   scapage(setpage);  /* shut off the screen  */
  499.                   scpgcur(1,0,0,CUR_ADJUST); /* shut off cursor */
  500. /* NOTE: for some reason, the cursor does NOT go off when  in Text mode.
  501.       I do not completely understand why not.   */
  502.  
  503.                   scoff = TRUE; /* set software flag indicating condition */
  504.                  }
  505.  
  506.          }
  507.         /* was a key pressed? */
  508.  
  509. /*  NOTE: the Interrupt handler IVCNTRL was changed in the IVKEYBD section
  510.           so that all key strokes except CTL-F1 and CTL-F2 would bear
  511.           the IV_KY_SERVICE flag. New version is called IVCNTRLX.       */
  512.  
  513. if (pevent->key.action  == IV_KY_SERVICE)
  514.         {
  515.           oldtime = time(&sec);   /* 1st, adjust the clock */
  516.            if( scoff == TRUE) /* is the screen turned off?  */
  517.            /* turn the screen back on */
  518.                 {
  519.                 scoff = FALSE;
  520.                 scapage(actpag); /* restore page */
  521.                 scpgcur(0,12,13,CUR_ADJUST); /* turn the cursor back on */
  522.                 }
  523.         }
  524. }
  525.  
  526.  
  527.  
  528.  
  529.