static char * consume_argument(struct argparse *ap) { char *ret = get_argument(ap); delete_argument(ap); return ret; }
int get_data(struct DESKTOP_ENTRY *data, char *path) { FILE *fp; char s[3000]; int len; bool is_link = false, is_app = false; int i; int result = 0; fp = fopen(path, "r"); if (fp == NULL) return 0; while (fgets(s, 3000, fp) != NULL) { /* chomp */ len = strlen(s); i = 2; while (i > 0) { if (s[len - i] == '\n' || s[len - i] == '\r') { s[len - i] = '\0'; break; } i--; } len -= i; /* Check the file is actually a .desktop file */ if (strstr(s, "[InternetShortcut]") != NULL) { is_link = true; is_app = false; data->type = LINK; result = 1; } if (strstr(s, "[Desktop Entry]") != NULL && !is_link) { is_app = true; data->type = APPLICATION; result = 1; } if (is_link) { /* Copy the URL found in the file */ if (strncmp(s, "URL=", 4) == 0) { data->url = html_encode(s + 4); } } else if (is_app) { if (strncmp(s, "Name=", 5) == 0 && data->name == NULL) { data->name = html_encode(s + 5); } else if (strncmp(s, "Exec=", 5) == 0 && data->command == NULL) { char *temp = html_encode(s + 5); data->command = delete_argument(temp); free(temp); } else if (strncmp(s, "Comment[ja]=", 12) == 0) { if (data->comment != NULL) free(data->comment); data->comment = html_encode(s + 12); } else if (strncmp(s, "Comment=", 8) == 0 && data->comment == NULL) { data->comment = html_encode(s + 8); } else if (strncmp(s, "Terminal=", 9) == 0) { for (i = 0; i < strlen(s); i++) { s[i] = tolower(s[i]); } if (strstr(s, "true") != NULL) { data->has_gui = false; } else if (strstr(s, "false") != NULL) { data->has_gui = true; } } } } fclose(fp); if (data->name == NULL) { char *a = strrchr(path, '/') + 1; char *b = strrchr(path, '.'); data->name = malloc(b - a + 1); strncpy(data->name, a, b - a); data->name[b - a] = '\0'; } if (result == 1 && data->type == APPLICATION) { char *fn = strrchr(path, '/') + 1; char *ex = strrchr(path, '.'); len = ex - fn; data->icon = malloc(len + 5); char *a = data->icon, *b = fn; while (*b != '\0' && b < ex) { if (*b == ' ') { *a = '-'; } else { *a = tolower(*b); } a++; b++; } *a = '\0'; } return result; }
struct pb_Parameters * pb_ReadParameters(int *_argc, char **argv) { char *err_message; struct argparse ap; struct pb_Parameters *ret = (struct pb_Parameters *)malloc(sizeof(struct pb_Parameters)); /* Initialize the parameters structure */ ret->outFile = NULL; ret->inpFiles = (char **)malloc(sizeof(char *)); ret->inpFiles[0] = NULL; /* Each argument */ initialize_argparse(&ap, *_argc, argv); while(!is_end_of_arguments(&ap)) { char *arg = get_argument(&ap); /* Single-character flag */ if ((arg[0] == '-') && (arg[1] != 0) && (arg[2] == 0)) { delete_argument(&ap); /* This argument is consumed here */ switch(arg[1]) { case 'o': /* Output file name */ if (is_end_of_arguments(&ap)) { err_message = "Expecting file name after '-o'\n"; goto error; } free(ret->outFile); ret->outFile = strdup(consume_argument(&ap)); break; case 'i': /* Input file name */ if (is_end_of_arguments(&ap)) { err_message = "Expecting file name after '-i'\n"; goto error; } ret->inpFiles = read_string_array(consume_argument(&ap)); break; case '-': /* End of options */ goto end_of_options; default: err_message = "Unexpected command-line parameter\n"; goto error; } } else { /* Other parameters are ignored */ next_argument(&ap); } } /* end for each argument */ end_of_options: *_argc = ap.argc; /* Save the modified argc value */ finalize_argparse(&ap); return ret; error: fputs(err_message, stderr); pb_FreeParameters(ret); return NULL; }