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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/* NVTV pipe -- Dirk Thierbach <dthierbach@gmx.de>
*
* This file is part of nvtv, a tool for tv-output on NVidia cards.
*
* nvtv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* nvtv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* $Id: pipe.c,v 1.12 2003/10/10 16:16:57 dthierbach Exp $
*
* Contents:
*
* Routines to access the named pipe for server/client communication
*
*/
#include "local.h" /* before everything else */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "pipe.h"
/*
* The communication will always include sizes and number of elements
* sent, so even if there are version mismatches, the other end will be
* able to properly synchronize.
* A message consists of
* - the command
* - the number of arguments
* - the size of each argument, followed by the argument itself
*/
/*
* Read a command from a pipe
*/
PipeCmd pipeReadCmd (FILE *pipe)
{
PipeCmd cmd;
RAISE (MSG_DEBUG, "pipe read cmd");
fread (&cmd, sizeof(PipeCmd), 1, pipe);
RAISE (MSG_DEBUG, "pipe read cmd, got %i", cmd);
return cmd;
}
/*
* Write a command to a pipe
*/
void pipeWriteCmd (FILE *pipe, PipeCmd cmd)
{
RAISE (MSG_DEBUG, "pipe write cmd %i", cmd);
fwrite (&cmd, sizeof(PipeCmd), 1, pipe);
fflush (pipe);
RAISE (MSG_DEBUG, "pipe write cmd done");
}
/*
* Write arguments to a pipe. Ellipsis paramaters are:
* size1, pointer1, size2, pointer2, ..., 0
* Ignore zero size of null pointer arguments.
*/
void pipeWriteArgs (FILE *pipe, int n, ...)
{
va_list ap;
int i, s;
void *p;
RAISE (MSG_DEBUG, "pipe write args");
fwrite (&n, sizeof(n), 1, pipe);
va_start(ap, n);
for (i = 0; i < n; i++) {
s = va_arg(ap, int);
p = va_arg(ap, void *);
if (!p) s = 0;
fwrite (&s, sizeof(s), 1, pipe);
if (s != 0) {
fwrite (p, s, 1, pipe);
}
}
fflush (pipe);
va_end(ap);
}
/*
* Implements the three following read routines. Allocate elements
* with zero size, and optionally set results.
*/
int pipeReadArgsMulti (FILE *pipe, int res, int n, va_list ap)
{
int i, j;
int m, s, t, r;
void *p;
void **q;
int ok;
r = 0;
fread (&m, sizeof(m), 1, pipe);
ok = (m == n);
for (i = 0; i < m; i++) {
fread (&t, sizeof(t), 1, pipe);
s = va_arg(ap, int);
p = va_arg(ap, void *);
if (ok) {
if (s != 0 && t != 0 && s != t) ok = 0;
}
if (ok) {
r++;
} else {
p = NULL;
}
if (t == 0) p = NULL;
if (s == 0 && p) { /* alloc zero size */
q = (void **) p;
p = malloc (t);
*q = p;
}
if (res) { /* store result pointer */
q = va_arg(ap, void **);
if (q) *q = p;
}
if (p) {
fread (p, t, 1, pipe);
} else {
for (j = 0; j < t; j++) fgetc (pipe);
}
}
return r;
}
/*
* Read arguments from a pipe. Ellipsis parameters are:
* size_1, pointer_1, size_2, pointer_2, ..., size_n, pointer_n
* Ignore null pointer arguments, and allocate arguments with zero size.
* Return number of initial arguments that matched input stream.
*/
int pipeReadArgs (FILE *pipe, int n, ...)
{
va_list ap;
int r;
RAISE (MSG_DEBUG, "pipe read args");
va_start(ap, n);
r = pipeReadArgsMulti (pipe, 0, n, ap);
va_end(ap);
return r;
}
/*
* Read optional arguments from a pipe. Ellipsis parameters are:
* size_1, pointer_1, result_1, ..., size_n, pointer_n, result_n
* Ignore zero size or null pointer data. Return number of initial
* arguments that matched input stream, and set each result either to
* NULL or to pointer.
*/
int pipeReadArgsOpt (FILE *pipe, int n, ...)
{
va_list ap;
int r;
RAISE (MSG_DEBUG, "pipe read args opt");
va_start(ap, n);
r = pipeReadArgsMulti (pipe, 1, n, ap);
va_end(ap);
return r;
}
/*
* Read list from a pipe, alloc elements, and return pointer to first
* element. The elements MUST contain the next field as first entry,
* and a string as second entry.
*/
void* pipeReadList (FILE *pipe, int size)
{
void *root, *p, *q;
int i, j, n, t;
char *s;
RAISE (MSG_DEBUG, "pipe read list");
root = p = NULL;
fread (&n, sizeof(n), 1, pipe);
n /= 2;
for (i = 0; i < n; i++) {
fread (&t, sizeof(t), 1, pipe);
if (t == size) {
q = malloc (t);
fread (q, t, 1, pipe);
if (p) {
* (void **) p = q;
} else {
root = q;
}
p = q;
} else {
q = NULL;
for (j = 0; j < t; j++) fgetc (pipe);
}
fread (&t, sizeof(t), 1, pipe);
if (q) {
s = NULL;
if (t != 0) {
s = malloc (t);
fread (s, sizeof(char), t, pipe);
}
* (((char **) q) + 1) = s;
} else {
for (j = 0; j < t; j++) fgetc (pipe);
}
}
return root;
}
/*
* Write list to a pipe. The elements MUST contain the next field as
* first entry, and a string as second entry.
*/
void pipeWriteList (FILE *pipe, int size, void *list)
{
void *l;
char *s;
int n, k;
RAISE (MSG_DEBUG, "pipe write list");
n = 0;
for (l = list; l; l = * (void **) l) n++;
n *= 2;
fwrite (&n, sizeof(n), 1, pipe);
for (l = list; l; l = * (void **) l) {
fwrite (&size, sizeof(size), 1, pipe);
fwrite (l, size, 1, pipe);
s = * (((char **) l) + 1);
if (s) {
k = strlen(s) + 1;
fwrite (&k, sizeof(k), 1, pipe);
fwrite (s, sizeof(char), k, pipe);
} else {
k = 0;
fwrite (&k, sizeof(k), 1, pipe);
}
}
fflush (pipe);
}
/*
* Read an array with 'nelem' elements. The elements themselves are
* written by their own pipeWrite commands to allow the transfer of
* embedded strings.
*/
void pipeReadArray (FILE *pipe, int *nelem)
{
int n;
RAISE (MSG_DEBUG, "pipe read array [");
fread (&n, sizeof(n), 1, pipe);
fread (nelem, sizeof(int), 1, pipe);
RAISE (MSG_DEBUG, "] %i (%i)", *nelem, n);
}
/*
* Write an array with 'nelem' elements, each of which has 'nsub' elements
* of size 'size' to the pipe. The elements themselves are written
* by their own pipeWrite commands to allow the transfer of embedded
* strings.
*/
void pipeWriteArray (FILE *pipe, int nelem, int nsub)
{
int n;
RAISE (MSG_DEBUG, "pipe write array %i * %i", nelem, nsub);
n = nelem * nsub + 1;
fwrite (&n, sizeof(n), 1, pipe);
fwrite (&nelem, sizeof(nelem), 1, pipe);
fflush (pipe);
}
|