tool_parsecfg.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include "tool_setup.h"
  25. #define ENABLE_CURLX_PRINTF
  26. /* use our own printf() functions */
  27. #include "curlx.h"
  28. #include "tool_cfgable.h"
  29. #include "tool_getparam.h"
  30. #include "tool_helpers.h"
  31. #include "tool_findfile.h"
  32. #include "tool_msgs.h"
  33. #include "tool_parsecfg.h"
  34. #include "dynbuf.h"
  35. #include "memdebug.h" /* keep this as LAST include */
  36. /* only acknowledge colon or equals as separators if the option was not
  37. specified with an initial dash! */
  38. #define ISSEP(x,dash) (!dash && (((x) == '=') || ((x) == ':')))
  39. static const char *unslashquote(const char *line, char *param);
  40. #define MAX_CONFIG_LINE_LENGTH (10*1024*1024)
  41. static bool my_get_line(FILE *fp, struct curlx_dynbuf *, bool *error);
  42. #ifdef _WIN32
  43. static FILE *execpath(const char *filename, char **pathp)
  44. {
  45. static char filebuffer[512];
  46. /* Get the filename of our executable. GetModuleFileName is already declared
  47. * via inclusions done in setup header file. We assume that we are using
  48. * the ASCII version here.
  49. */
  50. unsigned long len = GetModuleFileNameA(0, filebuffer, sizeof(filebuffer));
  51. if(len > 0 && len < sizeof(filebuffer)) {
  52. /* We got a valid filename - get the directory part */
  53. char *lastdirchar = strrchr(filebuffer, '\\');
  54. if(lastdirchar) {
  55. size_t remaining;
  56. *lastdirchar = 0;
  57. /* If we have enough space, build the RC filename */
  58. remaining = sizeof(filebuffer) - strlen(filebuffer);
  59. if(strlen(filename) < remaining - 1) {
  60. FILE *f;
  61. msnprintf(lastdirchar, remaining, "%s%s", DIR_CHAR, filename);
  62. *pathp = filebuffer;
  63. f = fopen(filebuffer, FOPEN_READTEXT);
  64. return f;
  65. }
  66. }
  67. }
  68. return NULL;
  69. }
  70. #endif
  71. /* return 0 on everything-is-fine, and non-zero otherwise */
  72. int parseconfig(const char *filename, struct GlobalConfig *global)
  73. {
  74. FILE *file = NULL;
  75. bool usedarg = FALSE;
  76. int rc = 0;
  77. struct OperationConfig *operation = global->last;
  78. char *pathalloc = NULL;
  79. if(!filename) {
  80. /* NULL means load .curlrc from homedir! */
  81. char *curlrc = findfile(".curlrc", CURLRC_DOTSCORE);
  82. if(curlrc) {
  83. file = fopen(curlrc, FOPEN_READTEXT);
  84. if(!file) {
  85. free(curlrc);
  86. return 1;
  87. }
  88. filename = pathalloc = curlrc;
  89. }
  90. #ifdef _WIN32 /* Windows */
  91. else {
  92. char *fullp;
  93. /* check for .curlrc then _curlrc in the dir of the executable */
  94. file = execpath(".curlrc", &fullp);
  95. if(!file)
  96. file = execpath("_curlrc", &fullp);
  97. if(file)
  98. /* this is the filename we read from */
  99. filename = fullp;
  100. }
  101. #endif
  102. }
  103. else {
  104. if(strcmp(filename, "-"))
  105. file = fopen(filename, FOPEN_READTEXT);
  106. else
  107. file = stdin;
  108. }
  109. if(file) {
  110. char *line;
  111. char *option;
  112. char *param;
  113. int lineno = 0;
  114. bool dashed_option;
  115. struct curlx_dynbuf buf;
  116. bool fileerror = FALSE;
  117. curlx_dyn_init(&buf, MAX_CONFIG_LINE_LENGTH);
  118. DEBUGASSERT(filename);
  119. while(!rc && my_get_line(file, &buf, &fileerror)) {
  120. int res;
  121. bool alloced_param = FALSE;
  122. lineno++;
  123. line = curlx_dyn_ptr(&buf);
  124. if(!line) {
  125. rc = 1; /* out of memory */
  126. break;
  127. }
  128. /* line with # in the first non-blank column is a comment! */
  129. while(*line && ISSPACE(*line))
  130. line++;
  131. switch(*line) {
  132. case '#':
  133. case '/':
  134. case '\r':
  135. case '\n':
  136. case '*':
  137. case '\0':
  138. curlx_dyn_reset(&buf);
  139. continue;
  140. }
  141. /* the option keywords starts here */
  142. option = line;
  143. /* the option starts with a dash? */
  144. dashed_option = option[0]=='-'?TRUE:FALSE;
  145. while(*line && !ISSPACE(*line) && !ISSEP(*line, dashed_option))
  146. line++;
  147. /* ... and has ended here */
  148. if(*line)
  149. *line++ = '\0'; /* null-terminate, we have a local copy of the data */
  150. #ifdef DEBUG_CONFIG
  151. fprintf(tool_stderr, "GOT: %s\n", option);
  152. #endif
  153. /* pass spaces and separator(s) */
  154. while(*line && (ISSPACE(*line) || ISSEP(*line, dashed_option)))
  155. line++;
  156. /* the parameter starts here (unless quoted) */
  157. if(*line == '\"') {
  158. /* quoted parameter, do the quote dance */
  159. line++;
  160. param = malloc(strlen(line) + 1); /* parameter */
  161. if(!param) {
  162. /* out of memory */
  163. rc = 1;
  164. break;
  165. }
  166. alloced_param = TRUE;
  167. (void)unslashquote(line, param);
  168. }
  169. else {
  170. param = line; /* parameter starts here */
  171. while(*line && !ISSPACE(*line))
  172. line++;
  173. if(*line) {
  174. *line = '\0'; /* null-terminate */
  175. /* to detect mistakes better, see if there's data following */
  176. line++;
  177. /* pass all spaces */
  178. while(*line && ISSPACE(*line))
  179. line++;
  180. switch(*line) {
  181. case '\0':
  182. case '\r':
  183. case '\n':
  184. case '#': /* comment */
  185. break;
  186. default:
  187. warnf(operation->global, "%s:%d: warning: '%s' uses unquoted "
  188. "whitespace", filename, lineno, option);
  189. warnf(operation->global, "This may cause side-effects. "
  190. "Consider using double quotes?");
  191. }
  192. }
  193. if(!*param)
  194. /* do this so getparameter can check for required parameters.
  195. Otherwise it always thinks there's a parameter. */
  196. param = NULL;
  197. }
  198. #ifdef DEBUG_CONFIG
  199. fprintf(tool_stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
  200. #endif
  201. res = getparameter(option, param, NULL, &usedarg, global, operation);
  202. operation = global->last;
  203. if(!res && param && *param && !usedarg)
  204. /* we passed in a parameter that wasn't used! */
  205. res = PARAM_GOT_EXTRA_PARAMETER;
  206. if(res == PARAM_NEXT_OPERATION) {
  207. if(operation->url_list && operation->url_list->url) {
  208. /* Allocate the next config */
  209. operation->next = malloc(sizeof(struct OperationConfig));
  210. if(operation->next) {
  211. /* Initialise the newly created config */
  212. config_init(operation->next);
  213. /* Set the global config pointer */
  214. operation->next->global = global;
  215. /* Update the last operation pointer */
  216. global->last = operation->next;
  217. /* Move onto the new config */
  218. operation->next->prev = operation;
  219. operation = operation->next;
  220. }
  221. else
  222. res = PARAM_NO_MEM;
  223. }
  224. }
  225. if(res != PARAM_OK && res != PARAM_NEXT_OPERATION) {
  226. /* the help request isn't really an error */
  227. if(!strcmp(filename, "-")) {
  228. filename = "<stdin>";
  229. }
  230. if(res != PARAM_HELP_REQUESTED &&
  231. res != PARAM_MANUAL_REQUESTED &&
  232. res != PARAM_VERSION_INFO_REQUESTED &&
  233. res != PARAM_ENGINES_REQUESTED) {
  234. const char *reason = param2text(res);
  235. errorf(operation->global, "%s:%d: '%s' %s",
  236. filename, lineno, option, reason);
  237. rc = res;
  238. }
  239. }
  240. if(alloced_param)
  241. Curl_safefree(param);
  242. curlx_dyn_reset(&buf);
  243. }
  244. curlx_dyn_free(&buf);
  245. if(file != stdin)
  246. fclose(file);
  247. if(fileerror)
  248. rc = 1;
  249. }
  250. else
  251. rc = 1; /* couldn't open the file */
  252. free(pathalloc);
  253. return rc;
  254. }
  255. /*
  256. * Copies the string from line to the buffer at param, unquoting
  257. * backslash-quoted characters and NUL-terminating the output string.
  258. * Stops at the first non-backslash-quoted double quote character or the
  259. * end of the input string. param must be at least as long as the input
  260. * string. Returns the pointer after the last handled input character.
  261. */
  262. static const char *unslashquote(const char *line, char *param)
  263. {
  264. while(*line && (*line != '\"')) {
  265. if(*line == '\\') {
  266. char out;
  267. line++;
  268. /* default is to output the letter after the backslash */
  269. switch(out = *line) {
  270. case '\0':
  271. continue; /* this'll break out of the loop */
  272. case 't':
  273. out = '\t';
  274. break;
  275. case 'n':
  276. out = '\n';
  277. break;
  278. case 'r':
  279. out = '\r';
  280. break;
  281. case 'v':
  282. out = '\v';
  283. break;
  284. }
  285. *param++ = out;
  286. line++;
  287. }
  288. else
  289. *param++ = *line++;
  290. }
  291. *param = '\0'; /* always null-terminate */
  292. return line;
  293. }
  294. /*
  295. * Reads a line from the given file, ensuring is NUL terminated.
  296. */
  297. static bool my_get_line(FILE *fp, struct curlx_dynbuf *db,
  298. bool *error)
  299. {
  300. char buf[4096];
  301. *error = FALSE;
  302. do {
  303. /* fgets() returns s on success, and NULL on error or when end of file
  304. occurs while no characters have been read. */
  305. if(!fgets(buf, sizeof(buf), fp))
  306. /* only if there's data in the line, return TRUE */
  307. return curlx_dyn_len(db) ? TRUE : FALSE;
  308. if(curlx_dyn_add(db, buf)) {
  309. *error = TRUE; /* error */
  310. return FALSE; /* stop reading */
  311. }
  312. } while(!strchr(buf, '\n'));
  313. return TRUE; /* continue */
  314. }