Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

128 рядки
3.2 KiB

  1. /*
  2. * neercs console-based window manager
  3. * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
  4. * 2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
  5. * All Rights Reserved
  6. *
  7. * This program is free software. It comes without any warranty, to
  8. * the extent permitted by applicable law. You can redistribute it
  9. * and/or modify it under the terms of the Do What The Fuck You Want
  10. * To Public License, Version 2, as published by Sam Hocevar. See
  11. * http://www.wtfpl.net/ for more details.
  12. */
  13. /*
  14. * mygetopt.c: getopt_long reimplementation
  15. */
  16. #if defined HAVE_CONFIG_H
  17. # include "config.h"
  18. #endif
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "caca_types.h"
  22. #include "mygetopt.h"
  23. int myoptind = 1;
  24. char *myoptarg = NULL;
  25. /* XXX: this getopt_long implementation should not be trusted for other
  26. applications without any serious peer reviewing. It “just works” with
  27. zzuf but may fail miserably in other programs. */
  28. int mygetopt(int argc, char *const _argv[], const char *optstring,
  29. const struct myoption *longopts, int *longindex)
  30. {
  31. char **argv = (char **)(uintptr_t) _argv;
  32. char *flag;
  33. int i;
  34. if (myoptind >= argc)
  35. return -1;
  36. flag = argv[myoptind];
  37. if (flag[0] == '-' && flag[1] != '-')
  38. {
  39. char const *tmp;
  40. int ret = flag[1];
  41. if (ret == '\0')
  42. return -1;
  43. tmp = strchr(optstring, ret);
  44. if (!tmp || ret == ':')
  45. return '?';
  46. myoptind++;
  47. if (tmp[1] == ':')
  48. {
  49. if (flag[2] != '\0')
  50. myoptarg = flag + 2;
  51. else if (myoptind >= argc)
  52. {
  53. if (tmp[2] != ':')
  54. {
  55. fprintf(stderr, "%s: `%s' needs an argument\n", argv[0],
  56. flag);
  57. return -2;
  58. }
  59. }
  60. else
  61. myoptarg = argv[myoptind++];
  62. return ret;
  63. }
  64. if (flag[2] != '\0')
  65. {
  66. flag[1] = '-';
  67. myoptind--;
  68. argv[myoptind]++;
  69. }
  70. return ret;
  71. }
  72. if (flag[0] == '-' && flag[1] == '-')
  73. {
  74. if (flag[2] == '\0')
  75. return -1;
  76. for (i = 0; longopts[i].name; i++)
  77. {
  78. size_t l = strlen(longopts[i].name);
  79. if (strncmp(flag + 2, longopts[i].name, l))
  80. continue;
  81. switch (flag[2 + l])
  82. {
  83. case '=':
  84. if (!longopts[i].has_arg)
  85. goto bad_opt;
  86. if (longindex)
  87. *longindex = i;
  88. myoptind++;
  89. myoptarg = flag + 2 + l + 1;
  90. return longopts[i].val;
  91. case '\0':
  92. if (longindex)
  93. *longindex = i;
  94. myoptind++;
  95. if (longopts[i].has_arg)
  96. myoptarg = argv[myoptind++];
  97. return longopts[i].val;
  98. default:
  99. break;
  100. }
  101. }
  102. bad_opt:
  103. fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag);
  104. return '?';
  105. }
  106. return -1;
  107. }