tool_parsecfg.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 (100*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;
  117. curlx_dyn_init(&buf, MAX_CONFIG_LINE_LENGTH);
  118. DEBUGASSERT(filename);
  119. while(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(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 in the line that may cause side-effects!\n",
  189. filename, lineno, option);
  190. }
  191. }
  192. if(!*param)
  193. /* do this so getparameter can check for required parameters.
  194. Otherwise it always thinks there's a parameter. */
  195. param = NULL;
  196. }
  197. #ifdef DEBUG_CONFIG
  198. fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
  199. #endif
  200. res = getparameter(option, param, NULL, &usedarg, global, operation);
  201. operation = global->last;
  202. if(!res && param && *param && !usedarg)
  203. /* we passed in a parameter that wasn't used! */
  204. res = PARAM_GOT_EXTRA_PARAMETER;
  205. if(res == PARAM_NEXT_OPERATION) {
  206. if(operation->url_list && operation->url_list->url) {
  207. /* Allocate the next config */
  208. operation->next = malloc(sizeof(struct OperationConfig));
  209. if(operation->next) {
  210. /* Initialise the newly created config */
  211. config_init(operation->next);
  212. /* Set the global config pointer */
  213. operation->next->global = global;
  214. /* Update the last operation pointer */
  215. global->last = operation->next;
  216. /* Move onto the new config */
  217. operation->next->prev = operation;
  218. operation = operation->next;
  219. }
  220. else
  221. res = PARAM_NO_MEM;
  222. }
  223. }
  224. if(res != PARAM_OK && res != PARAM_NEXT_OPERATION) {
  225. /* the help request isn't really an error */
  226. if(!strcmp(filename, "-")) {
  227. filename = "<stdin>";
  228. }
  229. if(res != PARAM_HELP_REQUESTED &&
  230. res != PARAM_MANUAL_REQUESTED &&
  231. res != PARAM_VERSION_INFO_REQUESTED &&
  232. res != PARAM_ENGINES_REQUESTED) {
  233. const char *reason = param2text(res);
  234. warnf(operation->global, "%s:%d: warning: '%s' %s\n",
  235. filename, lineno, option, reason);
  236. }
  237. }
  238. if(alloced_param)
  239. Curl_safefree(param);
  240. curlx_dyn_reset(&buf);
  241. }
  242. curlx_dyn_free(&buf);
  243. if(file != stdin)
  244. fclose(file);
  245. if(fileerror)
  246. rc = 1;
  247. }
  248. else
  249. rc = 1; /* couldn't open the file */
  250. free(pathalloc);
  251. return rc;
  252. }
  253. /*
  254. * Copies the string from line to the buffer at param, unquoting
  255. * backslash-quoted characters and NUL-terminating the output string.
  256. * Stops at the first non-backslash-quoted double quote character or the
  257. * end of the input string. param must be at least as long as the input
  258. * string. Returns the pointer after the last handled input character.
  259. */
  260. static const char *unslashquote(const char *line, char *param)
  261. {
  262. while(*line && (*line != '\"')) {
  263. if(*line == '\\') {
  264. char out;
  265. line++;
  266. /* default is to output the letter after the backslash */
  267. switch(out = *line) {
  268. case '\0':
  269. continue; /* this'll break out of the loop */
  270. case 't':
  271. out = '\t';
  272. break;
  273. case 'n':
  274. out = '\n';
  275. break;
  276. case 'r':
  277. out = '\r';
  278. break;
  279. case 'v':
  280. out = '\v';
  281. break;
  282. }
  283. *param++ = out;
  284. line++;
  285. }
  286. else
  287. *param++ = *line++;
  288. }
  289. *param = '\0'; /* always null-terminate */
  290. return line;
  291. }
  292. /*
  293. * Reads a line from the given file, ensuring is NUL terminated.
  294. */
  295. static bool my_get_line(FILE *fp, struct curlx_dynbuf *db,
  296. bool *error)
  297. {
  298. char buf[4096];
  299. *error = FALSE;
  300. do {
  301. /* fgets() returns s on success, and NULL on error or when end of file
  302. occurs while no characters have been read. */
  303. if(!fgets(buf, sizeof(buf), fp))
  304. /* only if there's data in the line, return TRUE */
  305. return curlx_dyn_len(db) ? TRUE : FALSE;
  306. if(curlx_dyn_add(db, buf)) {
  307. *error = TRUE; /* error */
  308. return FALSE; /* stop reading */
  309. }
  310. } while(!strchr(buf, '\n'));
  311. return TRUE; /* continue */
  312. }