summaryrefslogtreecommitdiff
path: root/app/fvwm/libs/Module.c
blob: aefe0555191d4d273374af20ef41749a18084e0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
** Module.c: code for modules to communicate with fvwm
*/
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include <errno.h>

#include "fvwmlib.h"
#include "../fvwm/module.h"


/************************************************************************
 *
 * Reads a single packet of info from fvwm. Prototype is:
 * unsigned long header[HEADER_SIZE];
 * unsigned long *body;
 * int fd[2];
 * void DeadPipe(int nonsense);  * Called if the pipe is no longer open  *
 *
 * ReadFvwmPacket(fd[1],header, &body);
 *
 * Returns:
 *   > 0 everything is OK.
 *   = 0 invalid packet.
 *   < 0 pipe is dead. (Should never occur)
 *   body is a malloc'ed space which needs to be freed
 *
 **************************************************************************/
int ReadFvwmPacket(int fd, unsigned long *header, unsigned long **body)
{
  int count,total,count2,body_length;
  char *cbody;
  extern RETSIGTYPE DeadPipe(int);

  errno = 0;
  if((count = read(fd,header,HEADER_SIZE*sizeof(unsigned long))) >0)
  {
    if(header[0] == START_FLAG)
    {
      body_length = header[2]-HEADER_SIZE;
      *body = (unsigned long *)
        safemalloc(body_length * sizeof(unsigned long));
      cbody = (char *)(*body);
      total = 0;
      while(total < body_length*sizeof(unsigned long))
      {
        errno = 0;
        if((count2=
            read(fd,&cbody[total],
                 body_length*sizeof(unsigned long)-total)) >0)
        {
          total += count2;
        }
        else if(count2 < 0)
        {
          DeadPipe(errno);
        }
      }
    }
    else
      count = 0;
  }
  if(count <= 0)
    DeadPipe(errno);
  return count;
}

/************************************************************************
 *
 * SendText - Sends arbitrary text/command back to fvwm
 *
 ***********************************************************************/
void SendText(int *fd,char *message,unsigned long window)
{
  int w;

  if(message != NULL)
  {
    write(fd[0],&window, sizeof(unsigned long));

    w=strlen(message);
    write(fd[0],&w,sizeof(int));
    if (w)
      write(fd[0],message,w);

    /* keep going */
    w = 1;
    write(fd[0],&w,sizeof(int));
  }
}

/***************************************************************************
 *
 * Sets the which-message-types-do-I-want mask for modules
 *
 **************************************************************************/
void SetMessageMask(int *fd, unsigned long mask)
{
  char set_mask_mesg[50];

  snprintf(set_mask_mesg, sizeof(set_mask_mesg), "SET_MASK %lu\n",mask);
  SendText(fd,set_mask_mesg,0);
}

/***************************************************************************
 * Gets a module configuration line from fvwm. Returns NULL if there are
 * no more lines to be had. "line" is a pointer to a char *.
 *
 * Changed 10/19/98 by Dan Espen:
 *
 * - The "isspace"  call was referring to  memory  beyond the end of  the
 * input area.  This could have led to the creation of a core file. Added
 * "body_size" to keep it in bounds.
 **************************************************************************/
void GetConfigLine(int *fd, char **tline)
{
  static int first_pass = 1;
  int count,done = 0;
  int body_size;
  static char *line = NULL;
  unsigned long header[HEADER_SIZE];

  if(line != NULL)
    free(line);

  if(first_pass)
  {
    SendInfo(fd,"Send_ConfigInfo",0);
    first_pass = 0;
  }

  while(!done)
  {
    count = ReadFvwmPacket(fd[1],header,(unsigned long **)&line);
    /* DB(("Packet count is %d", count)); */
    if (count <= 0)
      *tline = NULL;
    else {
      *tline = &line[3*sizeof(long)];
      body_size = header[2]-HEADER_SIZE;
      /* DB(("Config line (%d): `%s'", body_size, body_size ? *tline : "")); */
      while((body_size > 0)
            && isspace(**tline)) {
        (*tline)++;
        --body_size;
      }
    }

/*   fprintf(stderr,"%x %x\n",header[1],M_END_CONFIG_INFO);*/
    if(header[1] == M_CONFIG_INFO)
      done = 1;
    else if(header[1] == M_END_CONFIG_INFO)
    {
      done = 1;
      if(line != NULL)
        free(line);
      line = NULL;
      *tline = NULL;
    }
  }
}