zpmod  b19981f
High-performance Zsh module for script optimization and filesystem helpers
utils.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
6 #include "zpmod.mdh"
7 #include "zpmod.pro"
8 #include "zpmod_utils.h"
9 #include "zpmod_vendor_shims.h"
10 #include <string.h>
11 
13 int zp_has_option(char **argv, char opt) {
14  char *string;
15  while ((string = *argv)) {
16  if (string[0] == '-') {
17  if (string[1] == '-' && string[2] == '\0') {
18  return 0;
19  }
20  while (*++string) {
21  if (string[0] == opt) {
22  return 1;
23  }
24  }
25  }
26  ++argv;
27  }
28  return 0;
29 }
30 
35 int zp_take_opt_with_arg(char ***argvp, char opt, char **out_arg) {
36  char **argv = *argvp;
37  char *cur = *argv;
38  if (!cur || cur[0] != '-' || !cur[1]) {
39  return 0; /* not an option */
40  }
41  if (cur[1] == '-' && cur[2] == '\0') {
42  return 0; /* end of options marker */
43  }
44  if (cur[1] != opt) {
45  return 0; /* different option */
46  }
47  /* attached case: -oARG */
48  if (cur[2] != '\0') {
49  *out_arg = cur + 2;
50  *argvp = argv + 1;
51  return 1;
52  }
53  /* separate case: -o ARG */
54  if (!argv[1]) {
55  return -1; /* missing argument */
56  }
57  *out_arg = argv[1];
58  *argvp = argv + 2;
59  return 1;
60 }
61 
63 char *my_ztrdup_glen(const char *s, unsigned *len_ret) {
64  char *t;
65  if (!s) {
66  return NULL;
67  }
68  *len_ret = strlen((const char *)s);
69  t = (char *)zalloc(*len_ret + 1);
70  memcpy(t, s, *len_ret);
71  t[*len_ret] = '\0';
72  return t;
73 }
74 
76 char *zp_unmetafy_zalloc(const char *to_copy, int *new_len) {
77  char *work;
78  char *to_return;
79  int my_new_len = 0;
80  unsigned meta_length = 0;
81  work = my_ztrdup_glen(to_copy, &meta_length);
82  if (!work) {
83  return NULL;
84  }
85  work = unmetafy(work, &my_new_len);
86  if (new_len) {
87  *new_len = my_new_len;
88  }
89  to_return = (char *)zalloc((my_new_len + 1) * sizeof(char));
90  if (!to_return) {
91  zfree(work, meta_length);
92  return NULL;
93  }
94  memcpy(to_return, work, sizeof(char) * my_new_len);
95  to_return[my_new_len] = '\0';
96  zfree(work, meta_length);
97  return to_return;
98 }
int zp_take_opt_with_arg(char ***argvp, char opt, char **out_arg)
Consume a short option with required argument from argv.
Definition: utils.c:35
char * my_ztrdup_glen(const char *s, unsigned *len_ret)
zalloc-backed strdup with length out parameter.
Definition: utils.c:63
char * zp_unmetafy_zalloc(const char *to_copy, int *new_len)
Duplicate and unmetafy a zsh string with zalloc; see header for details.
Definition: utils.c:76
int zp_has_option(char **argv, char opt)
Lightweight argv short-option scanner (stops at "--").
Definition: utils.c:13
Module declaration header (mdh) for zpmod.
Prototype stub for zpmod when building out-of-tree.
char * unmetafy(char *s, int *len)
void * zalloc(size_t size)
void zfree(void *ptr, size_t size)
Utility helpers shared across module components.
Local, non-invasive shims to suppress benign vendor header warnings.