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.

drivers: Factor out driver option parsing

The option parsing code is factored out into
src/drviers/opitons.c.

This patch also removes support for indexed options.

Hampa Hug 568ef994 a1f49cfe

+333 -298
+18 -6
Makefile.dep
··· 1862 1862 1863 1863 src/drivers/char/char-posix.o: src/drivers/char/char-posix.c \ 1864 1864 src/drivers/char/char-posix.h \ 1865 - src/drivers/char/char.h 1865 + src/drivers/char/char.h \ 1866 + src/drivers/options.h 1866 1867 1867 1868 src/drivers/char/char-ppp.o: src/drivers/char/char-ppp.c \ 1868 1869 src/config.h \ 1869 1870 src/drivers/char/char-ppp.h \ 1870 1871 src/drivers/char/char.h \ 1872 + src/drivers/options.h \ 1871 1873 src/lib/tun.h 1872 1874 1873 1875 src/drivers/char/char-pty.o: src/drivers/char/char-pty.c \ 1874 1876 src/config.h \ 1875 1877 src/drivers/char/char-pty.h \ 1876 - src/drivers/char/char.h 1878 + src/drivers/char/char.h \ 1879 + src/drivers/options.h 1877 1880 1878 1881 src/drivers/char/char-slip.o: src/drivers/char/char-slip.c \ 1879 1882 src/config.h \ 1880 1883 src/drivers/char/char-slip.h \ 1881 1884 src/drivers/char/char.h \ 1885 + src/drivers/options.h \ 1882 1886 src/lib/tun.h 1883 1887 1884 1888 src/drivers/char/char-stdio.o: src/drivers/char/char-stdio.c \ 1885 1889 src/drivers/char/char-stdio.h \ 1886 - src/drivers/char/char.h 1890 + src/drivers/char/char.h \ 1891 + src/drivers/options.h 1887 1892 1888 1893 src/drivers/char/char-tcp.o: src/drivers/char/char-tcp.c \ 1889 1894 src/drivers/char/char-tcp.h \ 1890 - src/drivers/char/char.h 1895 + src/drivers/char/char.h \ 1896 + src/drivers/options.h 1891 1897 1892 1898 src/drivers/char/char-tios.o: src/drivers/char/char-tios.c \ 1893 1899 src/drivers/char/char-tios.h \ 1894 - src/drivers/char/char.h 1900 + src/drivers/char/char.h \ 1901 + src/drivers/options.h 1895 1902 1896 1903 src/drivers/char/char.o: src/drivers/char/char.c \ 1897 1904 src/config.h \ 1898 - src/drivers/char/char.h 1905 + src/drivers/char/char.h \ 1906 + src/drivers/options.h 1907 + 1908 + src/drivers/options.o: src/drivers/options.c \ 1909 + src/config.h \ 1910 + src/drivers/options.h 1899 1911 1900 1912 src/lib/brkpt.o: src/lib/brkpt.c \ 1901 1913 src/lib/brkpt.h \
+1
src/arch/ibmpc/Makefile.inc
··· 63 63 src/devices/nvram.o \ 64 64 src/devices/parport.o \ 65 65 src/devices/serport.o \ 66 + src/drivers/options.o \ 66 67 src/drivers/char/char.a \ 67 68 src/lib/brkpt.o \ 68 69 src/lib/cmd.o \
+1
src/arch/macplus/Makefile.inc
··· 67 67 src/chipset/e8530.o \ 68 68 src/devices/device.o \ 69 69 src/devices/memory.o \ 70 + src/drivers/options.o \ 70 71 src/drivers/char/char.a \ 71 72 src/lib/brkpt.o \ 72 73 src/lib/cmd.o \
+1
src/arch/sim405/Makefile.inc
··· 43 43 src/devices/pci-ata.o \ 44 44 src/devices/serport.o \ 45 45 src/devices/slip.o \ 46 + src/drivers/options.o \ 46 47 src/drivers/char/char.a \ 47 48 src/lib/brkpt.o \ 48 49 src/lib/cmd.o \
+1
src/arch/simarm/Makefile.inc
··· 39 39 src/devices/memory.o \ 40 40 src/devices/serport.o \ 41 41 src/devices/slip.o \ 42 + src/drivers/options.o \ 42 43 src/drivers/char/char.a \ 43 44 src/lib/brkpt.o \ 44 45 src/lib/cmd.o \
+1
src/arch/sims32/Makefile.inc
··· 35 35 src/devices/device.o \ 36 36 src/devices/memory.o \ 37 37 src/devices/serport.o \ 38 + src/drivers/options.o \ 38 39 src/drivers/char/char.a \ 39 40 src/lib/brkpt.o \ 40 41 src/lib/cmd.o \
+5
src/drivers/Makefile.inc
··· 4 4 5 5 DIRS += $(rel) 6 6 DIST += $(rel)/Makefile.inc 7 + 8 + CLN += $(rel)/options.o 9 + DIST += $(rel)/options.c $(rel)/options.h 10 + 11 + $(rel)/options.o: $(rel)/options.c
+5 -4
src/drivers/char/char-posix.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/drivers/char/char-posix.c * 7 7 * Created: 2009-03-10 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2010 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 30 30 #include <sys/stat.h> 31 31 #include <unistd.h> 32 32 33 + #include <drivers/options.h> 33 34 #include <drivers/char/char.h> 34 35 #include <drivers/char/char-posix.h> 35 36 ··· 164 165 drv->fd_read = -1; 165 166 drv->fd_write = -1; 166 167 167 - drv->name = chr_get_option (name, "file", 1); 168 + drv->name = drv_get_option (name, "file"); 168 169 169 170 if (drv->name == NULL) { 170 - drv->name_read = chr_get_option (name, "read", 1); 171 - drv->name_write = chr_get_option (name, "write", 2); 171 + drv->name_read = drv_get_option (name, "read"); 172 + drv->name_write = drv_get_option (name, "write"); 172 173 } 173 174 174 175 if (drv->name != NULL) {
+4 -3
src/drivers/char/char-ppp.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/drivers/char/char-ppp.c * 7 7 * Created: 2009-10-22 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2010 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 30 30 #include <lib/tun.h> 31 31 #endif 32 32 33 + #include <drivers/options.h> 33 34 #include <drivers/char/char.h> 34 35 #include <drivers/char/char-ppp.h> 35 36 ··· 66 67 unsigned i, j; 67 68 char *s; 68 69 69 - s = chr_get_option (str, name, 0xffff); 70 + s = drv_get_option (str, name); 70 71 71 72 if (s == NULL) { 72 73 return (1); ··· 1542 1543 drv->tun_name = NULL; 1543 1544 drv->tun_fd = -1; 1544 1545 1545 - drv->tun_name = chr_get_option (name, "if", 1); 1546 + drv->tun_name = drv_get_option (name, "if"); 1546 1547 1547 1548 if (drv->tun_name == NULL) { 1548 1549 return (1);
+3 -2
src/drivers/char/char-pty.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/drivers/char/char-pty.c * 7 7 * Created: 2009-03-08 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2010 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 31 31 #include <fcntl.h> 32 32 #include <poll.h> 33 33 34 + #include <drivers/options.h> 34 35 #include <drivers/char/char.h> 35 36 #include <drivers/char/char-pty.h> 36 37 ··· 183 184 184 185 drv->ptsname = NULL; 185 186 186 - drv->symlink = chr_get_option (name, "symlink", 1); 187 + drv->symlink = drv_get_option (name, "symlink"); 187 188 188 189 drv->fd = posix_openpt (O_RDWR | O_NOCTTY); 189 190
+3 -2
src/drivers/char/char-slip.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/drivers/char/char-slip.c * 7 7 * Created: 2009-03-10 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2010 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 30 30 #include <lib/tun.h> 31 31 #endif 32 32 33 + #include <drivers/options.h> 33 34 #include <drivers/char/char.h> 34 35 #include <drivers/char/char-slip.h> 35 36 ··· 252 253 drv->tun_name = NULL; 253 254 drv->tun_fd = -1; 254 255 255 - drv->tun_name = chr_get_option (name, "if", 1); 256 + drv->tun_name = drv_get_option (name, "if"); 256 257 257 258 if (drv->tun_name == NULL) { 258 259 return (1);
+4 -3
src/drivers/char/char-stdio.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/drivers/char/char-stdio.c * 7 7 * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2010 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 24 24 #include <stdio.h> 25 25 #include <string.h> 26 26 27 + #include <drivers/options.h> 27 28 #include <drivers/char/char.h> 28 29 #include <drivers/char/char-stdio.h> 29 30 ··· 89 90 90 91 drv->fp = NULL; 91 92 92 - drv->fname = chr_get_option (name, "file", 1); 93 - drv->flush = chr_get_option_bool (name, "flush", 0xffff, 1); 93 + drv->fname = drv_get_option (name, "file"); 94 + drv->flush = drv_get_option_bool (name, "flush", 1); 94 95 95 96 if (drv->fname != NULL) { 96 97 if (strcmp (drv->fname, "-") == 0) {
+5 -4
src/drivers/char/char-tcp.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/drivers/char/char-tcp.c * 7 7 * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2010 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 24 24 #include <string.h> 25 25 #include <limits.h> 26 26 27 + #include <drivers/options.h> 27 28 #include <drivers/char/char.h> 28 29 #include <drivers/char/char-tcp.h> 29 30 ··· 350 351 drv->listen_fd = -1; 351 352 drv->fd = -1; 352 353 353 - drv->host = chr_get_option (name, "host", 1); 354 - drv->connect = chr_get_option_bool (name, "connect", 0xffff, 0); 355 - drv->port = chr_get_option_uint (name, "port", 2, 5555); 354 + drv->host = drv_get_option (name, "host"); 355 + drv->connect = drv_get_option_bool (name, "connect", 0); 356 + drv->port = drv_get_option_uint (name, "port", 5555); 356 357 357 358 if (drv->host != NULL) { 358 359 str = tcp_host_resolve (drv->host);
+3 -2
src/drivers/char/char-tios.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/drivers/char/char-tios.c * 7 7 * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2010 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 31 31 #include <termios.h> 32 32 #include <unistd.h> 33 33 34 + #include <drivers/options.h> 34 35 #include <drivers/char/char.h> 35 36 #include <drivers/char/char-tios.h> 36 37 ··· 380 381 381 382 drv->fd = -1; 382 383 383 - drv->fname = chr_get_option (name, "file", 1); 384 + drv->fname = drv_get_option (name, "file"); 384 385 385 386 if (drv->fname != NULL) { 386 387 drv->fd = open (drv->fname, O_RDWR | O_NOCTTY);
+3 -268
src/drivers/char/char.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/drivers/char/char.c * 7 7 * Created: 2009-03-06 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2009 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2010 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 25 25 #include <stdlib.h> 26 26 #include <string.h> 27 27 28 + #include <drivers/options.h> 28 29 #include <drivers/char/char.h> 29 30 30 31 ··· 225 226 return (0); 226 227 } 227 228 228 - 229 - static 230 - const char *chr_skip_option (const char *str) 231 - { 232 - while (*str != 0) { 233 - if (*str == ':') { 234 - if (*(str + 1) == ':') { 235 - str += 2; 236 - } 237 - else { 238 - return (str + 1); 239 - } 240 - } 241 - else { 242 - str += 1; 243 - } 244 - } 245 - 246 - return (NULL); 247 - } 248 - 249 - static 250 - const char *chr_skip_space (const char *str) 251 - { 252 - while (1) { 253 - if (*str == ' ') { 254 - str += 1; 255 - } 256 - else if (*str == '\t') { 257 - str += 1; 258 - } 259 - else { 260 - return (str); 261 - } 262 - } 263 - } 264 - 265 - static 266 - const char *chr_get_option_value (const char *str, const char *name) 267 - { 268 - while (*name != 0) { 269 - if (*str != *name) { 270 - return (NULL); 271 - } 272 - 273 - str += 1; 274 - name += 1; 275 - } 276 - 277 - if (*str == '=') { 278 - str = chr_skip_space (str + 1); 279 - 280 - return (str); 281 - } 282 - 283 - return (NULL); 284 - } 285 - 286 - static 287 - const char *chr_get_option_name (const char *str) 288 - { 289 - unsigned i; 290 - 291 - str = chr_skip_space (str); 292 - 293 - i = 0; 294 - 295 - while (str[i] != 0) { 296 - if (str[i] == '=') { 297 - return (str); 298 - } 299 - else if (str[i] == ':') { 300 - if (str[i + 1] == ':') { 301 - i += 2; 302 - } 303 - else { 304 - return (NULL); 305 - } 306 - } 307 - else { 308 - i += 1; 309 - } 310 - } 311 - 312 - return (NULL); 313 - } 314 - 315 - static 316 - char *chr_option_dup (const char *str) 317 - { 318 - unsigned i, j, n; 319 - char *ret; 320 - 321 - n = 0; 322 - while (str[n] != 0) { 323 - if (str[n] == ':') { 324 - if (str[n + 1] == ':') { 325 - n += 2; 326 - } 327 - else { 328 - break; 329 - } 330 - } 331 - else { 332 - n += 1; 333 - } 334 - } 335 - 336 - while (n > 0) { 337 - if (str[n - 1] == ' ') { 338 - n -= 1; 339 - } 340 - else if (str[n - 1] == '\t') { 341 - n -= 1; 342 - } 343 - else { 344 - break; 345 - } 346 - } 347 - 348 - ret = malloc (n + 1); 349 - if (ret == NULL) { 350 - return (NULL); 351 - } 352 - 353 - i = 0; 354 - j = 0; 355 - 356 - while (i < n) { 357 - if ((str[i] == ':') && (str[i + 1] == ':')) { 358 - ret[j] = ':'; 359 - i += 2; 360 - } 361 - else { 362 - ret[j] = str[i]; 363 - i += 1; 364 - } 365 - 366 - j += 1; 367 - } 368 - 369 - ret[j] = 0; 370 - 371 - return (ret); 372 - } 373 - 374 - char *chr_get_named_option (const char *str, const char *name) 375 - { 376 - const char *val; 377 - 378 - while (str != NULL) { 379 - val = chr_get_option_value (str, name); 380 - 381 - if (val != NULL) { 382 - return (chr_option_dup (val)); 383 - } 384 - 385 - str = chr_skip_option (str); 386 - } 387 - 388 - return (NULL); 389 - } 390 - 391 - char *chr_get_indexed_option (const char *str, unsigned idx) 392 - { 393 - while (str != NULL) { 394 - if (chr_get_option_name (str) == NULL) { 395 - if (idx == 0) { 396 - str = chr_skip_space (str); 397 - 398 - return (chr_option_dup (str)); 399 - } 400 - 401 - idx -= 1; 402 - } 403 - 404 - str = chr_skip_option (str); 405 - } 406 - 407 - return (NULL); 408 - } 409 - 410 - char *chr_get_option (const char *str, const char *name, unsigned idx) 411 - { 412 - char *ret; 413 - 414 - if (name != NULL) { 415 - ret = chr_get_named_option (str, name); 416 - 417 - if (ret != NULL) { 418 - return (ret); 419 - } 420 - } 421 - 422 - if (idx != 0xffff) { 423 - ret = chr_get_indexed_option (str, idx); 424 - 425 - if (ret != NULL) { 426 - return (ret); 427 - } 428 - } 429 - 430 - return (NULL); 431 - } 432 - 433 - int chr_get_option_bool (const char *str, const char *name, unsigned idx, int def) 434 - { 435 - int r; 436 - char *s; 437 - 438 - s = chr_get_option (str, name, idx); 439 - 440 - if (s == NULL) { 441 - return (def); 442 - } 443 - 444 - r = def; 445 - 446 - if (strcmp (s, "1") == 0) { 447 - r = 1; 448 - } 449 - else if (strcmp (s, "true") == 0) { 450 - r = 1; 451 - } 452 - else if (strcmp (s, "yes") == 0) { 453 - r = 1; 454 - } 455 - else if (strcmp (s, "0") == 0) { 456 - r = 0; 457 - } 458 - else if (strcmp (s, "false") == 0) { 459 - r = 0; 460 - } 461 - else if (strcmp (s, "no") == 0) { 462 - r = 0; 463 - } 464 - 465 - free (s); 466 - 467 - return (r); 468 - } 469 - 470 - unsigned long chr_get_option_uint (const char *str, const char *name, unsigned idx, unsigned long def) 471 - { 472 - unsigned long val; 473 - char *end; 474 - char *s; 475 - 476 - s = chr_get_option (str, name, idx); 477 - 478 - if (s == NULL) { 479 - return (def); 480 - } 481 - 482 - val = strtoul (s, &end, 0); 483 - 484 - if ((end != NULL) && (*end != 0)) { 485 - free (s); 486 - return (def); 487 - } 488 - 489 - free (s); 490 - 491 - return (val); 492 - } 493 - 494 229 void chr_init (char_drv_t *cdrv, void *ext) 495 230 { 496 231 cdrv->ext = ext; ··· 672 407 return (NULL); 673 408 } 674 409 675 - str = chr_get_option (name, "log", 0xffff); 410 + str = drv_get_option (name, "log"); 676 411 677 412 if (str != NULL) { 678 413 chr_set_log (cdrv, str);
-4
src/drivers/char/char.h
··· 83 83 84 84 int chr_set_log (char_drv_t *cdrv, const char *fname); 85 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 86 char_drv_t *chr_open (const char *name); 91 87 char_drv_t *chr_null_open (const char *name); 92 88 char_drv_t *chr_posix_open (const char *name);
+227
src/drivers/options.c
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/drivers/options.c * 7 + * Created: 2009-10-17 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2010 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/options.h> 29 + 30 + 31 + static 32 + const char *drv_skip_option (const char *str) 33 + { 34 + while (*str != 0) { 35 + if (*str == ':') { 36 + if (*(str + 1) == ':') { 37 + str += 2; 38 + } 39 + else { 40 + return (str + 1); 41 + } 42 + } 43 + else { 44 + str += 1; 45 + } 46 + } 47 + 48 + return (NULL); 49 + } 50 + 51 + static 52 + const char *drv_skip_space (const char *str) 53 + { 54 + while (1) { 55 + if (*str == ' ') { 56 + str += 1; 57 + } 58 + else if (*str == '\t') { 59 + str += 1; 60 + } 61 + else { 62 + return (str); 63 + } 64 + } 65 + } 66 + 67 + static 68 + const char *drv_get_option_value (const char *str, const char *name) 69 + { 70 + str = drv_skip_space (str); 71 + 72 + while (*name != 0) { 73 + if (*str != *name) { 74 + return (NULL); 75 + } 76 + 77 + str += 1; 78 + name += 1; 79 + } 80 + 81 + str = drv_skip_space (str); 82 + 83 + if (*str == '=') { 84 + str = drv_skip_space (str + 1); 85 + 86 + return (str); 87 + } 88 + 89 + return (NULL); 90 + } 91 + 92 + static 93 + char *drv_option_dup (const char *str) 94 + { 95 + unsigned i, j, n; 96 + char *ret; 97 + 98 + n = 0; 99 + while (str[n] != 0) { 100 + if (str[n] == ':') { 101 + if (str[n + 1] == ':') { 102 + n += 2; 103 + } 104 + else { 105 + break; 106 + } 107 + } 108 + else { 109 + n += 1; 110 + } 111 + } 112 + 113 + while (n > 0) { 114 + if (str[n - 1] == ' ') { 115 + n -= 1; 116 + } 117 + else if (str[n - 1] == '\t') { 118 + n -= 1; 119 + } 120 + else { 121 + break; 122 + } 123 + } 124 + 125 + ret = malloc (n + 1); 126 + if (ret == NULL) { 127 + return (NULL); 128 + } 129 + 130 + i = 0; 131 + j = 0; 132 + 133 + while (i < n) { 134 + if ((str[i] == ':') && (str[i + 1] == ':')) { 135 + ret[j] = ':'; 136 + i += 2; 137 + } 138 + else { 139 + ret[j] = str[i]; 140 + i += 1; 141 + } 142 + 143 + j += 1; 144 + } 145 + 146 + ret[j] = 0; 147 + 148 + return (ret); 149 + } 150 + 151 + char *drv_get_option (const char *str, const char *name) 152 + { 153 + const char *val; 154 + 155 + while (str != NULL) { 156 + val = drv_get_option_value (str, name); 157 + 158 + if (val != NULL) { 159 + return (drv_option_dup (val)); 160 + } 161 + 162 + str = drv_skip_option (str); 163 + } 164 + 165 + return (NULL); 166 + } 167 + 168 + int drv_get_option_bool (const char *str, const char *name, int def) 169 + { 170 + int r; 171 + char *s; 172 + 173 + s = drv_get_option (str, name); 174 + 175 + if (s == NULL) { 176 + return (def); 177 + } 178 + 179 + r = def; 180 + 181 + if (strcmp (s, "1") == 0) { 182 + r = 1; 183 + } 184 + else if (strcmp (s, "true") == 0) { 185 + r = 1; 186 + } 187 + else if (strcmp (s, "yes") == 0) { 188 + r = 1; 189 + } 190 + else if (strcmp (s, "0") == 0) { 191 + r = 0; 192 + } 193 + else if (strcmp (s, "false") == 0) { 194 + r = 0; 195 + } 196 + else if (strcmp (s, "no") == 0) { 197 + r = 0; 198 + } 199 + 200 + free (s); 201 + 202 + return (r); 203 + } 204 + 205 + unsigned long drv_get_option_uint (const char *str, const char *name, unsigned long def) 206 + { 207 + unsigned long val; 208 + char *end; 209 + char *s; 210 + 211 + s = drv_get_option (str, name); 212 + 213 + if (s == NULL) { 214 + return (def); 215 + } 216 + 217 + val = strtoul (s, &end, 0); 218 + 219 + if ((end != NULL) && (*end != 0)) { 220 + free (s); 221 + return (def); 222 + } 223 + 224 + free (s); 225 + 226 + return (val); 227 + }
+48
src/drivers/options.h
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/drivers/options.h * 7 + * Created: 2009-10-17 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2009-2010 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_OPTIONS_H 24 + #define PCE_DRIVERS_OPTIONS_H 1 25 + 26 + 27 + /*!*************************************************************************** 28 + * @short Get a named driver option 29 + * @return The option value or NULL if the option is not defined in str 30 + * 31 + * The returned string is owned by the caller. 32 + *****************************************************************************/ 33 + char *drv_get_option (const char *str, const char *name); 34 + 35 + /*!*************************************************************************** 36 + * @short Get a named boolean driver option 37 + @ @return The option value 38 + *****************************************************************************/ 39 + int drv_get_option_bool (const char *str, const char *name, int def); 40 + 41 + /*!*************************************************************************** 42 + * @short Get a named integer driver option 43 + @ @return The option value 44 + *****************************************************************************/ 45 + unsigned long drv_get_option_uint (const char *str, const char *name, unsigned long def); 46 + 47 + 48 + #endif