seq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*++
  2. Copyright (c) 2016 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. seq.c
  5. Abstract:
  6. This module implements the seq (sequence) utility, which simply prints out
  7. a sequence of numbers.
  8. Author:
  9. Evan Green 9-May-2016
  10. Environment:
  11. POSIX
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <minoca/lib/types.h>
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include <errno.h>
  20. #include <getopt.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "swlib.h"
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. #define SEQ_VERSION_MAJOR 1
  28. #define SEQ_VERSION_MINOR 0
  29. #define SEQ_USAGE \
  30. "usage: seq [options] [[first] increment] last\n" \
  31. "The seq utility prints a sequence of numbers between the given range.\n" \
  32. "Options are:\n" \
  33. " -f, --format=format -- Specify the printf-style format to print " \
  34. "values in.\n" \
  35. " -s, --separator=string -- Specify the separator (default newline).\n" \
  36. " -w, --equal-width -- Pad with leading zeros so all values have the \n" \
  37. " same width.\n" \
  38. " --help -- Show this help text and exit.\n" \
  39. " --version -- Print the application version information and exit.\n"
  40. //
  41. // No permutations of arguments are allowed because something like -1 might be
  42. // specified as an argument.
  43. //
  44. #define SEQ_OPTIONS_STRING "+f:s:whV"
  45. //
  46. // ------------------------------------------------------ Data Type Definitions
  47. //
  48. //
  49. // ----------------------------------------------- Internal Function Prototypes
  50. //
  51. BOOL
  52. SeqCheckFormat (
  53. PSTR Format
  54. );
  55. //
  56. // -------------------------------------------------------------------- Globals
  57. //
  58. struct option SeqLongOptions[] = {
  59. {"format", no_argument, 0, 'f'},
  60. {"separator", no_argument, 0, 's'},
  61. {"equal-width", no_argument, 0, 'w'},
  62. {"help", no_argument, 0, 'h'},
  63. {"version", no_argument, 0, 'V'},
  64. {NULL, 0, 0, 0},
  65. };
  66. //
  67. // ------------------------------------------------------------------ Functions
  68. //
  69. INT
  70. SeqMain (
  71. INT ArgumentCount,
  72. CHAR **Arguments
  73. )
  74. /*++
  75. Routine Description:
  76. This routine is the main entry point for the seq utility.
  77. Arguments:
  78. ArgumentCount - Supplies the number of command line arguments the program
  79. was invoked with.
  80. Arguments - Supplies a tokenized array of command line arguments.
  81. Return Value:
  82. Returns an integer exit code. 0 for success, nonzero otherwise.
  83. --*/
  84. {
  85. PSTR AfterScan;
  86. PSTR Argument;
  87. INT ArgumentIndex;
  88. double Current;
  89. INT CurrentFractionDigits;
  90. PSTR CurrentSeparator;
  91. INT CurrentWidth;
  92. PSTR Decimal;
  93. double End;
  94. BOOL EqualWidth;
  95. PSTR Format;
  96. INT FractionDigits;
  97. double Increment;
  98. INT Index;
  99. INT LastCheck;
  100. INT Option;
  101. PSTR Separator;
  102. double Start;
  103. int Status;
  104. INT Width;
  105. EqualWidth = FALSE;
  106. Format = NULL;
  107. Separator = "\n";
  108. Start = 1;
  109. Increment = 1;
  110. //
  111. // Process the control arguments.
  112. //
  113. while (TRUE) {
  114. Option = getopt_long(ArgumentCount,
  115. Arguments,
  116. SEQ_OPTIONS_STRING,
  117. SeqLongOptions,
  118. NULL);
  119. if (Option == -1) {
  120. break;
  121. }
  122. if ((Option == '?') || (Option == ':')) {
  123. //
  124. // Watch out for the possibility of a negative number as the first
  125. // argument.
  126. //
  127. if ((optind > 1) && (Arguments[optind - 1][0] == '-') &&
  128. (isdigit(Arguments[optind - 1][1]))) {
  129. optind -= 1;
  130. break;
  131. }
  132. Status = 1;
  133. goto MainEnd;
  134. }
  135. switch (Option) {
  136. case 'f':
  137. Format = optarg;
  138. if (SeqCheckFormat(Format) == FALSE) {
  139. SwPrintError(0, Format, "Invalid format string");
  140. Status = 1;
  141. goto MainEnd;
  142. }
  143. break;
  144. case 's':
  145. Separator = optarg;
  146. break;
  147. case 'w':
  148. EqualWidth = TRUE;
  149. break;
  150. case 'V':
  151. SwPrintVersion(SEQ_VERSION_MAJOR, SEQ_VERSION_MINOR);
  152. return 1;
  153. case 'h':
  154. printf(SEQ_USAGE);
  155. return 1;
  156. default:
  157. assert(FALSE);
  158. Status = 1;
  159. goto MainEnd;
  160. }
  161. }
  162. Status = 1;
  163. if ((Format != NULL) && (EqualWidth != FALSE)) {
  164. SwPrintError(0, NULL, "Cannot have -f and -w together");
  165. goto MainEnd;
  166. }
  167. ArgumentIndex = optind;
  168. if (ArgumentIndex >= ArgumentCount) {
  169. SwPrintError(0, NULL, "Argument expected");
  170. goto MainEnd;
  171. }
  172. LastCheck = ArgumentCount - 1;
  173. //
  174. // Get the end.
  175. //
  176. Argument = Arguments[ArgumentCount - 1];
  177. End = strtod(Argument, &AfterScan);
  178. if (*AfterScan != '\0') {
  179. SwPrintError(0, Argument, "Invalid value");
  180. goto MainEnd;
  181. }
  182. //
  183. // If there are more arguments, then there's a start. If there's a third
  184. // argument, there's an increment. Any more is an error.
  185. //
  186. if (ArgumentIndex + 1 < ArgumentCount) {
  187. //
  188. // For some reason the ending value isn't checked for width if there's
  189. // a start and/or increment.
  190. //
  191. LastCheck = ArgumentCount - 2;
  192. Argument = Arguments[ArgumentIndex];
  193. Start = strtod(Argument, &AfterScan);
  194. if (*AfterScan != '\0') {
  195. SwPrintError(0, Argument, "Invalid value");
  196. goto MainEnd;
  197. }
  198. if (ArgumentIndex + 2 < ArgumentCount) {
  199. if (ArgumentIndex + 3 < ArgumentCount) {
  200. SwPrintError(0, NULL, "Too many arguments");
  201. goto MainEnd;
  202. }
  203. Argument = Arguments[ArgumentIndex + 1];
  204. Increment = strtod(Argument, &AfterScan);
  205. if (*AfterScan != '\0') {
  206. SwPrintError(0, Argument, "Invalid value");
  207. goto MainEnd;
  208. }
  209. }
  210. }
  211. //
  212. // Figure out the proper width to print.
  213. //
  214. Width = 0;
  215. FractionDigits = 0;
  216. while (ArgumentIndex <= LastCheck) {
  217. Argument = Arguments[ArgumentIndex];
  218. Decimal = strchr(Argument, '.');
  219. if (Decimal == NULL) {
  220. CurrentWidth = strlen(Argument);
  221. CurrentFractionDigits = 0;
  222. } else {
  223. CurrentWidth = Decimal - Argument;
  224. CurrentFractionDigits = strlen(Decimal);
  225. }
  226. if (Width < CurrentWidth) {
  227. Width = CurrentWidth;
  228. }
  229. if (FractionDigits < CurrentFractionDigits) {
  230. FractionDigits = CurrentFractionDigits;
  231. }
  232. ArgumentIndex += 1;
  233. }
  234. if (FractionDigits != 0) {
  235. FractionDigits -= 1;
  236. if (FractionDigits != 0) {
  237. Width += FractionDigits + 1;
  238. }
  239. }
  240. if (EqualWidth == FALSE) {
  241. Width = 0;
  242. }
  243. Status = 0;
  244. Index = 0;
  245. Current = Start;
  246. CurrentSeparator = "";
  247. while (TRUE) {
  248. if (Increment >= 0) {
  249. if (Current > End) {
  250. break;
  251. }
  252. } else {
  253. if (Current < End) {
  254. break;
  255. }
  256. }
  257. //
  258. // Print with the given format if supplied, or the default format
  259. // otherwise.
  260. //
  261. if (Format != NULL) {
  262. if (printf("%s", CurrentSeparator) < 0) {
  263. Status = 1;
  264. break;
  265. }
  266. if (printf(Format, Current) < 0) {
  267. Status = 1;
  268. break;
  269. }
  270. } else {
  271. Status = printf("%s%0*.*f",
  272. CurrentSeparator,
  273. Width,
  274. FractionDigits,
  275. Current);
  276. if (Status < 0) {
  277. Status = 1;
  278. break;
  279. }
  280. }
  281. CurrentSeparator = Separator;
  282. Index += 1;
  283. Current = Start + (Index * Increment);
  284. }
  285. if (Index != 0) {
  286. printf("\n");
  287. }
  288. MainEnd:
  289. return Status;
  290. }
  291. //
  292. // --------------------------------------------------------- Internal Functions
  293. //
  294. BOOL
  295. SeqCheckFormat (
  296. PSTR Format
  297. )
  298. /*++
  299. Routine Description:
  300. This routine ensures the given format string is correct for a double
  301. argument.
  302. Arguments:
  303. Format - Supplies a pointer to the potential format string.
  304. Return Value:
  305. TRUE if the format is okay.
  306. FALSE if the format string is invalid.
  307. --*/
  308. {
  309. INT Specifiers;
  310. Specifiers = 0;
  311. while (*Format != '\0') {
  312. //
  313. // Skip anything that's not a format specifier.
  314. //
  315. if (*Format != '%') {
  316. Format += 1;
  317. continue;
  318. }
  319. Format += 1;
  320. //
  321. // Skip a literal percent.
  322. //
  323. if (*Format == '%') {
  324. Format += 1;
  325. continue;
  326. }
  327. //
  328. // Only one specifier is allowed.
  329. //
  330. Specifiers += 1;
  331. if (Specifiers > 1) {
  332. return FALSE;
  333. }
  334. //
  335. // Swallow the flags.
  336. //
  337. while ((*Format == '\'') || (*Format == '-') || (*Format == '+') ||
  338. (*Format == ' ') || (*Format == '#') || (*Format == '0')) {
  339. Format += 1;
  340. }
  341. //
  342. // Swallow the field width.
  343. //
  344. while (isdigit(*Format)) {
  345. Format += 1;
  346. }
  347. //
  348. // Swallow the precision.
  349. //
  350. if (*Format == '.') {
  351. Format += 1;
  352. while (isdigit(*Format)) {
  353. Format += 1;
  354. }
  355. }
  356. //
  357. // Now it had better be a specifier.
  358. //
  359. if ((*Format != 'f') && (*Format != 'F') && (*Format != 'e') &&
  360. (*Format != 'E') && (*Format != 'g') && (*Format != 'G') &&
  361. (*Format != 'A') && (*Format != 'a')) {
  362. return FALSE;
  363. }
  364. Format += 1;
  365. }
  366. return TRUE;
  367. }