4 * Copyright (C) 2015 Alexander Andrejevic <theflash AT sdf DOT lonestar DOT org>
5 * Copyright (C) 2015 Jason Self <j@jxself.org>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>
31 enum { ZVERSION = 11, ZORKID, ZSERIAL };
33 static struct option const long_options[] =
35 { "help", no_argument, NULL, 'h' },
36 { "version", no_argument, NULL, 'V' },
37 { "output", required_argument, NULL, 'o' },
38 { "zversion", required_argument, NULL, ZVERSION },
39 { "zorkid", required_argument, NULL, ZORKID },
40 { "serial", required_argument, NULL, ZSERIAL },
46 int zversion; /* 0 - 8 */
47 int zorkid; /* 0 - 65535 */
48 char zserial[7]; /* YYMMDD */
51 void wrong_arg(const char *err, ...)
57 vfprintf(stderr, err, ap);
60 fprintf(stderr, "Try `" PACKAGE_NAME " --help' for more information.\n");
66 printf( PACKAGE_STRING "\n"
67 "License AGPLv3+: GNU AGPL version 3 or later\n"
68 "<http://gnu.org/licenses/agpl.html>\n"
69 "This is free software: you are free to change and redistribute it.\n"
70 "There is NO WARRANTY, to the extent permitted by law.\n"
75 void print_usage(int failed)
77 printf("Usage: " PACKAGE_NAME " [OPTION...] [FILES...]\n"
79 "--version Display program version and exit\n"
80 "--help Display this help\n"
82 "--zversion (accepts numbers 1 - 8, defaults to 1 if not specified)\n"
83 "--zorkid (integer between 0 and 65535, defaults to 0 if not specified)\n"
84 "--serial (six characters of ASCII, defaults to current date\n"
85 " in the form YYMMDD if not specified)\n"
90 void fill_zserial(void)
95 timeinfo = localtime(&t);
96 strftime (Config.zserial, sizeof(Config.zserial), "%y%m%d", timeinfo);
99 void fill_config(void)
106 void parse_intarg(int *dest, const char name[], int min, int max, int defval)
113 int n = atoi(optarg);
114 if (n >= min && n <= max)
119 wrong_arg("Wrong %s value %s, must be integer between %d and %d\n",
120 name, optarg, min, max);
123 void parse_zserial(void)
130 size_t n = strlen(optarg);
131 if (n == sizeof(Config.zserial) - 1)
134 while (*p && isalnum(*p))
136 if (!*p) /* ..optarg contains alphanumeric only? */
138 strncpy(Config.zserial, optarg, sizeof(Config.zserial));
142 wrong_arg("Wrong zserial value %s, must be 6 ascii characters\n", optarg);
145 int main(int argc, char *argv[], char *envp[])
147 const char *output_file = NULL;
153 while ((opt = getopt_long (argc, argv, "hVo:", long_options, NULL)) != -1)
162 if (output_file) wrong_arg("Output file must be given once\n");
163 output_file = argv[optind];
166 parse_intarg(&Config.zversion, "zversion", 1, 8, 1);
169 parse_intarg(&Config.zorkid, "zorkid", 0, 0xFFFF, 0);
179 // TODO: Everything :)
181 printf("Input files:\n");
182 for (i = optind; i < argc; i++)
183 printf("\t%s\n", argv[i]);
185 printf("Output file: %s\n\n", output_file ? output_file : "MISSING");
191 Config.zversion, Config.zorkid, Config.zserial