This is Unofficial EPICS BASE Doxygen Site
devStdio.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2008 UChicago Argonne LLC, as Operator of Argonne
3 * National Laboratory.
4 * EPICS BASE is distributed subject to a Software License Agreement found
5 * in file LICENSE that is included with this distribution.
6 \*************************************************************************/
7 
8 #include <stdio.h>
9 #include <string.h>
10 
11 #include "dbCommon.h"
12 #include "devSup.h"
13 #include "errlog.h"
14 #include "recGbl.h"
15 #include "recSup.h"
16 
17 #include "lsoRecord.h"
18 #include "printfRecord.h"
19 #include "stringoutRecord.h"
20 #include "epicsExport.h"
21 
22 typedef int (*PRINTFFUNC)(const char *fmt, ...);
23 
24 static int stderrPrintf(const char *fmt, ...);
25 static int logPrintf(const char *fmt, ...);
26 
27 
28 static struct outStream {
29  const char *name;
30  PRINTFFUNC print;
31 } outStreams[] = {
32  {"stdout", printf},
33  {"stderr", stderrPrintf},
34  {"errlog", logPrintf},
35  {NULL, NULL}
36 };
37 
38 static int stderrPrintf(const char *fmt, ...) {
39  va_list pvar;
40  int retval;
41 
42  va_start(pvar, fmt);
43  retval = vfprintf(stderr, fmt, pvar);
44  va_end (pvar);
45 
46  return retval;
47 }
48 
49 static int logPrintf(const char *fmt, ...) {
50  va_list pvar;
51  int retval;
52 
53  va_start(pvar, fmt);
54  retval = errlogVprintf(fmt, pvar);
55  va_end (pvar);
56 
57  return retval;
58 }
59 
60 
61 /* lso device support */
62 
63 static long add_lso(dbCommon *pcommon) {
64  lsoRecord *prec = (lsoRecord *) pcommon;
65  struct outStream *pstream;
66 
67  if (prec->out.type != INST_IO)
68  return S_dev_badOutType;
69 
70  for (pstream = outStreams; pstream->name; ++pstream) {
71  if (strcmp(prec->out.value.instio.string, pstream->name) == 0) {
72  prec->dpvt = pstream;
73  return 0;
74  }
75  }
76  prec->dpvt = NULL;
77  return -1;
78 }
79 
80 static long del_lso(dbCommon *pcommon) {
81  lsoRecord *prec = (lsoRecord *) pcommon;
82 
83  prec->dpvt = NULL;
84  return 0;
85 }
86 
87 static struct dsxt dsxtLsoStdio = {
88  add_lso, del_lso
89 };
90 
91 static long init_lso(int pass)
92 {
93  if (pass == 0) devExtend(&dsxtLsoStdio);
94  return 0;
95 }
96 
97 static long write_lso(lsoRecord *prec)
98 {
99  struct outStream *pstream = (struct outStream *)prec->dpvt;
100  if (pstream)
101  pstream->print("%s\n", prec->val);
102  return 0;
103 }
104 
105 lsodset devLsoStdio = {
106  { 5, NULL, init_lso, NULL, NULL }, write_lso
107 };
108 epicsExportAddress(dset, devLsoStdio);
109 
110 
111 /* printf device support */
112 
113 static long add_printf(dbCommon *pcommon) {
114  printfRecord *prec = (printfRecord *) pcommon;
115  struct outStream *pstream;
116 
117  if (prec->out.type != INST_IO)
118  return S_dev_badOutType;
119 
120  for (pstream = outStreams; pstream->name; ++pstream) {
121  if (strcmp(prec->out.value.instio.string, pstream->name) == 0) {
122  prec->dpvt = pstream;
123  return 0;
124  }
125  }
126  prec->dpvt = NULL;
127  return -1;
128 }
129 
130 static long del_printf(dbCommon *pcommon) {
131  printfRecord *prec = (printfRecord *) pcommon;
132 
133  prec->dpvt = NULL;
134  return 0;
135 }
136 
137 static struct dsxt dsxtPrintfStdio = {
138  add_printf, del_printf
139 };
140 
141 static long init_printf(int pass)
142 {
143  if (pass == 0) devExtend(&dsxtPrintfStdio);
144  return 0;
145 }
146 
147 static long write_printf(printfRecord *prec)
148 {
149  struct outStream *pstream = (struct outStream *)prec->dpvt;
150  if (pstream)
151  pstream->print("%s\n", prec->val);
152  return 0;
153 }
154 
155 printfdset devPrintfStdio = {
156  {5, NULL, init_printf, NULL, NULL }, write_printf
157 };
158 epicsExportAddress(dset, devPrintfStdio);
159 
160 
161 /* stringout device support */
162 
163 static long add_stringout(dbCommon *pcommon) {
164  stringoutRecord *prec = (stringoutRecord *) pcommon;
165  struct outStream *pstream;
166 
167  if (prec->out.type != INST_IO)
168  return S_dev_badOutType;
169 
170  for (pstream = outStreams; pstream->name; ++pstream) {
171  if (strcmp(prec->out.value.instio.string, pstream->name) == 0) {
172  prec->dpvt = pstream;
173  return 0;
174  }
175  }
176  prec->dpvt = NULL;
177  return -1;
178 }
179 
180 static long del_stringout(dbCommon *pcommon) {
181  stringoutRecord *prec = (stringoutRecord *) pcommon;
182 
183  prec->dpvt = NULL;
184  return 0;
185 }
186 
187 static struct dsxt dsxtSoStdio = {
188  add_stringout, del_stringout
189 };
190 
191 static long init_stringout(int pass)
192 {
193  if (pass == 0) devExtend(&dsxtSoStdio);
194  return 0;
195 }
196 
197 static long write_stringout(stringoutRecord *prec)
198 {
199  struct outStream *pstream = (struct outStream *)prec->dpvt;
200  if (pstream)
201  pstream->print("%s\n", prec->val);
202  return 0;
203 }
204 
205 stringoutdset devSoStdio = {
206  {5, NULL, init_stringout, NULL, NULL},
207  write_stringout
208 };
209 epicsExportAddress(dset, devSoStdio);
Definition: devSup.h:117
lsodset devLsoStdio
Definition: devStdio.c:105
stringoutdset devSoStdio
Definition: devStdio.c:205
void devExtend(dsxt *pdsxt)
Definition: dbStaticRun.c:60
int errlogVprintf(const char *pFormat, va_list pvar)
Definition: errlog.c:144
#define printf
Definition: epicsStdio.h:41
#define NULL
Definition: catime.c:38
epicsExportAddress(dset, devLsoStdio)
Device support routines.
Definition: devSup.h:140
#define S_dev_badOutType
Definition: devSup.h:172
int(* PRINTFFUNC)(const char *fmt,...)
Definition: devStdio.c:22
printfdset devPrintfStdio
Definition: devStdio.c:155
if(yy_init)
Definition: scan.c:972
#define stderr
Definition: epicsStdio.h:32
int prec
Definition: reader.c:29
Exporting IOC objects.