cmdline.c 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*
  2. File autogenerated by gengetopt version 2.22.6
  3. generated with the following command:
  4. /usr/bin/gengetopt --input=options.ggo
  5. The developers of gengetopt consider the fixed text that goes in all
  6. gengetopt output files to be in the public domain:
  7. we make no copyright claims on it.
  8. */
  9. /* If we use autoconf. */
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #ifndef FIX_UNUSED
  17. #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
  18. #endif
  19. #include <getopt.h>
  20. #include "cmdline.h"
  21. const char *gengetopt_args_info_purpose = "";
  22. const char *gengetopt_args_info_usage = "Usage: slop [options]";
  23. const char *gengetopt_args_info_versiontext = "Copyright (C) 2014 Dalton Nell, Slop Contributors\n(https://github.com/naelstrof/slop/graphs/contributors)";
  24. const char *gengetopt_args_info_description = "slop (Select Operation) is an application that queries for a selection from the\nuser and prints the region to stdout.";
  25. const char *gengetopt_args_info_help[] = {
  26. " -h, --help Print help and exit",
  27. " -V, --version Print version and exit",
  28. "Options",
  29. " --xdisplay=hostname:number.screen_number\n Sets the x display.",
  30. " --nokeyboard Disables the ability to cancel selections with\n the keyboard. (default=off)",
  31. " -b, --bordersize=INT Set the selection rectangle's thickness. Does\n nothing when --highlight is enabled.\n (default=`5')",
  32. " -p, --padding=INT Set the padding size of the selection. Can be\n negative. (default=`0')",
  33. " -t, --tolerance=INT How far in pixels the mouse can move after\n clicking and still be detected as a normal\n click instead of a click and drag. Setting\n this to 0 will disable window selections.\n (default=`2')",
  34. " -g, --gracetime=FLOAT Set the amount of time before slop will check\n for keyboard cancellations in seconds.\n (default=`0.4')",
  35. " -c, --color=FLOAT,FLOAT,FLOAT,FLOAT\n Set the selection rectangle's color. Supports\n RGB or RGBA values.\n (default=`0.5,0.5,0.5,1')",
  36. " -n, --nodecorations Attempt to select child windows in order to\n avoid window decorations. (default=off)",
  37. " --min=INT Set the minimum output of width or height\n values. This is useful to avoid outputting 0.\n Setting min and max to the same value\n disables drag selections. (default=`0')",
  38. " --max=INT Set the maximum output of width or height\n values. Setting min and max to the same value\n disables drag selections. (default=`0')",
  39. " -l, --highlight Instead of outlining selections, slop\n highlights it. This is only useful when\n --color is set to a transparent color.\n (default=off)",
  40. " --opengl Enable hardware acceleration. Only works with\n modern systems that are also running a\n compositor. (default=off)",
  41. " --magnify Display a magnifying glass when --opengl is\n also enabled. (default=off)",
  42. " --magstrength=FLOAT Sets how many times the magnification window\n size is multiplied. (default=`4')",
  43. " --magpixels=INT Sets how many pixels are displayed in the\n magnification. The less pixels the bigger the\n magnification. (default=`64')",
  44. " --theme=STRING Sets the theme of the selection, using textures\n from ~/.config/slop/ or /usr/share/.\n (default=`none')",
  45. " --shader=STRING Sets the shader to load and use from\n ~/.config/slop/ or /usr/share/.\n (default=`simple')",
  46. " -f, --format=STRING Set the output format string. Format specifiers\n are %x, %y, %w, %h, %i, %g, and %c.\n (default=`X=%x\\nY=%y\\nW=%w\\nH=%h\\nG=%g\\nID=%i\\nCancel=%c\\n')",
  47. "\nExamples\n $ # Gray, thick, transparent border for maximum visiblity.\n $ slop -b 20 -c 0.5,0.5,0.5,0.8\n\n $ # Remove window decorations.\n $ slop --nodecorations\n\n $ # Disable window selections. Useful for selecting individual pixels.\n $ slop -t 0\n\n $ # Classic Windows XP selection.\n $ slop -l -c 0.3,0.4,0.6,0.4\n\n $ # Wiggle wiggle!\n $ slop --opengl --shader wiggle\n\n $ # Edgy textures or something.\n $ slop --opengl --theme gothic\n\n $ # Change output format to use safer parsing\n $ slopoutput=$(slop -f \"%x %y %w %h\")\n $ X=$(echo $slopoutput | awk '{print $1}')\n $ Y=$(echo $slopoutput | awk '{print $2}')\n $ W=$(echo $slopoutput | awk '{print $3}')\n $ H=$(echo $slopoutput | awk '{print $4}')\n\nTips\n * You can use the arrow keys to move the starting point of a\ndrag-selection, just in case you missed it by a few pixels.\n * If you don't like a selection: you can cancel it by right-clicking\nregardless of which options are enabled or disabled for slop.\n * If slop doesn't seem to select a window accurately, the problem could be\nbecause of decorations getting in the way. Try enabling the --nodecorations\nflag.\n",
  48. 0
  49. };
  50. typedef enum {ARG_NO
  51. , ARG_FLAG
  52. , ARG_STRING
  53. , ARG_INT
  54. , ARG_FLOAT
  55. } cmdline_parser_arg_type;
  56. static
  57. void clear_given (struct gengetopt_args_info *args_info);
  58. static
  59. void clear_args (struct gengetopt_args_info *args_info);
  60. static int
  61. cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
  62. struct cmdline_parser_params *params, const char *additional_error);
  63. static char *
  64. gengetopt_strdup (const char *s);
  65. static
  66. void clear_given (struct gengetopt_args_info *args_info)
  67. {
  68. args_info->help_given = 0 ;
  69. args_info->version_given = 0 ;
  70. args_info->xdisplay_given = 0 ;
  71. args_info->nokeyboard_given = 0 ;
  72. args_info->bordersize_given = 0 ;
  73. args_info->padding_given = 0 ;
  74. args_info->tolerance_given = 0 ;
  75. args_info->gracetime_given = 0 ;
  76. args_info->color_given = 0 ;
  77. args_info->nodecorations_given = 0 ;
  78. args_info->min_given = 0 ;
  79. args_info->max_given = 0 ;
  80. args_info->highlight_given = 0 ;
  81. args_info->opengl_given = 0 ;
  82. args_info->magnify_given = 0 ;
  83. args_info->magstrength_given = 0 ;
  84. args_info->magpixels_given = 0 ;
  85. args_info->theme_given = 0 ;
  86. args_info->shader_given = 0 ;
  87. args_info->format_given = 0 ;
  88. }
  89. static
  90. void clear_args (struct gengetopt_args_info *args_info)
  91. {
  92. FIX_UNUSED (args_info);
  93. args_info->xdisplay_arg = NULL;
  94. args_info->xdisplay_orig = NULL;
  95. args_info->nokeyboard_flag = 0;
  96. args_info->bordersize_arg = 5;
  97. args_info->bordersize_orig = NULL;
  98. args_info->padding_arg = 0;
  99. args_info->padding_orig = NULL;
  100. args_info->tolerance_arg = 2;
  101. args_info->tolerance_orig = NULL;
  102. args_info->gracetime_arg = gengetopt_strdup ("0.4");
  103. args_info->gracetime_orig = NULL;
  104. args_info->color_arg = gengetopt_strdup ("0.5,0.5,0.5,1");
  105. args_info->color_orig = NULL;
  106. args_info->nodecorations_flag = 0;
  107. args_info->min_arg = 0;
  108. args_info->min_orig = NULL;
  109. args_info->max_arg = 0;
  110. args_info->max_orig = NULL;
  111. args_info->highlight_flag = 0;
  112. args_info->opengl_flag = 0;
  113. args_info->magnify_flag = 0;
  114. args_info->magstrength_arg = 4;
  115. args_info->magstrength_orig = NULL;
  116. args_info->magpixels_arg = 64;
  117. args_info->magpixels_orig = NULL;
  118. args_info->theme_arg = gengetopt_strdup ("none");
  119. args_info->theme_orig = NULL;
  120. args_info->shader_arg = gengetopt_strdup ("simple");
  121. args_info->shader_orig = NULL;
  122. args_info->format_arg = gengetopt_strdup ("X=%x\nY=%y\nW=%w\nH=%h\nG=%g\nID=%i\nCancel=%c\n");
  123. args_info->format_orig = NULL;
  124. }
  125. static
  126. void init_args_info(struct gengetopt_args_info *args_info)
  127. {
  128. args_info->help_help = gengetopt_args_info_help[0] ;
  129. args_info->version_help = gengetopt_args_info_help[1] ;
  130. args_info->xdisplay_help = gengetopt_args_info_help[3] ;
  131. args_info->nokeyboard_help = gengetopt_args_info_help[4] ;
  132. args_info->bordersize_help = gengetopt_args_info_help[5] ;
  133. args_info->padding_help = gengetopt_args_info_help[6] ;
  134. args_info->tolerance_help = gengetopt_args_info_help[7] ;
  135. args_info->gracetime_help = gengetopt_args_info_help[8] ;
  136. args_info->color_help = gengetopt_args_info_help[9] ;
  137. args_info->nodecorations_help = gengetopt_args_info_help[10] ;
  138. args_info->min_help = gengetopt_args_info_help[11] ;
  139. args_info->max_help = gengetopt_args_info_help[12] ;
  140. args_info->highlight_help = gengetopt_args_info_help[13] ;
  141. args_info->opengl_help = gengetopt_args_info_help[14] ;
  142. args_info->magnify_help = gengetopt_args_info_help[15] ;
  143. args_info->magstrength_help = gengetopt_args_info_help[16] ;
  144. args_info->magpixels_help = gengetopt_args_info_help[17] ;
  145. args_info->theme_help = gengetopt_args_info_help[18] ;
  146. args_info->shader_help = gengetopt_args_info_help[19] ;
  147. args_info->format_help = gengetopt_args_info_help[20] ;
  148. }
  149. void
  150. cmdline_parser_print_version (void)
  151. {
  152. printf ("%s %s\n",
  153. (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
  154. CMDLINE_PARSER_VERSION);
  155. if (strlen(gengetopt_args_info_versiontext) > 0)
  156. printf("\n%s\n", gengetopt_args_info_versiontext);
  157. }
  158. static void print_help_common(void) {
  159. cmdline_parser_print_version ();
  160. if (strlen(gengetopt_args_info_purpose) > 0)
  161. printf("\n%s\n", gengetopt_args_info_purpose);
  162. if (strlen(gengetopt_args_info_usage) > 0)
  163. printf("\n%s\n", gengetopt_args_info_usage);
  164. printf("\n");
  165. if (strlen(gengetopt_args_info_description) > 0)
  166. printf("%s\n\n", gengetopt_args_info_description);
  167. }
  168. void
  169. cmdline_parser_print_help (void)
  170. {
  171. int i = 0;
  172. print_help_common();
  173. while (gengetopt_args_info_help[i])
  174. printf("%s\n", gengetopt_args_info_help[i++]);
  175. }
  176. void
  177. cmdline_parser_init (struct gengetopt_args_info *args_info)
  178. {
  179. clear_given (args_info);
  180. clear_args (args_info);
  181. init_args_info (args_info);
  182. }
  183. void
  184. cmdline_parser_params_init(struct cmdline_parser_params *params)
  185. {
  186. if (params)
  187. {
  188. params->override = 0;
  189. params->initialize = 1;
  190. params->check_required = 1;
  191. params->check_ambiguity = 0;
  192. params->print_errors = 1;
  193. }
  194. }
  195. struct cmdline_parser_params *
  196. cmdline_parser_params_create(void)
  197. {
  198. struct cmdline_parser_params *params =
  199. (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
  200. cmdline_parser_params_init(params);
  201. return params;
  202. }
  203. static void
  204. free_string_field (char **s)
  205. {
  206. if (*s)
  207. {
  208. free (*s);
  209. *s = 0;
  210. }
  211. }
  212. static void
  213. cmdline_parser_release (struct gengetopt_args_info *args_info)
  214. {
  215. free_string_field (&(args_info->xdisplay_arg));
  216. free_string_field (&(args_info->xdisplay_orig));
  217. free_string_field (&(args_info->bordersize_orig));
  218. free_string_field (&(args_info->padding_orig));
  219. free_string_field (&(args_info->tolerance_orig));
  220. free_string_field (&(args_info->gracetime_arg));
  221. free_string_field (&(args_info->gracetime_orig));
  222. free_string_field (&(args_info->color_arg));
  223. free_string_field (&(args_info->color_orig));
  224. free_string_field (&(args_info->min_orig));
  225. free_string_field (&(args_info->max_orig));
  226. free_string_field (&(args_info->magstrength_orig));
  227. free_string_field (&(args_info->magpixels_orig));
  228. free_string_field (&(args_info->theme_arg));
  229. free_string_field (&(args_info->theme_orig));
  230. free_string_field (&(args_info->shader_arg));
  231. free_string_field (&(args_info->shader_orig));
  232. free_string_field (&(args_info->format_arg));
  233. free_string_field (&(args_info->format_orig));
  234. clear_given (args_info);
  235. }
  236. static void
  237. write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
  238. {
  239. FIX_UNUSED (values);
  240. if (arg) {
  241. fprintf(outfile, "%s=\"%s\"\n", opt, arg);
  242. } else {
  243. fprintf(outfile, "%s\n", opt);
  244. }
  245. }
  246. int
  247. cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
  248. {
  249. int i = 0;
  250. if (!outfile)
  251. {
  252. fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
  253. return EXIT_FAILURE;
  254. }
  255. if (args_info->help_given)
  256. write_into_file(outfile, "help", 0, 0 );
  257. if (args_info->version_given)
  258. write_into_file(outfile, "version", 0, 0 );
  259. if (args_info->xdisplay_given)
  260. write_into_file(outfile, "xdisplay", args_info->xdisplay_orig, 0);
  261. if (args_info->nokeyboard_given)
  262. write_into_file(outfile, "nokeyboard", 0, 0 );
  263. if (args_info->bordersize_given)
  264. write_into_file(outfile, "bordersize", args_info->bordersize_orig, 0);
  265. if (args_info->padding_given)
  266. write_into_file(outfile, "padding", args_info->padding_orig, 0);
  267. if (args_info->tolerance_given)
  268. write_into_file(outfile, "tolerance", args_info->tolerance_orig, 0);
  269. if (args_info->gracetime_given)
  270. write_into_file(outfile, "gracetime", args_info->gracetime_orig, 0);
  271. if (args_info->color_given)
  272. write_into_file(outfile, "color", args_info->color_orig, 0);
  273. if (args_info->nodecorations_given)
  274. write_into_file(outfile, "nodecorations", 0, 0 );
  275. if (args_info->min_given)
  276. write_into_file(outfile, "min", args_info->min_orig, 0);
  277. if (args_info->max_given)
  278. write_into_file(outfile, "max", args_info->max_orig, 0);
  279. if (args_info->highlight_given)
  280. write_into_file(outfile, "highlight", 0, 0 );
  281. if (args_info->opengl_given)
  282. write_into_file(outfile, "opengl", 0, 0 );
  283. if (args_info->magnify_given)
  284. write_into_file(outfile, "magnify", 0, 0 );
  285. if (args_info->magstrength_given)
  286. write_into_file(outfile, "magstrength", args_info->magstrength_orig, 0);
  287. if (args_info->magpixels_given)
  288. write_into_file(outfile, "magpixels", args_info->magpixels_orig, 0);
  289. if (args_info->theme_given)
  290. write_into_file(outfile, "theme", args_info->theme_orig, 0);
  291. if (args_info->shader_given)
  292. write_into_file(outfile, "shader", args_info->shader_orig, 0);
  293. if (args_info->format_given)
  294. write_into_file(outfile, "format", args_info->format_orig, 0);
  295. i = EXIT_SUCCESS;
  296. return i;
  297. }
  298. int
  299. cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
  300. {
  301. FILE *outfile;
  302. int i = 0;
  303. outfile = fopen(filename, "w");
  304. if (!outfile)
  305. {
  306. fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
  307. return EXIT_FAILURE;
  308. }
  309. i = cmdline_parser_dump(outfile, args_info);
  310. fclose (outfile);
  311. return i;
  312. }
  313. void
  314. cmdline_parser_free (struct gengetopt_args_info *args_info)
  315. {
  316. cmdline_parser_release (args_info);
  317. }
  318. /** @brief replacement of strdup, which is not standard */
  319. char *
  320. gengetopt_strdup (const char *s)
  321. {
  322. char *result = 0;
  323. if (!s)
  324. return result;
  325. result = (char*)malloc(strlen(s) + 1);
  326. if (result == (char*)0)
  327. return (char*)0;
  328. strcpy(result, s);
  329. return result;
  330. }
  331. int
  332. cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
  333. {
  334. return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
  335. }
  336. int
  337. cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
  338. struct cmdline_parser_params *params)
  339. {
  340. int result;
  341. result = cmdline_parser_internal (argc, argv, args_info, params, 0);
  342. if (result == EXIT_FAILURE)
  343. {
  344. cmdline_parser_free (args_info);
  345. exit (EXIT_FAILURE);
  346. }
  347. return result;
  348. }
  349. int
  350. cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
  351. {
  352. int result;
  353. struct cmdline_parser_params params;
  354. params.override = override;
  355. params.initialize = initialize;
  356. params.check_required = check_required;
  357. params.check_ambiguity = 0;
  358. params.print_errors = 1;
  359. result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
  360. if (result == EXIT_FAILURE)
  361. {
  362. cmdline_parser_free (args_info);
  363. exit (EXIT_FAILURE);
  364. }
  365. return result;
  366. }
  367. int
  368. cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
  369. {
  370. FIX_UNUSED (args_info);
  371. FIX_UNUSED (prog_name);
  372. return EXIT_SUCCESS;
  373. }
  374. static char *package_name = 0;
  375. /**
  376. * @brief updates an option
  377. * @param field the generic pointer to the field to update
  378. * @param orig_field the pointer to the orig field
  379. * @param field_given the pointer to the number of occurrence of this option
  380. * @param prev_given the pointer to the number of occurrence already seen
  381. * @param value the argument for this option (if null no arg was specified)
  382. * @param possible_values the possible values for this option (if specified)
  383. * @param default_value the default value (in case the option only accepts fixed values)
  384. * @param arg_type the type of this option
  385. * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
  386. * @param override @see cmdline_parser_params.override
  387. * @param no_free whether to free a possible previous value
  388. * @param multiple_option whether this is a multiple option
  389. * @param long_opt the corresponding long option
  390. * @param short_opt the corresponding short option (or '-' if none)
  391. * @param additional_error possible further error specification
  392. */
  393. static
  394. int update_arg(void *field, char **orig_field,
  395. unsigned int *field_given, unsigned int *prev_given,
  396. char *value, const char *possible_values[],
  397. const char *default_value,
  398. cmdline_parser_arg_type arg_type,
  399. int check_ambiguity, int override,
  400. int no_free, int multiple_option,
  401. const char *long_opt, char short_opt,
  402. const char *additional_error)
  403. {
  404. char *stop_char = 0;
  405. const char *val = value;
  406. int found;
  407. char **string_field;
  408. FIX_UNUSED (field);
  409. stop_char = 0;
  410. found = 0;
  411. if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
  412. {
  413. if (short_opt != '-')
  414. fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
  415. package_name, long_opt, short_opt,
  416. (additional_error ? additional_error : ""));
  417. else
  418. fprintf (stderr, "%s: `--%s' option given more than once%s\n",
  419. package_name, long_opt,
  420. (additional_error ? additional_error : ""));
  421. return 1; /* failure */
  422. }
  423. FIX_UNUSED (default_value);
  424. if (field_given && *field_given && ! override)
  425. return 0;
  426. if (prev_given)
  427. (*prev_given)++;
  428. if (field_given)
  429. (*field_given)++;
  430. if (possible_values)
  431. val = possible_values[found];
  432. switch(arg_type) {
  433. case ARG_FLAG:
  434. *((int *)field) = !*((int *)field);
  435. break;
  436. case ARG_INT:
  437. if (val) *((int *)field) = strtol (val, &stop_char, 0);
  438. break;
  439. case ARG_FLOAT:
  440. if (val) *((float *)field) = (float)strtod (val, &stop_char);
  441. break;
  442. case ARG_STRING:
  443. if (val) {
  444. string_field = (char **)field;
  445. if (!no_free && *string_field)
  446. free (*string_field); /* free previous string */
  447. *string_field = gengetopt_strdup (val);
  448. }
  449. break;
  450. default:
  451. break;
  452. };
  453. /* check numeric conversion */
  454. switch(arg_type) {
  455. case ARG_INT:
  456. case ARG_FLOAT:
  457. if (val && !(stop_char && *stop_char == '\0')) {
  458. fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
  459. return 1; /* failure */
  460. }
  461. break;
  462. default:
  463. ;
  464. };
  465. /* store the original value */
  466. switch(arg_type) {
  467. case ARG_NO:
  468. case ARG_FLAG:
  469. break;
  470. default:
  471. if (value && orig_field) {
  472. if (no_free) {
  473. *orig_field = value;
  474. } else {
  475. if (*orig_field)
  476. free (*orig_field); /* free previous string */
  477. *orig_field = gengetopt_strdup (value);
  478. }
  479. }
  480. };
  481. return 0; /* OK */
  482. }
  483. int
  484. cmdline_parser_internal (
  485. int argc, char **argv, struct gengetopt_args_info *args_info,
  486. struct cmdline_parser_params *params, const char *additional_error)
  487. {
  488. int c; /* Character of the parsed option. */
  489. int error_occurred = 0;
  490. struct gengetopt_args_info local_args_info;
  491. int override;
  492. int initialize;
  493. int check_required;
  494. int check_ambiguity;
  495. package_name = argv[0];
  496. override = params->override;
  497. initialize = params->initialize;
  498. check_required = params->check_required;
  499. check_ambiguity = params->check_ambiguity;
  500. if (initialize)
  501. cmdline_parser_init (args_info);
  502. cmdline_parser_init (&local_args_info);
  503. optarg = 0;
  504. optind = 0;
  505. opterr = params->print_errors;
  506. optopt = '?';
  507. while (1)
  508. {
  509. int option_index = 0;
  510. static struct option long_options[] = {
  511. { "help", 0, NULL, 'h' },
  512. { "version", 0, NULL, 'V' },
  513. { "xdisplay", 1, NULL, 0 },
  514. { "nokeyboard", 0, NULL, 0 },
  515. { "bordersize", 1, NULL, 'b' },
  516. { "padding", 1, NULL, 'p' },
  517. { "tolerance", 1, NULL, 't' },
  518. { "gracetime", 1, NULL, 'g' },
  519. { "color", 1, NULL, 'c' },
  520. { "nodecorations", 0, NULL, 'n' },
  521. { "min", 1, NULL, 0 },
  522. { "max", 1, NULL, 0 },
  523. { "highlight", 0, NULL, 'l' },
  524. { "opengl", 0, NULL, 0 },
  525. { "magnify", 0, NULL, 0 },
  526. { "magstrength", 1, NULL, 0 },
  527. { "magpixels", 1, NULL, 0 },
  528. { "theme", 1, NULL, 0 },
  529. { "shader", 1, NULL, 0 },
  530. { "format", 1, NULL, 'f' },
  531. { 0, 0, 0, 0 }
  532. };
  533. c = getopt_long (argc, argv, "hVb:p:t:g:c:nlf:", long_options, &option_index);
  534. if (c == -1) break; /* Exit from `while (1)' loop. */
  535. switch (c)
  536. {
  537. case 'h': /* Print help and exit. */
  538. cmdline_parser_print_help ();
  539. cmdline_parser_free (&local_args_info);
  540. exit (EXIT_SUCCESS);
  541. case 'V': /* Print version and exit. */
  542. cmdline_parser_print_version ();
  543. cmdline_parser_free (&local_args_info);
  544. exit (EXIT_SUCCESS);
  545. case 'b': /* Set the selection rectangle's thickness. Does nothing when --highlight is enabled.. */
  546. if (update_arg( (void *)&(args_info->bordersize_arg),
  547. &(args_info->bordersize_orig), &(args_info->bordersize_given),
  548. &(local_args_info.bordersize_given), optarg, 0, "5", ARG_INT,
  549. check_ambiguity, override, 0, 0,
  550. "bordersize", 'b',
  551. additional_error))
  552. goto failure;
  553. break;
  554. case 'p': /* Set the padding size of the selection. Can be negative.. */
  555. if (update_arg( (void *)&(args_info->padding_arg),
  556. &(args_info->padding_orig), &(args_info->padding_given),
  557. &(local_args_info.padding_given), optarg, 0, "0", ARG_INT,
  558. check_ambiguity, override, 0, 0,
  559. "padding", 'p',
  560. additional_error))
  561. goto failure;
  562. break;
  563. case 't': /* How far in pixels the mouse can move after clicking and still be detected as a normal click instead of a click and drag. Setting this to 0 will disable window selections.. */
  564. if (update_arg( (void *)&(args_info->tolerance_arg),
  565. &(args_info->tolerance_orig), &(args_info->tolerance_given),
  566. &(local_args_info.tolerance_given), optarg, 0, "2", ARG_INT,
  567. check_ambiguity, override, 0, 0,
  568. "tolerance", 't',
  569. additional_error))
  570. goto failure;
  571. break;
  572. case 'g': /* Set the amount of time before slop will check for keyboard cancellations in seconds.. */
  573. if (update_arg( (void *)&(args_info->gracetime_arg),
  574. &(args_info->gracetime_orig), &(args_info->gracetime_given),
  575. &(local_args_info.gracetime_given), optarg, 0, "0.4", ARG_STRING,
  576. check_ambiguity, override, 0, 0,
  577. "gracetime", 'g',
  578. additional_error))
  579. goto failure;
  580. break;
  581. case 'c': /* Set the selection rectangle's color. Supports RGB or RGBA values.. */
  582. if (update_arg( (void *)&(args_info->color_arg),
  583. &(args_info->color_orig), &(args_info->color_given),
  584. &(local_args_info.color_given), optarg, 0, "0.5,0.5,0.5,1", ARG_STRING,
  585. check_ambiguity, override, 0, 0,
  586. "color", 'c',
  587. additional_error))
  588. goto failure;
  589. break;
  590. case 'n': /* Attempt to select child windows in order to avoid window decorations.. */
  591. if (update_arg((void *)&(args_info->nodecorations_flag), 0, &(args_info->nodecorations_given),
  592. &(local_args_info.nodecorations_given), optarg, 0, 0, ARG_FLAG,
  593. check_ambiguity, override, 1, 0, "nodecorations", 'n',
  594. additional_error))
  595. goto failure;
  596. break;
  597. case 'l': /* Instead of outlining selections, slop highlights it. This is only useful when --color is set to a transparent color.. */
  598. if (update_arg((void *)&(args_info->highlight_flag), 0, &(args_info->highlight_given),
  599. &(local_args_info.highlight_given), optarg, 0, 0, ARG_FLAG,
  600. check_ambiguity, override, 1, 0, "highlight", 'l',
  601. additional_error))
  602. goto failure;
  603. break;
  604. case 'f': /* Set the output format string. Format specifiers are %x, %y, %w, %h, %i, %g, and %c.. */
  605. if (update_arg( (void *)&(args_info->format_arg),
  606. &(args_info->format_orig), &(args_info->format_given),
  607. &(local_args_info.format_given), optarg, 0, "X=%x\nY=%y\nW=%w\nH=%h\nG=%g\nID=%i\nCancel=%c\n", ARG_STRING,
  608. check_ambiguity, override, 0, 0,
  609. "format", 'f',
  610. additional_error))
  611. goto failure;
  612. break;
  613. case 0: /* Long option with no short option */
  614. /* Sets the x display.. */
  615. if (strcmp (long_options[option_index].name, "xdisplay") == 0)
  616. {
  617. if (update_arg( (void *)&(args_info->xdisplay_arg),
  618. &(args_info->xdisplay_orig), &(args_info->xdisplay_given),
  619. &(local_args_info.xdisplay_given), optarg, 0, 0, ARG_STRING,
  620. check_ambiguity, override, 0, 0,
  621. "xdisplay", '-',
  622. additional_error))
  623. goto failure;
  624. }
  625. /* Disables the ability to cancel selections with the keyboard.. */
  626. else if (strcmp (long_options[option_index].name, "nokeyboard") == 0)
  627. {
  628. if (update_arg((void *)&(args_info->nokeyboard_flag), 0, &(args_info->nokeyboard_given),
  629. &(local_args_info.nokeyboard_given), optarg, 0, 0, ARG_FLAG,
  630. check_ambiguity, override, 1, 0, "nokeyboard", '-',
  631. additional_error))
  632. goto failure;
  633. }
  634. /* Set the minimum output of width or height values. This is useful to avoid outputting 0. Setting min and max to the same value disables drag selections.. */
  635. else if (strcmp (long_options[option_index].name, "min") == 0)
  636. {
  637. if (update_arg( (void *)&(args_info->min_arg),
  638. &(args_info->min_orig), &(args_info->min_given),
  639. &(local_args_info.min_given), optarg, 0, "0", ARG_INT,
  640. check_ambiguity, override, 0, 0,
  641. "min", '-',
  642. additional_error))
  643. goto failure;
  644. }
  645. /* Set the maximum output of width or height values. Setting min and max to the same value disables drag selections.. */
  646. else if (strcmp (long_options[option_index].name, "max") == 0)
  647. {
  648. if (update_arg( (void *)&(args_info->max_arg),
  649. &(args_info->max_orig), &(args_info->max_given),
  650. &(local_args_info.max_given), optarg, 0, "0", ARG_INT,
  651. check_ambiguity, override, 0, 0,
  652. "max", '-',
  653. additional_error))
  654. goto failure;
  655. }
  656. /* Enable hardware acceleration. Only works with modern systems that are also running a compositor.. */
  657. else if (strcmp (long_options[option_index].name, "opengl") == 0)
  658. {
  659. if (update_arg((void *)&(args_info->opengl_flag), 0, &(args_info->opengl_given),
  660. &(local_args_info.opengl_given), optarg, 0, 0, ARG_FLAG,
  661. check_ambiguity, override, 1, 0, "opengl", '-',
  662. additional_error))
  663. goto failure;
  664. }
  665. /* Display a magnifying glass when --opengl is also enabled.. */
  666. else if (strcmp (long_options[option_index].name, "magnify") == 0)
  667. {
  668. if (update_arg((void *)&(args_info->magnify_flag), 0, &(args_info->magnify_given),
  669. &(local_args_info.magnify_given), optarg, 0, 0, ARG_FLAG,
  670. check_ambiguity, override, 1, 0, "magnify", '-',
  671. additional_error))
  672. goto failure;
  673. }
  674. /* Sets how many times the magnification window size is multiplied.. */
  675. else if (strcmp (long_options[option_index].name, "magstrength") == 0)
  676. {
  677. if (update_arg( (void *)&(args_info->magstrength_arg),
  678. &(args_info->magstrength_orig), &(args_info->magstrength_given),
  679. &(local_args_info.magstrength_given), optarg, 0, "4", ARG_FLOAT,
  680. check_ambiguity, override, 0, 0,
  681. "magstrength", '-',
  682. additional_error))
  683. goto failure;
  684. }
  685. /* Sets how many pixels are displayed in the magnification. The less pixels the bigger the magnification.. */
  686. else if (strcmp (long_options[option_index].name, "magpixels") == 0)
  687. {
  688. if (update_arg( (void *)&(args_info->magpixels_arg),
  689. &(args_info->magpixels_orig), &(args_info->magpixels_given),
  690. &(local_args_info.magpixels_given), optarg, 0, "64", ARG_INT,
  691. check_ambiguity, override, 0, 0,
  692. "magpixels", '-',
  693. additional_error))
  694. goto failure;
  695. }
  696. /* Sets the theme of the selection, using textures from ~/.config/slop/ or /usr/share/.. */
  697. else if (strcmp (long_options[option_index].name, "theme") == 0)
  698. {
  699. if (update_arg( (void *)&(args_info->theme_arg),
  700. &(args_info->theme_orig), &(args_info->theme_given),
  701. &(local_args_info.theme_given), optarg, 0, "none", ARG_STRING,
  702. check_ambiguity, override, 0, 0,
  703. "theme", '-',
  704. additional_error))
  705. goto failure;
  706. }
  707. /* Sets the shader to load and use from ~/.config/slop/ or /usr/share/.. */
  708. else if (strcmp (long_options[option_index].name, "shader") == 0)
  709. {
  710. if (update_arg( (void *)&(args_info->shader_arg),
  711. &(args_info->shader_orig), &(args_info->shader_given),
  712. &(local_args_info.shader_given), optarg, 0, "simple", ARG_STRING,
  713. check_ambiguity, override, 0, 0,
  714. "shader", '-',
  715. additional_error))
  716. goto failure;
  717. }
  718. break;
  719. case '?': /* Invalid option. */
  720. /* `getopt_long' already printed an error message. */
  721. goto failure;
  722. default: /* bug: option not considered. */
  723. fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
  724. abort ();
  725. } /* switch */
  726. } /* while */
  727. cmdline_parser_release (&local_args_info);
  728. if ( error_occurred )
  729. return (EXIT_FAILURE);
  730. return 0;
  731. failure:
  732. cmdline_parser_release (&local_args_info);
  733. return (EXIT_FAILURE);
  734. }