// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11ParseFilespec
int dos11ParseFilespec(
  char *ptr,
  struct dos11FileSpec *spec,
  unsigned char user,
  unsigned char group,
  int wildcard
)
{
  char term[] = { '\0', '.', '[', ',', ']' };
  char flags[] =
    { 0, DOS11_WC_NAME, DOS11_WC_EXT, DOS11_WC_GROUP, DOS11_WC_USER };
  int state = P_NAME;
  unsigned int uic, i;
  char filename[6], ext[3];

  memset(spec, 0, sizeof(struct dos11FileSpec));
  spec->user = user;
  spec->group = group;

  memset(&filename, ' ', sizeof(filename));
  memset(&ext, ' ', sizeof(ext));

  if (wildcard == DOS11_M_NONAME)
    if ((*ptr == '\0') || (*ptr == '['))
      spec->flags = DOS11_WC_NAME | DOS11_WC_EXT;

  while (state != P_DONE) {
    if (wildcard) {
      if (*ptr == '*') {
        spec->flags |= flags[state];
        ptr++;
        if (*ptr == '\0')
          state = P_DONE;
        else if (*ptr == term[state]) {
          if (state == P_USER)
            state = P_DONE;
          else state++;
        } else return 0;
        ptr++;
        continue;
      }
    }

    i = 0;

    switch (state) {
      case P_NAME:
        while ((*ptr != '\0') &&
               (*ptr != '.') &&
               (strchr(rad50, toupper(*ptr)) != NULL) &&
               (i < sizeof(filename)))
          filename[i++] = toupper(*ptr++);

        switch (*ptr++) {
          case '\0':
            state = P_DONE;
            break;

          case '.':
            state = P_EXT;
            break;

          case '[':
            state = P_GROUP;
            break;

          default:
            return 0;
        }
        break;

      case P_EXT:
        while ((*ptr != '\0') &&
               (strchr(rad50, toupper(*ptr)) != NULL) &&
               (i < sizeof(ext)))
          ext[i++] = toupper(*ptr++);
        
        switch (*ptr++) {
          case '\0':
            state = P_DONE;
            break;

          case '[':
            state = P_GROUP;
            break;

          default:
            return 0;
        }
        break;

      case P_GROUP:
        uic = 0;
        while ((strchr("01234567", *ptr) != NULL) && (i < 3)) {
          uic = (uic << 3) | (*ptr++ - '0');
          i++;
        }
        if ((uic == 0) || (uic > 0377))
          return 0;
        spec->group = uic & 0377;

        if (*ptr++ != ',')
          return 0;

        state = P_USER;
        break;

      case P_USER:
        uic = 0;
        while ((strchr("01234567", *ptr) != NULL) && (i < 3)) {
          uic = (uic << 3) | (*ptr++ - '0');
          i++;
        }
        if ((uic == 0) || (uic > 0377))
          return 0;
        spec->user = uic & 0377;

        if (*ptr++ != ']')
          return 0;

        state = P_DONE;
        break;
    }
  }

  spec->name[0] = ascR50(&filename[0]);
  spec->name[1] = ascR50(&filename[3]);
  spec->ext = ascR50(&ext[0]);
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
andq $0x0, (%rsi)
movq %rdi, %r14
movb %dl, 0x8(%rsi)
movb %cl, 0x9(%rsi)
movw $0x2020, %ax            # imm = 0x2020
movw %ax, 0xc(%rsp)
movl $0x20202020, 0x8(%rsp)  # imm = 0x20202020
movw %ax, 0x4(%rsp)
movb $0x20, 0x6(%rsp)
movl $0x0, %eax
movl %r8d, 0x1c(%rsp)
cmpl $0x2, %r8d
jne 0x6722
movzbl (%r14), %eax
cmpl $0x5b, %eax
je 0x6717
testl %eax, %eax
jne 0x6720
movb $0x3, (%r12)
movb $0x3, %al
jmp 0x6722
xorl %eax, %eax
movq %rax, 0x20(%rsp)
pushq $0x1
popq %rcx
pushq $0x9
popq %r13
movq %r12, 0x10(%rsp)
testl %ecx, %ecx
je 0x697c
cmpl $0x0, 0x1c(%rsp)
je 0x6793
cmpb $0x2a, (%r14)
jne 0x6793
movl %ecx, %eax
movq 0x20(%rsp), %rdx
leaq 0xa10f(%rip), %rsi      # 0x10865
orb (%rax,%rsi), %dl
movq %rdx, 0x20(%rsp)
movb %dl, (%r12)
movb 0x1(%r14), %dl
testb %dl, %dl
je 0x6930
leaq 0xa0eb(%rip), %rsi      # 0x10860
cmpb (%rax,%rsi), %dl
jne 0x69b1
cmpl $0x4, %ecx
leal 0x1(%rcx), %eax
movl $0x0, %ecx
cmovel %ecx, %eax
movl %eax, %ecx
jmp 0x6932
decl %ecx
leaq 0xa0b4(%rip), %rdx      # 0x10850
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
xorl %ebp, %ebp
movzbl (%r14,%rbp), %r12d
movsbq %r12b, %r15
testl %r12d, %r12d
je 0x6907
cmpl $0x2e, %r12d
je 0x6907
callq 0x3030
movq (%rax), %rax
movl (%rax,%r15,4), %ebx
leaq 0x10e4a(%rip), %rdi     # 0x17620
movl %ebx, %esi
callq 0x3150
testq %rax, %rax
je 0x6907
cmpq $0x5, %rbp
ja 0x6907
movb %bl, 0x8(%rsp,%rbp)
incq %rbp
jmp 0x67a7
incq %r14
xorl %r15d, %r15d
xorl %ebp, %ebp
movsbl -0x1(%r14), %ebx
leaq 0xa0fb(%rip), %rdi      # 0x10908
movl %ebx, %esi
movq %r13, %rdx
callq 0x31a0
testq %rax, %rax
je 0x6832
cmpl $0x2, %r15d
ja 0x6832
shll $0x3, %ebp
addl $-0x30, %ebx
orl %ebx, %ebp
incl %r15d
incq %r14
jmp 0x6801
leal -0x100(%rbp), %ecx
xorl %eax, %eax
cmpl $0xffffff01, %ecx       # imm = 0xFFFFFF01
jb 0x69b3
movb %bpl, 0x9(%r12)
cmpb $0x2c, -0x1(%r14)
pushq $0x4
popq %rcx
je 0x6733
jmp 0x69b3
incq %r14
xorl %r15d, %r15d
xorl %ebp, %ebp
movsbl -0x1(%r14), %ebx
leaq 0xa096(%rip), %rdi      # 0x10908
movl %ebx, %esi
movq %r13, %rdx
callq 0x31a0
testq %rax, %rax
je 0x6897
cmpl $0x2, %r15d
ja 0x6897
shll $0x3, %ebp
addl $-0x30, %ebx
orl %ebx, %ebp
incl %r15d
incq %r14
jmp 0x6866
leal -0x100(%rbp), %eax
cmpl $0xffffff01, %eax       # imm = 0xFFFFFF01
jb 0x69b1
movb %bpl, 0x8(%r12)
xorl %ecx, %ecx
cmpb $0x5d, -0x1(%r14)
movl $0x0, %eax
je 0x6733
jmp 0x69b3
xorl %r15d, %r15d
leaq (%r14,%r15), %rbp
incq %rbp
movsbq -0x1(%rbp), %r12
testq %r12, %r12
je 0x693b
callq 0x3030
movq (%rax), %rax
movl (%rax,%r12,4), %ebx
leaq 0x10d35(%rip), %rdi     # 0x17620
movl %ebx, %esi
callq 0x3150
testq %rax, %rax
je 0x694a
cmpq $0x2, %r15
ja 0x694a
movb %bl, 0x4(%rsp,%r15)
incq %r15
jmp 0x68c7
addq %rbp, %r14
incq %r14
cmpb $0x5b, %r12b
je 0x696d
movzbl %r15b, %eax
cmpl $0x2e, %eax
movq 0x10(%rsp), %r12
je 0x6963
testl %eax, %eax
jne 0x69b1
xorl %ecx, %ecx
jmp 0x6733
xorl %ecx, %ecx
addq $0x2, %r14
jmp 0x6733
xorl %ecx, %ecx
movq %rbp, %r14
movq 0x10(%rsp), %r12
jmp 0x6733
pushq $0x3
popq %rax
movl %eax, %ecx
movq %rbp, %r14
cmpb $0x5b, %r12b
movq 0x10(%rsp), %r12
je 0x6733
jmp 0x69b1
pushq $0x2
popq %rax
movl %eax, %ecx
jmp 0x6733
pushq $0x3
popq %rax
movl %eax, %ecx
movq 0x10(%rsp), %r12
jmp 0x6733
leaq 0x8(%rsp), %rdi
callq 0x5119
movw %ax, 0x2(%r12)
leaq 0xb(%rsp), %rdi
callq 0x5119
movw %ax, 0x4(%r12)
leaq 0x4(%rsp), %rdi
callq 0x5119
movw %ax, 0x6(%r12)
pushq $0x1
popq %rax
jmp 0x69b3
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq