options.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Copyright 2011 Bert Muennich
  2. *
  3. * This file is part of sxiv.
  4. *
  5. * sxiv is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * sxiv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "sxiv.h"
  19. #define _IMAGE_CONFIG
  20. #include "config.h"
  21. #include "version.h"
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. opt_t _options;
  26. opt_t *options = (opt_t*) &_options;
  27. void print_usage(void)
  28. {
  29. printf("usage: sxiv [-abcfhiopqrtvZO] [-A FRAMERATE] [-e WID] [-G GAMMA] "
  30. "[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] [-z ZOOM] "
  31. "[-F FILE] FILES...\n");
  32. }
  33. void print_version(void)
  34. {
  35. puts("sxiv " VERSION);
  36. }
  37. void parse_options(int argc, char **argv)
  38. {
  39. int n, opt;
  40. char *end, *s;
  41. const char *scalemodes = "dfwh";
  42. progname = strrchr(argv[0], '/');
  43. progname = progname ? progname + 1 : argv[0];
  44. _options.from_stdin = false;
  45. _options.to_stdout = false;
  46. _options.recursive = false;
  47. _options.startnum = 0;
  48. _options.startfile = NULL;
  49. _options.scalemode = SCALE_DOWN;
  50. _options.zoom = 1.0;
  51. _options.animate = false;
  52. _options.gamma = 0;
  53. _options.slideshow = 0;
  54. _options.framerate = 0;
  55. _options.fullscreen = false;
  56. _options.embed = 0;
  57. _options.hide_bar = true;
  58. _options.geometry = NULL;
  59. _options.res_name = NULL;
  60. _options.quiet = false;
  61. _options.thumb_mode = false;
  62. _options.clean_cache = false;
  63. _options.private_mode = false;
  64. _options.old = false;
  65. while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:F:N:opqrS:s:tvZz:O")) != -1) {
  66. switch (opt) {
  67. case '?':
  68. print_usage();
  69. exit(EXIT_FAILURE);
  70. case 'A':
  71. n = strtol(optarg, &end, 0);
  72. if (*end != '\0' || n <= 0)
  73. error(EXIT_FAILURE, 0, "Invalid argument for option -A: %s", optarg);
  74. _options.framerate = n;
  75. /* fall through */
  76. case 'a':
  77. _options.animate = true;
  78. break;
  79. case 'b':
  80. _options.hide_bar = false;
  81. break;
  82. case 'c':
  83. _options.clean_cache = true;
  84. break;
  85. case 'e':
  86. n = strtol(optarg, &end, 0);
  87. if (*end != '\0')
  88. error(EXIT_FAILURE, 0, "Invalid argument for option -e: %s", optarg);
  89. _options.embed = n;
  90. break;
  91. case 'f':
  92. _options.fullscreen = true;
  93. break;
  94. case 'G':
  95. n = strtol(optarg, &end, 0);
  96. if (*end != '\0')
  97. error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg);
  98. _options.gamma = n;
  99. break;
  100. case 'g':
  101. _options.geometry = optarg;
  102. break;
  103. case 'h':
  104. print_usage();
  105. exit(EXIT_SUCCESS);
  106. case 'i':
  107. _options.from_stdin = true;
  108. break;
  109. case 'n':
  110. n = strtol(optarg, &end, 0);
  111. if (*end != '\0' || n <= 0)
  112. error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg);
  113. _options.startnum = n - 1;
  114. break;
  115. case 'F':
  116. _options.startfile = optarg;
  117. break;
  118. case 'N':
  119. _options.res_name = optarg;
  120. break;
  121. case 'o':
  122. _options.to_stdout = true;
  123. break;
  124. case 'p':
  125. _options.private_mode = true;
  126. break;
  127. case 'q':
  128. _options.quiet = true;
  129. break;
  130. case 'r':
  131. _options.recursive = true;
  132. break;
  133. case 'S':
  134. n = strtof(optarg, &end) * 10;
  135. if (*end != '\0' || n <= 0)
  136. error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg);
  137. _options.slideshow = n;
  138. break;
  139. case 's':
  140. s = strchr(scalemodes, optarg[0]);
  141. if (s == NULL || *s == '\0' || strlen(optarg) != 1)
  142. error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
  143. _options.scalemode = s - scalemodes;
  144. break;
  145. case 't':
  146. _options.thumb_mode = true;
  147. break;
  148. case 'v':
  149. print_version();
  150. exit(EXIT_SUCCESS);
  151. case 'Z':
  152. _options.scalemode = SCALE_ZOOM;
  153. _options.zoom = 1.0;
  154. break;
  155. case 'z':
  156. n = strtol(optarg, &end, 0);
  157. if (*end != '\0' || n <= 0)
  158. error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg);
  159. _options.scalemode = SCALE_ZOOM;
  160. _options.zoom = (float) n / 100.0;
  161. break;
  162. case 'O':
  163. _options.old = true;
  164. break;
  165. }
  166. }
  167. _options.filenames = argv + optind;
  168. _options.filecnt = argc - optind;
  169. if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
  170. _options.filenames++;
  171. _options.filecnt--;
  172. _options.from_stdin = true;
  173. }
  174. }