/* fetch NOAA report */ int download_Metar(char *station) { CURL *curlhandle = NULL; CURLcode res; char url[URL_MAXSIZE]; char tmp[URL_MAXSIZE]; curlhandle = curl_easy_init(); if (!curlhandle) return 1; memset(tmp, 0x0, URL_MAXSIZE); if (getenv("METARURL") == NULL) { strncpy(tmp, METARURL, URL_MAXSIZE); } else { strncpy(tmp, getenv("METARURL"), URL_MAXSIZE); if (verbose) printf("Using environment variable METARURL: %s\n", tmp); } if (snprintf(url, URL_MAXSIZE, "%s/%s.TXT", tmp, strupc(station)) < 0) return 1; if (verbose) printf("Retrieving URL %s\n", url); curl_easy_setopt(curlhandle, CURLOPT_URL, url); curl_easy_setopt(curlhandle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, receiveData); memset(noaabuffer, 0x0, METAR_MAXSIZE); res = curl_easy_perform(curlhandle); curl_easy_cleanup(curlhandle); return 0; }
SYMTAB * env2symb (void) { SYMTAB *symtab; /* Allocated symbol table */ char *next_entry, /* Environment variable + value */ *equals; /* Position of '=' in string */ int string_nbr; /* Index into string table */ /* We create the table here, instead of passing through strt2symb(), since we have to ensure that environment variable names are stored in uppercase. Some systems (NT) return mixed-case names. */ symtab = sym_create_table (); if (symtab) { for (string_nbr = 0; environ [string_nbr]; string_nbr++) { next_entry = mem_strdup (environ [string_nbr]); equals = strchr (next_entry, '='); if (equals) { *equals = '\0'; /* Cut into two strings */ strupc (next_entry); sym_assume_symbol (symtab, next_entry, equals + 1); } mem_free (next_entry); } } return (symtab); }
MODULE get_user_command_input (THREAD *thread) { static struct { char *name; event_t event; } keywords [] = { { "LIST", list_event }, { "STATUS", status_event }, { "START", start_event }, { "PAUSE", pause_event }, { "STOP", stop_event }, { "HALT", halt_event }, { "EXIT", exit_event }, { "QUIT", exit_event }, { "HELP", help_event }, { "VERSION", version_event }, { "REFRESH", refresh_event }, { NULL, 0 } }; int keyword_nbr; /* If we got a command from the user, use it, else prompt */ if (command && strused (command)) { strncpy (input_line, command, LINE_MAX); input_line [LINE_MAX] = '\0'; /* Ensure delimited, if looong */ command = "exit"; /* Next time treat as Exit */ } else { /* Show syscli prompt and wait for user command */ printf ("syscli> "); fflush (stdout); if (fgets (input_line, LINE_MAX, stdin) == NULL) strclr (input_line); /* Treat EOF as empty */ } if (token_list) tok_free (token_list); token_list = tok_split (input_line); /* Get event corresponding to user command */ if (token_list [0] && *token_list [0]) { the_next_event = error_event; strupc (token_list [0]); for (keyword_nbr = 0; keywords [keyword_nbr].name; keyword_nbr++) if (streq (token_list [0], keywords [keyword_nbr].name)) { the_next_event = keywords [keyword_nbr].event; break; } /* Specific handling for stop command without arguments */ if (the_next_event == stop_event && token_list [1] == NULL) the_next_event = stop_all_event; } else the_next_event = empty_event; }
MODULE get_user_command_input (THREAD *thread) { static char input_line [LINE_MAX + 1]; static struct { char *name; event_t event; } keywords [] = { { "LIST", list_event }, { "START", start_event }, { "STOP", stop_event }, { "STATUS", status_event }, { "HALT", halt_event }, { "EXIT", exit_event }, { "QUIT", exit_event }, { "HELP", help_event }, { "VERSION", version_event }, { NULL, 0 } }; int keyword_nbr; /* If we got function arguments, use those, else prompt the user */ if (args && strused (args)) { strncpy (input_line, args, LINE_MAX); input_line [LINE_MAX] = '\0'; /* Ensure delimited, if looong */ args = "exit"; /* Next time treat as Exit */ } else { /* Show upmc prompt and wait for user command */ printf ("upmc> "); fflush (stdout); if (fgets (input_line, LINE_MAX, stdin) == NULL) strclr (input_line); /* Treat EOF as empty */ } if (token_list) tok_free (token_list); token_list = tok_split (input_line); /* Get event corresponding to user command */ if (token_list [0] && *token_list [0]) { the_next_event = error_event; strupc (token_list [0]); for (keyword_nbr = 0; keywords [keyword_nbr].name; keyword_nbr++) if (streq (token_list [0], keywords [keyword_nbr].name)) { the_next_event = keywords [keyword_nbr].event; break; } } else the_next_event = empty_event; }
int main(int argc, char * argv[]) { char * str ; str = "\t\tI'm a lumberkack and I'm OK " ; printf("lowercase: [%s]\n", strlwc(str)); printf("uppercase: [%s]\n", strupc(str)); printf("skipped : [%s]\n", strskp(str)); printf("cropped : [%s]\n", strcrop(str)); printf("stripped : [%s]\n", strstrip(str)); return 0 ; }
char * env_get_string ( const char *name, const char *default_value) { char *variable_name, *variable_value; variable_name = mem_strdup (name); variable_value = getenv (strupc (variable_name)); mem_free (variable_name); return (variable_value? variable_value: (char *) default_value); }
char * conv_date_pict ( long date, const char *picture) { static char *month_name [] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }, *day_name [] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }, formatted [FORMAT_MAX + 1]; /* Formatted return string */ int century, /* Century component of date */ year, /* Year component of date */ month, /* Month component of date */ day, /* Day component of date */ cursize; /* Size of current component */ char *dest, /* Store formatted data here */ ch, /* Next character in picture */ lastch = '0'; /* Last character we output */ long full_date = date; conv_reason = 0; /* No conversion errors so far */ /* Zero date is returned as empty string */ if (date == 0) { strclr (formatted); return (formatted); } default_century (&full_date); century = GET_CENTURY (full_date); year = GET_YEAR (full_date); month = GET_MONTH (full_date); day = GET_DAY (full_date); ASSERT (month > 0 && month <= 12); ASSERT (day > 0 && day <= 31); /* Scan through picture, converting each component */ dest = formatted; *dest = 0; /* string is empty */ while (*picture) { /* Get character and count number of occurences */ ch = *picture++; for (cursize = 1; *picture == ch; cursize++) picture++; switch (ch) { /* cc century 2 digits, 01-99 */ case 'c': if (cursize == 2) sprintf (dest, "%02d", century); break; /* y day of year, 1-366 */ /* yy year 2 digits, 00-99 */ /* yyyy year 4 digits, 0100-9999 */ case 'y': /* y = day of year */ if (cursize == 1) sprintf (dest, "%d", julian_date (full_date)); else if (cursize == 2) sprintf (dest, "%02d", year); else if (cursize == 4) sprintf (dest, "%02d%02d", century, year); break; /* m month, 1-12 */ /* mm month, 01-12 */ /* mmm month, 3 letters */ /* mmmm month, full name */ case 'm': if (cursize == 1) sprintf (dest, (isdigit (lastch)? "%2d": "%d"), month); else if (cursize == 2) sprintf (dest, "%02d", month); else if (cursize == 3) { memcpy (dest, month_name [month - 1], 3); dest [3] = 0; } else if (cursize == 4) strcpy (dest, month_name [month - 1]); break; /* MMM month, 3-letters, ucase */ /* MMMM month, full name, ucase */ case 'M': if (cursize == 3) { memcpy (dest, month_name [month - 1], 3); dest [3] = 0; strupc (dest); } else if (cursize == 4) { strcpy (dest, month_name [month - 1]); strupc (dest); } break; /* d day, 1-31 */ /* dd day, 01-31 */ /* ddd day of week, Sun-Sat */ /* dddd day of week, Sunday-Saturday */ case 'd': if (cursize == 1) sprintf (dest, (isdigit (lastch)? "%2d": "%d"), day); else if (cursize == 2) sprintf (dest, "%02d", day); else if (cursize == 3) { memcpy (dest, day_name [day_of_week (full_date)], 3); dest [3] = 0; } else if (cursize == 4) strcpy (dest, day_name [day_of_week (full_date)]); break; /* DDD day of week, SUN-SAT */ /* DDDD day of week, SUNDAY-SATURDAY */ case 'D': if (cursize == 3) { memcpy (dest, day_name [day_of_week (full_date)], 3); dest [3] = 0; strupc (dest); } else if (cursize == 4) { strcpy (dest, day_name [day_of_week (full_date)]); strupc (dest); } break; /* w day of week, 1-7 (1=Sunday) */ /* ww week of year, 1-53 */ case 'w': if (cursize == 1) sprintf (dest, "%d", day_of_week (full_date) + 1); else if (cursize == 2) sprintf (dest, "%d", week_of_year (full_date)); break; /* q year quarter, 1-4 */ case 'q': if (cursize == 1) sprintf (dest, "%d", year_quarter (full_date)); break; /* \x literal character x */ case '\\': ch = *picture++; } if (*dest) /* If something was output, */ while (*dest) /* skip to end of string */ dest++; else while (cursize--) /* Else output ch once or more */ *dest++ = ch; /* and bump dest pointer */ lastch = *(dest - 1); /* Get previous character */ *dest = 0; /* Terminate the string nicely */ } return (formatted); }
int pretty_print (VALUE *result, RESULT_NODE *pretty, char *example, int space, char **error_text) { char *tokens, *token, *c, *newstring, *oldpos, *newpos; Bool use_example, new_word; if (error_text) *error_text = NULL; /* No errors yet */ string_value (result); use_example = (example != NULL); if (pretty) { if (strlen (string_value (& pretty-> value)) == 0) use_example = FALSE; tokens = mem_strdup (pretty-> value. s); token = strtok (tokens, ", "); while (token) { strlwc (token); if (streq (token, "lower")) { use_example = FALSE; strlwc (result-> s); } else if (streq (token, "upper")) { use_example = FALSE; strupc (result-> s); } else if (streq (token, "camel") || streq (token, "pascal")) { use_example = FALSE; oldpos = result->s; newpos = result->s; new_word = streq (token, "pascal") ? TRUE: FALSE; while (*oldpos) { if (!isalnum (*oldpos)) new_word = TRUE; else if (new_word) { *newpos = toupper (*oldpos); newpos++; new_word = FALSE; } else { *newpos = *oldpos; newpos++; } oldpos++; } *newpos = 0; } else if (streq (token, "no")) use_example = FALSE; else if (streq (token, "neat")) { use_example = FALSE; strneat (result-> s); } else if (streq (token, "c")) { c = result-> s; if (*c && !isalpha (*c)) *c = '_'; while (*c) { if (!(isalpha (*c) || isdigit (*c))) *c = '_'; c++; } } else if (streq (token, "cobol")) { c = result-> s; if (*c && !isalpha (*c)) *c = '-'; while (*c) { if (!(isalpha (*c) || isdigit (*c))) *c = '-'; c++; } } else if (streq (token, "justify")) { newstring = strjustify (result-> s, space); mem_free (result-> s); result-> s = newstring; } else if (streq (token, "left")) { newstring = strleft (result-> s); mem_free (result-> s); result-> s = newstring; } else if (streq (token, "block")) result-> type = TYPE_BLOCK; else if (error_text) { snprintf (error_buffer, LINE_MAX, "Unknown pretty-print modifier: %s", token); mem_free (tokens); *error_text = error_buffer; return -1; } token = strtok (NULL, ", "); } mem_free (tokens); } if ((use_example) && (strlen (example) > 1)) { c = example; if (isupper (*c)) while ((isupper (*c) || !isalpha (*c)) && (*c)) c++; if (*c == 0) strupc (result-> s); else if (c == example + 1) { if (islower (*c)) { while ((islower (*c) || !isalpha (*c)) && (*c)) c++; if (!isupper (*c)) strneat (result-> s); } } else if (c == example) { if (islower (*c)) while ((islower (*c) || !isalpha (*c)) && (*c)) c++; if (*c == 0) strlwc (result-> s); } } return 0; }