getargv.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * signed getargv (signed argc, char const * argv [])
  11. *
  12. * symbol.h
  13. *
  14. * read one line from stdin; fill argv [] with fields from that
  15. * line; return the number of fields found; ignore blank lines
  16. * and script style comment lines; this implementation inserts
  17. * a program name at argv [0] to emulate a true argv [];
  18. *
  19. * Contributor(s):
  20. * Charles Maier <cmaier@qca.qualcomm.com>
  21. *
  22. *--------------------------------------------------------------------*/
  23. #ifndef GETARGV_SOURCE
  24. #define GETARGV_SOURCE
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <memory.h>
  28. #include "../tools/symbol.h"
  29. #include "../tools/chars.h"
  30. signed getargv (signed argc, char const * argv [])
  31. {
  32. extern char const * program_name;
  33. static char string [2048];
  34. char const ** argp = argv;
  35. char * sp = string;
  36. signed c = getc (stdin);
  37. memset (string, 0, sizeof (string));
  38. memset ((char **) (argv), 0, argc * sizeof (char const *));
  39. while (nobreak (c))
  40. {
  41. if (isspace (c))
  42. {
  43. do
  44. {
  45. c = getc (stdin);
  46. }
  47. while (isspace (c));
  48. }
  49. if (c == '#')
  50. {
  51. do
  52. {
  53. c = getc (stdin);
  54. }
  55. while (nobreak (c));
  56. c = getc (stdin);
  57. continue;
  58. }
  59. * argp++ = program_name;
  60. * argp++ = sp = string;
  61. while (nobreak (c))
  62. {
  63. if (c == '#')
  64. {
  65. do
  66. {
  67. c = getc (stdin);
  68. }
  69. while (nobreak (c));
  70. break;
  71. }
  72. if (isblank (c))
  73. {
  74. c = (char) (0);
  75. * argp = sp + 1;
  76. }
  77. else if (sp == * argp)
  78. {
  79. if ((signed) (argp - argv) < argc)
  80. {
  81. argp++;
  82. }
  83. }
  84. * sp++ = (char) (c);
  85. c = getc (stdin);
  86. }
  87. * argp = (char const *) (0);
  88. * sp = (char) (0);
  89. }
  90. return ((unsigned) (argp - argv));
  91. }
  92. /*====================================================================*
  93. *
  94. *--------------------------------------------------------------------*/
  95. #if 0
  96. char const * program_name = "program";
  97. #include <stdio.h>
  98. int main (int argc, char const * argv [])
  99. {
  100. char const * fields [16];
  101. signed count;
  102. while ((count = getargv (SIZEOF (fields), fields)))
  103. {
  104. signed field;
  105. for (field = 0; field < count; field++)
  106. {
  107. printf ("field[%d]=[%s]\n", field, fields [field]);
  108. }
  109. printf ("\n");
  110. }
  111. return (0);
  112. }
  113. #endif
  114. /*====================================================================*
  115. *
  116. *--------------------------------------------------------------------*/
  117. #endif