home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume14 / xtoolplaces / part01 / addon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-26  |  4.7 KB  |  173 lines

  1. /*Copyright (c) 1991 Xerox Corporation.  All Rights Reserved.
  2.  
  3.   Permission to use,  copy,  modify  and  distribute  without
  4.   charge this software, documentation, images, etc. is grant-
  5.   ed, provided that this copyright and the author's  name  is
  6.   retained.
  7.  
  8.   A fee may be charged for this program ONLY to recover costs
  9.   for distribution (i.e. media costs).  No profit can be made
  10.   on this program.
  11.  
  12.   The author assumes no responsibility for disasters (natural
  13.   or otherwise) as a consequence of use of this software.
  14.  
  15.   Adam Stein (stein.wbst129@xerox.com)
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <X11/Xos.h>
  21. #include "addon.h"
  22.  
  23. ADDON *head = NULL;
  24. extern char *program,*addon;
  25. extern FILE *fp;
  26.  
  27. /*This function will read in a file containing strings to put onto the
  28.   command line.  This is used when WM_COMMAND isn't complete.  In the case
  29.   of xrolo, the data file is never listed in WM_COMMAND and must be put
  30.   added.  I don't know if this is an XVIEW bug or not.  I haven't seen it
  31.   with any other program (yet).
  32.  
  33.   Inputs:  none
  34.   Outputs: none
  35.   Locals:  fp      - file pointer to read from
  36.        line    - line of text read in
  37.   Globals: addon   - name of file containing commands to add on
  38.        program - name of currently executing program
  39.        stderr  - standard error
  40.        NULL    - 0
  41. */
  42. read_addon()
  43. {
  44.     register char line[200];
  45.     register FILE *fp;
  46.  
  47.     /*Open the file for reading*/
  48.     if((fp = fopen(addon,"r")) == NULL) {
  49.       fprintf(stderr,"%s: can't open {%s}\n",program,addon);
  50.       exit(1);
  51.     }
  52.  
  53.     /*Read lines from file until there is no more*/
  54.     while(fgets(line,200,fp) != NULL)
  55.       switch(line[0]) {
  56.         case '#':            /*Comment character - ignore line*/
  57.         case '\n':            /*Blank line - ignore line*/
  58.             break;
  59.         default:
  60.             line[strlen(line)-1] = '\0';  /*Take out NL char*/
  61.             insert(line);
  62.  
  63.             break;
  64.       }
  65. }
  66.  
  67. /*This function will insert commands to be added into a linked list.
  68.  
  69.   Inputs:  line    - line containing program name and commands to be added
  70.   Outputs: none
  71.   Locals:  next    - next available structure entry to fill in
  72.        pointer - point to : to separater program from commands and to
  73.              skip over white space to point to commands
  74.   Globals: addon   - name of file containing commands to add on
  75.        head    - pointer to head of linked list
  76.        program - name of currently executing program
  77.        stderr  - standard error
  78.        ADDON   - structure holding program and command line info
  79.        NULL    - 0
  80. */
  81. insert(line)
  82. register char *line;
  83. {
  84.     register char *pointer;
  85.     register ADDON *next;
  86.     char *calloc(),*strdup();
  87.  
  88.     /*Find colon to make it a NULL thereby making line equal to just
  89.       the program name.  Syntax error if the colon isn't found*/
  90.     if((pointer = index(line,':')) == NULL) {
  91.       fprintf(stderr,"%s: syntax error in {%s}\n",program,addon);
  92.       exit(1);
  93.     }
  94.  
  95.     /*Insert new entry*/
  96.     if(!head) {
  97.       if((head = (ADDON *) calloc(1,sizeof(ADDON))) == NULL) {
  98.         perror(program);
  99.         exit(1);
  100.       }
  101.  
  102.       next = head;
  103.     } else {
  104.          for(next = head;next->next;next = next->next)
  105.            ;
  106.  
  107.          if((next->next = (ADDON *) calloc(1,sizeof(ADDON))) == NULL) { 
  108.            perror(program);
  109.            exit(1);
  110.          }
  111.  
  112.          next = next->next;
  113.            }
  114.  
  115.     /*Make colon NULL so that line equals just the program name*/
  116.     *pointer = '\0';
  117.  
  118.     if((next->program = strdup(line)) == NULL) {
  119.       perror(program);
  120.       exit(1);
  121.     }
  122.  
  123.     /*Put colon back and skip past whitespace to get to first character
  124.       in command line*/
  125.     *(pointer) = ':';
  126.     while(isspace(*(++pointer))) ;
  127.  
  128.     if((next->cmmd_line = strdup(pointer)) == NULL) {
  129.       perror(program);
  130.       exit(1);
  131.     }
  132.  
  133.     next->next = (ADDON *) NULL;
  134. }
  135.  
  136. /*This function will find the corresponding addon commands to the program
  137.   being written out and at the commands to the command line being written
  138.   out.
  139.  
  140.   Inputs:  name - name of program being written out
  141.   Outputs: none
  142.   Locals:  list    - pointer to go through linked list
  143.        pointer - pointer to make name equal to just the first word in string
  144.   Globals: fp      - file pointer to write window information to
  145.        head    - pointer to head of linked list
  146.        NULL    - 0
  147. */
  148. addto(name)
  149. register char *name;
  150. {
  151.     register char *pointer;
  152.     register ADDON *list;
  153.  
  154.     /*If there is whitespace in the name string then put in a NULL to
  155.       make name equal to just the first word in the string, otherwise
  156.       assume the string is only made up of one word*/
  157.     if((pointer = index(name,' ')) != NULL)
  158.       *pointer = '\0';
  159.  
  160.     /*Loop through linked list to match the program information saved with
  161.       the name of the program being written out.  Write out saved addon
  162.       command line when we find a match*/
  163.     for(list = head;list;list = list->next)
  164.       if(!strcmp(name,list->program)) {
  165.         fprintf(fp,"%s ",list->cmmd_line);
  166.         break;
  167.       }
  168.  
  169.     /*Put whitespace back to put the string back to normal*/
  170.     if(pointer) *pointer = ' ';
  171. }
  172.  
  173.