tool_parsecfg.c 10 KB

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