A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

171 lines
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] [-e WID] [-G GAMMA] [-g GEOMETRY] "
  31. "[-N NAME] [-n NUM] [-S DELAY] [-s MODE] [-z ZOOM] 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, "abce:fG:g:hin:N:oqrS:s:tvZz:")) != -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 'e':
  76. n = strtol(optarg, &end, 0);
  77. if (*end != '\0')
  78. error(EXIT_FAILURE, 0, "Invalid argument for option -e: %s", optarg);
  79. _options.embed = n;
  80. break;
  81. case 'f':
  82. _options.fullscreen = true;
  83. break;
  84. case 'G':
  85. n = strtol(optarg, &end, 0);
  86. if (*end != '\0')
  87. error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg);
  88. _options.gamma = n;
  89. break;
  90. case 'g':
  91. _options.geometry = optarg;
  92. break;
  93. case 'h':
  94. print_usage();
  95. exit(EXIT_SUCCESS);
  96. case 'i':
  97. _options.from_stdin = true;
  98. break;
  99. case 'n':
  100. n = strtol(optarg, &end, 0);
  101. if (*end != '\0' || n <= 0)
  102. error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg);
  103. _options.startnum = n - 1;
  104. break;
  105. case 'N':
  106. _options.res_name = optarg;
  107. break;
  108. case 'o':
  109. _options.to_stdout = true;
  110. break;
  111. case 'q':
  112. _options.quiet = true;
  113. break;
  114. case 'r':
  115. _options.recursive = true;
  116. break;
  117. case 'S':
  118. n = strtol(optarg, &end, 0);
  119. if (*end != '\0' || n <= 0)
  120. error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg);
  121. _options.slideshow = n;
  122. break;
  123. case 's':
  124. s = strchr(scalemodes, optarg[0]);
  125. if (s == NULL || *s == '\0' || strlen(optarg) != 1)
  126. error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
  127. _options.scalemode = s - scalemodes;
  128. break;
  129. case 't':
  130. _options.thumb_mode = true;
  131. break;
  132. case 'v':
  133. print_version();
  134. exit(EXIT_SUCCESS);
  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. }