fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Implement a character device abstraction

Hampa Hug ba62314e 0210d285

+1171 -16
+6
config.inc.in
··· 161 161 PCE_TERM_HDR += terminal/sdl.h 162 162 PCE_TERM_OBJ += $(pcedst)/terminal/sdl.o 163 163 endif 164 + 165 + PCE_CHAR_OBJ := $(pcedst)/drivers/char/char.o 166 + PCE_CHAR_OBJ += $(pcedst)/drivers/char/char-null.o 167 + PCE_CHAR_OBJ += $(pcedst)/drivers/char/char-stdio.o 168 + PCE_CHAR_HDR := drivers/char/char.h 169 + PCE_CHAR_LIB :=
+23 -15
configure.in
··· 868 868 fi 869 869 870 870 871 + char1=" null stdio" 872 + char2="" 873 + 874 + 871 875 option1="" 872 876 option2="" 873 877 ··· 900 904 src/devices/block/Makefile 901 905 src/devices/clock/Makefile 902 906 src/devices/video/Makefile 907 + src/drivers/Makefile 908 + src/drivers/char/Makefile 903 909 src/lib/Makefile 904 910 src/libini/Makefile 905 911 src/terminal/Makefile ··· 916 922 echo "" 917 923 echo "pce $PCE_VERSION_STR is now configured:" 918 924 919 - echo " CC: $CC $CFLAGS" 920 - echo " LD: $CC $LDFLAGS" 921 - echo "" 922 - echo " prefix: $prefix" 923 - echo "" 924 - echo " Emulators built: $emu1" 925 - echo " Emulators not built: $emu2" 926 - echo " CPU cores built: $core1" 927 - echo " CPU cores not built: $core2" 928 - echo " Terminals built: $term1" 929 - echo " Terminals not built: $term2" 930 - echo " Enabled options: $option1" 931 - echo " Disabled options: $option2" 925 + echo " CC: $CC $CFLAGS" 926 + echo " LD: $CC $LDFLAGS" 927 + echo 928 + echo " prefix: $prefix" 929 + echo 930 + echo " Emulators built: $emu1" 931 + echo " Emulators not built: $emu2" 932 + echo " CPU cores built: $core1" 933 + echo " CPU cores not built: $core2" 934 + echo " Terminals built: $term1" 935 + echo " Terminals not built: $term2" 936 + echo " Char drivers built: $char1" 937 + echo " Char drivers not built:$char2" 938 + echo " Enabled options: $option1" 939 + echo " Disabled options: $option2" 932 940 933 941 if test "x$PCE_HAVE_NASM" = "x0" ; then 934 - echo " Build PC BIOS: no" 942 + echo " Build PC BIOS: no" 935 943 else 936 - echo " Build PC BIOS: yes" 944 + echo " Build PC BIOS: yes" 937 945 fi
+1 -1
src/Makefile.in
··· 2 2 3 3 srcdir := @srcdir@ 4 4 topdir := @top_srcdir@ 5 - subdir := lib libini cpu chipset devices terminal arch utils 5 + subdir := lib libini cpu chipset devices drivers terminal arch utils 6 6 reldir := .. 7 7 8 8
+17
src/drivers/Makefile.in
··· 1 + # src/drivers/Makefile 2 + 3 + srcdir := @srcdir@ 4 + topdir := @top_srcdir@ 5 + subdir := char 6 + reldir := ../.. 7 + 8 + 9 + include $(reldir)/config.inc 10 + 11 + 12 + DCL := Makefile 13 + 14 + DIST := Makefile.in 15 + 16 + 17 + include $(reldir)/rules.inc
+39
src/drivers/char/Makefile.in
··· 1 + # src/drivers/char/Makefile 2 + 3 + srcdir := @srcdir@ 4 + topdir := @top_srcdir@ 5 + subdir := 6 + reldir := ../../.. 7 + 8 + 9 + include $(reldir)/config.inc 10 + 11 + 12 + ALLFILES := char char-null char-stdio 13 + 14 + FILES := char char-null char-stdio 15 + 16 + ALLSRC := $(foreach f,$(ALLFILES),$(f).c) 17 + ALLOBJ := $(foreach f,$(ALLFILES),$(f).o) 18 + ALLHDR := $(foreach f,$(ALLFILES),$(f).h) 19 + 20 + SRC := $(foreach f,$(FILES),$(f).c) 21 + OBJ := $(foreach f,$(FILES),$(f).o) 22 + HDR := $(foreach f,$(FILES),$(f).h) 23 + SDP := Makefile 24 + 25 + CLN := $(ALLOBJ) $(BIN) 26 + DCL := Makefile 27 + 28 + DIST := $(ALLSRC) $(ALLHDR) Makefile.in 29 + 30 + 31 + include $(reldir)/rules.inc 32 + 33 + 34 + all: $(BIN) $(OBJ) 35 + 36 + 37 + char.o: char.c char.h $(SDP) 38 + char-null.o: char-null.c char-null.h char.h $(SDP) 39 + char-stdio.o: char-stdio.c char-stdio.h char.h $(SDP)
+124
src/drivers/char/char-null.c
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/drivers/char/char-null.c * 7 + * Created: 2009-03-10 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 9 + *****************************************************************************/ 10 + 11 + /***************************************************************************** 12 + * This program is free software. You can redistribute it and / or modify it * 13 + * under the terms of the GNU General Public License version 2 as published * 14 + * by the Free Software Foundation. * 15 + * * 16 + * This program is distributed in the hope that it will be useful, but * 17 + * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 + * Public License for more details. * 20 + *****************************************************************************/ 21 + 22 + 23 + #include <stdlib.h> 24 + #include <stdio.h> 25 + #include <string.h> 26 + 27 + #include <drivers/char/char.h> 28 + #include <drivers/char/char-null.h> 29 + 30 + 31 + static 32 + void chr_null_close (char_drv_t *cdrv) 33 + { 34 + char_null_t *drv; 35 + 36 + drv = cdrv->ext; 37 + 38 + free (drv); 39 + } 40 + 41 + static 42 + unsigned chr_null_read (char_drv_t *cdrv, void *buf, unsigned cnt) 43 + { 44 + char_null_t *drv; 45 + 46 + drv = cdrv->ext; 47 + 48 + return (0); 49 + } 50 + 51 + static 52 + unsigned chr_null_write (char_drv_t *cdrv, const void *buf, unsigned cnt) 53 + { 54 + char_null_t *drv; 55 + 56 + drv = cdrv->ext; 57 + 58 + return (cnt); 59 + } 60 + 61 + static 62 + int chr_null_get_ctl (char_drv_t *cdrv, unsigned *ctl) 63 + { 64 + char_null_t *drv; 65 + 66 + drv = cdrv->ext; 67 + 68 + *ctl = PCE_CHAR_DSR | PCE_CHAR_CTS | PCE_CHAR_CD; 69 + 70 + return (0); 71 + } 72 + 73 + static 74 + int chr_null_set_ctl (char_drv_t *cdrv, unsigned ctl) 75 + { 76 + char_null_t *drv; 77 + 78 + drv = cdrv->ext; 79 + 80 + return (0); 81 + } 82 + 83 + static 84 + int chr_null_set_params (char_drv_t *cdrv, unsigned long bps, unsigned bpc, unsigned parity, unsigned stop) 85 + { 86 + char_null_t *drv; 87 + 88 + drv = cdrv->ext; 89 + 90 + return (0); 91 + } 92 + 93 + static 94 + int chr_null_init (char_null_t *drv, const char *name) 95 + { 96 + chr_init (&drv->cdrv, drv); 97 + 98 + drv->cdrv.close = chr_null_close; 99 + drv->cdrv.read = chr_null_read; 100 + drv->cdrv.write = chr_null_write; 101 + drv->cdrv.get_ctl = chr_null_get_ctl; 102 + drv->cdrv.set_ctl = chr_null_set_ctl; 103 + drv->cdrv.set_params = chr_null_set_params; 104 + 105 + return (0); 106 + } 107 + 108 + char_drv_t *chr_null_open (const char *name) 109 + { 110 + char_null_t *drv; 111 + 112 + drv = malloc (sizeof (char_null_t)); 113 + 114 + if (drv == NULL) { 115 + return (NULL); 116 + } 117 + 118 + if (chr_null_init (drv, name)) { 119 + free (drv); 120 + return (NULL); 121 + } 122 + 123 + return (&drv->cdrv); 124 + }
+35
src/drivers/char/char-null.h
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/drivers/char/char-null.h * 7 + * Created: 2009-03-10 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 9 + *****************************************************************************/ 10 + 11 + /***************************************************************************** 12 + * This program is free software. You can redistribute it and / or modify it * 13 + * under the terms of the GNU General Public License version 2 as published * 14 + * by the Free Software Foundation. * 15 + * * 16 + * This program is distributed in the hope that it will be useful, but * 17 + * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 + * Public License for more details. * 20 + *****************************************************************************/ 21 + 22 + 23 + #ifndef PCE_DRIVERS_CHAR_NULL_H 24 + #define PCE_DRIVERS_CHAR_NULL_H 1 25 + 26 + 27 + #include <drivers/char/char.h> 28 + 29 + 30 + typedef struct char_null_t { 31 + char_drv_t cdrv; 32 + } char_null_t; 33 + 34 + 35 + #endif
+127
src/drivers/char/char-stdio.c
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/drivers/char/char-stdio.c * 7 + * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 9 + *****************************************************************************/ 10 + 11 + /***************************************************************************** 12 + * This program is free software. You can redistribute it and / or modify it * 13 + * under the terms of the GNU General Public License version 2 as published * 14 + * by the Free Software Foundation. * 15 + * * 16 + * This program is distributed in the hope that it will be useful, but * 17 + * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 + * Public License for more details. * 20 + *****************************************************************************/ 21 + 22 + 23 + #include <stdlib.h> 24 + #include <stdio.h> 25 + #include <string.h> 26 + 27 + #include <drivers/char/char.h> 28 + #include <drivers/char/char-stdio.h> 29 + 30 + 31 + static 32 + void chr_stdio_close (char_drv_t *cdrv) 33 + { 34 + char_stdio_t *drv; 35 + 36 + drv = cdrv->ext; 37 + 38 + if (drv->fname != NULL) { 39 + free (drv->fname); 40 + } 41 + 42 + if (drv->fp != NULL) { 43 + if ((drv->fp != stdin) && (drv->fp != stdout) && (drv->fp != stderr)) { 44 + fclose (drv->fp); 45 + } 46 + } 47 + 48 + free (drv); 49 + } 50 + 51 + static 52 + unsigned chr_stdio_read (char_drv_t *cdrv, void *buf, unsigned cnt) 53 + { 54 + char_stdio_t *drv; 55 + 56 + drv = cdrv->ext; 57 + 58 + return (0); 59 + } 60 + 61 + static 62 + unsigned chr_stdio_write (char_drv_t *cdrv, const void *buf, unsigned cnt) 63 + { 64 + char_stdio_t *drv; 65 + 66 + drv = cdrv->ext; 67 + 68 + if (drv->fp == NULL) { 69 + return (cnt); 70 + } 71 + 72 + cnt = fwrite (buf, 1, cnt, drv->fp); 73 + 74 + if ((drv->fp == stdout) || (drv->fp == stderr)) { 75 + fflush (drv->fp); 76 + } 77 + 78 + return (cnt); 79 + } 80 + 81 + static 82 + int chr_stdio_init (char_stdio_t *drv, const char *name) 83 + { 84 + chr_init (&drv->cdrv, drv); 85 + 86 + drv->cdrv.close = chr_stdio_close; 87 + drv->cdrv.read = chr_stdio_read; 88 + drv->cdrv.write = chr_stdio_write; 89 + 90 + drv->fp = NULL; 91 + 92 + drv->fname = chr_get_option (name, "file", 1); 93 + 94 + if (drv->fname != NULL) { 95 + if (strcmp (drv->fname, "-") == 0) { 96 + drv->fp = stdout; 97 + } 98 + else { 99 + drv->fp = fopen (drv->fname, "wb"); 100 + 101 + if (drv->fp == NULL) { 102 + return (1); 103 + } 104 + } 105 + } 106 + 107 + return (0); 108 + } 109 + 110 + char_drv_t *chr_stdio_open (const char *name) 111 + { 112 + char_stdio_t *drv; 113 + 114 + drv = malloc (sizeof (char_stdio_t)); 115 + 116 + if (drv == NULL) { 117 + return (NULL); 118 + } 119 + 120 + if (chr_stdio_init (drv, name)) { 121 + chr_stdio_close (&drv->cdrv); 122 + 123 + return (NULL); 124 + } 125 + 126 + return (&drv->cdrv); 127 + }
+41
src/drivers/char/char-stdio.h
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/drivers/char/char-stdio.h * 7 + * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 9 + *****************************************************************************/ 10 + 11 + /***************************************************************************** 12 + * This program is free software. You can redistribute it and / or modify it * 13 + * under the terms of the GNU General Public License version 2 as published * 14 + * by the Free Software Foundation. * 15 + * * 16 + * This program is distributed in the hope that it will be useful, but * 17 + * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 + * Public License for more details. * 20 + *****************************************************************************/ 21 + 22 + 23 + #ifndef PCE_DRIVERS_CHAR_STDIO_H 24 + #define PCE_DRIVERS_CHAR_STDIO_H 1 25 + 26 + 27 + #include <stdio.h> 28 + 29 + #include <drivers/char/char.h> 30 + 31 + 32 + typedef struct char_stdio_t { 33 + char_drv_t cdrv; 34 + 35 + char *fname; 36 + 37 + FILE *fp; 38 + } char_stdio_t; 39 + 40 + 41 + #endif
+663
src/drivers/char/char.c
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/drivers/char/char.c * 7 + * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 9 + *****************************************************************************/ 10 + 11 + /***************************************************************************** 12 + * This program is free software. You can redistribute it and / or modify it * 13 + * under the terms of the GNU General Public License version 2 as published * 14 + * by the Free Software Foundation. * 15 + * * 16 + * This program is distributed in the hope that it will be useful, but * 17 + * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 + * Public License for more details. * 20 + *****************************************************************************/ 21 + 22 + 23 + #include <config.h> 24 + 25 + #include <stdlib.h> 26 + #include <string.h> 27 + 28 + #include <drivers/char/char.h> 29 + 30 + 31 + struct chr_drv_list { 32 + const char *prefix; 33 + char_drv_t *(*open) (const char *name); 34 + }; 35 + 36 + struct chr_drv_list drvtab[] = { 37 + { "null", chr_null_open }, 38 + { "stdio", chr_stdio_open }, 39 + { NULL, NULL } 40 + }; 41 + 42 + 43 + static 44 + void chr_log_bytes (char_drv_t *cdrv, int out, const unsigned char *buf, unsigned cnt) 45 + { 46 + int c; 47 + unsigned i, n; 48 + 49 + while (cnt > 0) { 50 + n = (cnt < 16) ? cnt : 16; 51 + 52 + fprintf (cdrv->log_fp, "%s %02X", out ? "->" : "<-", *buf); 53 + 54 + for (i = 1; i < n; i++) { 55 + fprintf (cdrv->log_fp, " %02X", buf[i]); 56 + } 57 + 58 + for (i = n; i < 16; i++) { 59 + fputs (" ", cdrv->log_fp); 60 + } 61 + 62 + fputs (" ", cdrv->log_fp); 63 + 64 + for (i = 0; i < n; i++) { 65 + c = buf[i]; 66 + 67 + if ((c < 0x20) || (c > 0x7e)) { 68 + c = '.'; 69 + } 70 + 71 + fputc (c, cdrv->log_fp); 72 + } 73 + 74 + fputs ("\n", cdrv->log_fp); 75 + 76 + buf += n; 77 + cnt -= n; 78 + } 79 + 80 + fflush (cdrv->log_fp); 81 + } 82 + 83 + static 84 + void chr_log_flush (char_drv_t *cdrv) 85 + { 86 + if (cdrv->log_cnt > 0) { 87 + chr_log_bytes (cdrv, cdrv->log_out, cdrv->log_buf, cdrv->log_cnt); 88 + } 89 + 90 + cdrv->log_cnt = 0; 91 + cdrv->log_out = 0; 92 + } 93 + 94 + static 95 + void chr_log_data (char_drv_t *cdrv, int out, const unsigned char *buf, unsigned cnt) 96 + { 97 + unsigned n; 98 + 99 + if (cdrv->log_fp == NULL) { 100 + return; 101 + } 102 + 103 + if (cnt == 0) { 104 + return; 105 + } 106 + 107 + if ((cdrv->log_cnt > 0) && (cdrv->log_out != out)) { 108 + chr_log_flush (cdrv); 109 + } 110 + 111 + while (cnt > 0) { 112 + n = 16 - cdrv->log_cnt; 113 + 114 + if (cnt < n) { 115 + n = cnt; 116 + } 117 + 118 + memcpy (cdrv->log_buf + cdrv->log_cnt, buf, n); 119 + 120 + cdrv->log_cnt += n; 121 + cdrv->log_out = out; 122 + 123 + if (cdrv->log_cnt >= 16) { 124 + chr_log_flush (cdrv); 125 + } 126 + 127 + buf += n; 128 + cnt -= n; 129 + } 130 + } 131 + 132 + static 133 + void chr_log_params (char_drv_t *cdrv) 134 + { 135 + const char *par; 136 + 137 + if (cdrv->log_fp == NULL) { 138 + return; 139 + } 140 + 141 + chr_log_flush (cdrv); 142 + 143 + switch (cdrv->parity) { 144 + case 0: 145 + par = "N"; 146 + break; 147 + 148 + case 1: 149 + par = "O"; 150 + break; 151 + 152 + case 2: 153 + par = "E"; 154 + break; 155 + 156 + default: 157 + par = "?"; 158 + break; 159 + } 160 + 161 + fprintf (cdrv->log_fp, "-- %lu %u%s%u\n", 162 + cdrv->bps, cdrv->bpc, par, cdrv->stop 163 + ); 164 + 165 + fflush (cdrv->log_fp); 166 + } 167 + 168 + static 169 + void chr_log_signal (char_drv_t *cdrv, const char *name, unsigned msk, unsigned old, unsigned new) 170 + { 171 + if ((old ^ new) & msk) { 172 + fprintf (cdrv->log_fp, "-- %s=%d\n", name, (new & msk) != 0); 173 + } 174 + } 175 + 176 + static 177 + void chr_log_ctl (char_drv_t *cdrv, unsigned old, unsigned new) 178 + { 179 + if ((cdrv->log_fp == NULL) || (old == new)) { 180 + return; 181 + } 182 + 183 + chr_log_flush (cdrv); 184 + 185 + chr_log_signal (cdrv, "CTS", PCE_CHAR_CTS, old, new); 186 + chr_log_signal (cdrv, "DTR", PCE_CHAR_DTR, old, new); 187 + chr_log_signal (cdrv, "RTS", PCE_CHAR_RTS, old, new); 188 + chr_log_signal (cdrv, "DSR", PCE_CHAR_DSR, old, new); 189 + chr_log_signal (cdrv, "CD", PCE_CHAR_CD, old, new); 190 + chr_log_signal (cdrv, "RI", PCE_CHAR_RI, old, new); 191 + } 192 + 193 + int chr_set_log (char_drv_t *cdrv, const char *fname) 194 + { 195 + if (cdrv->log_fp != NULL) { 196 + chr_log_flush (cdrv); 197 + fclose (cdrv->log_fp); 198 + } 199 + 200 + cdrv->log_fp = fopen (fname, "w"); 201 + 202 + if (cdrv->log_fp == NULL) { 203 + return (1); 204 + } 205 + 206 + return (0); 207 + } 208 + 209 + 210 + static 211 + const char *chr_skip_option (const char *str) 212 + { 213 + while (*str != 0) { 214 + if (*str == ':') { 215 + if (*(str + 1) == ':') { 216 + str += 2; 217 + } 218 + else { 219 + return (str + 1); 220 + } 221 + } 222 + else { 223 + str += 1; 224 + } 225 + } 226 + 227 + return (NULL); 228 + } 229 + 230 + static 231 + const char *chr_skip_space (const char *str) 232 + { 233 + while (1) { 234 + if (*str == ' ') { 235 + str += 1; 236 + } 237 + else if (*str == '\t') { 238 + str += 1; 239 + } 240 + else { 241 + return (str); 242 + } 243 + } 244 + } 245 + 246 + static 247 + const char *chr_get_option_value (const char *str, const char *name) 248 + { 249 + while (*name != 0) { 250 + if (*str != *name) { 251 + return (NULL); 252 + } 253 + 254 + str += 1; 255 + name += 1; 256 + } 257 + 258 + if (*str == '=') { 259 + str = chr_skip_space (str + 1); 260 + 261 + return (str); 262 + } 263 + 264 + return (NULL); 265 + } 266 + 267 + static 268 + const char *chr_get_option_name (const char *str) 269 + { 270 + unsigned i; 271 + 272 + str = chr_skip_space (str); 273 + 274 + i = 0; 275 + 276 + while (str[i] != 0) { 277 + if (str[i] == '=') { 278 + return (str); 279 + } 280 + else if (str[i] == ':') { 281 + if (str[i + 1] == ':') { 282 + i += 2; 283 + } 284 + else { 285 + return (NULL); 286 + } 287 + } 288 + else { 289 + i += 1; 290 + } 291 + } 292 + 293 + return (NULL); 294 + } 295 + 296 + static 297 + char *chr_option_dup (const char *str) 298 + { 299 + unsigned i; 300 + char *ret; 301 + 302 + i = 0; 303 + while (str[i] != 0) { 304 + if (str[i] == ':') { 305 + if (str[i + 1] == ':') { 306 + i += 2; 307 + } 308 + else { 309 + break; 310 + } 311 + } 312 + else { 313 + i += 1; 314 + } 315 + } 316 + 317 + while (i > 0) { 318 + if (str[i - 1] == ' ') { 319 + i -= 1; 320 + } 321 + else if (str[i - 1] == '\t') { 322 + i -= 1; 323 + } 324 + else { 325 + break; 326 + } 327 + } 328 + 329 + ret = malloc (i + 1); 330 + if (ret == NULL) { 331 + return (NULL); 332 + } 333 + 334 + memcpy (ret, str, i); 335 + ret[i] = 0; 336 + 337 + return (ret); 338 + } 339 + 340 + char *chr_get_named_option (const char *str, const char *name) 341 + { 342 + const char *val; 343 + 344 + while (str != NULL) { 345 + val = chr_get_option_value (str, name); 346 + 347 + if (val != NULL) { 348 + return (chr_option_dup (val)); 349 + } 350 + 351 + str = chr_skip_option (str); 352 + } 353 + 354 + return (NULL); 355 + } 356 + 357 + char *chr_get_indexed_option (const char *str, unsigned idx) 358 + { 359 + while (str != NULL) { 360 + if (chr_get_option_name (str) == NULL) { 361 + if (idx == 0) { 362 + str = chr_skip_space (str); 363 + 364 + return (chr_option_dup (str)); 365 + } 366 + 367 + idx -= 1; 368 + } 369 + 370 + str = chr_skip_option (str); 371 + } 372 + 373 + return (NULL); 374 + } 375 + 376 + char *chr_get_option (const char *str, const char *name, unsigned idx) 377 + { 378 + char *ret; 379 + 380 + if (name != NULL) { 381 + ret = chr_get_named_option (str, name); 382 + 383 + if (ret != NULL) { 384 + return (ret); 385 + } 386 + } 387 + 388 + if (idx != 0xffff) { 389 + ret = chr_get_indexed_option (str, idx); 390 + 391 + if (ret != NULL) { 392 + return (ret); 393 + } 394 + } 395 + 396 + return (NULL); 397 + } 398 + 399 + int chr_get_option_bool (const char *str, const char *name, unsigned idx, int def) 400 + { 401 + int r; 402 + char *s; 403 + 404 + s = chr_get_option (str, name, idx); 405 + 406 + if (s == NULL) { 407 + return (def); 408 + } 409 + 410 + r = def; 411 + 412 + if (strcmp (s, "1") == 0) { 413 + r = 1; 414 + } 415 + else if (strcmp (s, "true") == 0) { 416 + r = 1; 417 + } 418 + else if (strcmp (s, "yes") == 0) { 419 + r = 1; 420 + } 421 + else if (strcmp (s, "0") == 0) { 422 + r = 0; 423 + } 424 + else if (strcmp (s, "false") == 0) { 425 + r = 0; 426 + } 427 + else if (strcmp (s, "no") == 0) { 428 + r = 0; 429 + } 430 + 431 + free (s); 432 + 433 + return (r); 434 + } 435 + 436 + unsigned long chr_get_option_uint (const char *str, const char *name, unsigned idx, unsigned long def) 437 + { 438 + unsigned long val; 439 + char *end; 440 + char *s; 441 + 442 + s = chr_get_option (str, name, idx); 443 + 444 + if (s == NULL) { 445 + return (def); 446 + } 447 + 448 + val = strtoul (s, &end, 0); 449 + 450 + if ((end != NULL) && (*end != 0)) { 451 + free (s); 452 + return (def); 453 + } 454 + 455 + free (s); 456 + 457 + return (val); 458 + } 459 + 460 + void chr_init (char_drv_t *cdrv, void *ext) 461 + { 462 + cdrv->ext = ext; 463 + 464 + cdrv->bps = 0; 465 + cdrv->bpc = 0; 466 + cdrv->parity = 0; 467 + cdrv->stop = 0; 468 + 469 + cdrv->ctl_inp = 0; 470 + cdrv->ctl_out = 0; 471 + 472 + cdrv->log_cnt = 0; 473 + cdrv->log_out = 0; 474 + cdrv->log_fp = NULL; 475 + 476 + cdrv->close = NULL; 477 + 478 + cdrv->read = NULL; 479 + cdrv->write = NULL; 480 + 481 + cdrv->get_ctl = NULL; 482 + cdrv->set_ctl = NULL; 483 + 484 + cdrv->set_params = NULL; 485 + } 486 + 487 + void chr_close (char_drv_t *cdrv) 488 + { 489 + if (cdrv == NULL) { 490 + return; 491 + } 492 + 493 + if (cdrv->log_fp != NULL) { 494 + chr_log_flush (cdrv); 495 + fclose (cdrv->log_fp); 496 + } 497 + 498 + if (cdrv->close == NULL) { 499 + return; 500 + } 501 + 502 + cdrv->close (cdrv); 503 + } 504 + 505 + unsigned chr_read (char_drv_t *cdrv, void *buf, unsigned cnt) 506 + { 507 + unsigned ret; 508 + 509 + if ((cdrv == NULL) || (cdrv->read == NULL)) { 510 + return (0); 511 + } 512 + 513 + ret = cdrv->read (cdrv, buf, cnt); 514 + 515 + chr_log_data (cdrv, 0, buf, ret); 516 + 517 + return (ret); 518 + } 519 + 520 + unsigned chr_write (char_drv_t *cdrv, const void *buf, unsigned cnt) 521 + { 522 + unsigned ret; 523 + 524 + if ((cdrv == NULL) || (cdrv->write == NULL)) { 525 + return (cnt); 526 + } 527 + 528 + ret = cdrv->write (cdrv, buf, cnt); 529 + 530 + chr_log_data (cdrv, 1, buf, ret); 531 + 532 + return (ret); 533 + } 534 + 535 + int chr_get_ctl (char_drv_t *cdrv, unsigned *ctl) 536 + { 537 + if ((cdrv == NULL) || (cdrv->get_ctl == NULL)) { 538 + return (1); 539 + } 540 + 541 + if (cdrv->get_ctl (cdrv, ctl)) { 542 + return (1); 543 + } 544 + 545 + chr_log_ctl (cdrv, cdrv->ctl_inp, *ctl); 546 + 547 + cdrv->ctl_inp = *ctl; 548 + 549 + return (0); 550 + } 551 + 552 + int chr_set_ctl (char_drv_t *cdrv, unsigned ctl) 553 + { 554 + if (cdrv == NULL) { 555 + return (1); 556 + } 557 + 558 + if (cdrv->ctl_out == ctl) { 559 + return (0); 560 + } 561 + 562 + chr_log_ctl (cdrv, cdrv->ctl_out, ctl); 563 + 564 + cdrv->ctl_out = ctl; 565 + 566 + if (cdrv->set_ctl == NULL) { 567 + return (1); 568 + } 569 + 570 + return (cdrv->set_ctl (cdrv, ctl)); 571 + } 572 + 573 + static 574 + int chr_check_params (char_drv_t *cdrv, unsigned long bps, unsigned bpc, unsigned parity, unsigned stop) 575 + { 576 + if (cdrv->bps != bps) { 577 + return (0); 578 + } 579 + 580 + if (cdrv->bpc != bpc) { 581 + return (0); 582 + } 583 + 584 + if (cdrv->parity != parity) { 585 + return (0); 586 + } 587 + 588 + if (cdrv->stop != stop) { 589 + return (0); 590 + } 591 + 592 + return (1); 593 + } 594 + 595 + int chr_set_params (char_drv_t *cdrv, unsigned long bps, unsigned bpc, unsigned parity, unsigned stop) 596 + { 597 + if (cdrv == NULL) { 598 + return (1); 599 + } 600 + 601 + if (chr_check_params (cdrv, bps, bpc, parity, stop)) { 602 + return (0); 603 + } 604 + 605 + cdrv->bps = bps; 606 + cdrv->bpc = bpc; 607 + cdrv->parity = parity; 608 + cdrv->stop = stop; 609 + 610 + chr_log_params (cdrv); 611 + 612 + if (cdrv->set_params == NULL) { 613 + return (1); 614 + } 615 + 616 + return (cdrv->set_params (cdrv, bps, bpc, parity, stop)); 617 + } 618 + 619 + static 620 + char_drv_t *chr_open_cdrv (char_drv_t *cdrv, const char *name) 621 + { 622 + char *str; 623 + 624 + if (cdrv == NULL) { 625 + return (NULL); 626 + } 627 + 628 + str = chr_get_option (name, "log", 0xffff); 629 + 630 + if (str != NULL) { 631 + chr_set_log (cdrv, str); 632 + 633 + free (str); 634 + } 635 + 636 + return (cdrv); 637 + } 638 + 639 + char_drv_t *chr_open (const char *name) 640 + { 641 + unsigned i; 642 + const char *s, *d; 643 + 644 + i = 0; 645 + 646 + while (drvtab[i].prefix != NULL) { 647 + s = name; 648 + d = drvtab[i].prefix; 649 + 650 + while ((*d != 0) && (*d == *s)) { 651 + d += 1; 652 + s += 1; 653 + } 654 + 655 + if ((*d == 0) && ((*s == ':') || (*s == 0))) { 656 + return (chr_open_cdrv (drvtab[i].open (name), name)); 657 + } 658 + 659 + i += 1; 660 + } 661 + 662 + return (NULL); 663 + }
+95
src/drivers/char/char.h
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/drivers/char/char.h * 7 + * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 9 + *****************************************************************************/ 10 + 11 + /***************************************************************************** 12 + * This program is free software. You can redistribute it and / or modify it * 13 + * under the terms of the GNU General Public License version 2 as published * 14 + * by the Free Software Foundation. * 15 + * * 16 + * This program is distributed in the hope that it will be useful, but * 17 + * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 + * Public License for more details. * 20 + *****************************************************************************/ 21 + 22 + 23 + #ifndef PCE_DRIVERS_CHAR_CHAR_H 24 + #define PCE_DRIVERS_CHAR_CHAR_H 1 25 + 26 + 27 + #include <stdio.h> 28 + 29 + 30 + #define PCE_CHAR_DTR 0x0001 31 + #define PCE_CHAR_RTS 0x0002 32 + 33 + #define PCE_CHAR_DSR 0x0100 34 + #define PCE_CHAR_CTS 0x0200 35 + #define PCE_CHAR_CD 0x0400 36 + #define PCE_CHAR_RI 0x0800 37 + 38 + 39 + /*!*************************************************************************** 40 + * @short The character driver context 41 + *****************************************************************************/ 42 + typedef struct char_drv_t { 43 + void *ext; 44 + 45 + unsigned long bps; 46 + unsigned bpc; 47 + unsigned parity; 48 + unsigned stop; 49 + 50 + unsigned ctl_inp; 51 + unsigned ctl_out; 52 + 53 + unsigned log_cnt; 54 + unsigned char log_buf[16]; 55 + int log_out; 56 + FILE *log_fp; 57 + 58 + void (*close) (struct char_drv_t *cdrv); 59 + 60 + unsigned (*read) (struct char_drv_t *cdrv, void *buf, unsigned cnt); 61 + unsigned (*write) (struct char_drv_t *cdrv, const void *buf, unsigned cnt); 62 + 63 + int (*get_ctl) (struct char_drv_t *cdrv, unsigned *ctl); 64 + int (*set_ctl) (struct char_drv_t *cdrv, unsigned ctl); 65 + 66 + int (*set_params) (struct char_drv_t *cdrv, 67 + unsigned long bps, unsigned bpc, unsigned parity, unsigned stop 68 + ); 69 + } char_drv_t; 70 + 71 + 72 + void chr_init (char_drv_t *cdrv, void *ext); 73 + 74 + void chr_close (char_drv_t *cdrv); 75 + 76 + unsigned chr_read (char_drv_t *cdrv, void *buf, unsigned cnt); 77 + unsigned chr_write (char_drv_t *cdrv, const void *buf, unsigned cnt); 78 + 79 + int chr_get_ctl (char_drv_t *cdrv, unsigned *ctl); 80 + int chr_set_ctl (char_drv_t *cdrv, unsigned ctl); 81 + 82 + int chr_set_params (char_drv_t *cdrv, unsigned long bps, unsigned bpc, unsigned parity, unsigned stop); 83 + 84 + int chr_set_log (char_drv_t *cdrv, const char *fname); 85 + 86 + char *chr_get_option (const char *str, const char *name, unsigned idx); 87 + int chr_get_option_bool (const char *str, const char *name, unsigned idx, int def); 88 + unsigned long chr_get_option_uint (const char *str, const char *name, unsigned idx, unsigned long def); 89 + 90 + char_drv_t *chr_open (const char *name); 91 + char_drv_t *chr_null_open (const char *name); 92 + char_drv_t *chr_stdio_open (const char *name); 93 + 94 + 95 + #endif