A Simple X Image Viewer
 
 
 
 
 
 

171 行
4.2 KiB

  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 <stdlib.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include "options.h"
  23. #include "util.h"
  24. #define _IMAGE_CONFIG
  25. #include "config.h"
  26. options_t _options;
  27. const options_t *options = (const options_t*) &_options;
  28. void print_usage(void)
  29. {
  30. printf("usage: sxiv [-abcfhioqrtvZ] [-G GAMMA] [-g GEOMETRY] [-n NUM] "
  31. "[-N NAME] [-S DELAY] [-s MODE] [-z ZOOM] [-w WID] FILES...\n");
  32. }
  33. void print_version(void)
  34. {
  35. printf("sxiv %s - Simple X Image Viewer\n", 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.scalemode = SCALE_DOWN;
  49. _options.zoom = 1.0;
  50. _options.animate = false;
  51. _options.gamma = 0;
  52. _options.slideshow = 0;
  53. _options.fullscreen = false;
  54. _options.embed = 0;
  55. _options.hide_bar = false;
  56. _options.geometry = NULL;
  57. _options.res_name = NULL;
  58. _options.quiet = false;
  59. _options.thumb_mode = false;
  60. _options.clean_cache = false;
  61. while ((opt = getopt(argc, argv, "abcfG:g:hin:N:oqrS:s:tvw:Zz:")) != -1) {
  62. switch (opt) {
  63. case '?':
  64. print_usage();
  65. exit(EXIT_FAILURE);
  66. case 'a':
  67. _options.animate = true;
  68. break;
  69. case 'b':
  70. _options.hide_bar = true;
  71. break;
  72. case 'c':
  73. _options.clean_cache = true;
  74. break;
  75. case 'f':
  76. _options.fullscreen = true;
  77. break;
  78. case 'G':
  79. n = strtol(optarg, &end, 0);
  80. if (*end != '\0')
  81. error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg);
  82. _options.gamma = n;
  83. break;
  84. case 'g':
  85. _options.geometry = optarg;
  86. break;
  87. case 'h':
  88. print_usage();
  89. exit(EXIT_SUCCESS);
  90. case 'i':
  91. _options.from_stdin = true;
  92. break;
  93. case 'n':
  94. n = strtol(optarg, &end, 0);
  95. if (*end != '\0' || n <= 0)
  96. error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg);
  97. _options.startnum = n - 1;
  98. break;
  99. case 'N':
  100. _options.res_name = optarg;
  101. break;
  102. case 'o':
  103. _options.to_stdout = true;
  104. break;
  105. case 'q':
  106. _options.quiet = true;
  107. break;
  108. case 'r':
  109. _options.recursive = true;
  110. break;
  111. case 'S':
  112. n = strtol(optarg, &end, 0);
  113. if (*end != '\0' || n <= 0)
  114. error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg);
  115. _options.slideshow = n;
  116. break;
  117. case 's':
  118. s = strchr(scalemodes, optarg[0]);
  119. if (s == NULL || *s == '\0' || strlen(optarg) != 1)
  120. error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
  121. _options.scalemode = s - scalemodes;
  122. break;
  123. case 't':
  124. _options.thumb_mode = true;
  125. break;
  126. case 'v':
  127. print_version();
  128. exit(EXIT_SUCCESS);
  129. case 'w':
  130. n = strtol(optarg, &end, 0);
  131. if (*end != '\0')
  132. error(EXIT_FAILURE, 0, "Invalid argument for option -w: %s", optarg);
  133. _options.embed = n;
  134. break;
  135. case 'Z':
  136. _options.scalemode = SCALE_ZOOM;
  137. _options.zoom = 1.0;
  138. break;
  139. case 'z':
  140. n = strtol(optarg, &end, 0);
  141. if (*end != '\0' || n <= 0)
  142. error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg);
  143. _options.scalemode = SCALE_ZOOM;
  144. _options.zoom = (float) n / 100.0;
  145. break;
  146. }
  147. }
  148. _options.filenames = argv + optind;
  149. _options.filecnt = argc - optind;
  150. if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
  151. _options.filenames++;
  152. _options.filecnt--;
  153. _options.from_stdin = true;
  154. }
  155. }