···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: src/drivers/char/char-null.c *
77+ * Created: 2009-03-10 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#include <stdlib.h>
2424+#include <stdio.h>
2525+#include <string.h>
2626+2727+#include <drivers/char/char.h>
2828+#include <drivers/char/char-null.h>
2929+3030+3131+static
3232+void chr_null_close (char_drv_t *cdrv)
3333+{
3434+ char_null_t *drv;
3535+3636+ drv = cdrv->ext;
3737+3838+ free (drv);
3939+}
4040+4141+static
4242+unsigned chr_null_read (char_drv_t *cdrv, void *buf, unsigned cnt)
4343+{
4444+ char_null_t *drv;
4545+4646+ drv = cdrv->ext;
4747+4848+ return (0);
4949+}
5050+5151+static
5252+unsigned chr_null_write (char_drv_t *cdrv, const void *buf, unsigned cnt)
5353+{
5454+ char_null_t *drv;
5555+5656+ drv = cdrv->ext;
5757+5858+ return (cnt);
5959+}
6060+6161+static
6262+int chr_null_get_ctl (char_drv_t *cdrv, unsigned *ctl)
6363+{
6464+ char_null_t *drv;
6565+6666+ drv = cdrv->ext;
6767+6868+ *ctl = PCE_CHAR_DSR | PCE_CHAR_CTS | PCE_CHAR_CD;
6969+7070+ return (0);
7171+}
7272+7373+static
7474+int chr_null_set_ctl (char_drv_t *cdrv, unsigned ctl)
7575+{
7676+ char_null_t *drv;
7777+7878+ drv = cdrv->ext;
7979+8080+ return (0);
8181+}
8282+8383+static
8484+int chr_null_set_params (char_drv_t *cdrv, unsigned long bps, unsigned bpc, unsigned parity, unsigned stop)
8585+{
8686+ char_null_t *drv;
8787+8888+ drv = cdrv->ext;
8989+9090+ return (0);
9191+}
9292+9393+static
9494+int chr_null_init (char_null_t *drv, const char *name)
9595+{
9696+ chr_init (&drv->cdrv, drv);
9797+9898+ drv->cdrv.close = chr_null_close;
9999+ drv->cdrv.read = chr_null_read;
100100+ drv->cdrv.write = chr_null_write;
101101+ drv->cdrv.get_ctl = chr_null_get_ctl;
102102+ drv->cdrv.set_ctl = chr_null_set_ctl;
103103+ drv->cdrv.set_params = chr_null_set_params;
104104+105105+ return (0);
106106+}
107107+108108+char_drv_t *chr_null_open (const char *name)
109109+{
110110+ char_null_t *drv;
111111+112112+ drv = malloc (sizeof (char_null_t));
113113+114114+ if (drv == NULL) {
115115+ return (NULL);
116116+ }
117117+118118+ if (chr_null_init (drv, name)) {
119119+ free (drv);
120120+ return (NULL);
121121+ }
122122+123123+ return (&drv->cdrv);
124124+}
+35
src/drivers/char/char-null.h
···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: src/drivers/char/char-null.h *
77+ * Created: 2009-03-10 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#ifndef PCE_DRIVERS_CHAR_NULL_H
2424+#define PCE_DRIVERS_CHAR_NULL_H 1
2525+2626+2727+#include <drivers/char/char.h>
2828+2929+3030+typedef struct char_null_t {
3131+ char_drv_t cdrv;
3232+} char_null_t;
3333+3434+3535+#endif
+127
src/drivers/char/char-stdio.c
···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: src/drivers/char/char-stdio.c *
77+ * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#include <stdlib.h>
2424+#include <stdio.h>
2525+#include <string.h>
2626+2727+#include <drivers/char/char.h>
2828+#include <drivers/char/char-stdio.h>
2929+3030+3131+static
3232+void chr_stdio_close (char_drv_t *cdrv)
3333+{
3434+ char_stdio_t *drv;
3535+3636+ drv = cdrv->ext;
3737+3838+ if (drv->fname != NULL) {
3939+ free (drv->fname);
4040+ }
4141+4242+ if (drv->fp != NULL) {
4343+ if ((drv->fp != stdin) && (drv->fp != stdout) && (drv->fp != stderr)) {
4444+ fclose (drv->fp);
4545+ }
4646+ }
4747+4848+ free (drv);
4949+}
5050+5151+static
5252+unsigned chr_stdio_read (char_drv_t *cdrv, void *buf, unsigned cnt)
5353+{
5454+ char_stdio_t *drv;
5555+5656+ drv = cdrv->ext;
5757+5858+ return (0);
5959+}
6060+6161+static
6262+unsigned chr_stdio_write (char_drv_t *cdrv, const void *buf, unsigned cnt)
6363+{
6464+ char_stdio_t *drv;
6565+6666+ drv = cdrv->ext;
6767+6868+ if (drv->fp == NULL) {
6969+ return (cnt);
7070+ }
7171+7272+ cnt = fwrite (buf, 1, cnt, drv->fp);
7373+7474+ if ((drv->fp == stdout) || (drv->fp == stderr)) {
7575+ fflush (drv->fp);
7676+ }
7777+7878+ return (cnt);
7979+}
8080+8181+static
8282+int chr_stdio_init (char_stdio_t *drv, const char *name)
8383+{
8484+ chr_init (&drv->cdrv, drv);
8585+8686+ drv->cdrv.close = chr_stdio_close;
8787+ drv->cdrv.read = chr_stdio_read;
8888+ drv->cdrv.write = chr_stdio_write;
8989+9090+ drv->fp = NULL;
9191+9292+ drv->fname = chr_get_option (name, "file", 1);
9393+9494+ if (drv->fname != NULL) {
9595+ if (strcmp (drv->fname, "-") == 0) {
9696+ drv->fp = stdout;
9797+ }
9898+ else {
9999+ drv->fp = fopen (drv->fname, "wb");
100100+101101+ if (drv->fp == NULL) {
102102+ return (1);
103103+ }
104104+ }
105105+ }
106106+107107+ return (0);
108108+}
109109+110110+char_drv_t *chr_stdio_open (const char *name)
111111+{
112112+ char_stdio_t *drv;
113113+114114+ drv = malloc (sizeof (char_stdio_t));
115115+116116+ if (drv == NULL) {
117117+ return (NULL);
118118+ }
119119+120120+ if (chr_stdio_init (drv, name)) {
121121+ chr_stdio_close (&drv->cdrv);
122122+123123+ return (NULL);
124124+ }
125125+126126+ return (&drv->cdrv);
127127+}
+41
src/drivers/char/char-stdio.h
···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: src/drivers/char/char-stdio.h *
77+ * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#ifndef PCE_DRIVERS_CHAR_STDIO_H
2424+#define PCE_DRIVERS_CHAR_STDIO_H 1
2525+2626+2727+#include <stdio.h>
2828+2929+#include <drivers/char/char.h>
3030+3131+3232+typedef struct char_stdio_t {
3333+ char_drv_t cdrv;
3434+3535+ char *fname;
3636+3737+ FILE *fp;
3838+} char_stdio_t;
3939+4040+4141+#endif