curlcl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. *
  24. ***************************************************************************/
  25. /* CL interface program to curl cli tool. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <milib.h>
  29. #include <miptrnam.h>
  30. #include <mih/callpgmv.h>
  31. #ifndef CURLPGM
  32. #define CURLPGM "CURL"
  33. #endif
  34. /* Variable-length string, with 16-bit length. */
  35. struct vary2 {
  36. short len;
  37. char string[5000];
  38. };
  39. /* Arguments from CL command. */
  40. struct arguments {
  41. char *pgm; /* Program name. */
  42. struct vary2 *cmdargs; /* Command line arguments. */
  43. };
  44. static int
  45. is_ifs(char c)
  46. {
  47. return c == ' ' || c == '\t' || c == '\r' || c == '\n';
  48. }
  49. static int
  50. parse_command_line(const char *cmdargs, size_t len,
  51. size_t *argc, char **argv,
  52. size_t *argsize, char *argbuf)
  53. {
  54. const char *endline = cmdargs + len;
  55. char quote = '\0';
  56. int inarg = 0;
  57. *argc = 0;
  58. *argsize = 0;
  59. while(cmdargs < endline) {
  60. char c = *cmdargs++;
  61. if(!inarg) {
  62. /* Skip argument separator. */
  63. if(is_ifs(c))
  64. continue;
  65. /* Start a new argument. */
  66. ++*argc;
  67. if(argv)
  68. *argv++ = argbuf;
  69. inarg = 1;
  70. }
  71. /* Check for quoting end. */
  72. if(quote && quote == c) {
  73. quote = '\0';
  74. continue;
  75. }
  76. /* Check for backslash-escaping. */
  77. if(quote != '\'' && c == '\\') {
  78. if(cmdargs >= endline) {
  79. fputs("Trailing backslash in command\n", stderr);
  80. return -1;
  81. }
  82. c = *cmdargs++;
  83. }
  84. else if(!quote && is_ifs(c)) { /* Check for end of argument. */
  85. inarg = 0;
  86. c = '\0'; /* Will store a string terminator. */
  87. }
  88. /* Store argument character and count it. */
  89. if(argbuf)
  90. *argbuf++ = c;
  91. ++*argsize;
  92. }
  93. if(quote) {
  94. fprintf(stderr, "Unterminated quote: %c\n", quote);
  95. return -1;
  96. }
  97. /* Terminate last argument. */
  98. if(inarg) {
  99. if(argbuf)
  100. *argbuf = '\0';
  101. ++*argsize;
  102. }
  103. /* Terminate argument list. */
  104. if(argv)
  105. *argv = NULL;
  106. return 0;
  107. }
  108. int
  109. main(int argsc, struct arguments *args)
  110. {
  111. size_t argc;
  112. char **argv;
  113. size_t argsize;
  114. int i;
  115. int exitcode;
  116. char library[11];
  117. /* Extract current program library name. */
  118. for(i = 0; i < 10; i++) {
  119. char c = args->pgm[i];
  120. if(!c || c == '/')
  121. break;
  122. library[i] = c;
  123. }
  124. library[i] = '\0';
  125. /* Measure arguments size. */
  126. exitcode = parse_command_line(args->cmdargs->string, args->cmdargs->len,
  127. &argc, NULL, &argsize, NULL);
  128. if(!exitcode) {
  129. /* Allocate space for parsed arguments. */
  130. argv = (char **) malloc((argc + 1) * sizeof(*argv) + argsize);
  131. if(!argv) {
  132. fputs("Memory allocation error\n", stderr);
  133. exitcode = -2;
  134. }
  135. else {
  136. _SYSPTR pgmptr = rslvsp(WLI_PGM, (char *) CURLPGM, library, _AUTH_NONE);
  137. _LU_Work_Area_T *luwrka = (_LU_Work_Area_T *) _LUWRKA();
  138. parse_command_line(args->cmdargs->string, args->cmdargs->len,
  139. &argc, argv, &argsize, (char *) (argv + argc + 1));
  140. /* Call program. */
  141. _CALLPGMV((void *) &pgmptr, argv, argc);
  142. exitcode = luwrka->LU_RC;
  143. free(argv);
  144. }
  145. }
  146. return exitcode;
  147. }