home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / comms / x_sun_psio / code / psfilt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-30  |  6.1 KB  |  219 lines

  1. /* (C) Tim Graves 20th April 1994
  2.    This code is supplied AS IS. no warrantee either expressed or implied 
  3.    is provided. This code may be freeley modified and modified as long as my
  4.    origional authorship is acknowledged. 
  5.    
  6.    Tim Graves
  7.     Sun Microsystems
  8.      
  9.      */
  10. /* this file contains the filter subsystem, it is used to automaticaly 
  11. perform filter operations on programs
  12. NOTE filters are set throughout the psion program and are not affected by 
  13. calling run 
  14. NOTE it is nessecary that the pscmd files will leave the output in the 
  15. same file as the input */
  16. #include <stdio.h>
  17. #include "psion.h"
  18. #include "psfilt.h"
  19. #include <ctype.h>
  20. #include <string.h>
  21. extern int debugcall ;
  22. static char vsn[]="@(#) psfilt.c 3.2@(#)" ;
  23. /* the structures to hold the filter entries */
  24. struct filterstruct * filters;
  25. initfilters()
  26. {
  27.     if (debugcall >= FILTCALLDEBUG)
  28.         fprintf(stderr, "CALL: initfilters()\n") ;
  29.     filters == NULL ;
  30. }
  31.  
  32. setupfilter (filttype, suffix, pscmd)
  33. int filttype ;
  34. char * suffix, *pscmd ;
  35. {
  36.     struct filterstruct * current;
  37.     if (debugcall >= FILTCALLDEBUG)
  38.         fprintf(stderr, "CALL: setupfilter (filttype = %d, suffix = %s, pscmd = %s)\n",filttype, suffix, pscmd) ;
  39.     current = filters ;
  40.     /* remove the existing filter (if there is one) */
  41.     removefilter(filttype, suffix) ;
  42.     /* add the new filter */
  43.     return (newfilter(filttype, suffix, pscmd)) ;
  44. }
  45. removefilter (filttype, suffix)
  46. int filttype ;
  47. char * suffix ;
  48. {
  49.     struct filterstruct * current, *prev;
  50.     if (debugcall >= FILTCALLDEBUG)
  51.         fprintf(stderr, "CALL: removefilter (filttype = %d, suffix = %s, )\n",filttype, suffix) ;
  52.     if (filters == NULL) 
  53.         return(OK) ;
  54.     current = filters ;
  55.     /* check if the filter is the first in the list */
  56.     if ((filters->direction == filttype) && (strcmp(filters->suffix, suffix) == 0))
  57.     {
  58.         /* make sure we have a start pointer */
  59.         filters = filters->nextfilter ;
  60.         /* free the data */
  61.         free(current->suffix) ;
  62.         free(current->pscmd) ;
  63.         free(current) ;
  64.         return(OK) ;
  65.     }
  66.     prev = filters ;
  67.     while (current != NULL)
  68.     {
  69.         if ((current->direction == filttype) && (strcmp(current->suffix, suffix) == 0))
  70.         {
  71.             /* remove from the list */
  72.             prev->nextfilter = current->nextfilter ;
  73.             /* free the data */
  74.             free(current->suffix) ;
  75.             free(current->pscmd) ;
  76.             free (current);
  77.             return (OK);
  78.         }
  79.         /* next item on the list */
  80.         prev = current ;
  81.         current = current->nextfilter ;
  82.     }
  83.     return(OK) ;
  84. }
  85.     
  86. /* install a filter */
  87. installfilter (filttype, suffix, pscmd, current)
  88. int filttype;
  89. struct filterstruct *current ;
  90. char * suffix, *pscmd ;
  91. {
  92.     if (debugcall >= FILTCALLDEBUG)
  93.         fprintf(stderr, "CALL: installfilter (filttype = %d, suffix = %s, pscmd = %s, current = (OMITED))\n",filttype, suffix, pscmd) ;
  94.     /* lets do the setup */
  95.     /* the direction */
  96.     current->direction = filttype ;
  97.     /* the suffix, if there is a . remove it */
  98.     if (suffix[0] == '.')
  99.         suffix ++ ;
  100.     /* allocate the space for the strings */
  101.     current->suffix = (char * ) calloc(1, strlen(suffix) + 1) ;
  102.     current->pscmd = (char *) calloc(1, strlen(pscmd) + 1) ;
  103.     strcpy(current->suffix, suffix) ;
  104.     /* and the pscmd file */
  105.     strcpy(current->pscmd , pscmd) ;
  106.     return (OK) ;
  107. }
  108.  
  109. /* newfilter, create a filter add it to the list then fill it */
  110. newfilter(filttype, suffix, pscmd)
  111. int filttype;
  112. char * suffix, *pscmd ;
  113. {
  114.     struct filterstruct *new ;
  115.     if (debugcall >= FILTCALLDEBUG)
  116.         fprintf(stderr, "CALL: newfilter (filttype = %d, suffix = %s, pscmd = %s, )\n",filttype, suffix, pscmd) ;
  117.     new = (struct filterstruct *) calloc (1, sizeof (struct filterstruct)) ;
  118.     /* add it to the head of the list */
  119.     new->nextfilter = filters ;
  120.     filters = new ;
  121.     /* create the filter */
  122.     return (installfilter(filttype, suffix, pscmd, filters));
  123. }
  124. char * filtertype (direction)
  125. int direction ;
  126. {
  127.     if (direction == OUTFILTER)
  128.         return("Output, ") ;
  129.     else if (direction == INFILTER)
  130.         return("Input,  ") ;
  131.     else
  132.         return("Restore,") ;
  133. }
  134. /* print each item in the filter list */
  135. printfilters()
  136. {
  137.     struct filterstruct *current ;
  138.     if (debugcall >= FILTCALLDEBUG)
  139.         fprintf(stderr, "CALL: printfilters ()\n") ;
  140.     /* initialise to the head of the list */
  141.     current = filters ;
  142.     if (filters == NULL) 
  143.     {
  144.         printf("No filters currently installed\n") ;
  145.         return(OK) ;
  146.     }
  147.     printf("Current installed filters\n") ;
  148.     /* run through the list */
  149.     while (current != NULL)
  150.     {
  151.         printf("Direction %s Suffix %s, Pscmd %s\n", filtertype(current->direction) , current->suffix, current->pscmd) ;
  152.         /* next item on the list */
  153.         current = current->nextfilter ;
  154.     }
  155. }
  156.  
  157.  
  158. /* locate the filter and return a pointer to the pscmd string 
  159. return NULL is there is no filter */
  160. char * locatefilter(direction, suffix)
  161. int direction ;
  162. char * suffix ;
  163. {
  164.     struct filterstruct * current;
  165.     if (debugcall >= FILTCALLDEBUG)
  166.         fprintf(stderr, "CALL: locatefilter (direction = %d, suffix = %s)\n",direction, suffix) ;
  167.     current = filters ;
  168.     /* lets doit */
  169.     while (current != NULL)
  170.     {
  171.         if ((current->direction == direction) && (strcmp(current->suffix, suffix) == 0))
  172.         {
  173.             return (current->pscmd) ;
  174.         }
  175.         current = current->nextfilter ;
  176.     }
  177.     /* we've got here and not found a filter return NULL*/
  178.     return (NULL) ;
  179. }
  180. filefilt(direction,fname)
  181. int direction ;
  182. char * fname ;
  183. {
  184.     char tmp[1000] ;
  185.     char * suffix ;
  186.     char * pscmd ;
  187.     if (debugcall >= FILTCALLDEBUG)
  188.         fprintf(stderr, "CALL: filefilt (direction = %d, fname = %s )\n",direction, fname) ;
  189.     /* locate the . in the suffix, if it does not exist just return
  190.        unless the helpfull variable is set inwhich case print out a 
  191.        suitable massage before returning */
  192.     if ((suffix = strchr(fname,'.')) == NULL)
  193.     {
  194.         if (findvar(VAR_HELPFULL) == NULL)
  195.             return(OK) ;
  196.         else
  197.         {
  198.             printf("HELP: You have initiated a transfer of (%s) for which there\n", fname) ;
  199.             printf("is no suffix, no filter will be run\n") ;
  200.             return(OK) ;
  201.         }
  202.     }
  203.     /* suffix will point to the . so increment one */
  204.     suffix ++ ;
  205.     /* locate the filter to call */
  206.     if ((pscmd = locatefilter(direction, suffix)) == NULL)
  207.         return(OK) ;
  208.     /* build the filter command */
  209.     strcpy(tmp, "run ") ;
  210.     strcat(tmp, pscmd) ;
  211.     strcat(tmp," ") ;
  212.     strcat(tmp,fname) ;
  213.     /* be talkative is we want */
  214.     if (findvar(VAR_VERBOSE) != NULL)
  215.         printf("Calling %s filter (%s)\n", filtertype(direction),tmp) ;
  216.     /* call the command interpreter to do the work */
  217.     cmdintr(tmp) ;
  218. }
  219.